Exemplo n.º 1
0
 /**
  * @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]);
 }