Code snippets for symfony 1.x

Navigation

Popular Tags

action admin ajax batch cache cli criteria css culture custom database date debug doctrine email filter form forms generator helper i18n javascript model navigation object pager pagination pake propel query routing session sql symfony template test user validation validator view widget
This is a public source code repository where you can find, rate and comment code snippets published by others, or store you owns and categorize them with tags for easy retrieval.

Latest snippets

Converting the form fields in paragraphs with a decorator

I created a reusable object, where I want to display select fields blocked, because this way I display only the value in DIVs, adding an input hidden (or not).

Not resolved the idea of a print version because only a few fields have the option renderer_class.

If someone yells at me to think of something :)

Decorator

<?php
 
class g1mrWidgetFormPrint extends sfWidgetFormInputHidden
{
 
  protected function configure($options = array(), $attributes = array())
  {
    $this->addOption('choices');
    $this->addOption('template');
    $this->addOption('renderHidden', true);
    $this->addOption('class', 'form_print');
    $this->addOption('break', "<br>\n");
    $this->addOption('print_template', <<<EOF
<div class="%class%">
    %value%
    %hiddenField%
</div>
EOF
);
   parent::configure($options, $attributes);
  }
 
  /**
   * Renders the widget.
   *
   * @param  string $name        The element name
   * @param  string $value       The value selected in this widget
   * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
   * @param  array  $errors      An array of errors for the field
   *
   * @return string An HTML tag string
   *
   * @see sfWidgetForm
   */
  public function render($name, $value = null, $attributes = array(), $errors = array())
  {
    if (is_null($value))
    {
      $value = '';
    }
 
    $choices = $this->getOption('choices');
    if ($choices instanceof sfCallable)
    {
      $choices = $choices->call();
    }
 
      if (is_array($choices)) 
      {  
        $vc = $value;
        if (!is_array($vc))
        {
         $vc= array($vc);
        }
        foreach ($choices as $k => $array)
        {
          if (is_array($array))
          {
            foreach($array as $key => $o)
            {
              if (in_array(strval($key), $vc))
              {
                 $associated[$key] = $o;
              }
            }  
          }
          else
          {
            foreach($choices as $key => $o)
            {
              if (in_array(strval($key), $vc))
              {
                 $associated[$key] = $o;
              }
            }
 
          }
        } 
      }
      else
      {
        $associated = $value;
      } 
 
    $hiddenField = '';
    if($this->getOption('renderHidden'))
    {
        $hiddenField = parent::render($name, $value, $attributes, $errors);
    }
 
    return strtr($this->getOption('print_template'), array(
      '%class%'              => $this->getOption('class'),
      '%value%'              => $this->processValue($associated),
      '%hiddenField%'        => $hiddenField
    ));
  }
 
  public function processValue($x)
  {
    if(is_array($x))
    {
      return implode($this->getOption('break'),$x); 
    }
    else
    {
      return $x;
    } 
  }
 
}
 

Form

//...
 
    public function setPrintable($field, $renderHidden = true)
    {
        if($this->widgetSchema[$field]->hasOption('renderer_class'))
        {
            $this->widgetSchema[$field]->setOption('renderer_class', 'g1mrWidgetFormPrint');
            $this->widgetSchema[$field]->setOption('renderer_options', array('renderHidden' => $renderHidden));
        }
    }
 
 
    public function setPrintables($renderHidden = true)
    {
        foreach($this->getFormFieldSchema()->getWidget()->getFields() as $key => $object)
        {
            $this->setPrintable($key, $renderHidden);
        }  
    }
 
//..    
 

Controler

//...
  public function executeEdit(sfWebRequest $request)
  {
    $this->pedido = $this->getRoute()->getObject();
    $this->form = $this->configuration->getForm($this->pedido);
    if($this->pedido->getIsSent() == 1)
    {
      $this->form->setPrintables(); 
    }
  }
//...
 
by Gilmar Pupo on 2010-10-02, tagged decorator  forms  widget 

symfony 1.x form framework: how to add extra fields into forms?

Assumption: there is a UserMessage class and the related form UserMessageForm. Both were generated by Doctrine based on a schema.yml declaration. Problem: one wishes to you add input fields into this form.

Approach: one can add any pairs of widgets/validators which have no direct relationship to the presented model. Doing so, one need to care about two main aspects:

  1. configuring the form with the extra field
  2. processing of the submitted value (if any) at the proper moment without disturbing the big rest of form functionality.

