?
<?php
/**
* Created on 15 janv. 09 by Eric Jeker <eric.jeker@gmail.com> (http://www.wowww.ch)
*
* Decorators are inspired from http://sprawsm.com/uni-form/
**/
class My_Form_Uni extends Zend_Form {
/**
* Array of default element decorators
*/
protected $_defaultElementDecorators = null ;
/**
* Enable or disable the default decorators
*/
protected $_disableDefaultDecorators = false ;
/**
* Standard elements list
*/
protected $_standardElements = array(
'Zend_Form_Element_Button',
'Zend_Form_Element_Captcha',
'Zend_Form_Element_Hash',
'Zend_Form_Element_Checkbox',
'Zend_Form_Element_File',
'Zend_Form_Element_Multiselect',
'Zend_Form_Element_Password',
'Zend_Form_Element_Select',
'Zend_Form_Element_Text',
'Zend_Form_Element_Textarea',
'Zend_Form_Element_Xhtml',
) ;
/**
* Hidden elements list
*/
protected $_hiddenElements = array(
'Zend_Form_Element_Hidden'
) ;
/**
* Action elements list (no label displayed)
*/
protected $_actionElements = array(
'Zend_Form_Element_Image',
'Zend_Form_Element_Submit',
'Zend_Form_Element_Reset',
) ;
/**
* Multi elements list
*/
protected $_multiElements = array(
'Zend_Form_Element_Multi',
'Zend_Form_Element_MultiCheckbox',
'Zend_Form_Element_Radio',
) ;
public function __construct($options = null) {
if (is_array($options)) {
$this->setOptions($options);
} elseif ($options instanceof Zend_Config) {
$this->setConfig($options);
}
// Init the default decorators
$this->initDefaultElementsDecorators() ;
// Extensions...
$this->init();
$this->loadDefaultDecorators();
}
/**
* Set to true if you do not want to use the default decorators
*/
public function setDisableDefaultDecorators($flag) {
$this->_disableDefaultDecorators = (bool) $flag ;
return $this ;
}
/**
* Overriding the standard addElement method and call addElement
* with the default decorators if needed
*/
public function addElement($element, $name = null, $options = null) {
if ($this->_disableDefaultDecorators) {
return parent::addElement($element, $name, $options) ;
}
return $this->addElementWithDefaultDecorators($element, $name, $options) ;
}
/**
* Same as addElement but set the elements decorators
*/
public function addElementWithDefaultDecorators($element, $name = null, $options = null) {
if (is_string($element)) {
$element = $this->createElement($element, $name, $options) ;
}
if (!$options || !array_key_exists('decorators', $options)) {
$element->setDecorators($this->getDefaultElementDecorators(get_class($element))) ;
}
return parent::addElement($element, $name, $options) ;
}
/**
* Set decorators for an element class
*/
public function setDefaultElementDecorators($className, $decorators) {
$this->_defaultElementDecorators[$className] = $decorators ;
return $this ;
}
/**
* Get decorators from an element class name
*/
public function getDefaultElementDecorators($className) {
return $this->_defaultElementDecorators[$className] ;
}
/**
* Set decorators for an array of element class
*/
public function setDefaultElementsDecorators(array $classNames, $decorators) {
foreach ($classNames as $className) {
$this->setDefaultElementDecorators($className, $decorators) ;
}
return $this ;
}
/**
* Initialize the default element decorators
*/
public function initDefaultElementsDecorators() {
// Standard
$standardDecorators = array(
'ViewHelper',
'Label',
'Errors',
array('decorator' => array('Holder' => 'HtmlTag'), 'options' => array('tag' => 'div', 'class' => 'ctrlHolder'))
) ;
$this->setDefaultElementsDecorators($this->_standardElements, $standardDecorators) ;
// Hidden
$hiddenDecorators = array(
'ViewHelper',
array('decorator' => array('Holder' => 'HtmlTag'), 'options' => array('tag' => 'div', 'class' => 'hiddenHolder'))
) ;
$this->setDefaultElementsDecorators($this->_hiddenElements, $hiddenDecorators) ;
// No label
$actionDecorators = array(
'ViewHelper',
array('decorator' => array('Holder' => 'HtmlTag'), 'options' => array('tag' => 'div', 'class' => 'actionHolder'))
) ;
$this->setDefaultElementsDecorators($this->_actionElements, $actionDecorators) ;
// Multi Form Elements
$multiDecorators = array(
'ViewHelper',
array('decorator' => array('MultiHolder' => 'HtmlTag'), 'options' => array('tag' => 'div', 'class' => 'multiHolder')),
'Label',
array('decorator' => array('Holder' => 'HtmlTag'), 'options' => array('tag' => 'div', 'class' => 'ctrlHolder'))
) ;
$this->setDefaultElementsDecorators($this->_multiElements, $multiDecorators) ;
}
/**
* Load the default decorators for the current form
*/
public function loadDefaultDecorators() {
if ($this->loadDefaultDecoratorsIsDisabled()) {
return;
}
$decorators = $this->getDecorators();
if (empty($decorators)) {
$this->addDecorator('FormElements')
->addDecorator('Form');
}
}
}