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