Symfony Eclipse integration plugin

I wrote simple symfony plugin that integrates symfony tasks into Eclipse/PDT.

Plugin features:

  • Run core tasks, plugin tasks and your custom tasks.
  • Ask interactively for required tasks parameters.
  • Tasks list is always up-to-date, it is updated each time you run clear:cache.

Basically this plugin just creates proper xml build-file for Ant Eclipse plugin. Here how it looks like:

(more…)

Sunday, October 24th, 2010

Running remote tasks with symfony

I was tired of logging to production servers and clearing cache each time after deploy. So I wrote this simple plugin which allows to run any remote tasks right from your dev environment. All you need now is to type something like:

./symfony project:remote production cc

To install plugin:

  1. download attached package oxtRemoteTaskPlugin-1.0.0.tgz
  2. install it
    ./symfony plugin:install oxtRemoteTaskPlugin-1.0.0.tgz
  3. clear the cache
    ./symfony cc

Configure remote host if you haven’t done that yet:

[production]
  host=your.hostname
  port=22
  user=username
  dir=/path/to/symfony/project/

Now you are ready to run remote tasks.
If remote task consists of more than one word, enclose it in quotes, just like you do for any command line argument:

symfony project:remote production 'cc --app=frontend'

This plugin is basically a wrapper for ‘ssh’ command. It won’t work on vanilla Windows.

Monday, October 18th, 2010

Doctrine Sluggable and translit

Doctrine Sluggable behaviour doesn’t work with non-ASCII characters – it just removes them from the slug. For some languages, e.g. Russian with Cyrillic alphabet, it becomes unusable. The obvious way is to use transliteration.
Luckily Doctrine has easy way to do that, you can pass method for building slug:

Artist:
  actAs:
    Sluggable:
      fields: [name]
      builder: [oxtSluggableTranslit, urlize]

The easiest way I found to transliterate non-ASCII text is to use PECL translit package. For Debian-based systems you can install it with dh-make-php.
Here’s the code:

class oxtSluggableTranslit
{
  /**
   * Convert any passed string to a url friendly string. Converts 'My first blog post' to 'my-first-blog-post'
   *
   * @param  string $text  Text to urlize
   * @return string $text  Urlized text
   */
  static public function urlize($text)
  {
    return Doctrine_Inflector::urlize(transliterate($text, array('cyrillic_transliterate', 'remove_punctuation'), 'utf-8', 'utf-8'));
  }
}

You can view all available transliterate filters by running

php -r 'print_r(transliterate_filters_get());'

Monday, February 1st, 2010

symfony: sfValidatorFile – invalid mime type (text/plain) for PNG images

On some systems sfValidatorFile may give you an error when trying to upoad PNG images. It says that it has invalid mime type – text/plain, though you have 'mime_types' = 'web_images' and other image formats work well.

It may be caused by wrong result from mime_content_type() function. To check if it’s your case create simple php script with just one line:

echo mime_content_type('path_to_your_png_image');

If it will give you text/plain then you have three options:

  1. Upgrade your PHP to 5.3.0, though I doubt it’s available for most *nix distributions at this time.
  2. Install PECL package Fileinfo.
  3. The easiest, modify form class.

In first two options sfValidatorFile will use finfo_open function prior to mime_content_type and it should give correct results for PNG image. However, upgrading PHP or installing PECL package isn’t always possible.

The simplest way to fix this bug is to apply small modifications to your form class, just reorder mime type guessers:

public function configure()
{
  $this->validatorSchema['image'] = new sfValidatorFile(array(
      'required'	=> false,
      'path'       => sfConfig::get('sf_upload_dir') . '/directory',
      'mime_types' => 'web_images'
    ));
  $this->validatorSchema['image']->setOption('mime_type_guessers', array(
    array($this->validatorSchema['image'], 'guessFromFileinfo'),
    array($this->validatorSchema['image'], 'guessFromFileBinary'),
    array($this->validatorSchema['image'], 'guessFromMimeContentType')
  ));
}

In this case validator will first try fileinfo (which should be available since PHP 5.3.0), then 'file -bi' which should work correctly on *nix platform and only if both failed it will use mime_content_type

Monday, July 27th, 2009

Calling phing tasks from Eclipse