For step 1, configuring the form: add the widget and the validator to the form.

In .../UserMessageForm.class.php:
 
  public function configure()
  {
    $this->widgetSchema['comment_for_log'] = new sfWidgetFormInputText();
    $this->validatorSchema['comment_for_log'] = new sfValidatorString();
  }
 

For step 2 processing user input after submission: process the value and act accordingly. Typically this happens only after validation did succeed (i.e. not validation errors did occur).

For this purpose, a dedicated method for each field gets called (in case that the method exists). The following example checks whether a non-empty text was entered by the user. If so, it does store a log entry. The function in this example returns a boolean 'false' value to tell symfony not to process this value further.

In .../UserMessageForm.class.php:
 
  public function updateCommentForLogColumn($value)  // constructed method name with camelized field name
  {
    if (! empty($value)) {     // e.g. only if the user entered a comment (btw this is the cleaned value)
      // do whatever you need to do based on the contents of this field.
      // e.g. we create and save a log entry as follows:
      $logEntry = new AppLogEntry();    // assume we have this sort of logging feature
      $logEntry->setComment($value);    // the comment entered by the user
      $logEntry->save();
    }
    return false;  // tell symfony that we have cared about this value, so no further processing will occur
  }
}
 

Basically that's it.

An alternative to the dedicated updateYourSpecialFieldColumn() method could be to override the doUpdate($values) method in the form, allowing to do more general things because there is access to all submitted values (that's basically the only difference between the two approaches, though). I personally prefer the dedicated method whenever possible, as it doesn't disturb any other logic that may (at the same time, or sometimes later) implemented in the same form.

Another good thing is that since all this code is all packed in a Doctrine transaction, so either both the form object (UserMessage) will be saved as well as the log (LogEntry) be generated, or none of the two objects be altered/created at all.

(15.9.2010/SCH)

by Raphael Schumacher on 2010-09-15, tagged doctrine  fields  form  symfony14  validator  widget 

Unit Test to check the configuration of sending emails in symfony 1.4 (c / SwiftMailer)

<?php
/**
 * test/unit/maillerTest.php
 *
 * @author     Gilmar Pupo <g@g1mr.com>
 * 
 *  Unit Test to check the configuration of sending emails in symfony 1.4 (c / SwiftMailer)
 */
 
require_once dirname(__FILE__).'/../bootstrap/unit.php';
$t = new lime_test(1);
require_once sfConfig::get('sf_symfony_lib_dir').'/vendor/swiftmailer/classes/Swift.php';
Swift::registerAutoload();
sfMailer::initialize();
$dispatcher = new sfEventDispatcher();
$factories = sfYaml::load(sfConfig::get('sf_root_dir') . '/apps/backend/config/factories.yml');
$mailer = new sfMailer($dispatcher, $factories['all']['mailer']['param']);
$message = Swift_Message::newInstance('Subject test')
  ->setFrom(sfConfig::get('app_contact_emailfrom', 'from@noreply.com'))
  ->setTo(array('g@g1mr.com' => 'Gilmar Pupo'))
  ->setBody('Email content');
$numSent = $mailer->send($message);
$t->ok( $numSent > 0 );
 
by Gilmar Pupo on 2010-09-08, tagged mail  test 

script for to generate (all) modules

This Bash script transform: 'BaseModel.class.php' => 'BaseModel' => 'Model' then call doctrine:generate-admin frontend 'Model', for all files from lib/model/doctrine/base.

#!/bin/bash
for file_class in `ls lib/model/doctrine/base`  do
    base_model=${file_class/%.class.php/}      
    model=${base_model/#Base/}                 
    ./symfony doctrine:generate-admin --env="prod" frontend $model
done
 
by Vlad Bazon on 2010-08-01, tagged bash  doctrine  generateadmin  model  modul  symfony 

sfModularPHPView

This module able you to get alternative template file from a default module

in app.yml

all:
    common_module: common_module_name
 
 
 
 
 
 
 
<?php
/**
 *
 * @subpackage lib
 * @author Benoit Montuelle
 *
 */
 
class sfModularPHPView extends sfPHPView
{
  /**
   * Configurer le template
   * Soit le fichier est défini pour le module en cours, soit on utilise le module commun
   *
   * @return void
   */
  public function configure()
  {
    parent::configure();
 
    if (!$this->directory)
    {
      $moduleTemplateDirs =  sfContext::getInstance()->getConfiguration()->getTemplateDirs($this->getModuleName());
 
      //tous les chemins possibles pour les templates du module courant
      foreach ($moduleTemplateDirs as $templateDir)
      {
        if (is_readable($templateDir.'/'.$this->getTemplate()))
        {
          $this->setDirectory($templateDir);
          return;
        }
      }
 
      //rien trouvé on regarde le module commun
      $moduleCommunTemplateDirs = str_replace($this->getModuleName(), sfConfig::get('app_common_module'), $moduleTemplateDirs);
      foreach ($moduleCommunTemplateDirs as $templateDir)
      {
        if (is_readable($templateDir.'/'.$this->getTemplate()))
        {
          $this->setDirectory($templateDir);
          return;
        }
      }
    }
  }
 
}
 
by Benoit Montuelle on 2010-07-30, tagged 14  modular  view 

CSS link class adder

<?php
 
/**
 * Automatically adds a class name to a link (or a parent tag) if the link route matches
 * the current module or route.
 * (based on the work made by scott meves on 2007-02-24 : http://snippets.symfony-project.org/snippet/153)
 *
 * @author  ESCANDELL Stéphane <stephane.escandell[a]gmail.com>
 * @date    2010-07-16
 *
 *
 * @param  <name> : string name of the link, i.e. string to appear between the <a> tags
 * @param  <internal_uri> string 'module/action' or '@rule' of the action
 * @param  <options> array additional style options
 * @return string XHTML compliant <a href> tag (encapsulated by an optionnal tag)
 * @see    link_to
 * 
 *
 *
 * <b>Available Options:</b>
 *    -> All classic options from link_to() method
 *
 *    -> 'moduleOnly'
 *      recognized values:
 *        -> true
 *        -> false
 *      If set to <true>, the link is identify as matched only if the module of the route associated to <internal_uri>
 *      is matching to the current module.
 *      If not set, the default value is false
 *
 *    -> 'currentClass'
 *      recognized values : 
 *        -> string 
 *      The class identifier added to the link (or the parent tag, see below) if <internal_uri> is matching to
 *      the current page.
 *      If not set, the default class added is 'active'.
 *
 *    -> 'separator'
 *      recognized values:
 *        -> string
 *      Used only if the <class> option is set. If the link match, this separator will be used between the <class>
 *      option and <currentClass> option (the generated class will be <class>.<separator>.<currentClass>).
 *      If not set, the default value is a space caracter ' '.
 *
 *    ->  'tag'
 *      recognized values:
 *        -> html tag name
 *      Identify an html tag to encapsulate the link. If this option appears, the <currentClass> name will be added
 *      to this tag rather than the <a> tag.
 *
 *    -> 'tagOptions'
 *      recognized values:
 *        -> array of html parameters
 *      Used only if the parameter <tag> exists. <tagOptions> is used to generate the tag to encapsulate the link with
 *      user specific options.
 *
 * <b>Examples:</b>
 * <code>
 *   <?php echo link_to_styled( 'home',
 *                           '@homepage',
 *                           array('title' => 'Home')); ?>
 *   if the current route is '@homepage', then:
 *     => <a href="path/to/homepage/action" class="active" title="Home">home</a>
 * 
 *   <?php echo link_to_styled( 'home',
 *                           '@homepage',
 *                           array('title' => 'Home', 'currentClass' => 'myActiveClass')); ?>
 *   if the current route is '@homepage', then:
 *     => <a href="path/to/homepage/action" class="myActiveClass" title="Home">home</a>
 *
 *   <?php echo link_to_styled( 'home',
 *                           '@homepage',
 *                           array('title' => 'Home', 'separator' => '_')); ?>
 *   if the current route is '@homepage', then:
 *     => <a href="path/to/homepage/action" class="active" title="Home">home</a>
 *
 *   <?php echo link_to_styled( 'home',
 *                           '@homepage',
 *                           array('title' => 'Home', 'class' => 'myClass tab', 'separator' => '_')); ?>
 *   if the current route is '@homepage', then:
 *     => <a href="path/to/homepage/action" class="myClass tab_active" title="Home">home</a>
 *
 *   <?php echo link_to_styled( 'home',
 *                           '@homepage',
 *                           array('title' => 'Home', 'currentClass' => 'myActiveClass',
 *                                 'tag' => 'li')); ?>
 *   if the current route is '@homepage', then:
 *     => <li class="myActiveClass"><a href="path/to/homepage/action" title="Home">home</a></li>
 *
 *   <?php echo link_to_styled( 'home',
 *                           '@homepage',
 *                           array('title' => 'Home',
 *                                 'tag' => 'li',
 *                                  'tagOptions' => array('class' => 'anotherClass',
 *                                                        'width' => '250px')
 *                                )); ?>
 *   if the current route is '@homepage', then:
 *     => <li class="anotherClass active" width="250px"><a href="path/to/homepage/action" title="Home">home</a></li>
 *
 *   <?php echo link_to_styled( 'home',
 *                           '@homepage',
 *                           array('title' => 'Home',
 *                                 'currentClass' => myActiveClass,
 *                                 'tag' => 'li',
 *                                  'tagOptions' => array('class' => 'anotherClass other',
 *                                                        'width' => '250px')
 *                                )); ?>
 *   if the current route is '@homepage', then:
 *     => <li class="anotherClass other myActiveClass" width="250px"><a href="path/to/homepage/action" title="Home">home</a></li>
 * </code>
 *
 *
 * Changelog :
 *    2010/07/16 :
 *      Author : ESCANDELL Stéphane <stephane.escandell[a]gmail.com>
 *      Modification :
 *        First version (based on the work made by scott meves on 2007-02-24 : http://snippets.symfony-project.org/snippet/153)
 */
function link_to_styled($name, $internal_uri, $options = array())
{
  static $context = null;
  static $currentModuleName = null;
  static $applicationConfiguration = null;
 
  if(is_null($context))
  {
    $context = sfContext::getInstance();
  }
  if(is_null($currentModuleName))
  {
    $currentModuleName = $context->getModuleName();
  }
  if(is_null($applicationConfiguration))
  {
    $applicationConfiguration = $context->getConfiguration();
  }
 
  $options    = _parse_attributes($options);
  $separator  = isset($options['separator']) ? $options['separator'] : ' ';
  $tag        = isset($options['tag']) ? $options['tag'] : null;
  $tagOptions = isset($options['tagOptions']) ? $options['tagOptions'] : array();
 
  $isCurrent = false;
  // In order to findRoute() works, we need to delete the reference to
  // the file controller used
  //
  $route = $context->getRouting()->findRoute( str_replace('/'.$applicationConfiguration->getApplication().'_'.$applicationConfiguration->getEnvironment().'.php',
                                                      '',
                                                      url_for($internal_uri,false)
                                                      ));
  if(isset($options['moduleOnly']) && true===$options['moduleOnly'])
  {
    $isCurrent = isset($route['parameters']) ? (isset($route['parameters']['module']) ? $route['parameters']['module']==$currentModuleName : false) : false;
  }
  else
  {
    $isCurrent = url_for($internal_uri,false) == url_for($context->getRouting()->getCurrentInternalUri(),false);
  }
 
  if($isCurrent)
  {
    if(is_null($tag))
    {
      if(!isset($options['class']))
        $options['class'] = isset($options['currentClass']) ? $options['currentClass'] : 'active';
      else
        $options['class'] .= isset($options['currentClass']) ? $separator.$options['currentClass'] : $separator.'active';
    }
    else
    {
      if(!isset($tagOptions['class']))
        $tagOptions['class']  = isset($options['currentClass']) ? $options['currentClass'] : 'active';
      else
        $tagOptions['class'] .= isset($options['currentClass']) ? $separator.$options['currentClass'] : $separator.'active';
    }
  }
 
 
  unset($options['moduleOnly'],$options['currentClass'],$options['separator'],$options['tag'],$options['tagOptions']);
 
  if(is_null($tag))
    return link_to($name, $internal_uri, $options);
  else
    return content_tag($tag, link_to($name, $internal_uri, $options), $tagOptions);
}
 
by Stéphane Escandell on 2010-07-16, tagged css  helper  link  menu  navigation 
(2 comments)

Executing MySQL stored prodcedure in Symfony 1.2

I've written this code in my module action to get resultset returned by a stored procedure

$sql = sprintf('CALL sp_name(%s)', "parameter_value");      
 
$con_pdo = Propel::getConnection();
 
$result_pdo_stat = $con_pdo->query($sql);
 
$this->transactions = $result_pdo_stat->fetchAll(); //setting $transactions for my template
 

Please let me know if you find it useful

by shaheedulhaq sadi on 2010-06-24, tagged propel  storedprodcedure  symfony 
(1 comment)

Custom Filebased Configuration Class

This class can be used to store some simple info(number of first page posts,etc.) in a yml file. Usage is like sfConfig class.

<?php
  <?php
 
 
class mz_config 
{
  protected static $notLoaded = true;
  public static $autosave = true;
  protected static
    $config = array();
 
  /**
   * Retrieves a config parameter.
   *
   * @param string $name    A config parameter name
   * @param mixed  $default A default config parameter value
   *
   * @return mixed A config parameter value, if the config parameter exists, otherwise null
   */
  public static function get($name, $default = null)
  {
    if(self::$notLoaded)self::loadConfig();
    return isset(self::$config[$name]) ? self::$config[$name] : $default;
  }
 
  /**
   * Indicates whether or not a config parameter exists.
   *
   * @param string $name A config parameter name
   *
   * @return bool true, if the config parameter exists, otherwise false
   */
  public static function has($name)
  {
    return array_key_exists($name, self::$config);
  }
 
  /**
   * Sets a config parameter.
   *
   * If a config parameter with the name already exists the value will be overridden.
   *
   * @param string $name  A config parameter name
   * @param mixed  $value A config parameter value
   */
  public static function set($name, $value)
  {
 
    self::$config[$name] = $value;
    if(self::$autosave)self::saveConfig();
  }
 
  /**
   * Sets an array of config parameters.
   *
   * If an existing config parameter name matches any of the keys in the supplied
   * array, the associated value will be overridden.
   *
   * @param array $parameters An associative array of config parameters and their associated values
   */
  public static function add($parameters = array())
  {
    self::$config = array_merge(self::$config, $parameters);
    if(self::$autosave)self::saveConfig();
  }
 
  /**
   * Retrieves all configuration parameters.
   *
   * @return array An associative array of configuration parameters.
   */
  public static function getAll()
  {
    if(self::$notLoaded)self::loadConfig();
    return self::$config;
  }
 
  /**
   * Clears all current config parameters.
   */
  public static function clear()
  {
    self::$config = array();
    if(self::$autosave)self::saveConfig();
  }
  public static function saveConfig($filename = null)
  {
    if($filename == null)$filename = sfConfig::get('sf_app');
    $path = sfConfig::get('sf_data_dir');
    $dump_file = "{$path}/{$filename}.yml";
    $yml = sfYaml::dump(self::getAll());
    file_put_contents($dump_file,$yml);
  }
  public static function loadConfig($filename = null)
  {
    $notLoaded = false;
    if($filename == null)$filename = sfConfig::get('sf_app');
    $path = sfConfig::get('sf_data_dir');
    $dump_file = "{$path}/{$filename}.yml";
    $yml = file_get_contents($dump_file);
    $parser = new sfYamlParser();
    $data = $parser->parse($yml);
 
    self::$config = array_merge(self::$config, $data);
  }
}
 
by Mojtaba Faez on 2010-05-27, tagged config 
(1 comment)

Set "class" or any attribute of ALL fields in Symfony Form

class ClientForm extends BaseClientForm
{
  public function configure() {
    $fields = $this->getWidgetSchema()->getFields();
    foreach ($fields as $f) {
        $f->setAttribute("class", "client_form_field");
    }
  }
}
 
by gerry eng on 2010-05-09, tagged attribute  form 

Generating Symfony URLs in other projects and subdirectories

Integrating Phorum into a Symfony project at http://www.example.com/forum, I wanted to use my existing Symfony URLs in my Phorum templates:

// This path should be the path to your config/ProjectConfiguration.class.php file
// in your Symfony project
require_once(dirname(__FILE__).'/../../../config/ProjectConfiguration.class.php');
$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', sfConfig::get("sf_environment"), true);
$instance = sfContext::createInstance($configuration);
 
$routing = $instance->getRouting();
 
// Our URLs may have a prefix (eg /forum/index.php).  Remove this
$routingOptions = $routing->getOptions();
$urlPrefix = (array_key_exists("prefix", $routingOptions["context"]) ? $routingOptions["context"]["prefix"] : "");
 
$myURL = $routing->generate("default", array("module" => "foo", "action" => "bar"));
$myURL = str_replace($urlPrefix, "", $myURL);
 
by Rich Sage on 2010-05-06, tagged phorum  route  url 
(2 comments)