addElementPrefixPath('App_Settings_Validate', 'App/Settings/Validate/', 'validate'); return $this; } protected function _getDataObj() { if (null === $this->_dataObj) { $this->_dataObj = new App_Settings_Obj(); } return $this->_dataObj; } public function setIdCategory($idCategory) { $this->_idCategory = $idCategory; $this->_getDataObj()->setIdCategory($this->_idCategory); return $this; } protected function _prepareFieldLabel(&$label) { if (false !== strpos($label, '<')) { $matches = array(); if (preg_match_all('/(?:\<[^\>]+\>)|(?:&[a-zA-Z]+;)|(?:&#[0-9+];)/', $label, $matches)) { $matches = $matches[0]; $replacements = array(); foreach ($matches as $index => $string) { $placeholder = '__TAG_PLACEHOLDER_' . $index . '__'; $replacements[$placeholder] = $string; $label = str_replace($string, $placeholder, $label); } $label = htmlspecialchars($label); $label = htmlspecialchars($label); $label = str_replace(array_keys($replacements), array_values($replacements), $label); } } return $this; } protected function _initElements() { $this->addElement('hidden', 'idCategory', array('value' => $this->_idCategory)); $fields = $this->_getDataObj()->getList(); foreach ($fields as $field) { $elementType = ucfirst($field['fieldType']); $options = $this->_getDataObj()->getFieldOptions($field['name']); $this->_prepareFieldLabel($field['label']); switch ($elementType) { case 'Emails': $defaultOptions = array( 'label' => $field['label'], 'required' => ('y' == $field['required']), 'rows' => 5, 'description' => $field['description'], 'decorators' => array( array('Description', array('tag' => 'p', 'class' => 'description')), 'ViewHelper', 'Errors', array('HtmlTag', array('tag' => 'dd', 'id' => $field['name'] . '-element')), array('Label', array('tag' => 'dt')) ) ); $this->addElement('textarea', $field['name'], array_merge($defaultOptions, $options)); $this->{$field['name']}->addValidator('EmailAddresses') ->addFilter('EmailAddresses'); break; case 'Select': $defaultOptions = array( 'label' => $field['label'], 'required' => ('y' == $field['required']), 'size' => $options['size'], 'description' => $field['description'], 'multiple' => $options['multiple'] ); $this->addElement($elementType, $field['name'], array_merge($defaultOptions, $options)); break; case 'HtmlEditor': $defaultOptions = array( 'label' => $field['label'], 'required' => ('y' == $field['required']), 'description' => $field['description'] ); $this->addElement($elementType, $field['name'], array_merge($defaultOptions, $options)); break; case 'Header': if (!empty($field['label'])) { $label = $field['label']; } else { $this->_prepareFieldLabel($field['value']); $label = $field['value']; } $defaultOptions = array('label' => $label); $this->addElement($elementType, $field['name'], array_merge($defaultOptions, $options)); break; case 'Static': $defaultOptions = array('label' => $field['label'], 'value' => $field['value']); $this->addElement($elementType, $field['name'], array_merge($defaultOptions, $options)); break; case 'Url': $defaultOptions = array( 'label' => $field['label'], 'required' => ('y' == $field['required']), 'description' => $field['description'] ); $this->addElement('text', $field['name'], array_merge($defaultOptions, $options)); $this->{$field['name']}->addValidator('Url')->addFilter('StringTrim'); break; case 'Checkbox': $defaultOptions = array( 'label' => $field['label'], 'value' => $field['value'], 'decoration' => 'simple'); $this->addElement('checkbox', $field['name'], array_merge($defaultOptions, $options)); break; default: $defaultOptions = array( 'label' => $field['label'], 'required' => ('y' == $field['required']), 'description' => $field['description'] ); if (class_exists($field['fieldType'])) { $reflection = new ReflectionClass($field['fieldType']); if ($reflection->isSubclassOf('Zend_Form_Element')) { $element = $reflection->newInstance($field['name'], array_merge($defaultOptions, $options)); $this->addElement($element); break; } } $this->addElement($elementType, $field['name'], array_merge($defaultOptions, $options)); break; } $element = $this->getElement($field['name']); if (($label = $element->getDecorator('Label'))) { $label->setOption('escape', false); } if ($element->isRequired() && $element->getValidator('Int')) { if (!$element->getValidator('NotEmpty')) { $validators = $element->getValidators(); $notEmpty = array('validator' => 'NotEmpty', 'breakChainOnFailure' => true); array_unshift($validators, $notEmpty); $element->setValidators($validators); } $types = array(Zend_Validate_NotEmpty::INTEGER, Zend_Validate_NotEmpty::ZERO, Zend_Validate_NotEmpty::SPACE, Zend_Validate_NotEmpty::NULL, Zend_Validate_NotEmpty::STRING); $element->getValidator('NotEmpty')->setType($types); } } return $this; } public function render(Zend_View_Interface $view = null) { /** @var $doc App_Doc_Admin */ $doc = Zend_Registry::get('doc'); $doc->addScript('js/lib/settings.js'); $doc->addInitFunction('App_Settings.init', array($this->getId())); return parent::render($view); } }