/**
  * @param $formDataSample
  * @return EntiTerziForm
  */
 protected function setupForm($formDataSample)
 {
     $form = new EntiTerziForm();
     $inputFilter = new EntiTerziFormInputFilter();
     $form->setInputFilter($inputFilter->getInputFilter());
     $form->setData($formDataSample);
     return $form;
 }
 public function indexAction()
 {
     $mainLayout = $this->initializeAdminArea();
     $id = $this->params()->fromRoute('id');
     $lang = $this->params()->fromRoute('lang');
     $em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
     $helper = new EntiTerziControllerHelper();
     $entiTerziRecords = $helper->recoverWrapperRecordsById(new EntiTerziGetterWrapper(new EntiTerziGetter($em)), array('id' => $id, 'limit' => 1), $id);
     $form = new EntiTerziForm();
     if (!empty($entiTerziRecords)) {
         $form->setData($entiTerziRecords[0]);
         $submitButtonValue = 'Modifica';
         $formTitle = 'Modifica ente terzo';
         $formAction = $this->url()->fromRoute('admin/enti-terzi-update', array('lang' => $lang));
     } else {
         $formTitle = 'Nuovo ente terzo';
         $submitButtonValue = 'Inserisci';
         $formAction = $this->url()->fromRoute('admin/enti-terzi-insert', array('lang' => $lang));
     }
     $this->layout()->setVariables(array('formTitle' => $formTitle, 'formDescription' => "Compila i dati relativi a all'ente terzo", 'form' => $form, 'formAction' => $formAction, 'submitButtonValue' => $submitButtonValue, 'formBreadCrumbCategory' => 'Enti terzi', 'formBreadCrumbCategoryLink' => $this->url()->fromRoute('admin/enti-terzi-summary', array('lang' => $lang)), '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 EntiTerziFormInputFilter();
     $form = new EntiTerziForm();
     $form->setBindOnValidate(false);
     $form->setInputFilter($inputFilter->getInputFilter());
     $form->setData($post);
     $this->initializeAdminArea();
     $userDetails = $this->recoverUserDetails();
     $helper = new EntiTerziControllerHelper();
     $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);
         $helper->insert($inputFilter);
         $lastInsertId = $helper->getConnection()->lastInsertId();
         $helper->getConnection()->commit();
         $logWriter = new LogWriter($connection);
         $logWriter->writeLog(array('user_id' => $userDetails->id, 'module_id' => ModulesContainer::contenuti_id, 'message' => "Inserito nuovo ente terzo " . $inputFilter->nome . " ID: " . $lastInsertId, 'type' => 'info', 'reference_id' => $lastInsertId, 'backend' => 1));
         $this->layout()->setVariables(array('messageType' => 'success', 'messageTitle' => 'Ente terzo inserito correttamente', 'messageText' => 'I dati sono stati processati correttamente dal sistema', 'showLinkResetFormAndShowIt' => 1, 'backToSummaryLink' => $this->url()->fromRoute('admin/enti-terzi-summary', array('lang' => $this->params()->fromRoute('lang'))), 'backToSummaryText' => "Elenco enti", 'insertAgainLabel' => "Inserisci un altro ente"));
     } 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 nuovo ente terzo: " . $inputFilter->nome, 'type' => 'error', 'description' => $e->getMessage(), 'backend' => 1));
         $this->layout()->setVariables(array('messageType' => 'danger', 'messageTitle' => 'Errore inserimento nuovo ente terzo', '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');
 }