/** * @return JsonModel */ public function updateEffortAction() { $value = str_replace(',', '.', $this->params()->fromPost('value')); if ($value < 0) { print 'Error: '; return new JsonModel(['The value cannot be smaller than 0']); } list($affiliationId, $workpackageId, $year) = explode(',', $this->params()->fromPost('pk')); $affiliation = $this->getAffiliationService()->setAffiliationId($affiliationId)->getAffiliation(); $workpackage = $this->getWorkpackageService()->setWorkpackageId($workpackageId)->getWorkpackage(); $editYears = $this->getProjectService()->setProject($affiliation->getProject())->parseEditYearRange(); if (!in_array($year, $editYears)) { print 'Error: '; return new JsonModel([sprintf('The given year cannot be edited. Only %s are possible', implode(', ', $editYears))]); } /* * Add the resource for the project */ $this->getProjectService()->addResource($affiliation->getProject(), ProjectAssertion::class); $hasProjectEditRights = $this->isAllowed($affiliation->getProject(), 'edit-community'); /* * Add the resource for the workpackage */ $this->getWorkpackageService()->addResource($workpackage, WorkpackageAssertion::class); $hasWorkpackageRights = $this->isAllowed($workpackage, 'edit-community'); /* * Add the resource for the workpackage */ $this->getAffiliationService()->addResource($affiliation, AffiliationAssertion::class); $hasAffiliationRights = $this->isAllowed($affiliation, 'edit-affiliation'); if ($hasProjectEditRights || $hasAffiliationRights || $hasWorkpackageRights) { //Try to find the given cost_version $effort = $this->getProjectService()->findEffortByAffiliationAndWorkpackageAndYear($affiliation, $workpackage, $year); /* * Remove the entity when 0 is given */ if (!is_null($effort) && ($value == '0' || empty($value))) { $this->getProjectService()->removeEntity($effort); } if ($value > 0) { /* * Create the effort if it cannot be found */ if (is_null($effort)) { $effort = new Effort(); $effort->setAffiliation($affiliation); $effort->setWorkpackage($workpackage); $dateStart = new \DateTime(); $effort->setDateStart($dateStart->modify('first day of january ' . $year)); $dateEnd = new \DateTime(); $effort->setDateEnd($dateEnd->modify('last day of december ' . $year)); } $effort->setEffort($value); $this->getProjectService()->updateEntity($effort); } return new JsonModel(['success' => true]); } else { print 'Error: '; return new JsonModel(['Access denied']); } }
/** * @return ViewModel */ public function costAndEffortAction() { set_time_limit(0); $projectService = $this->getProjectService()->setProjectId($this->getEvent()->getRouteMatch()->getParam('id')); if ($projectService->isEmpty()) { return $this->notFoundAction(); } /* * Create the resource beforehand */ $this->getProjectService()->addResource($projectService->getProject(), ProjectAssertion::class); $hasProjectEditRights = $this->isAllowed($projectService->getProject(), 'edit-community'); $formData = []; foreach ($projectService->getAffiliation() as $affiliationService) { /* * Add the resource on the fly to the ACL and check the access */ $affiliation = $affiliationService->getAffiliation(); $this->getAffiliationService()->addResource($affiliation, AffiliationAssertion::class); foreach ($projectService->parseEditYearRange() as $year) { /* * Only store the allowed values from the database */ if ($this->isAllowed($affiliationService->getAffiliation(), 'edit-affiliation') || $hasProjectEditRights) { /* * sum over the cost */ $costPerYear = $projectService->findTotalCostByAffiliationPerYear($affiliationService->getAffiliation()); if (!array_key_exists($year, $costPerYear)) { $costPerYear[$year] = 0; } $formData['costPerAffiliationAndYear'][$affiliationService->getAffiliation()->getId()][$year] = ['cost' => $costPerYear[$year] / 1000]; } /* * Sum over the effort, this is grouped per workpackage */ foreach ($this->getWorkpackageService()->findWorkpackageByProjectAndWhich($projectService->getProject()) as $workpackage) { $this->getWorkpackageService()->addResource($workpackage, WorkpackageAssertion::class); /* * Only store the allowed values from the database */ if ($hasProjectEditRights || $this->isAllowed($affiliationService->getAffiliation(), 'edit-affiliation') || $this->isAllowed($workpackage, 'edit-community')) { $effortPerWorkpackageAndYear = $projectService->findTotalEffortByWorkpackageAndAffiliationPerYear($workpackage, $affiliationService->getAffiliation()); if (!array_key_exists($year, $effortPerWorkpackageAndYear)) { $effortPerWorkpackageAndYear[$year] = 0; } $formData['effortPerAffiliationAndYear'][$workpackage->getId()][$affiliationService->getAffiliation()->getId()][$year] = ['effort' => $effortPerWorkpackageAndYear[$year]]; } } } } $form = new CostAndEffort($projectService, $this->getWorkpackageService()); $form->setData($formData); if ($this->getRequest()->isPost() && $form->setData($_POST) && $form->isValid()) { $formData = $form->getData(); /* * Handle the cancel request */ if (!is_null($this->getRequest()->getPost()->get('cancel'))) { return $this->redirect()->toRoute('community/project/project/cost-and-effort', ['docRef' => $projectService->getProject()->getDocRef()]); } /* * Update the cost */ foreach ($formData['costPerAffiliationAndYear'] as $affiliationId => $costPerYear) { $affiliation = $this->getAffiliationService()->findEntityById('Affiliation', $affiliationId); if ($this->isAllowed($affiliation, 'edit-affiliation') || $hasProjectEditRights) { foreach ($costPerYear as $year => $costValue) { $cost = $this->getProjectService()->findCostByAffiliationAndYear($affiliation, $year); if (($costValue['cost'] == '0' || empty($costValue['cost'])) && !is_null($cost)) { $this->getProjectService()->removeEntity($cost); } if ((double) $costValue['cost'] > 0) { /* * Create a new if not set yet */ if (is_null($cost)) { $cost = new Cost(); $cost->setAffiliation($affiliation); $dateStart = new \DateTime(); $cost->setDateStart($dateStart->modify('first day of january ' . $year)); $dateEnd = new \DateTime(); $cost->setDateEnd($dateEnd->modify('last day of december ' . $year)); } $cost->setCosts($costValue['cost'] * 1000); $this->getProjectService()->updateEntity($cost); } } } } /* * Update the effort */ foreach ($formData['effortPerAffiliationAndYear'] as $workpackageId => $effortPerAffiliationAndYear) { $workpackage = $this->getProjectService()->findEntityById('Workpackage\\Workpackage', $workpackageId); foreach ($effortPerAffiliationAndYear as $affiliationId => $effortPerYear) { $affiliation = $this->getAffiliationService()->findEntityById('Affiliation', $affiliationId); if ($this->isAllowed($affiliation, 'edit-affiliation') || $this->isAllowed($workpackage, 'edit-community') || $hasProjectEditRights) { foreach ($effortPerYear as $year => $effortValue) { $effort = $this->getProjectService()->findEffortByAffiliationAndWorkpackageAndYear($affiliation, $workpackage, $year); if (($effortValue['effort'] == '0' || empty($effortValue['effort'])) && !is_null($effort)) { $this->getProjectService()->removeEntity($effort); } if ((double) $effortValue['effort'] > 0) { /* * Create a new if not set yet */ if (is_null($effort)) { $effort = new Effort(); $effort->setAffiliation($affiliation); $effort->setWorkpackage($workpackage); $dateStart = new \DateTime(); $effort->setDateStart($dateStart->modify('first day of january ' . $year)); $dateEnd = new \DateTime(); $effort->setDateEnd($dateEnd->modify('last day of december ' . $year)); } $effort->setEffort($effortValue['effort']); $this->getProjectService()->updateEntity($effort); } } } } } //Update the mode of the project when the saved is pressed $project = $projectService->getProject(); $project->setMode($projectService->getNextMode()->mode); $this->getProjectService()->updateEntity($project); $this->flashMessenger()->setNamespace('success')->addMessage(sprintf(_("txt-cost-and-effort-of-project-%s-has-successfully-been-updated"), $projectService->parseFullName())); return $this->redirect()->toRoute('community/project/project/cost-and-effort', ['docRef' => $projectService->getProject()->getDocRef()]); } return new ViewModel(['projectService' => $projectService, 'hasProjectEditRights' => $hasProjectEditRights, 'workpackageService' => $this->getWorkpackageService(), 'options' => $this->getOptions(), 'form' => $form]); }