GcDevelopment\Form\DocumentType::setValues PHP 메소드

setValues() 공개 메소드

Set values and create tabs and properties FieldSet from parameter
public setValues ( mixed $element ) : null | DocumentType
$element mixed \Gc\DocumentType\Model | array
리턴 null | DocumentType
    public function setValues($element)
    {
        if ($element instanceof DocumentTypeModel) {
            $infosForm = $this->getInfos();
            $infosForm->get('name')->setValue($element->getName());
            $infosForm->get('description')->setValue($element->getDescription());
            $infosForm->get('icon_id')->setValue($element->getIconId());
            $dependency = $infosForm->get('infos[dependency]');
            if (!empty($dependency)) {
                $dependency->setValue($element->getDependencies());
            }
            $viewsForm = $this->getViews();
            $viewsForm->get('default_view')->setValue($element->getDefaultViewId());
            $viewsCollection = $element->getAvailableViews();
            $viewsForm->get('available_views')->setValue($viewsCollection->getSelect());
            $tabs = $element->getTabs();
            $tabCollection = new Tab\Collection();
            $this->getTabs()->get('tabs_list')->setValueOptions($tabCollection->getImportableTabs($element->getId()));
            foreach ($tabs as $tabId => $tab) {
                $this->addTab($tab);
                $properties = $tab->getProperties();
                foreach ($properties as $property) {
                    $this->addProperty($property);
                }
            }
        } else {
            if (empty($element['tabs'])) {
                return;
            }
            $tabSelect = array();
            foreach ($element['tabs'] as $tabId => $tab) {
                if (!is_array($tab)) {
                    continue;
                }
                $tab['id'] = $tabId;
                $this->addTab($tab);
                $tabSelect[$tabId] = $tab['name'];
            }
            foreach ($element['properties'] as $propertyId => $property) {
                if (!is_array($property)) {
                    continue;
                }
                $property['id'] = $propertyId;
                $this->addProperty($property);
            }
        }
        return $this;
    }

Usage Example

예제 #1
0
 /**
  * Edit document type
  *
  * @return \Zend\View\Model\ViewModel|array
  */
 public function editAction()
 {
     $documentTypeId = $this->getRouteMatch()->getParam('id');
     $documentType = DocumentType\Model::fromId($documentTypeId);
     if (empty($documentType)) {
         return $this->redirect()->toRoute('development/document-type/create');
     }
     $form = new DocumentTypeForm();
     $form->setAttribute('action', $this->url()->fromRoute('development/document-type/edit', array('id' => $documentTypeId)));
     $request = $this->getRequest();
     $session = $this->getSession();
     if (!$request->isPost()) {
         $form->setValues($documentType);
         $this->prepareDocumentTypeSession($session, $documentType);
     } else {
         $validators = $form->getInputFilter()->get('infos')->get('name')->getValidatorChain()->getValidators();
         foreach ($validators as $validator) {
             if ($validator['instance'] instanceof Validator\Db\NoRecordExists) {
                 $validator['instance']->setExclude(array('field' => 'id', 'value' => $documentType->getId()));
             }
         }
         $postData = $this->getRequest()->getPost()->toArray();
         $form->setData($postData);
         $form->setValues($postData);
         if (!$form->isValid()) {
             $this->flashMessenger()->addErrorMessage('Can not save document type');
             $this->useFlashMessenger();
         } else {
             $propertyCollection = new Property\Collection();
             $input = $form->getInputFilter();
             $infosSubform = $input->get('infos');
             $viewsSubform = $input->get('views');
             $documentType->addData(array('name' => $infosSubform->getValue('name'), 'icon_id' => $infosSubform->getValue('icon_id'), 'description' => $infosSubform->getValue('description'), 'default_view_id' => $viewsSubform->getValue('default_view')));
             $documentType->getAdapter()->getDriver()->getConnection()->beginTransaction();
             try {
                 $availableViews = $viewsSubform->getValue('available_views');
                 if (empty($availableViews)) {
                     $availableViews = array();
                 }
                 $documentType->addViews($availableViews);
                 $documentType->setDependencies($infosSubform->getValue('dependency'));
                 $documentType->save();
                 $existingTabs = $this->saveTabs($input->get('tabs'), $documentType);
                 $tabCollection = new Tab\Collection();
                 $tabs = $tabCollection->load($documentType->getId())->getTabs();
                 foreach ($tabs as $tab) {
                     if (!in_array($tab->getId(), $existingTabs)) {
                         $tab->delete();
                     }
                 }
                 $existingProperties = $this->saveProperties($input->get('properties'), $existingTabs);
                 $propertyCollection = new Property\Collection();
                 $properties = $propertyCollection->load($documentType->getId())->getProperties();
                 foreach ($properties as $property) {
                     if (!in_array($property->getId(), $existingProperties)) {
                         $property->delete();
                     }
                 }
                 $documentType->getAdapter()->getDriver()->getConnection()->commit();
                 $this->flashMessenger()->addSuccessMessage('This document type has been saved');
                 return $this->redirect()->toRoute('development/document-type/edit', array('id' => $documentTypeId));
             } catch (Exception $e) {
                 $documentType->getAdapter()->getDriver()->getConnection()->rollBack();
                 throw new \Gc\Exception('Error Processing Request ' . print_r($e, true), 1);
             }
         }
     }
     return array('form' => $form);
 }
All Usage Examples Of GcDevelopment\Form\DocumentType::setValues