/**
  * @return \Zend\View\Model\ViewModel
  */
 public function editAction()
 {
     $loiService = $this->getLoiService()->setLoiId($this->params('id'));
     if (is_null($loiService)) {
         return $this->notFoundAction();
     }
     $data = array_merge_recursive($this->getRequest()->getPost()->toArray(), $this->getRequest()->getFiles()->toArray());
     $form = $this->getFormService()->prepare('loi', $loiService->getLoi(), $data);
     //Get contacts in an organisation
     $this->getContactService()->findContactsInAffiliation($loiService->getLoi()->getAffiliation());
     $form->get('loi')->get('contact')->setValueOptions($this->getContactService()->toFormValueOptions());
     if ($this->getRequest()->isPost()) {
         if (isset($data['cancel'])) {
             return $this->redirect()->toRoute('zfcadmin/affiliation/loi/view', ['id' => $loiService->getLoi()->getId()]);
         }
         if (isset($data['delete'])) {
             $this->flashMessenger()->setNamespace('success')->addMessage(sprintf(_("txt-project-loi-for-organisation-%s-in-project-%s-has-been-removed"), $loiService->getLoi()->getAffiliation()->getOrganisation(), $loiService->getLoi()->getAffiliation()->getProject()));
             $this->getLoiService()->removeEntity($loiService->getLoi());
             return $this->redirect()->toRoute('zfcadmin/affiliation/loi/list');
         }
         if ($form->isValid()) {
             /*
              * @var Loi
              */
             $loi = $form->getData();
             $fileData = $this->params()->fromFiles();
             if ($fileData['loi']['file']['error'] === 0) {
                 /*
                  * Replace the content of the object
                  */
                 if (!$loi->getObject()->isEmpty()) {
                     $loi->getObject()->first()->setObject(file_get_contents($fileData['loi']['file']['tmp_name']));
                 } else {
                     $loiObject = new LoiObject();
                     $loiObject->setObject(file_get_contents($fileData['loi']['file']['tmp_name']));
                     $loiObject->setLoi($loi);
                     $this->getLoiService()->newEntity($loiObject);
                 }
                 //Create a article object element
                 $fileSizeValidator = new FilesSize(PHP_INT_MAX);
                 $fileSizeValidator->isValid($fileData['loi']['file']);
                 $loi->setSize($fileSizeValidator->size);
                 $loi->setContentType($this->getGeneralService()->findContentTypeByContentTypeName($fileData['loi']['file']['type']));
             }
             $this->getLoiService()->updateEntity($loi);
             $this->flashMessenger()->setNamespace('success')->addMessage(sprintf(_("txt-project-loi-for-organisation-%s-in-project-%s-has-been-updated"), $loi->getAffiliation()->getOrganisation(), $loi->getAffiliation()->getProject()));
             return $this->redirect()->toRoute('zfcadmin/affiliation/loi/view', ['id' => $loi->getId()]);
         }
     }
     return new ViewModel(['loi' => $loiService->getLoi(), 'form' => $form]);
 }
 /**
  * Action to replace an mis-uploaded LoI.
  *
  * @return ViewModel
  *
  * @throws \Zend\Form\Exception\InvalidArgumentException
  * @throws \InvalidArgumentException
  * @throws \Zend\Mvc\Exception\DomainException
  * @throws \Zend\Form\Exception\DomainException
  */
 public function replaceAction()
 {
     /*
      * @var Loi
      */
     $loi = $this->getAffiliationService()->findEntityById('Loi', $this->params('id'));
     $this->getAffiliationService()->setAffiliation($loi->getAffiliation());
     if (is_null($loi) || sizeof($loi->getObject()) === 0) {
         return $this->notFoundAction();
     }
     $data = array_merge_recursive($this->getRequest()->getPost()->toArray(), $this->getRequest()->getFiles()->toArray());
     $form = new UploadLoi();
     $form->get('submit')->setValue(_("txt-replace-loi"));
     $form->setData($data);
     if ($this->getRequest()->isPost()) {
         if (isset($data['cancel'])) {
             return $this->redirect()->toRoute('community/affiliation/affiliation', ['id' => $loi->getAffiliation()->getId()], ['fragment' => 'details']);
         }
         if ($form->isValid()) {
             $fileData = $this->params()->fromFiles();
             /*
              * Remove the current entity
              */
             foreach ($loi->getObject() as $object) {
                 $this->getAffiliationService()->removeEntity($object);
             }
             //Create a article object element
             $affiliationLoiObject = new Entity\LoiObject();
             $affiliationLoiObject->setObject(file_get_contents($fileData['file']['tmp_name']));
             $fileSizeValidator = new FilesSize(PHP_INT_MAX);
             $fileSizeValidator->isValid($fileData['file']);
             $loi->setSize($fileSizeValidator->size);
             $loi->setContact($this->zfcUserAuthentication()->getIdentity());
             $loi->setDateSigned(new \DateTime());
             $loi->setContentType($this->getGeneralService()->findContentTypeByContentTypeName($fileData['file']['type']));
             $affiliationLoiObject->setLoi($loi);
             $this->getAffiliationService()->newEntity($affiliationLoiObject);
             $this->flashMessenger()->setNamespace('success')->addMessage(sprintf(_("txt-project-loi-for-organisation-%s-in-project-%s-has-been-replaced"), $loi->getAffiliation()->getOrganisation(), $loi->getAffiliation()->getProject()));
             return $this->redirect()->toRoute('community/affiliation/affiliation', ['id' => $loi->getAffiliation()->getId()], ['fragment' => 'details']);
         }
     }
     return new ViewModel(['affiliationService' => $this->getAffiliationService(), 'form' => $form]);
 }