Skip to content

Instantly share code, notes, and snippets.

@stephaneerard
Created February 11, 2011 09:23
Show Gist options
  • Select an option

  • Save stephaneerard/822121 to your computer and use it in GitHub Desktop.

Select an option

Save stephaneerard/822121 to your computer and use it in GitHub Desktop.
My BaseFormDoctrine to manage setup for edit || save (+some dm behaviors management)
<?php
/**
* Project doctrine form base class.
*/
abstract class BaseFormDoctrine extends dmFormDoctrine
{
/**
* @var sfDateFormat
*/
protected static $dateFormatter;
/**
* This method proposes a way to call three different methods
* according to the object's state.
* It's simple as :
* call parent::configure();
* $this->isNew() ? call $this->configureNew()
* else call $this->configureEdit();
* anywa call $this->configureAll();
*
* (non-PHPdoc)
* @see dmFormDoctrine::configure()
*/
public function setup()
{
parent::setup();
if($this->isNew())
{
$this->setupForNew();
}
else
{
$this->setupForEdit();
}
$this->setupForAll();
}
/**
* This method is executed when the form->isNew() returns true.
* It's called by ->configure();
*
* It unset some fields of the array.
*
* @todo make the fields to unset configurable
*
*/
public function setupForNew()
{
foreach(array('created_by', 'created_at', 'updated_by', 'updated_at') as $field)
{
unset($this[$field]);
}
foreach($this->getObject()->getTable()->getRelations() as $relationName => $relation)
{
if($relation instanceof Doctrine_Relation_LocalKey && isset($this->widgetSchema[$relation['local']]))
{
$this->getWidget($relation['local'])->addOption('add_empty', true);
}
}
}
/**
* This method is executed when the !form->isNew() returns true.
* It's called by ->configure();
*
* It sets created_by and updated_by to disabled so users cannot modify
* them.
*
*/
public function setupForEdit()
{
//$this->setDefaultsForTimestampableFields();
foreach(array('created_by', 'updated_by', 'created_at', 'updated_at') as $field)
{
if(isset($this->widgetSchema[$field]))
{
$widget = $this->getWidget($field);
$widget->setAttribute('disabled', true);
if($widget instanceof sfWidgetFormDoctrineChoice)
{
$this->getWidget($field)->addOption('query', $this->createQueryForBlameableField($field));
}
}
}
}
/**
* @param unknown_type $field
* @param unknown_type $table
* @param unknown_type $rootAlias
*/
protected function createQueryForBlameableField($field, $table = 'DmUser', $rootAlias ='u')
{
return dmDb::table($table)->createQuery($rootAlias)->limit(1)->andWhere('u.id = ?', $this->object->get($field));
}
/**
* This method is called by ->configure()
* It is called after the form-state configuration (edit||new).
*
* It mainly manage the deleted_at default value and format it for end-user.
*/
public function setupForAll()
{
if($deletedAt = $this->getDefault('deleted_at', false))
{
$this->setDefault('deleted_at', self::getDateFormater()->format($deletedAt));
}else{
unset($this['deleted_at']);
}
}
/**
* (non-PHPdoc)
* @see dmFormDoctrine::getAutoFieldsToUnset()
*/
public function getAutoFieldsToUnset()
{
$fields = array_values(parent::getAutoFieldsToUnset());
if(!$this->isNew())
{
$fields = array_diff($fields, array('created_by', 'created_at', 'updated_by', 'updated_at'));
}
return $fields;
}
/**
* Overloads parent::setDefaults() so we can automatically
* format the created_at & updated_at fields.
*
* (non-PHPdoc)
* @see sfForm::setDefaults()
*/
public function setDefaults($defaults)
{
parent::setDefaults($defaults);
$this->setDefaultsForTimestampableFields(array('created_at' => isset($defaults['created_at']) ? $defaults['created_at'] : null,
'updated_at' => isset($defaults['updated_at']) ? $defaults['updated_at'] : null));
}
/**
*
* @param array $defaults
*/
protected function setDefaultsForTimestampableFields($defaults)
{
foreach(array('created_at', 'updated_at') as $field)
{
if((isset($defaults[$field]) || $this->getObject()->getTable()->hasColumn($field)) && isset($this->widgetSchema[$field]))
{
if(!$this->getWidget($field) instanceof sfWidgetFormInputText)
{
$this->setWidget($field, new sfWidgetFormInputText(array(), array('disabled'=>true)));
$this->setValidator($field, new sfValidatorPass());
}
$value = isset($defaults[$field]) ? $defaults[$field] : $this->getObject()->get($field);
try{
$this->defaults[$field] = self::getDateFormater()->format($value, BaseFormDoctrine::getDatePattern($this->getUser()->getCulture()));
}catch(Exception $e){}
}
}
}
/**
* Returns the correct formatting pattern
* for date according to user culture.
* @return string the correct pattern to format dates for user
*/
public static function getDatePattern($culture)
{
$dateFormater = BaseFormDoctrine::getDateFormater();
$cultureInfo = new sfCultureInfo($culture);
return $cultureInfo->getDateTimeFormat()->getLongDatePattern() . ' ' . $cultureInfo->getDateTimeFormat()->getLongTimePattern();
}
/**
* Creating a sfDateFormat is time and memory consuming.
*
* @return sfDateFormat
*/
public static function getDateFormater()
{
if(null === self::$dateFormatter)
{
self::$dateFormatter = new sfDateFormat($culture = dmContext::getInstance()->getServiceContainer()->getService('user')->getCulture());
}
return self::$dateFormatter;
}
/**
* @return myUser
*/
public function getUser()
{
return $this->getService('user');
}
/**
* (non-PHPdoc)
* @see sfFormSymfony::doBind()
*/
protected function doBind(array $values)
{
unset($this['created_at'], $this['created_by'], $this['updated_at'], $this['updated_by']);
parent::doBind($values);
}
/**
* (non-PHPdoc)
* @see sfFormDoctrine::doUpdateObject()
*/
protected function doUpdateObject($values)
{
unset($values['created_at'], $values['created_by'], $values['updated_at'], $values['updated_by']);
parent::doUpdateObject($values);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment