Wednesday, April 23, 2008

useful string trim function

/**
 * Cut string to n symbols and add delim but do not break words.
 *
 * Example:
 * <code>
 *  $string = 'this sentence is way too long';
 *  echo neat_trim($string, 16);
 * </code>
 *
 * Output: 'this sentence is…'
 *
 * @access public
 * @param string string we are operating with
 * @param integer character count to cut to
 * @param string|NULL delimiter. Default: '…'
 * @return string processed string
 **/

function neat_trim($str, $n, $delim='…') {
   $len = strlen($str);
   if ($len > $n) {
       preg_match('/(.{' . $n . '}.*?)\b/', $str, $matches);
       return rtrim($matches[1]) . $delim;
   }
   else {
       return $str;
   }
}

Wednesday, April 16, 2008

how to user symfony sfAjaxUploaderPlugin

This is from symfony forum

http://www.symfony-project.org/forum/index.php/mv/msg/9279/0/40/

for more information, please check the post.




It looks to me like many people having problems using this plugin, and for good reasons. It is not the best plugin around and it was built very quickly, but it can be used.

To help you all I have made a small demo project which I friezed and attached.

The project was build by init a new project, then init an application called "test" and then init module called "test".


The index template contains the form with a file input field to be uploaded.



Theses are the changes done to various files:

filters.yml:


rendering: ~
web_debug: ~
security: ~

# generally, you will want to insert your own filters here
ajaxUploader:
class: AjaxUploaderFilter

cache: ~
common: ~
flash: ~
execution: ~




settings.yml:


prod:
.settings:
no_script_name: on

dev:
.settings:
error_reporting:

web_debug: on
cache: off
no_script_name: off
etag: off

test:
.settings:
error_reporting:
cache: off
web_debug: off
no_script_name: off
etag: off

all:
.settings:
enabled_modules: [default, ajaxUploader]




view.yml


default:
http_metas:
content-type: text/html

metas:
title: symfony project
robots: index, follow
description: symfony project
keywords: symfony, project
language: en

stylesheets: [main]

javascripts: [%SF_PROTOTYPE_WEB_DIR%/js/prototype, /sfAjaxUploaderPlugin/js/ajaxUploader]

has_layout: on
layout: layout





There is no code in the actions, only some templates.

The index template is a form with file field:




'myAjaxUploader()',
'update' => 'target_div',
'url' => 'test/upload',
)); ?>









and the response template just echos some info:


getParameter('name'))) {
echo "file " . $sf_request->getParameter('name') . " was uploaded";
} else {
echo "no file was uploaded";
}
?>





The AjaxUploaderHelper.php was buggy (as few of you have noticed). It should be:

url_for("ajaxUploader/uploader") . "?name=$name", "name"=>"ajaxUploader", "frameborder"=>0, "width"=>320, "height"=>25), true)
. tag("/iframe") . input_hidden_tag($name);
}

?>





And thats it, AJAX uploads!

symfony get insert_id

To get the last inserted record id in symfony, you can do as below:

$newHistory = new History();
$newHistory->setUpdateTime($now);
$newHistory->save();
$newHistory->getId()

Wednesday, April 02, 2008

Symfony Cheat Sheets Index

Cheat Sheets Index

This page lists all the know cheat cheets dealing with symfony.

Official

User-contributed

TopicAuthorEnglishPortuguêsEspañolFrançais Deutsch
Directory Structure and CLI Andréia BohnerJPG PDF JPG PDF JPG PDF JPG PDF JPG PDF
ViewAndréia BohnerPDF
View: Partials, Components, Slots and Component Slots Andréia BohnerPDF
Form HelpersAndréia BohnerJPG PDF JPG PDF
Javascript and Ajax HelpersAndréia Bohner PDFPDF
Server ValidationAndréia BohnerPDF PDF
ModelAndréia BohnerPDF
Model: SchemaAndréia BohnerPDF
Lime Unit & Functional TestingBenjamin MeynellPDF
ORMAndréia BohnerJPG PDF PDF
Criteria/Criterion/RSAndréia BohnerPDF

