/**
  * Creating a new ambassador document.
  *
  * @return ViewModel
  */
 public function newAction()
 {
     $ambassador = $this->getAmbassadorService()->findAmbassadorByContact($this->zfcUserAuthentication()->getIdentity());
     //depends on whether the request was made via JSON
     if (is_null($ambassador)) {
         return $this->notFoundAction();
     }
     $document = new Document();
     $document->setAmbassador($ambassador);
     $entityManager = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager');
     $data = array_merge_recursive($this->getRequest()->getPost()->toArray(), $this->getRequest()->getFiles()->toArray());
     $form = new CreateAmbassadorDocument($entityManager);
     $form->bind($document);
     $form->setData($data);
     if ($this->getRequest()->isPost() && $form->isValid()) {
         /*
          * Return when cancel is pressed
          */
         $document = $form->getData();
         $file = $form->get('file')->getValue();
         if (!empty($file['name']) && $file['error'] === 0) {
             /*
              * Update the document
              */
             $fileSizeValidator = new FilesSize(PHP_INT_MAX);
             $fileSizeValidator->isValid($file);
             $document->setSize($fileSizeValidator->size);
             $document->setContentType($this->getGeneralService()->findContentTypeByContentTypeName($file['type']));
             $document = $this->getAmbassadorService()->newEntity($document);
             /*
              * Update the object
              */
             $documentObject = new Document\Object();
             $documentObject->setDocument($document);
             $documentObject->setObject(file_get_contents($file['tmp_name']));
             $this->getAmbassadorService()->newEntity($documentObject);
         }
         $this->flashMessenger()->setNamespace('success')->addMessage(sprintf(_('txt-ambassador-document-item-%s-has-been-created-successfully'), $document->getDocument()));
         $this->getAmbassadorService()->sendUpdateEmail();
         return $this->redirect()->toRoute('community/ambassador/ambassador');
     }
     return new ViewModel(['form' => $form]);
 }
 /**
  * @return ViewModel
  */
 public function editAction()
 {
     $documentService = $this->getDocumentService()->setDocumentId($this->getEvent()->getRouteMatch()->getParam('id'));
     if ($documentService->isEmpty()) {
         return $this->notFoundAction();
     }
     $ambassadorService = $this->getAmbassadorService()->setAmbassador($documentService->getDocument()->getAmbassador());
     $entityManager = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager');
     $data = array_merge_recursive($this->getRequest()->getPost()->toArray(), $this->getRequest()->getFiles()->toArray());
     $form = new CreateDocument($ambassadorService, $entityManager);
     $form->bind($documentService->getDocument());
     $form->getInputFilter()->get('file')->setRequired(false);
     $form->getInputFilter()->get('contact')->setRequired(false);
     $form->getInputFilter()->get('project')->setRequired(false);
     $form->setData($data);
     if ($this->getRequest()->isPost()) {
         /*
          * Remove the file if delete is pressed
          */
         if (isset($data['cancel'])) {
             $this->flashMessenger()->setNamespace('info')->addMessage(sprintf(_('txt-edit-a-document-%s-in-project-%s-has-been-cancelled'), $documentService->getDocument()->parseFileName(), $ambassadorService->parseFullName()));
             return $this->redirect()->toRoute('community/ambassador/document/document', ['id' => $documentService->getDocument()->getId()]);
         }
         /*
          * Remove the file if delete is pressed
          */
         if (isset($data['delete'])) {
             $document = $documentService->getDocument();
             $this->getAmbassadorService()->removeEntity($document);
             $this->flashMessenger()->setNamespace('info')->addMessage(sprintf(_('txt-ambassador-document-%s-for-ambassador-%s-has-been-removed'), $documentService->getDocument()->parseFileName(), $ambassadorService->parseFullName()));
             return $this->redirect()->toRoute('community/ambassador/ambassador', ['docRef' => $this->getAmbassador()->getAmbassador()->getDocRef()]);
         }
         if ($form->isValid()) {
             $document = $form->getData();
             /*
              * Remove the file if delete is pressed
              */
             if (isset($data['delete'])) {
                 $this->flashMessenger()->setNamespace('success')->addMessage(sprintf(_('txt-document-%s-successfully-removed'), $document->parseFileName()));
                 $this->getDocumentService()->removeEntity($document);
                 return $this->redirect()->toRoute('community/ambassador/ambassador/management', ['docRef' => $document->getProject()->getDocRef()]);
             }
             /*
              * Handle when
              */
             $file = $form->get('file')->getValue();
             if (!empty($file['name']) && $file['error'] === 0) {
                 /*
                  * Update the document
                  */
                 $fileSizeValidator = new FilesSize(PHP_INT_MAX);
                 $fileSizeValidator->isValid($file);
                 $document->setSize($fileSizeValidator->size);
                 $document->setContentType($this->getGeneralService()->findContentTypeByContentTypeName($file['type']));
                 /*
                  * Update the object
                  */
                 $documentObject = $document->getObject()->first();
                 if (!$documentObject) {
                     $documentObject = new Object();
                     $documentObject->setDocument($document);
                 }
                 $documentObject->setObject(file_get_contents($file['tmp_name']));
                 $this->getDocumentService()->updateEntity($documentObject);
             }
             $this->getDocumentService()->updateEntity($document);
             $this->flashMessenger()->setNamespace('success')->addMessage(sprintf(_('txt-document-%s-successfully-updated'), $document->parseFileName()));
             return $this->redirect()->toRoute('community/ambassador/document/document', ['id' => $document->getId()]);
         }
     }
     return new ViewModel(['documentService' => $documentService, 'form' => $form]);
 }