function add() { if ($this->request->isAsyncCall() || $this->request->isMobileDevice() || $this->request->isApiCall() && $this->request->isSubmitted()) { if (Milestones::canAdd($this->logged_user, $this->active_project)) { $milestone_data = $this->request->post('milestone'); $this->smarty->assign('milestone_data', $milestone_data); if ($this->request->isSubmitted()) { try { DB::beginWork('Creating milestone @ ' . __CLASS__); $this->active_milestone = new RemediaMilestone(); $this->active_milestone->setAttributes($milestone_data); $start_on = $this->active_milestone->getStartOn(); if ($start_on instanceof DateValue) { if (Globalization::isWeekend($start_on) || Globalization::isDayOff($start_on)) { throw new Error(lang('Start date needs to be set on working day')); } //if } //if $due_on = $this->active_milestone->getDueOn(); if ($due_on instanceof DateValue) { if (Globalization::isWeekend($due_on) || Globalization::isDayOff($due_on)) { throw new Error(lang('Due date needs to be set on working day')); } //if } //if $this->active_milestone->setProjectId($this->active_project->getId()); $this->active_milestone->setCreatedBy($this->logged_user); $this->active_milestone->setState(STATE_VISIBLE); $this->active_milestone->setVisibility(VISIBILITY_NORMAL); $this->active_milestone->save(); /* INIZIO frosso hack */ if (AngieApplication::isModuleLoaded('tracking') && TrackingObjects::canAdd($this->logged_user, $this->active_project)) { $estimate_value = isset($milestone_data['estimate_value']) && $milestone_data['estimate_value'] ? (double) $milestone_data['estimate_value'] : null; $estimate_job_type = isset($milestone_data['estimate_job_type_id']) && $milestone_data['estimate_job_type_id'] ? JobTypes::findById($milestone_data['estimate_job_type_id']) : null; $estimate_comment = isset($milestone_data['estimate_comment']) ? $milestone_data['estimate_comment'] : null; if ($estimate_value > 0 && $estimate_job_type instanceof JobType) { $this->active_milestone->tracking()->setEstimate($estimate_value, $estimate_job_type, $estimate_comment, $this->logged_user); } else { if ($this->active_milestone->tracking()->getEstimate() instanceof Estimate) { $this->active_milestone->tracking()->setEstimate($estimate_value, $estimate_job_type, $estimate_comment, $this->logged_user); } // if } // if } // if /* FINE frosso hack */ $this->active_milestone->subscriptions()->set(array_unique(array_merge((array) $this->logged_user->getId(), (array) $this->active_project->getLeaderId(), (array) array_var($milestone_data, 'subscribers', array()))), false); DB::commit('Milestone created @ ' . __CLASS__); $this->logged_user->notifier()->notifySubscribers($this->active_milestone, 'system/new_milestone'); if ($this->request->isPageCall()) { $this->flash->success('Milestone ":name" has been created', array('name' => $this->active_milestone->getName())); $this->response->redirectToUrl($this->active_milestone->getViewUrl()); } else { $this->response->respondWithData($this->active_milestone, array('as' => 'milestone', 'detailed' => true)); } // if } catch (Exception $e) { DB::rollback('Failed to create milestone @ ' . __CLASS__); if ($this->request->isPageCall()) { $this->smarty->assign('errors', $e); } else { $this->response->exception($e); } // if } // try } // if } else { $this->response->forbidden(); } // if } else { $this->response->badRequest(); } // if }