Showing posts with label zend form. Show all posts
Showing posts with label zend form. Show all posts

Sunday, February 15, 2009

Internalization and Zend Form

One of the many growing requirements that I am experiencing at work is for internalization of our forms.  In this particular use case the languages available are limited to English and Spanish so I am not able to use Zend_Locale::BROWSER exclusively.  To make sure that correct form displays, in my form model class I created an array of allowed locales.  Before the form instantiated in the controller the language param is set the registry, and then that locale is checked against allowed locales.

References:
Translations - located in my models directory: Models/Languages/En.php and Es.php

// En.php

return array(
              'name'    => 'First Name',

              'address' => 'Address'
       );


// Es.php

return array (

         'name'     => 'nombre',         'address' => 'dirección'

       );



// Controller snippet
 /**
  * Add language to the registry
  */
  $locale = new Zend_Locale($this->_getParam('lang'));
  Zend_Registry::set('locale', $locale);

// Form snippet
  /**
   * Instance of Zend_Locale
   *
   * @var Zend_Locale
   */
  protected $_locale         = null;

  /**
   * Allowed locale regions
   *
   * @var array
   */
  protected $_allowedLocales = array('en', 'es');

  protected $_translations   = null;

    $this->addElement('note', 'quanityInstructions', array(
      'decorators'  => $this->_standardNoteDecorator,
      'description' => "{$this->_translations->_('quanityInstructions')}"
    ));

  /**
   * Prepare the form
   *
   * @return boolean
   */
  private function _prepare() {

    /**
     * Grab the browsers locale
     */
    $this->_locale = Zend_Registry::get('locale');

    /**
     * If the browser is locale isn't English or Spanish then default to
     * English
     */
    if (!$this->_checkForValidLocaleRegion($this->_locale)) {
      $this->_locale = new Zend_Locale('en_US');
    }

    $transFile = ucfirst($this->_locale->getLanguage()).'.php';

    $this->_translations = new Zend_Translate('array', realpath(dirname(__FILE__).'/../../Models/HemOnc/Languages/'.$transFile), $this->_locale);

    return true;
  }

  /**
   * Check to make sure that browser's locale is english or spanish.  The region
   * doesn't matter.
   *
   * @param Zend_Locale $zl
   * @return boolean
   */
  private function _checkForValidLocaleRegion(Zend_Locale $zl) {
    return in_array($zl->getLanguage(), $this->_allowedLocales) ? true : false;
  }


If locale is allowed then the form label translation is set.

For further info please contact me.

Saturday, January 10, 2009

Using Zend View for Email Message Body in Your Model

Lately in the PHP/Zend Framework blog world there has been much discussion concerning what constitutes a model in an MVC app.  In the current implementation of the MVC apps at work our ZF Form implementations are processed in a corresponding model class as well as a 'notify' method which handles emailing a response after successful submission.  I was able to abstract all aspects of the email properties except for the message body.

Most of the time the requirement is to have, basically, the same form view but with the values populate.  That usually means creating some long and kludgy looking heredoc or worse a huge string of crazy html intermingled with escapes.  Neither solution was very appealing to me.  I kept thinking more and more that the email body was really a view.  So I treated as such.  So my solution was to create a global views directory and a view script that was the email body template and passed it's render method to Zend_Mail instance.

This allows for further separation of models and views.  I know that technically the email body should be apart of the model, but far too many times I have to change how the body of the mail looks, not the data, so it lends itself to more of a view.

Please feel free to comment.

::NOTE::
Forms as model architecture taken from http://weierophinney.net/matthew/archives/200-Using-Zend_Form-in-Your-Models.html

//Example Code
private function _notifyGroup() {
 
  $data = array();
  $data = $this->getForm()->getValues();
 
  /**
   *  Setup view instance to pass to notifier
   */
  $view = new Zend_View();

  /**
   * Assign all form data to view property
   */
  $view->assign("formvalues", $data);

 
 /**
   * Location of view scripts
   */
  $view->addScriptPath(dirname(__FILE__).'/../../../../views');

  $body = $view->render('email-templates/mir.phtml');

  /**
   *  Custom notify class that abstracts different 'notify' methodologies.  For example you can notfiy
   *  by writing to a log or email.  Email can be Zend_Mail, PEAR or plain ol' php mail() 
   */
  $notifier = CW_Notify::factory('mail_zend', $body, $options['email']);

  $notifier->notify();

}