/** * Create document * * @return \Zend\View\Model\ViewModel|array */ public function createAction() { $documentForm = new Form\DocumentInformation(); $parentId = $this->getRouteMatch()->getParam('id'); if (!empty($parentId)) { $routeName = 'content/document/create-w-parent'; } else { $routeName = 'content/document/create'; } $documentForm->setAttribute('action', $this->url()->fromRoute($routeName, array('id' => $parentId))); $documentTypeCollection = new DocumentType\Collection(); $documentTypeElement = $documentForm->get('document_type'); if (empty($parentId)) { $documentTypeElement->setValueOptions(array('' => 'Select document type') + $documentTypeCollection->getSelect()); } else { $documentForm->get('parent')->setValue($parentId); $documentTypeCollection->init(DocumentModel::fromId($parentId)->getDocumentTypeId()); $documentTypeElement->setValueOptions(array('' => 'Select document type') + $documentTypeCollection->getSelect()); } if ($this->getRequest()->isPost()) { $documentForm->getInputFilter()->add(array('required' => true, 'validators' => array(array('name' => 'not_empty'))), 'document_type'); $documentForm->setData($this->getRequest()->getPost()->toArray()); if (!$documentForm->isValid()) { $this->flashMessenger()->addErrorMessage('Invalid document data'); $this->useFlashMessenger(); } else { $documentName = $documentForm->getValue('document-name'); $documentUrlKey = $documentForm->getValue('document-url_key'); $documentTypeId = $documentForm->getValue('document_type'); $parentId = $documentForm->getValue('parent'); $document = new DocumentModel(); $document->setName($documentName)->setDocumentTypeId($documentTypeId)->setParentId($parentId)->setUrlKey(!empty($documentUrlKey) ? $documentUrlKey : $this->checkUrlKey($documentName))->setUserId($this->getServiceLocator()->get('Auth')->getIdentity()->getId()); $document->save(); $this->flashMessenger()->addSuccessMessage('This document has been saved'); $this->redirect()->toRoute('content/document/edit', array('id' => $document->getId())); } } return array('form' => $documentForm); }
/** * Load properties from document type, tab and document * * @param integer $documentTypeId Document type id * @param DocumentModel $document Document model * @param ServiceManager $serviceLocator Service manager * * @return array */ public function load($documentTypeId, DocumentModel $document, ServiceManager $serviceLocator) { $tabs = $this->loadTabs($documentTypeId); $tabsArray = array(); $idx = 1; foreach ($tabs as $tab) { $tabsArray[] = $tab->getName(); $properties = $this->loadProperties($documentTypeId, $tab->getId(), $document->getId()); $fieldset = new ZendForm\Fieldset('tabs-' . $idx); foreach ($properties as $property) { $elements = AbstractForm::addContent($fieldset, Datatype\Model::loadEditor($serviceLocator, $property)); if (!is_array($elements)) { $elements = array($elements); } foreach ($elements as $element) { if (empty($element)) { continue; } $element->setOption('required', $property->isRequired()); $element->setOption('description', $property->getDescription()); } } $this->add($fieldset); $idx++; } $formDocumentAdd = new DocumentInformation(); $formDocumentAdd->load($document, $serviceLocator->get('Config')); $formDocumentAdd->setAttribute('name', 'tabs-' . $idx); $this->add($formDocumentAdd); return $tabsArray; }