Here you can read how to run phing tasks using built-in Ant tool in Eclipse.

In this post I’ll describe what to do if your phing task requires user input. The solution is simple – ask for user property in ant and pass it back to phing:
(more…)

Thursday, June 11th, 2009

Add absolute link menu item to user menu in Drupal

First of all, user menu (local tasks) in Drupal cannot be edited via admin. To add items to the menu you need to create new module and implement hook_menu() in it.
The following example shows how to do it in Drupal 6:

function yourmodule_menu() {
  $items['http://some.url'] = array(
    'tab_root'	=> 'user',
    'tab_parent'	=> 'user',
    'title' => 'External URL',
    'page callback' => 'user_page',
    'access callback' => 'user_is_anonymous',
    'type' => MENU_LOCAL_TASK,
    'weight'	=> 5
  );
 
  return $items;
}

Change ‘access callback’ to whatever function (or constant) you want, but don’t forget to set ‘page callback’. It is being checked when building menu structure even if we don’t have any page callback actually.

Thursday, May 21st, 2009

symfony l10n – going further

As you may know symfony offers localized objects out of the box. It gives you an easy way to have different data for different cultures.

Sometimes it is not enough. I needed two localized version of website but with completely different content – everything including users, admin and super admin. Both version were supposed to run under the same domain and to be distinguished just by locale part in the url. I was too lazy and didn’t want to copy applications or projects (moreover, it is a problem in the future, you’ll have to modify each copy). So, I decided to use different databases with exact schema for different cultures.

Here’s guide how to implement this.
(more…)

Sunday, April 19th, 2009

Theming language selector in Drupal

Unfortunately Drupal (as in version 6.5) doesn’t allow easy theming for language selector. However it’s still possible with theme function overriding. Look at the code.

function yourtheme_links($links, $attributes = array('class' => 'links')) {
	list(,$first_link) = each($links);
	if (($first_link['attributes']['class'] == 'language-link')) {
		global $language;
		$new_links = array();
		$num_links = count($links);
		$i = 0;
		foreach ($links as $link) {
			$i ++;
			$link['title'] = '<span class="bg">' . $link['title'] . '</span>';
			$link['html']	= true;
			if ($link['language']->language == $language->language)
				$link['attributes']['class'] = 'selected';
			$new_links[] = $link;
			if ($i < $num_links)
				$new_links[] = array(
					'title'	=> '|',
					'attributes'	=> array('class'	=> 'separator')
				);
		}
		$links = $new_links;
	}
	return theme_links($links, $attributes);
}

It adds ‘selected’ class to the current language (which I think logical) and adds separator element between language links. You can customize them as you wish.

Thursday, November 13th, 2008

symfony admin generator – populating create form from filters

symfony has basic CRUD-like admin generator, which allows you to create controls for filtering list results. Naturally, if you have filtered list and press ‘create’ button it would be nice to have new form with populated from filter form fields.

Here’s basic idea how to do it. In my example I have Work entity which has foreign key to Category entity. I have list of works filterable by category id.
To populate category id on work creation form you need to override generated action:

class worksActions extends autoworksActions
{
  public function executeEdit()
  {
  	parent::executeEdit();
  	if ($this->getRequestParameter('action') == 'create')
  	{
  		$filters = $this->getUser()->getAttributeHolder()->getAll('sf_admin/work/filters');
  		if (isset($filters['category_id']))
  			$this->work->setCategoryId($filters['category_id']);
  	}
  }
}

Look into admin generated actions if you want to more clearly understand why we need such code. It is located at cache/admin/dev/modules/autoWorks/actions/actions.class.php

Sunday, October 5th, 2008

Error in PayPal NVP ExpressCheckout with Zend Framework

If you are using classes from Zend Framework Laboratory for PayPal integration (which is somewhat unfinished) you probably will have an error when trying

Zend_Service_Paypal::setExpressCheckout();

The error has strange description ‘Security header is not valid’ and not listed in PayPal documentation.

To fix the error you’ll have to change line in Zend/Service/PayPal.php (in Laboratory tree)

const SERVICE_URI = 'https://api-3t.sandbox.paypal.com/nvp';

to

const SERVICE_URI = 'https://api-3t.paypal.com/nvp';

Friday, October 3rd, 2008