saveEditor() public static method

Save editor
public static saveEditor ( Zend\ServiceManager\ServiceManager $serviceManager, Model $property ) : boolean
$serviceManager Zend\ServiceManager\ServiceManager Service manager
$property Gc\Property\Model Property
return boolean
Example #1
0
 /**
  * Edit Document
  *
  * @return \Zend\View\Model\ViewModel|array
  */
 public function editAction()
 {
     $documentId = $this->getRouteMatch()->getParam('id');
     $document = DocumentModel::fromId($documentId);
     if (empty($document)) {
         $this->flashMessenger()->addErrorMessage('Document does not exists!');
         return $this->redirect()->toRoute('content');
     }
     $documentForm = new Form\Document();
     $documentForm->init($this->url()->fromRoute('content/document/edit', array('id' => $documentId)));
     $hasError = false;
     $documentTypeId = $document->getDocumentTypeId();
     $oldUrlKey = $document->getUrlKey();
     if ($this->getRequest()->isPost()) {
         $documentVars = $this->getRequest()->getPost()->toArray();
         $document->setName(empty($documentVars['document-name']) ? $document->getName() : $documentVars['document-name']);
         $document->setStatus(empty($documentVars['document-status']) ? DocumentModel::STATUS_DISABLE : DocumentModel::STATUS_ENABLE);
         $document->showInNav(empty($documentVars['document-show_in_nav']) ? false : $documentVars['document-show_in_nav']);
         $document->canBeCached(empty($documentVars['document-can_be_cached']) ? false : $documentVars['document-can_be_cached']);
         $document->setLayoutId(empty($documentVars['document-layout']) ? false : $documentVars['document-layout']);
         $document->setViewId(empty($documentVars['document-view']) ? $document->getViewId() : $documentVars['document-view']);
         $document->setUrlKey(empty($documentVars['document-url_key']) ? '' : $documentVars['document-url_key']);
         $document->setLocale(empty($documentVars['document-locale']) ? null : $documentVars['document-locale']);
         $tabs = $documentForm->loadTabs($documentTypeId);
         foreach ($tabs as $tab) {
             $properties = $documentForm->loadProperties($documentTypeId, $tab->getId(), $document->getId());
             $connection = $document->getAdapter()->getDriver()->getConnection();
             try {
                 $connection->beginTransaction();
                 foreach ($properties as $property) {
                     $property->setDocumentId($document->getId())->loadValue();
                     if (!Datatype\Model::saveEditor($this->getServiceLocator(), $property)) {
                         $hasError = true;
                     }
                 }
                 if ($hasError) {
                     $connection->rollBack();
                 } else {
                     $connection->commit();
                 }
             } catch (Exception $e) {
                 $connection->rollBack();
             }
         }
     }
     $tabsArray = $documentForm->load($documentTypeId, $document, $this->getServiceLocator());
     $tabsArray[] = $this->getServiceLocator()->get('MvcTranslator')->translate('Document information');
     $formDocumentAdd = $documentForm->get('tabs-' . count($tabsArray));
     if ($this->getRequest()->isPost()) {
         $formDocumentAdd->setData($this->getRequest()->getPost()->toArray());
         if ($hasError or !$formDocumentAdd->isValid()) {
             $document->setStatus(DocumentModel::STATUS_DISABLE);
             $document->setUrlKey($oldUrlKey);
             $this->flashMessenger()->addErrorMessage('This document cannot be saved because one or more properties values are required !');
             $this->useFlashMessenger();
         } else {
             $this->flashMessenger()->addSuccessMessage('This document has been saved');
             $document->addData($formDocumentAdd->getInputFilter()->getValues());
             $document->save();
             return $this->redirect()->toRoute('content/document/edit', array('id' => $documentId));
         }
     }
     $this->layout()->setVariable('documentId', $documentId);
     return array('form' => $documentForm, 'tabs' => new Component\Tabs($tabsArray), 'document' => $document);
 }
Example #2
0
 /**
  * Test
  *
  * @return void
  */
 public function testSaveEditor()
 {
     $viewModel = ViewModel::fromArray(array('name' => 'View Name', 'identifier' => 'View identifier', 'description' => 'View Description', 'content' => 'View Content'));
     $viewModel->save();
     $layoutModel = LayoutModel::fromArray(array('name' => 'Layout Name', 'identifier' => 'Layout identifier', 'description' => 'Layout Description', 'content' => 'Layout Content'));
     $layoutModel->save();
     $userModel = UserModel::fromArray(array('lastname' => 'User test', 'firstname' => 'User test', 'email' => '*****@*****.**', 'login' => 'test', 'user_acl_role_id' => 1));
     $userModel->setPassword('test');
     $userModel->save();
     $documentTypeModel = DocumentTypeModel::fromArray(array('name' => 'Document Type Name', 'description' => 'Document Type description', 'icon_id' => 1, 'defaultview_id' => $viewModel->getId(), 'user_id' => $userModel->getId()));
     $documentTypeModel->save();
     $tabModel = TabModel::fromArray(array('name' => 'TabTest', 'description' => 'TabTest', 'sort_order' => 1, 'document_type_id' => $documentTypeModel->getId()));
     $tabModel->save();
     $documentModel = DocumentModel::fromArray(array('name' => 'DocumentTest', 'url_key' => 'document-test', 'status' => DocumentModel::STATUS_ENABLE, 'sort_order' => 1, 'show_in_nav' => true, 'user_id' => $userModel->getId(), 'document_type_id' => $documentTypeModel->getId(), 'view_id' => $viewModel->getId(), 'layout_id' => $layoutModel->getId(), 'parent_id' => 0));
     $documentModel->save();
     $propertyModel = PropertyModel::fromArray(array('datatype_id' => $this->object->getId(), 'document_id' => $documentModel->getId(), 'tab_id' => $tabModel->getId(), 'name' => 'PropertyTest', 'identifier' => 'PropertyTest', 'description' => 'PropertyTest', 'required' => true, 'sort_order' => 1));
     $propertyModel->save();
     $this->assertFalse(Model::saveEditor(Registry::get('Application')->getServiceManager(), $propertyModel));
     $propertyModel->isRequired(false);
     $this->assertTrue(Model::saveEditor(Registry::get('Application')->getServiceManager(), $propertyModel));
     $propertyModel->delete();
     $documentModel->delete();
     $tabModel->delete();
     $documentTypeModel->delete();
     $viewModel->delete();
     $layoutModel->delete();
 }