load() public method

Load properties from document type, tab and document
public load ( integer $documentTypeId, Model $document, Zend\ServiceManager\ServiceManager $serviceLocator ) : array
$documentTypeId integer Document type id
$document Gc\Document\Model Document model
$serviceLocator Zend\ServiceManager\ServiceManager Service manager
return array
コード例 #1
0
 /**
  * Test
  *
  * @return void
  */
 public function testLoad()
 {
     $user = UserModel::fromArray(array('lastname' => 'Test', 'firstname' => 'Test', 'email' => '*****@*****.**', 'login' => 'test-user-model', 'user_acl_role_id' => 1));
     $user->setPassword('test-user-model-password');
     $user->save();
     $view = ViewModel::fromArray(array('name' => 'View', 'identifier' => 'ViewIdentifier', 'description' => 'Description', 'content' => ''));
     $view->save();
     $layout = LayoutModel::fromArray(array('name' => 'View', 'identifier' => 'ViewIdentifier', 'description' => 'Description', 'content' => ''));
     $layout->save();
     $documenttype = DocumentTypeModel::fromArray(array('name' => 'DocumentType', 'description' => 'description', 'icon_id' => 1, 'default_view_id' => $view->getId(), 'user_id' => $user->getId()));
     $documenttype->save();
     $documenttype->setDependencies(array($documenttype->getId()));
     $documenttype->save();
     $datatype = DatatypeModel::fromArray(array('name' => 'DatatypeTest', 'model' => 'Textstring'));
     $datatype->save();
     $tab = TabModel::fromArray(array('name' => 'test', 'description' => 'test', 'document_type_id' => $documenttype->getId()));
     $tab->save();
     $property = PropertyModel::fromArray(array('name' => 'test', 'identifier' => 'test', 'description' => 'test', 'tab_id' => $tab->getId(), 'datatype_id' => $datatype->getId(), 'is_required' => true));
     $property->save();
     $document = DocumentModel::fromArray(array('name' => 'test', 'url_key' => '', 'status' => DocumentModel::STATUS_ENABLE, 'user_id' => $user->getId(), 'document_type_id' => $documenttype->getId(), 'view_id' => $view->getId(), 'layout_id' => $layout->getId(), 'parent_id' => null));
     $document->save();
     $this->assertNull($this->object->load($document, array('locales' => array('en_GB' => 'English'))));
     $this->object->setData(array());
     $this->assertFalse($this->object->isValid());
     $document->delete();
     $documenttype->delete();
     $property->delete();
     $tab->delete();
     $view->delete();
     $layout->delete();
     $user->delete();
     $datatype->delete();
     unset($documenttype);
     unset($document);
     unset($property);
     unset($tab);
     unset($view);
     unset($layout);
     unset($user);
     unset($datatype);
 }
コード例 #2
0
ファイル: DocumentController.php プロジェクト: gotcms/gotcms
 /**
  * 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);
 }