Exemple #1
0
 /**
  * Sets the form instance to be used
  *
  * @param Bvb_Grid_Form $crud Bvb_Grid_Form Instance
  *
  * @return Bvb_Grid_Deploy_Table
  *
  */
 public function setForm(Bvb_Grid_Form $crud)
 {
     $this->emitEvent('crud.set_form', array('form' => $crud));
     $this->_bvbForm = $crud;
     $this->setAjax(false);
     $oldElements = $crud->getElements();
     $crud->clearElements();
     $displayGroupElements = array();
     if (count($crud->getForm()->getDisplayGroups()) > 0) {
         foreach ($crud->getForm()->getDisplayGroups() as $group) {
             $displayGroupElements = array_merge($displayGroupElements, $group->getElements());
         }
     }
     $formElements = $this->getSource()->buildForm($crud->getInputsType());
     if ($crud->getUsePreDefinedFormElements() == true) {
         $formElements = array();
     }
     $this->emitEvent('crud.elements_loaded', array('elements' => &$formElements));
     if ($this->getParam('add')) {
         $formsCount = $crud->getBulkAdd() > 0 ? $crud->getBulkAdd() : 1;
     } elseif ($this->getParam('edit')) {
         $totalMassIds = explode(',', $this->getParam('postMassIds'));
         $totalMassIds = count($totalMassIds);
         $formsCount = $totalMassIds > 0 ? $totalMassIds : 1;
     } else {
         $formsCount = 1;
     }
     if ($crud->getBulkDelete() == true) {
         $this->getMassActions()->addMassActions(array(array('url' => $this->getUrl() . '/zfmassremove' . $this->getGridId() . '/1/', 'caption' => $this->__('Remove Selected Records'), 'confirm' => $this->__('Are you sure?'))));
     }
     if ($crud->getBulkEdit() == true) {
         $editMassUrl = $this->getUrl() . '/zfmassedit' . $this->getGridId() . '/1/edit' . $this->getGridId() . '/1';
         $this->getMassActions()->addMassActions(array(array('url' => $editMassUrl, 'caption' => $this->__('Edit Selected Records'))));
     }
     $this->_crud = $crud;
     $arr = array();
     if ($crud->getUseVerticalInputs() === false) {
         $arr[0] = new Zend_Form_SubForm($formElements);
         if ($formsCount > 1) {
             $arr[0]->addElement('checkbox', 'ZFIGNORE', array('label' => $this->__('Ignore'), 'order' => 0));
         }
         $arr[0]->setElementDecorators($crud->getSubformElementTitle());
         if ($crud->getUseDecorators() === true) {
             if ($crud->getUseVerticalInputs()) {
                 $verticalInputs = $crud->getSubFormDecorator();
             } else {
                 $verticalInputs = $crud->getSubFormDecoratorVertical();
             }
             $arr[0]->setDecorators($verticalInputs);
         }
         $crud->getForm()->addSubForm($arr[0], 0);
         foreach ($crud->getForm()->getSubForm(0)->getElements() as $value) {
             $value->clearValidators();
             $value->setRequired(false);
         }
     }
     for ($i = 1; $i <= $formsCount; $i++) {
         $arr[$i] = new Zend_Form_SubForm($formElements);
         if ($formsCount > 1) {
             $arr[$i]->addElement('checkbox', 'ZFIGNORE', array('label' => $this->__('Ignore record'), 'order' => 0));
         }
         if ($crud->getUseDecorators() === true) {
             if ($crud->getUseVerticalInputs()) {
                 $verticalInputs = $crud->getSubFormDecorator();
             } else {
                 $verticalInputs = $crud->getSubFormDecoratorVertical();
             }
             $arr[$i]->setDecorators($verticalInputs);
         }
         if ($this->getParam('edit')) {
             $arr[$i]->addElement('hidden', 'ZFPK', array('decorators' => $crud->getButtonHiddenDecorator()));
         }
         if ($this->getParam('add')) {
             $fieldsDisallowed = array_merge($crud->getDisallowedFields(), array_keys($crud->getOnAddForce()));
             $crud->setDisallowedFields($fieldsDisallowed);
         }
         if ($this->getParam('edit')) {
             $fieldsDisallowed = array_merge($crud->getDisallowedFields(), array_keys($crud->getOnEditForce()));
             $crud->setDisallowedFields($fieldsDisallowed);
         }
         $crud->getForm()->addSubForm($arr[$i], $i);
         $form = $crud->getForm()->getSubForm($i);
         foreach ($oldElements as $key => $value) {
             if ($value->helper == 'formHidden') {
                 $value->setDecorators($crud->getButtonHiddenDecorator());
             }
             $form->addElement($value);
         }
         if (count($form->getElements()) > 0) {
             foreach ($form->getElements() as $key => $value) {
                 if ($value->helper == 'formHidden') {
                     continue;
                 }
                 if ($crud->getUseDecorators() === true) {
                     if ($crud->getUseVerticalInputs()) {
                         $decorator = $crud->getSubformElementDecorator();
                     } else {
                         $decorator = $crud->getSubformElementDecoratorVertical();
                     }
                     $value->setDecorators($decorator);
                 }
             }
         }
         if ($crud->getFieldsBasedOnQuery() == 1) {
             $finalFieldsForm = array();
             $fieldsToForm = $this->getFields(true);
             foreach ($fieldsToForm as $key => $value) {
                 $field = substr($value['field'], strpos($value['field'], '.') + 1);
                 $finalFieldsForm[] = $field;
             }
             foreach ($form->getElements() as $key => $value) {
                 if ($key == 'ZFIGNORE' || $key == 'ZFPK') {
                     continue;
                 }
                 if (!in_array($key, $finalFieldsForm)) {
                     $form->removeElement($key);
                 }
             }
         }
         if (count($crud->getAllowedFields()) > 0) {
             foreach ($form->getElements() as $key => $value) {
                 if ($key == 'ZFIGNORE' || $key == 'ZFPK') {
                     continue;
                 }
                 if (!in_array($key, $crud->getAllowedFields())) {
                     $form->removeElement($key);
                 }
             }
         }
         if (count($crud->getDisallowedFields()) > 0) {
             foreach ($form->getElements() as $key => $value) {
                 if ($key == 'ZFIGNORE' || $key == 'ZFPK') {
                     continue;
                 }
                 if (in_array($key, $crud->getDisallowedFields())) {
                     $form->removeElement($key);
                 }
             }
         }
         foreach ($this->_data['fields'] as $key => $title) {
             if ($form->getElement($key) && strlen($form->getElement($key)->getLabel()) == 0) {
                 $form->getElement($key)->setLabel($title['title']);
             }
         }
         if (count($form->getElements()) == 0) {
             throw new Bvb_Grid_Exception($this->__("Your form does not have any fields"));
         }
         if (count($displayGroupElements) > 0) {
             foreach ($displayGroupElements as $key => $value) {
                 $form->removeElement($key);
             }
         }
         foreach ($form->getElements() as $element) {
             if ($element->helper == 'formFile') {
                 if ($crud->getUseDecorators() === true) {
                     $element->setDecorators($crud->getFileDecorator());
                 }
             }
             if ($element->helper == 'formHidden') {
                 $element->setLabel('');
             }
         }
         if ($formsCount == 1 && $crud->getFormTitle() && $crud->getUseVerticalInputs()) {
             $form->addElement('text', 'infoTextHeader', array('ignore' => true, 'label' => $crud->getFormTitle(), 'required' => false, 'order' => -1, 'decorators' => array(array('Label'), array(array('data2' => 'HtmlTag'), array('tag' => 'th', 'colspan' => '2')), array(array('row' => 'HtmlTag'), array('tag' => 'thead'), array('tag' => 'tr')))));
         }
     }
     if ($crud->getUseVerticalInputs() === false) {
         foreach ($crud->getForm()->getSubForm(0)->getElements() as $key => $value) {
             if (!in_array($key, array_keys($crud->getForm()->getSubForm(1)->getElements()))) {
                 $crud->getForm()->getSubForm(0)->removeElement($key);
             }
         }
     }
     if (count($crud->getForm()->getDisplayGroups()) > 0) {
         foreach ($crud->getForm()->getDisplayGroups() as $group) {
             $group->setDecorators($crud->getDisplayGroupsDecorator());
         }
     }
     if ($crud->getUseDecorators() === true) {
         $crud->getForm()->setDecorators($crud->getFormDecorator());
     } else {
         $crud->getForm()->setDecorators($crud->getFormDecoratorSimple());
     }
     $crud->getForm()->setMethod('post');
     if (isset($crud->options['saveAndAddButton']) && $crud->options['saveAndAddButton'] == true && !$this->getParam('edit')) {
         $crud->getForm()->addElement('submit', 'saveAndAdd' . $this->getGridId(), array('label' => $this->__('Save And New'), 'class' => 'submit', 'decorators' => $crud->getButtonHiddenDecorator()));
     }
     $crud->getForm()->addElement('submit', 'form_submit' . $this->getGridId(), array('label' => $this->__('Save'), 'class' => 'submit', 'decorators' => $crud->getButtonHiddenDecorator()));
     $crud->getForm()->addElement('hidden', 'zfg_form_edit' . $this->getGridId(), array('value' => 1, 'decorators' => $crud->getButtonHiddenDecorator()));
     if ($crud->getUseCSRF() == 1) {
         $crud->addElement('hash', 'zfg_csrf' . $this->getGridId(), array('salt' => 'unique', 'decorators' => $crud->getButtonHiddenDecorator()));
     }
     $url = $this->getUrl(array_merge(array('add', 'postMassIds', 'zfmassedit', 'edit', 'form_reset'), array_keys($crud->getForm()->getElements())));
     $crud->getForm()->addElement('button', 'form_reset' . $this->getGridId(), array('onclick' => "window.location='{$url}'", 'label' => $this->__('Cancel'), 'class' => 'reset', 'decorators' => $crud->getButtonHiddenDecorator()));
     $crud->getForm()->addDisplayGroup(array('zfg_csrf' . $this->getGridId(), 'zfg_form_edit' . $this->getGridId(), 'form_submit' . $this->getGridId(), 'saveAndAdd' . $this->getGridId(), 'form_reset' . $this->getGridId()), 'buttons', array('decorators' => $crud->getSubformGroupDecorator()));
     $crud->setAction($this->getUrl(array_keys($crud->getForm()->getElements())));
     $this->emitEvent('crud.form_built', array('form' => $crud));
     $this->_crudOptions['addForce'] = $crud->getOnAddForce();
     $this->_crudOptions['editForce'] = $crud->getOnEditForce();
     $this->_crudOptions['editAddCondition'] = $crud->getOnEditAddCondition();
     $this->_crudOptions['deleteAddCondition'] = $crud->getOnDeleteAddCondition();
     $this->_form = $crud->getForm();
     $this->_bvbForm = $crud;
     $crud = $this->_object2array($crud);
     $options = $crud['options'];
     if (isset($options['table']) && is_string($options['table'])) {
         $this->_crudTable = $options['table'];
     }
     if (isset($options['isPerformCrudAllowed']) && $options['isPerformCrudAllowed'] == 0) {
         $this->_crudTableOptions['add'] = 0;
         $this->_crudTableOptions['edit'] = 0;
         $this->_crudTableOptions['delete'] = 0;
     } else {
         $this->_crudTableOptions['add'] = 1;
         $this->_crudTableOptions['edit'] = 1;
         $this->_crudTableOptions['delete'] = 1;
     }
     if (isset($options['isPerformCrudAllowedForAddition']) && $options['isPerformCrudAllowedForAddition'] == 1) {
         $this->_crudTableOptions['add'] = 1;
     } elseif (isset($options['isPerformCrudAllowedForAddition']) && $options['isPerformCrudAllowedForAddition'] == 0) {
         $this->_crudTableOptions['add'] = 0;
     }
     if (isset($options['isPerformCrudAllowedForEdition']) && $options['isPerformCrudAllowedForEdition'] == 1) {
         $this->_crudTableOptions['edit'] = 1;
     } elseif (isset($options['isPerformCrudAllowedForEdition']) && $options['isPerformCrudAllowedForEdition'] == 0) {
         $this->_crudTableOptions['edit'] = 0;
     }
     if (isset($options['isPerformCrudAllowedForDeletion']) && $options['isPerformCrudAllowedForDeletion'] == 1) {
         $this->_crudTableOptions['delete'] = 1;
     } elseif (isset($options['isPerformCrudAllowedForDeletion']) && $options['isPerformCrudAllowedForDeletion'] == 0) {
         $this->_crudTableOptions['delete'] = 0;
     }
     $this->_info['doubleTables'] = $this->getInfo("doubleTables");
     if (isset($options['delete'])) {
         if ($options['delete'] == 1) {
             $this->_allowDelete = true;
             if (isset($options['onDeleteAddWhere'])) {
                 $this->_info['delete']['where'] = $options['onDeleteAddWhere'];
             }
         }
     }
     if (isset($options['add']) && $options['add'] == 1) {
         if (!isset($options['addButton'])) {
             $options['addButton'] = 0;
         }
         $this->_allowAdd = true;
         $this->_allowAddButton = $options['addButton'];
     }
     if (isset($options['edit']) && $options['edit'] == 1) {
         $this->_allowEdit = true;
     }
     $this->_buildFormValues();
     return $this;
 }