/**
  * @param array $formDataSample
  *
  * @return OperatoriForm
  */
 protected function setupForm($formDataSample)
 {
     $form = new OperatoriForm();
     $inputFilter = new OperatoriFormInputFilter();
     $form->setInputFilter($inputFilter->getInputFilter());
     $form->setData($formDataSample);
     return $form;
 }
 public function indexAction()
 {
     $mainLayout = $this->initializeAdminArea();
     $em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
     $id = $this->params()->fromRoute('id');
     $lang = $this->params()->fromRoute('lang');
     $helper = new ContrattiPubbliciControllerHelper();
     $operatoriRecords = $helper->recoverWrapperRecordsById(new OperatoriGetterWrapper(new OperatoriGetter($em)), array('id' => $id, 'limit' => 1), $id);
     $form = new OperatoriForm();
     if ($operatoriRecords) {
         $form->setData($operatoriRecords[0]);
         $submitButtonValue = 'Modifica';
         $formTitle = $operatoriRecords[0]['nome'];
         $formAction = $this->url()->fromRoute('admin/contratti-pubblici-operatori-update', array('lang' => $lang));
     } else {
         $formTitle = 'Nuova azienda';
         $submitButtonValue = 'Inserisci';
         $formAction = $this->url()->fromRoute('admin/contratti-pubblici-operatori-insert', array('lang' => $lang));
     }
     $this->layout()->setVariables(array('form' => $form, 'formAction' => $formAction, 'formTitle' => $formTitle, 'formDescription' => 'Compila dati operatore abilitato a partecipare ai bandi di gara e contratti pubblici', 'submitButtonValue' => $submitButtonValue, 'formBreadCrumbCategory' => array(array('label' => 'Contratti pubblici', 'href' => $this->url()->fromRoute('admin/contratti-pubblici-summary', array('lang' => $lang)), 'title' => 'Elenco contratti pubblici'), array('label' => 'Aziende', 'href' => $this->url()->fromRoute('admin/contratti-pubblici-operatori-summary', array('lang' => $lang)), 'title' => 'Elenco aziende')), '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 OperatoriFormInputFilter();
     $form = new OperatoriForm();
     $form->setBindOnValidate(false);
     $form->setInputFilter($inputFilter->getInputFilter());
     $form->setData($post);
     $this->initializeAdminArea();
     $userDetails = $this->recoverUserDetails();
     $helper = new OperatoriControllerHelper();
     $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);
         if (!$helper->isValidCodiceFiscale($inputFilter->cf) and !$helper->isValidPartitaIVA($inputFilter->cf)) {
             throw new NullException("Codice fiscale o Partita IVA non valido");
         }
         $lastInsertId = $helper->insert($inputFilter);
         $logWriter = new LogWriter($connection);
         $logWriter->writeLog(array('user_id' => $userDetails->id, 'module_id' => ModulesContainer::contratti_pubblici_id, 'message' => "Inserita nuova sezione " . $inputFilter->nome, 'type' => 'info', 'reference_id' => $lastInsertId, 'backend' => 1));
         $this->layout()->setVariables(array('messageType' => 'success', 'messageTitle' => 'Azienda inserita correttamente', 'messageText' => 'I dati sono stati processati correttamente dal sistema', 'showLinkResetFormAndShowIt' => 1, 'backToSummaryLink' => $this->url()->fromRoute('admin/contratti-pubblici-operatori-summary', array('lang' => $this->params()->fromRoute('lang'))), 'backToSummaryText' => "Elenco aziende", 'insertAgainLabel' => "Inserisci un'altra azienda"));
         $helper->getConnection()->commit();
     } 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::contratti_pubblici_id, 'message' => "Errore inserimento nuova azienda: " . $inputFilter->nome, 'type' => 'error', 'description' => $e->getMessage(), 'backend' => 1));
         $this->layout()->setVariables(array('messageType' => 'danger', 'messageTitle' => 'Errore inserimento nuova azienda', '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');
 }