/** * Action for the creation of a new project. * * @return ViewModel */ public function createAction() { $project = new Entity\Project(); $data = array_merge_recursive($this->getRequest()->getPost()->toArray()); $data['contact'] = $this->zfcUserAuthentication()->getIdentity()->getId(); $form = new ProjectBasics($this->getServiceLocator(), $project); $form->setInputFilter($this->getServiceLocator()->get('project_project_form_filter')); $form->bind($project); $form->setData($data); $call = $this->getCallService()->findOpenCall(Entity\Version\Type::TYPE_PO); if ($this->getRequest()->isPost() && $form->isValid()) { $project = $form->getData(); //Set the project leader to the logged in user $projectChallenge = new Entity\Challenge(); $projectChallenge->setProject($project); $projectChallenge->setChallenge($this->getGeneralService()->findChallengeById($data['projectChallenge'])); $arrayCollection = new ArrayCollection([$projectChallenge]); $project->addProjectChallenge($arrayCollection); $project->setNumber($this->getProjectService()->findNextProjectNumberInCall($call)); $project->setCall($call); $project = $this->getProjectService()->newEntity($project); //Reload the project to have the docRef /* * We need the contactService to add the contact and organisation in the affiliation */ $contactService = clone $this->getContactService(); $contactService->setContact($this->zfcUserAuthentication()->getIdentity()); //Create the affiliation $affiliation = new Affiliation(); $affiliation->setProject($project); $affiliation->setContact($contactService->getContact()); $affiliation->setOrganisation($contactService->findOrganisationService()->getOrganisation()); $this->getAffiliationService()->newEntity($affiliation); $project = $this->getProjectService()->findEntityById('project', $project->getId()); return $this->redirect()->toRoute('community/project/project/basics', ['docRef' => $project->getDocRef()]); } return new ViewModel(['form' => $form, 'call' => $call]); }
/** * @return ViewModel */ public function projectAction() { $projectService = $this->getProjectService()->setProjectId($this->getEvent()->getRouteMatch()->getParam('id')); if ($projectService->isEmpty()) { return $this->notFoundAction(); } $data = array_merge_recursive($this->getRequest()->getPost()->toArray()); $form = new ProjectBasics($this->getServiceLocator(), $projectService->getProject()); $form->setInputFilter($this->getServiceLocator()->get('project_project_form_filter')); $project = $projectService->getProject(); $projectChallenge = $projectService->getProject()->getProjectChallenge()->first(); $project->setProjectChallenge(new ArrayCollection()); $form->bind($project); //Explicitly bind the contact $form->get('contact')->setValue($project->getContact()->getId()); if ($projectChallenge) { $form->get('projectChallenge')->setValue($projectChallenge->getChallenge()->getId()); } if ($this->getRequest()->isPost()) { $projectChallengeId = $data['projectChallenge']; unset($data['projectChallenge']); $form->setData($data); $form->getInputFilter()->get('projectChallenge')->setRequired(false); if (isset($_POST['cancel'])) { $this->flashMessenger()->setNamespace('info')->addMessage(sprintf(_("txt-edit-project-basics-of-project-%s-has-been-cancelled"), $projectService->parseFullName())); return $this->redirect()->toRoute('community/project/project/basics', ['docRef' => $projectService->getProject()->getDocRef()]); } if ($form->isValid()) { /* * @var Project */ $project = $form->getData(); /* * Because the form field is different that the model, we need to remove * the current challenges with trick */ $this->getProjectService()->removeChallengesFromProject($project); $projectChallenge = new Challenge(); $projectChallenge->setProject($project); $projectChallenge->setChallenge($this->getGeneralService()->findChallengeById($projectChallengeId)); $arrayCollection = new ArrayCollection([$projectChallenge]); $project->addProjectChallenge($arrayCollection); //Update the mode of the project when the saved is pressed $project->setMode($projectService->getNextMode()->mode); $this->getProjectService()->updateEntity($project); $this->flashMessenger()->setNamespace('success')->addMessage(sprintf(_("txt-project-%s-has-successfully-been-updated"), $projectService->parseFullName())); return $this->redirect()->toRoute('community/project/project/basics', ['docRef' => $projectService->getProject()->getDocRef()]); } } return new ViewModel(['projectService' => $projectService, 'form' => $form]); }