Skip to content

Instantly share code, notes, and snippets.

@fc
Created August 17, 2008 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fc/5839 to your computer and use it in GitHub Desktop.
Save fc/5839 to your computer and use it in GitHub Desktop.
<?php
class Project_Form extends Zend_Form
{
private $_uid = 0;
/*
public $elementDecorators = array(
'ViewHelper',
'Errors',
array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')),
array('Label', array('tag' => 'td'),
array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
));
public $buttonDecorators = array(
'ViewHelper',
array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')),
array(array('label' => 'HtmlTag'), array('tag' => 'td', 'placement' => 'prepend')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
);
*/
public function __construct($options = null)
{
if (is_array($options)) {
$this->setOptions($options);
} elseif ($options instanceof Zend_Config) {
$this->setConfig($options);
}
$this->getPluginLoader(Zend_Form::ELEMENT)->addPrefixPath('Project_Form_Element', 'Project/Form/Element');
$this->addElementPrefixPath('Project_Filter', 'Project/Filter', Zend_Form_Element::FILTER);
$this->addElementPrefixPath('Project_Validate', 'Project/Validate', Zend_Form_Element::VALIDATE);
$this->init();
$this->loadDefaultDecorators();
/* example
$this->setElementDecorators(array(
array('ViewHelper'),
array('Description', array('placement' => Zend_Form_Decorator_Abstract::PREPEND)),
array('Errors'),
array('HtmlTag', array('tag' => 'dd')),
array('Label', array('tag' => 'dt')),
)
);
*/
}
public function setOptions(array $options) {
parent::setOptions($options);
}
public function setValues($defaults) {
if ($defaults instanceof Zend_Db_Table_Rowset_Abstract) {
return $this->setDefaults($defaults->current()->toArray());
}
return $this->setDefaults($defaults);
}
public function addDescription($desc, $label=null) {
$decorators = array(
array('Description', array('placement' => Zend_Form_Decorator_Abstract::PREPEND)),
);
if ($label!==null) {
$decorators[] = array('Label', array('tag' => 'dt'));
}
$t = $this->addElement('text', 'desc_'.$this->_uid++, array(
'label' => $label,
'description' => $desc,
'decorators' => $decorators,
));
}
private $_tableGroupBase = '__table-group-';
public function addTableGroup(array $rowsets, $options=null) {
if ($options===null) {
$options = array(
'return' => false,
'class' => '',
);
} else {
if (!isset($options['return'])) {
$options['return'] = false;
}
}
$form = new Project_Form_SubForm();
$form->setIsArray(false);
$form->clearDecorators();
$body = new Project_Form_SubForm();
$body->setIsArray(false);
foreach($rowsets as $id => $rowset){
$rowForm = new Project_Form_SubForm();
$rowForm->setIsArray(false);
foreach ($rowset as $el) {
if (is_array($el)) {
$newOptions = $options;
$newOptions['return'] = true;
$el = $this->addTableGroup( $el , $newOptions);
$tmp = new Project_Form_SubForm();
$tmp->setIsArray(false);
$tmp->clearDecorators();
$tmp->setDecorators(array(
'FormElements',
array('HtmlTag', array('tag' => 'td')),
));
$tmp->addSubForm($el, $this->_tableGroupBase.$this->_uid++);
$rowForm->addSubForm($tmp, $this->_tableGroupBase.$this->_uid++);
continue;
} else if( is_null($el) ) {
$el = $this->createElement('hidden', $this->_tableGroupBase.$this->_uid++, array('ignore' => true));
}
$rowForm->addElement( $el );
$rowForm->setElementDecorators(array(
'ViewHelper',
'Errors',
'Label',
array('HtmlTag', array('tag' => 'td')),
));
}
$body->addSubForm($rowForm, $this->_tableGroupBase.$this->_uid++);
}
$form->setDecorators(array(
'FormElements',
array('HtmlTag', array('tag' => 'table', 'class' => $options['class'])),
));
$body->setDecorators(array(
'FormElements',
array('HtmlTag', array('tag' => 'tbody')),
));
$body->setSubFormDecorators(array(
'FormElements',
array('HtmlTag', array('tag'=>'tr')),
));
$form->addSubForm($body, $this->_tableGroupBase.$this->_uid++);
if ($options['return']) {
return $form;
}
$this->addSubForm($form, $this->_tableGroupBase.$this->_uid++);
}
public function getValues($suppressArrayNotation = false)
{
$values = array();
foreach ($this->getElements() as $key => $element) {
if (!$element->getIgnore()) {
$values[$key] = $element->getValue();
}
}
foreach ($this->getSubForms() as $key => $subForm) {
$array = $this->_getArrayName($subForm->getElementsBelongTo());
if (empty($array)) {
if (preg_match('~^'.$this->_tableGroupBase.'~', $key)) {
$values = array_merge($values, $subForm->getValues(true));
} else {
$values[$key] = $subForm->getValues(true);
}
} else {
$values[$array] = $subForm->getValues(true);
}
}
if (!$suppressArrayNotation && $this->isArray()) {
$values = array(
$this->getElementsBelongTo() => $values
);
}
return $values;
}
}
<?php
class Project_Form_Course extends Project_Form {
public function init() {
$this->addTableGroup(array(array(
$this->createElement('textarea', 'other_description', array(
'label' => 'other_description',
'rows' => 6,
'cols' => 25,
'description' => '',
'decorators' => array(
array('ViewHelper'),
array('Description', array('placement' => Zend_Form_Decorator_Abstract::PREPEND)),
array('Errors'),
array('HtmlTag', array('tag' => 'dd')),
array('Label', array('tag' => 'dt')),
)
)),
$this->createElement('text', 'other_requests', array(
'label' => 'budget_request$',
)),
)), array('class'=>'budget-request'));
}
}
<?php
class Project_Form_SubForm extends Project_Form
{
/**
* Whether or not form elements are members of an array
* @var bool
*/
protected $_isArray = true;
/**
* Load the default decorators
*
* @return void
*/
public function loadDefaultDecorators()
{
if ($this->loadDefaultDecoratorsIsDisabled()) {
return;
}
$decorators = $this->getDecorators();
if (empty($decorators)) {
$this->addDecorator('FormElements')
->addDecorator('HtmlTag', array('tag' => 'dl'))
->addDecorator('Fieldset')
->addDecorator('DtDdWrapper');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment