/**
* 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);
}