public function indexAction()
 {
     $mainLayout = $this->initializeAdminArea();
     $id = $this->params()->fromRoute('id');
     $lang = $this->params()->fromRoute('lang');
     $languageSelection = $this->params()->fromRoute('languageSelection');
     $formtype = $this->params()->fromRoute('formtype');
     $em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
     $helper = new PostsControllerHelper();
     $recordFromDb = $helper->recoverWrapperRecordsById(new PostsCategoriesGetterWrapper(new PostsCategoriesGetter($em)), array('id' => $id, 'limit' => 1), $id);
     $form = new PostsCategoriesForm();
     if (!empty($recordFromDb)) {
         $form->setData($recordFromDb[0]);
         $submitButtonValue = 'Modifica';
         $formTitle = $recordFromDb[0]['name'];
         $formAction = $this->url()->fromRoute('admin/posts-categories-update', array('lang' => $lang, 'languageSelection' => $languageSelection, 'formtype' => $formtype));
     } else {
         $form->setData(array('posizione' => 1, 'lingua' => 1, 'isAmmTrasparente' => $formtype != 'contenuti' ? 1 : 0));
         $formTitle = 'Nuova categoria';
         $submitButtonValue = 'Inserisci';
         $formAction = $this->url()->fromRoute('admin/posts-categories-insert', array('lang' => $lang, 'languageSelection' => $languageSelection, 'formtype' => $formtype));
     }
     $this->layout()->setVariables(array('form' => $form, 'formAction' => $formAction, 'formTitle' => $formTitle, 'formDescription' => 'Compila i dati relativi alla categoria', 'submitButtonValue' => $submitButtonValue, 'formBreadCrumbCategory' => array(array('href' => $this->url()->fromRoute('admin/posts-categories-summary', array('lang' => $lang, 'moduleCode' => 'blogs', 'languageSelection' => $languageSelection)), 'label' => $formtype == 'blogs' ? ucfirst($formtype) : 'Foto', 'title' => 'Elenco ' . $formtype), array('href' => $this->url()->fromRoute('admin/posts-categories-summary', array('lang' => $lang, 'languageSelection' => $languageSelection, 'moduleCode' => $formtype)), 'label' => 'Categorie', 'title' => 'Categorie ')), 'templatePartial' => self::formTemplate));
     $this->layout()->setTemplate($mainLayout);
 }
 public function indexAction()
 {
     /**
      * @var \Doctrine\ORM\EntityManager $em
      */
     $em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
     /**
      * @var \Doctrine\DBAL\Connection $connection
      */
     $connection = $em->getConnection();
     $request = $this->getRequest();
     $post = array_merge_recursive($request->getPost()->toArray(), $request->getFiles()->toArray());
     if (!($request->isXmlHttpRequest() or $request->isPost())) {
         return $this->redirect()->toRoute('main');
     }
     $inputFilter = new PostsCategoriesFormInputFilter();
     $form = new PostsCategoriesForm();
     $form->setBindOnValidate(false);
     $form->setInputFilter($inputFilter->getInputFilter());
     $form->setData($post);
     $this->initializeAdminArea();
     $userDetails = $this->recoverUserDetails();
     $helper = new PostsCategoriesControllerHelper();
     $helper->setConnection($connection);
     $helper->getConnection()->beginTransaction();
     try {
         if (!$form->isValid()) {
             throw new NullException("The form is not valid");
         }
         $inputFilter->exchangeArray($form->getData());
         $helper->setLoggedUser($userDetails);
         $inputFilter->moduleId = ModulesContainer::recoverIdFromModuleCode($this->params()->fromRoute('modulename'));
         $languageRecords = $helper->recoverWrapperRecords(new LanguagesGetterWrapper(new LanguagesGetter($em)), array('abbrev1' => $this->params()->fromRoute('languageSelection'), 'fields' => 'languages.id'));
         $helper->checkRecords($languageRecords, 'Nessun dato relativo alle lingue');
         $inputFilter->languageId = $languageRecords[0]['id'];
         $inputFilter->moduleId = ModulesContainer::recoverIdFromModuleCode($this->params()->fromRoute('formtype'));
         $lastInsertId = $helper->insert($inputFilter);
         $helper->getConnection()->commit();
         $logWriter = new LogWriter($connection);
         $logWriter->writeLog(array('user_id' => $userDetails->id, 'module_id' => ModulesContainer::blogs, 'message' => "Inserita nuova categoria " . $this->params()->fromRoute('formtype') . ' ' . $inputFilter->name, 'type' => 'info', 'reference_id' => $lastInsertId, 'backend' => 1));
         $this->layout()->setVariables(array('messageType' => 'success', 'messageTitle' => 'Categoria inserita correttamente', 'messageText' => 'I dati sono stati processati correttamente dal sistema', 'showLinkResetFormAndShowIt' => 1, 'backToSummaryLink' => $this->url()->fromRoute('admin/posts-categories-summary', array('lang' => $this->params()->fromRoute('lang'), 'languageSelection' => $this->params()->fromRoute('languageSelection'), 'moduleCode' => $this->params()->fromRoute('formtype'))), 'backToSummaryText' => "Elenco categorie"));
     } catch (\Exception $e) {
         try {
             $helper->getConnection()->rollBack();
         } catch (\Doctrine\DBAL\ConnectionException $e) {
         }
         $logWriter = new LogWriter($connection);
         $logWriter->writeLog(array('user_id' => $userDetails->id, 'module_id' => ModulesContainer::contenuti_id, 'message' => "Errore inserimento nuova categoria: " . $inputFilter->name, 'type' => 'error', 'description' => $e->getMessage(), 'backend' => 1));
         $this->layout()->setVariables(array('messageType' => 'danger', 'messageTitle' => 'Errore inserimento nuova categoria', 'messageText' => 'Messaggio generato: ' . $e->getMessage(), 'form' => $form, 'formInputFilter' => $inputFilter->getInputFilter(), 'messageShowFormLink' => 1, 'messageShowForm' => 'Torna al form di inserimento dati'));
     }
     $this->layout()->setTemplate($this->layout()->getVariable('templateDir') . 'message.phtml');
 }
 public function indexAction()
 {
     /**
      * @var \Doctrine\ORM\EntityManager $em
      */
     $em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
     /**
      * @var \Doctrine\DBAL\Connection $connection
      */
     $connection = $em->getConnection();
     $request = $this->getRequest();
     $post = array_merge_recursive($request->getPost()->toArray(), $request->getFiles()->toArray());
     if (!($request->isXmlHttpRequest() or $request->isPost())) {
         return $this->redirect()->toRoute('main');
     }
     $inputFilter = new PostsCategoriesFormInputFilter();
     $form = new PostsCategoriesForm();
     $form->setBindOnValidate(false);
     $form->setInputFilter($inputFilter->getInputFilter());
     $form->setData($post);
     $this->initializeAdminArea();
     $userDetails = $this->recoverUserDetails();
     try {
         if (!$form->isValid()) {
             throw new NullException("The form is not valid");
         }
         $inputFilter->exchangeArray($form->getData());
         $helper = new PostsCategoriesControllerHelper();
         $helper->setConnection($connection);
         $helper->getConnection()->beginTransaction();
         $helper->setLoggedUser($userDetails);
         $helper->update($inputFilter);
         $helper->getConnection()->commit();
         $logWriter = new LogWriter($connection);
         $logWriter->writeLog(array('user_id' => $userDetails->id, 'module_id' => ModulesContainer::blogs, 'message' => "Aggiornata categoria posts " . $inputFilter->name, 'type' => 'info', 'reference_id' => $inputFilter->id, 'backend' => 1));
         $this->layout()->setVariables(array('messageType' => 'success', 'messageTitle' => 'Categoria aggiornata correttamente', 'messageText' => 'I dati sono stati processati correttamente dal sistema', 'messageShowFormLink' => 1, 'messageShowForm' => "Torna ai dati della categoria", 'backToSummaryLink' => $this->url()->fromRoute('admin/posts-categories-summary', array('lang' => $this->params()->fromRoute('lang'), 'languageSelection' => $this->params()->fromRoute('languageSelection'), 'moduleCode' => $this->params()->fromRoute('formtype'))), 'backToSummaryText' => "Elenco categorie"));
         $this->layout()->setTemplate($this->layout()->getVariable('templateDir') . 'message.phtml');
     } catch (\Exception $e) {
         $logWriter = new LogWriter($connection);
         $logWriter->writeLog(array('user_id' => $userDetails->id, 'module_id' => ModulesContainer::blogs, 'message' => "Errore aggiornamento categoria posts ", 'type' => 'error', 'description' => $e->getMessage(), 'reference_id' => $inputFilter->id, 'backend' => 1));
         $this->layout()->setVariables(array('messageType' => 'danger', 'messageTitle' => 'Errore aggiornamento categoria posts', 'messageText' => $e->getMessage(), 'form' => $form, 'formInputFilter' => $inputFilter->getInputFilter(), 'messageShowFormLink' => 1, 'messageShowForm' => 'Torna ai dati della categoria'));
         $this->layout()->setTemplate($this->layout()->getVariable('templateDir') . 'message.phtml');
     }
 }