Tuesday, April 01, 2008

deploying a Symfony 1.0 project to a shared hosting envirnoment without Symfony installed

Deploying 1.0 To A Shared Host

This page describes a method of deploying a Symfony 1.0 project to a shared hosting envirnoment without Symfony installed.

Prerequisites

In order for this method to work, you need to meet the following conditions:

  • A Shared Host with PHP5
  • A functional copy of the project you plan to deploy in a local environment with Symfony 1.0 installed
  • An FTP account to the host you plan to deploy to.

Preparing Your Local Project

Several things need to be dealt with before beginning to upload.

  1. Update Symfony - Make sure that you're using the latest update of Symfony 1.0 (at the time of this writing the most recent update is 1.0beta1). For example, if you are using a pear install of Symfony 1.0beta1 on linux, you might execute this command:
    sudo pear upgrade symfony/symfony-beta
  2. Update Project - If you've recently updated your symfony install, be sure to update the project for the new version:
    symfony upgrade 1.0
  3. Freeze Project - Now comes the easy part. When you freeze a project, like we're about to do, Symfony automatically packages the current pear libraries into the project directories and updates the configuration to point to these internal libraries. This tool has several purposes, of which easing deployment is one of the most useful. So, just issue the command in your root project directory, and watch the magic happen:
    symfony freeze

You will then see a long list of things which Symfony is doing, most of which involve copying files to their new locations. Most of these will be in the lib/ and data/ directories, but there are a few in other places, such as the web/ directory.

Preparing Your Host Space

Directory Structure

Most shared hosting companies provide you with a base directory where you may upload your files. A standard layout for the subdirectory structure looks like this:

/
+-/public_ftp
+-/public_html
+-/tmp

There may be other folders, but they are not important to us.

Uploading the Files

Uploading the files is really quite easy, provided you know how to use an FTP client. If not, I would suggest you go read up on them, as they are very useful tools.

All you need to do is upload the following directories to the root folder of your webspace (Note: Do not upload the files into your public_html or similar folder. This is a severe security risk'''):

/
+-/apps
+-/cache
+-/config
+-/data
+-/lib
+-/log
+-/plugins (Optional, depending upon whether you have any plugins installed)

The next thing you need to do is upload the files in your project web/ directory into your remote public_html/ directory, or whatever directory you have that fulfills the function of holding your publically-viewable files.

/web/*    =>    /public_html/

Permissions

Now that you've uploaded the files, you need to ensure that the permissions of certain folders didn't get messed up while uploading.

symfony fix-perms
  • /cache : This folder should have world-writable permissions (777)
  • /log : This folder should have world-writable permissions (777)
  • All others: The other folders and file should be world-readable and executable (755)

Miscellaneous

There are a few other things you might want to do now that you're mostly finished.

Dev Controllers

I would definitely recommend that you deleted the dev controllers (/public_html/*_dev.php), since they are not needed and have the potential to deliver unwanted information to anyone attempting to hack your website.

Plugin Web Directories

Depending upon the method of installing your plugins and the platform you have developed your project on, the plugin web/ directories may need attending to. Generally if they worked on your local system, they'll work on your host, but if you encounter broken images and an absence of certain javascript files and stylesheets, then you may need to fix this. You have two options:

  1. If you are on a host that supports symlinks, you can create symlinks from the main public_html directory to the plugin web directories.
  2. The universal method of fixing a problem like this is to simply copy the contents of the plugin web/ directories into your main public_html/ directory.

Problems

Problem with stylesheet (css)

If the link to stylesheet doesn't work, add the following line to index.php:

$_SERVER['SCRIPT_NAME']='/index.php';

Based on this thread (OVH context).

Other

If you experience problems with this, simply ask on the forums or mailing list and you will most likely get an answer soon.