Exemple #1
0
 /**
  * @param unknown_type $formName
  * @param unknown_type $options
  * @throws Exception
  * @return Zend_Form
  */
 public function getForm($formName, $options = null, $useTemplate = false)
 {
     if (is_null(self::$_forms)) {
         if (is_file(APPLICATION_PATH . '/configs/forms.ini')) {
             $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/forms.ini');
         } else {
             $config = new Zend_Config(include_once APPLICATION_PATH . '/configs/forms.php');
         }
         $config = $config->toArray();
         self::$_forms = $config;
     }
     if (!array_key_exists($formName, $config)) {
         throw new Exception("Form not exist");
     }
     if ($useTemplate) {
         //$form = new Zend_Form_Template($config[$formName]);
         //$form->setTemplate($formName, $options);
         $form = new Zend_Form($config[$formName]);
         $elements = $form->getElements();
         foreach ($elements as &$element) {
             $element->removeDecorator('Label');
             $element->removeDecorator('HtmlTag');
             $element->removeDecorator('DtDdWrapper');
             $element->removeDecorator('Description');
             $element->removeDecorator('Errors');
             $element->addDecorator(array('data' => 'Errors'), array('tag' => 'p', 'class' => 'description'));
         }
         $filter = new Zend_Filter_Word_CamelCaseToDash();
         $formName = $filter->filter($formName);
         $options['viewScript'] = 'forms/' . $formName . '.phtml';
         $form->setDecorators(array(array('viewScript', $options)));
     } else {
         $form = new Zend_Form($config[$formName]);
         /*  $form->addElementPrefixPath('Zend_Decorator',
             'Zend/Decorator/',
             'decorator');
             $form->setDecorators(array('Default'));*/
         //Zend_Debug::dump($form->getDecorator('Errors'));
         /*$elements = $form->getElements();
                     foreach ($elements as &$element) {
         
                         $element->setDecorators(
                             array(
                             'ViewHelper',
                             'Errors',
                             array(array('data' => 'HtmlTag'), array('tag' => 'div', 'class' => 'element')),
                             'Label',
                             array(array('row' => 'HtmlTag'), array('tag' => 'li'))
                             )
                         );
         
                         //Zend_Debug::dump($element->getDecorator('Errors'));
         
                     };*/
         /*
                     $form->setElementDecorators(array(
                         'ViewHelper',
                         array('Errors', array('class' => 'help-inline control-error')),
                         array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'controls')),
                         array(array('label' => 'Label'), array('class' => 'control-label')),
                         array(array('data' => 'HtmlTag'), array('tag' => 'div', 'class' => 'control-group')),
                     ));
         */
         // $form->addElement("text", "naem", $opt);
         /* $form->setDecorators(array(
                'FormElements',
                array('Form', array('class' => 'form-horizontal'))
            ));*/
         //Zend_Debug::dump($form);
         // вынести декораторы
     }
     $formAction = $form->getAction();
     $routes = $this->getRouterNames();
     $actionParams = array();
     if (is_array($options) && array_key_exists('actionParams', $options)) {
         $actionParams = $options['actionParams'];
     }
     if (in_array($formAction, $routes)) {
         if (array_key_exists("actionParams", $config[$formName]) && is_array($config[$formName]['actionParams'])) {
             $actionParams = $config[$formName]['actionParams'];
         }
         $form->setAction($this->_url($actionParams, $formAction));
     }
     //Zend_Debug::dump($form);
     return $form;
 }