public function indexAction()
 {
     $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');
     }
     $modulename = $this->params()->fromRoute('modulename');
     /* $modulenameLabel = str_replace("-", " ", $modulename); */
     $inputFilter = new ContenutiFormInputFilter();
     $form = new ContenutiForm();
     $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 ContenutiControllerHelper();
         $helper->setConnection($connection);
         $helper->setLoggedUser($userDetails);
         $helper->getConnection()->beginTransaction();
         $lastInsertId = $helper->insert($inputFilter);
         // TODO: insert in home, update home field if home is selected
         $helper->setLogWriter(new LogWriter($connection));
         $helper->writeLog(array('user_id' => $userDetails->id, 'module_id' => ModulesContainer::contenuti_id, 'message' => "Inserito nuovo contenuto \\ articolo " . $inputFilter->titolo, 'type' => 'info', 'reference_id' => $lastInsertId, 'backend' => 1));
         $this->layout()->setVariables(array('messageType' => 'success', 'messageTitle' => 'Articolo inserito correttamente', 'messageText' => 'I dati sono stati processati correttamente dal sistema', 'showLinkResetFormAndShowIt' => 1, 'backToSummaryLink' => $this->url()->fromRoute('admin/contenuti-summary', array('lang' => $this->params()->fromRoute('lang'), 'languageSelection' => $this->params()->fromRoute('languageSelection'), 'modulename' => $modulename)), 'backToSummaryText' => "Elenco contenuti", 'insertAgainLabel' => "Inserisci un altro articolo \\ contenuto", 'attachmentsLink' => $this->url()->fromRoute('admin/attachments-summary', array('lang' => $this->params()->fromRoute('languageSelection'), 'module' => 'contenuti', 'referenceId' => $lastInsertId))));
         $helper->getConnection()->commit();
         $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::contenuti_id, 'message' => "Errore inserimento nuovo contenuto \\ articolo: " . $inputFilter->titolo, 'description' => $e->getMessage() . "; Titolo: " . $inputFilter->titolo . " Testo: " . $inputFilter->testo . ' Sottosezione: ' . $inputFilter->sottosezione, 'type' => 'error', 'backend' => 1));
         $this->layout()->setVariables(array('messageType' => 'danger', 'messageTitle' => 'Errore inserimento contenuto \\ articolo', 'messageText' => $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');
     }
 }
 /**
  * Update contenuti and render a message view
  *
  * @return \Zend\Http\Response
  */
 public function indexAction()
 {
     $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 ContenutiFormInputFilter();
     $form = new ContenutiForm();
     $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 ContenutiControllerHelper();
         $helper->setConnection($connection);
         $helper->getConnection()->beginTransaction();
         $helper->update($inputFilter);
         $helper->getConnection()->commit();
         // TODO: insert or delete in home, update home field
         $helper->setLogWriter(new LogWriter($connection));
         $helper->writeLog(array('user_id' => $userDetails->id, 'module_id' => ModulesContainer::contenuti_id, 'message' => "Aggiornato il contenuto " . $inputFilter->titolo, 'type' => 'info', 'reference_id' => $inputFilter->id, 'backend' => 1));
         $this->layout()->setVariables(array('messageType' => 'success', 'messageTitle' => 'Contenuto aggiornato correttamente', 'messageText' => 'I dati sono stati processati correttamente dal sistema', 'messageShowFormLink' => 1, 'messageShowForm' => 'Torna al contenuto', 'backToSummaryLink' => $this->url()->fromRoute('admin/contenuti-summary', array('lang' => $this->params()->fromRoute('lang'), 'languageSelection' => $this->params()->fromRoute('languageSelection'), 'modulename' => $this->params()->fromRoute('modulename'))), 'backToSummaryText' => "Elenco contenuti", 'attachmentsLink' => $this->url()->fromRoute('admin/attachments-summary', array('lang' => $this->params()->fromRoute('lang'), 'module' => 'contenuti', 'referenceId' => $inputFilter->id))));
         $this->layout()->setTemplate($this->layout()->getVariable('templateDir') . 'message.phtml');
     } catch (\Exception $e) {
         $logWriter = new LogWriter($connection);
         $logWriter->writeLog(array('user_id' => isset($userDetails->id) ? $userDetails->id : null, 'module_id' => ModulesContainer::contenuti_id, 'message' => "Errore aggiornamento contenuto ID: " . $inputFilter->id, 'type' => 'error', 'description' => $e->getMessage(), 'reference_id' => $inputFilter->id, 'backend' => 1));
         $this->layout()->setVariables(array('messageType' => 'danger', 'messageTitle' => 'Errore aggiornamento contenuto', 'messageText' => 'Messaggio generato: ' . $e->getMessage(), 'form' => $form, 'formInputFilter' => $inputFilter->getInputFilter(), 'messageShowFormLink' => 1, 'messageShowForm' => 'Torna al contenuto'));
         $this->layout()->setTemplate($this->layout()->getVariable('templateDir') . 'message.phtml');
     }
 }