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 = []; if (preg_match_all('/(?:\<[^\>]+\>)|(?:&[a-zA-Z]+;)|(?:&#[0-9+];)/', $label, $matches)) { $matches = $matches[0]; $replacements = []; 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', ['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 = [ 'label' => $field['label'], 'required' => ('y' == $field['required']), 'rows' => 5, 'description' => $field['description'], 'decorators' => [ ['Description', ['tag' => 'p', 'class' => 'description']], 'ViewHelper', 'Errors', ['HtmlTag', ['tag' => 'dd', 'id' => $field['name'] . '-element']], ['Label', ['tag' => 'dt']], ], ]; $this->addElement('textarea', $field['name'], array_merge($defaultOptions, $options)); $this->{$field['name']}->addValidator('EmailAddresses') ->addFilter('EmailAddresses'); break; case 'Select': $defaultOptions = [ 'label' => $field['label'], 'required' => ('y' == $field['required']), 'size' => (isset($options['size'])) ? $options['size'] : null, 'description' => (isset($field['description'])) ? $field['description'] : null, 'multiple' => (isset($options['multiple'])) ? $options['multiple'] : [], ]; $this->addElement($elementType, $field['name'], array_merge($defaultOptions, $options)); break; case 'HtmlEditor': $defaultOptions = [ '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 = ['label' => $label]; $this->addElement($elementType, $field['name'], array_merge($defaultOptions, $options)); break; case 'Static': $defaultOptions = ['label' => $field['label'], 'value' => $field['value']]; $this->addElement($elementType, $field['name'], array_merge($defaultOptions, $options)); break; case 'Url': $defaultOptions = [ '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; default: $defaultOptions = [ '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']); /** @var Qs_Form_Decorator_Label $label */ if (($label = $element->getDecorator('Label'))) { $label->setOption('escape', false); if (!$element->getLabel()) { $label->setTagOption('class', 'hidden'); } } if ($element->isRequired() && $element->getValidator('Int')) { if (!$element->getValidator('NotEmpty')) { $validators = $element->getValidators(); $notEmpty = ['validator' => 'NotEmpty', 'breakChainOnFailure' => true]; array_unshift($validators, $notEmpty); $element->setValidators($validators); } $types = [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', [$this->getId()]); return parent::render($view); } }