/** * Create a new project. * * @return ViewModel */ public function createAction() { $projectService = $this->getProjectService()->setProjectId($this->getEvent()->getRouteMatch()->getParam('project')); $data = array_merge_recursive($this->getRequest()->getPost()->toArray(), $this->getRequest()->getFiles()->toArray()); $today = new \DateTime(); $currentPeriod = $this->getReportService()->getPeriod($today); $data['period'] = sprintf("%s-%s", $today->format("Y"), $currentPeriod->semester); $form = new CreateReport($projectService); $form->setInputFilter(new FilterCreateReport()); $form->setData($data); if ($this->getRequest()->isPost() && $form->isValid()) { $formData = $form->getData(); /* * Create the period */ $period = $this->getReportService()->getDateFromPeriod($formData['period']); /* * Check first if we have already a report for this project and period */ $report = $this->getReportService()->findReportByProjectAndPeriod($projectService->getProject(), $period); /* * Create a new report if no report is found */ if (is_null($report)) { $report = new Report(); $report->setDatePeriod($period); $report->setContact($this->zfcUserAuthentication()->getIdentity()); $report->setProject($projectService->getProject()); } /* * Create the item * when no name is given, take the name of the uploaded file */ $item = new Item(); if (empty($formData['item'])) { $item->setItem($formData['file']['name']); } else { $item->setItem($formData['item']); } $item->setReport($report); $item->setContact($this->zfcUserAuthentication()->getIdentity()); $fileSizeValidator = new FilesSize(PHP_INT_MAX); $fileSizeValidator->isValid($formData['file']); $item->setSize($fileSizeValidator->size); $item->setContentType($this->getGeneralService()->findContentTypeByContentTypeName($formData['file']['type'])); /* * Save the object */ $reportItemObject = new ItemObject(); $reportItemObject->setObject(file_get_contents($formData['file']['tmp_name'])); $reportItemObject->setItem($item); $this->getReportService()->newEntity($reportItemObject); /* * Send the email tot he consortium */ $email = $this->getEmailService()->create(); $this->getEmailService()->setTemplate("/project/report-item/submit:mail"); $email->addSelection($this->getContactService()->findEntityById('selection', 219), $this->getContactService()); $email->setDocumentName($item); $email->setProject($report->getProject()); $email->setSubmitter($report->getContact()); $email->setContentType($item->getContentType()->getDescription()); $this->getEmailService()->send($email); $this->flashMessenger()->setNamespace('success')->addMessage(sprintf(_("txt-report-%s-for-project-%s-has-successfully-been-created"), $report->parseName(), $projectService->parseFullName())); return $this->redirect()->toRoute('community/project/report/report', ['id' => $reportItemObject->getItem()->getReport()->getId()]); } return new ViewModel(['projectService' => $projectService, 'form' => $form]); }