/**
  * Show and process edit milestone form
  *
  * @access public
  * @param void
  * @return null
  */
 function edit()
 {
     $this->setTemplate('add_milestone');
     $milestone = ProjectMilestones::findById(get_id());
     if (!$milestone instanceof ProjectMilestone) {
         flash_error(lang('milestone dnx'));
         $this->redirectTo('milestone', 'index');
     }
     // if
     if (!$milestone->canEdit(logged_user())) {
         flash_error(lang('no access permissions'));
         $this->redirectToReferer(get_url('milestone'));
     }
     $milestone_data = array_var($_POST, 'milestone');
     if (!is_array($milestone_data)) {
         $tag_names = $milestone->getTagNames();
         $milestone_data = array('name' => $milestone->getName(), 'due_date' => $milestone->getDueDate(), 'description' => $milestone->getDescription(), 'assigned_to' => $milestone->getAssignedToCompanyId() . ':' . $milestone->getAssignedToUserId(), 'tags' => is_array($tag_names) ? implode(', ', $tag_names) : '', 'is_private' => $milestone->isPrivate());
         // array
     }
     // if
     tpl_assign('milestone_data', $milestone_data);
     tpl_assign('milestone', $milestone);
     if (is_array(array_var($_POST, 'milestone'))) {
         $old_owner = $milestone->getAssignedTo();
         // remember the old owner
         $milestone_data['due_date'] = DateTimeValueLib::make(0, 0, 0, array_var($_POST, 'milestone_due_date_month', 1), array_var($_POST, 'milestone_due_date_day', 1), array_var($_POST, 'milestone_due_date_year', 1970));
         $assigned_to = explode(':', array_var($milestone_data, 'assigned_to', ''));
         $old_is_private = $milestone->isPrivate();
         $milestone->setFromAttributes($milestone_data);
         if (!logged_user()->isMemberOfOwnerCompany()) {
             $milestone->setIsPrivate($old_is_private);
         }
         $milestone->setProjectId(active_project()->getId());
         $milestone->setAssignedToCompanyId(array_var($assigned_to, 0, 0));
         $milestone->setAssignedToUserId(array_var($assigned_to, 1, 0));
         try {
             DB::beginWork();
             $milestone->save();
             $milestone->setTagsFromCSV(array_var($milestone_data, 'tags'));
             ApplicationLogs::createLog($milestone, active_project(), ApplicationLogs::ACTION_EDIT);
             DB::commit();
             // If owner is changed send notification but don't break submission
             try {
                 $new_owner = $milestone->getAssignedTo();
                 if (array_var($milestone_data, 'send_notification') == 'checked') {
                     if ($old_owner instanceof User) {
                         // We have a new owner and it is different than old owner
                         if ($new_owner instanceof User && $new_owner->getId() != $old_owner->getId()) {
                             Notifier::milestoneAssigned($milestone);
                         }
                     } else {
                         // We have new owner
                         if ($new_owner instanceof User) {
                             Notifier::milestoneAssigned($milestone);
                         }
                     }
                     // if
                 }
                 // if
             } catch (Exception $e) {
             }
             // try
             flash_success(lang('success edit milestone', $milestone->getName()));
             $this->redirectTo('milestone');
         } catch (Exception $e) {
             DB::rollback();
             tpl_assign('error', $e);
         }
         // try
     }
     // if
 }
	/**
	 * Show and process add milestone form
	 *
	 * @access public
	 * @param void
	 * @return null
	 */
	function add() {
		if (logged_user()->isGuest()) {
			flash_error(lang('no access permissions'));
			ajx_current("empty");
			return;
		}
		$this->setTemplate('add_milestone');
		
		$notAllowedMember = '' ;
		if(!ProjectMilestone::canAdd(logged_user(), active_context(), $notAllowedMember)) {
			if (str_starts_with($notAllowedMember, '-- req dim --')) flash_error(lang('must choose at least one member of', str_replace_first('-- req dim --', '', $notAllowedMember, $in)));
			else flash_error(lang('no context permissions to add',lang("milestones"),$notAllowedMember));
			ajx_current("empty");
			return;
		} 

		$milestone_data = array_var($_POST, 'milestone');
		$now = DateTimeValueLib::now();
		$due_date = DateTimeValueLib::make(0, 0, 0, array_var($_GET, 'due_month', $now->getMonth()), array_var($_GET, 'due_day', $now->getDay()), array_var($_GET, 'due_year', $now->getYear()));
		if(!is_array($milestone_data)) {
			$milestone_data = array(
				'due_date' => $due_date,
				'name' => array_var($_GET, 'name', ''),
				'assigned_to' => array_var($_GET, 'assigned_to', '0'),
				'is_template' => array_var($_GET, "is_template", false)
			); // array
		} // if
		$milestone = new ProjectMilestone();
		tpl_assign('milestone_data', $milestone_data);
		tpl_assign('milestone', $milestone);

		if (is_array(array_var($_POST, 'milestone'))) {
			$milestone_data['due_date'] = getDateValue(array_var($milestone_data, 'due_date_value'),DateTimeValueLib::now()->beginningOfDay());
			$milestone_data['object_type_id'] = $milestone->getObjectTypeId();
			
			$milestone->setFromAttributes($milestone_data);
			
			$urgent = array_var($milestone_data, 'is_urgent') == 'checked';
			$milestone->setIsUrgent($urgent);

			try {
				$member_ids = json_decode(array_var($_POST, 'members'));
				
				DB::beginWork();

				$milestone->save();
				$object_controller = new ObjectController();
			    $object_controller->add_to_members($milestone, $member_ids);
			    $object_controller->add_subscribers($milestone);
			    $object_controller->link_to_new_object($milestone);
				$object_controller->add_custom_properties($milestone);
				$object_controller->add_reminders($milestone);

				if (array_var($_GET, 'copyId', 0) > 0) {
					// copy remaining stuff from the milestone with id copyId
					$toCopy = ProjectMilestones::findById(array_var($_GET, 'copyId'));
					if ($toCopy instanceof ProjectMilestone) {
						ProjectMilestones::copyTasks($toCopy, $milestone, array_var($milestone_data, 'is_template', false));
					}
				}
				
				ApplicationLogs::createLog($milestone, ApplicationLogs::ACTION_ADD);
				
				DB::commit();

				// Send notification
				try {
					if(!$milestone->getIsTemplate() && array_var($milestone_data, 'send_notification') == 'checked') {
						Notifier::milestoneAssigned($milestone); // send notification
					} // if
				} catch(Exception $e) {

				} // try

				if ($milestone->getIsTemplate()) {
					flash_success(lang('success add template', $milestone->getObjectName()));
				} else {
					flash_success(lang('success add milestone', $milestone->getObjectName()));
				}
				ajx_current("back");

			} catch(Exception $e) {
				DB::rollback();
				flash_error($e->getMessage());
				ajx_current("empty");
			} // try
		} // if
	} // add
 /**
  * Show and process edit milestone form
  *
  * @access public
  * @param void
  * @return null
  */
 function edit()
 {
     if (logged_user()->isGuest()) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     $this->setTemplate('add_milestone');
     $milestone = ProjectMilestones::findById(get_id());
     if (!$milestone instanceof ProjectMilestone) {
         flash_error(lang('milestone dnx'));
         ajx_current("empty");
         return;
     }
     // if
     if (!$milestone->canEdit(logged_user())) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     $milestone_data = array_var($_POST, 'milestone');
     if (!is_array($milestone_data)) {
         $tag_names = $milestone->getTagNames();
         $milestone_data = array('name' => $milestone->getName(), 'due_date' => $milestone->getDueDate(), 'description' => $milestone->getDescription(), 'assigned_to' => $milestone->getAssignedToCompanyId() . ':' . $milestone->getAssignedToUserId(), 'tags' => is_array($tag_names) ? implode(', ', $tag_names) : '', 'is_private' => $milestone->isPrivate(), 'is_urgent' => $milestone->getIsUrgent());
         // array
     }
     // if
     tpl_assign('milestone_data', $milestone_data);
     tpl_assign('milestone', $milestone);
     if (is_array(array_var($_POST, 'milestone'))) {
         if (array_var($milestone_data, 'due_date_value') != '') {
             $milestone_data['due_date'] = getDateValue(array_var($milestone_data, 'due_date_value'));
         } else {
             $now = DateTimeValueLib::now();
             $milestone_data['due_date'] = DateTimeValueLib::make(0, 0, 0, $now->getMonth(), $now->getDay(), $now->getYear());
         }
         $old_owner = $milestone->getAssignedTo();
         // remember the old owner
         $assigned_to = explode(':', array_var($milestone_data, 'assigned_to', ''));
         $old_is_private = $milestone->isPrivate();
         $milestone->setFromAttributes($milestone_data);
         $urgent = array_var($milestone_data, 'is_urgent') == 'checked';
         $milestone->setIsUrgent($urgent);
         if (!logged_user()->isMemberOfOwnerCompany()) {
             $milestone->setIsPrivate($old_is_private);
         }
         $old_project_id = $milestone->getProjectId();
         $project_id = array_var($_POST, 'ws_ids');
         if ($old_project_id != $project_id) {
             $newProject = Projects::findById($project_id);
             if (!$milestone->canAdd(logged_user(), $newProject)) {
                 flash_error(lang('no access permissions'));
                 ajx_current("empty");
                 return;
             }
             // if
             $milestone->move_inconsistent_tasks($newProject);
         }
         $milestone->setAssignedToCompanyId(array_var($assigned_to, 0, 0));
         $milestone->setAssignedToUserId(array_var($assigned_to, 1, 0));
         try {
             DB::beginWork();
             $milestone->save();
             $milestone->setTagsFromCSV(array_var($milestone_data, 'tags'));
             $object_controller = new ObjectController();
             $object_controller->add_to_workspaces($milestone);
             $object_controller->link_to_new_object($milestone);
             $object_controller->add_subscribers($milestone);
             $object_controller->add_custom_properties($milestone);
             $object_controller->add_reminders($milestone);
             ApplicationLogs::createLog($milestone, $milestone->getWorkspaces(), ApplicationLogs::ACTION_EDIT);
             DB::commit();
             // If owner is changed send notification but don't break submission
             try {
                 $new_owner = $milestone->getAssignedTo();
                 if (array_var($milestone_data, 'send_notification') == 'checked') {
                     if ($old_owner instanceof User) {
                         // We have a new owner and it is different than old owner
                         if ($new_owner instanceof User && $new_owner->getId() != $old_owner->getId()) {
                             Notifier::milestoneAssigned($milestone);
                         }
                     } else {
                         // We have new owner
                         if ($new_owner instanceof User) {
                             Notifier::milestoneAssigned($milestone);
                         }
                     }
                     // if
                 }
                 // if
             } catch (Exception $e) {
             }
             // try
             flash_success(lang('success edit milestone', $milestone->getName()));
             ajx_current("back");
         } catch (Exception $e) {
             DB::rollback();
             flash_error($e->getMessage());
             ajx_current("empty");
         }
         // try
     }
     // if
 }
 /**
  * Show and process add milestone form
  *
  * @access public
  * @param void
  * @return null
  */
 function add()
 {
     if (logged_user()->isGuest()) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     $this->setTemplate('add_milestone');
     $notAllowedMember = '';
     if (!ProjectMilestone::canAdd(logged_user(), active_context(), $notAllowedMember)) {
         if (str_starts_with($notAllowedMember, '-- req dim --')) {
             flash_error(lang('must choose at least one member of', str_replace_first('-- req dim --', '', $notAllowedMember, $in)));
         } else {
             trim($notAllowedMember) == "" ? flash_error(lang('you must select where to keep', lang('the milestone'))) : flash_error(lang('no context permissions to add', lang("milestones"), $notAllowedMember));
         }
         ajx_current("empty");
         return;
     }
     $milestone_data = array_var($_POST, 'milestone');
     if (!is_array($milestone_data)) {
         // set layout for modal form
         if (array_var($_REQUEST, 'modal')) {
             $this->setLayout("json");
             tpl_assign('modal', true);
         }
         $milestone_data = array('due_date' => '', 'name' => array_var($_GET, 'name', ''), 'assigned_to' => array_var($_GET, 'assigned_to', '0'), 'is_template' => array_var($_GET, "is_template", false));
         // array
     }
     // if
     //is template milestone?
     if (array_var($_REQUEST, 'template_milestone') == true) {
         $milestone = new TemplateMilestone();
         $this->setTemplate(get_template_path('add_template_milestone', 'template_milestone'));
     } else {
         $milestone = new ProjectMilestone();
     }
     tpl_assign('milestone_data', $milestone_data);
     tpl_assign('milestone', $milestone);
     if (is_array(array_var($_POST, 'milestone'))) {
         $milestone_data['due_date'] = getDateValue(array_var($milestone_data, 'due_date_value'), DateTimeValueLib::now()->beginningOfDay());
         $milestone_data['object_type_id'] = $milestone->getObjectTypeId();
         $milestone->setFromAttributes($milestone_data);
         $urgent = array_var($milestone_data, 'is_urgent');
         $milestone->setIsUrgent($urgent);
         try {
             $member_ids = json_decode(array_var($_POST, 'members'));
             if ($milestone instanceof TemplateMilestone) {
                 $milestone->setSessionId(logged_user()->getId());
             }
             DB::beginWork();
             $milestone->save();
             $object_controller = new ObjectController();
             $object_controller->add_to_members($milestone, $member_ids);
             $object_controller->add_subscribers($milestone);
             $object_controller->link_to_new_object($milestone);
             $object_controller->add_custom_properties($milestone);
             $object_controller->add_reminders($milestone);
             if (array_var($_GET, 'copyId', 0) > 0) {
                 // copy remaining stuff from the milestone with id copyId
                 $toCopy = ProjectMilestones::findById(array_var($_GET, 'copyId'));
                 if ($toCopy instanceof ProjectMilestone) {
                     ProjectMilestones::copyTasks($toCopy, $milestone, array_var($milestone_data, 'is_template', false));
                 }
             }
             DB::commit();
             ApplicationLogs::createLog($milestone, ApplicationLogs::ACTION_ADD);
             //Send Template milestone to view
             if ($milestone instanceof TemplateMilestone) {
                 $object = array("action" => "add", "object_id" => $milestone->getObjectId(), "type" => $milestone->getObjectTypeName(), "id" => $milestone->getId(), "name" => $milestone->getObjectName(), "ico" => "ico-milestone", "manager" => get_class($milestone->manager()));
                 evt_add("template object added", array('object' => $object));
             }
             // Send notification
             try {
                 if (!$milestone instanceof TemplateMilestone && array_var($milestone_data, 'send_notification')) {
                     Notifier::milestoneAssigned($milestone);
                     // send notification
                 }
                 // if
             } catch (Exception $e) {
             }
             // try
             $is_template = $milestone instanceof TemplateMilestone;
             if (array_var($_REQUEST, 'modal')) {
                 ajx_current("empty");
                 $this->setLayout("json");
                 $this->setTemplate(get_template_path("empty"));
                 // reload milestone info because plugins may have updated some task info (for example: name prefix)
                 if ($is_template) {
                     $milestone = TemplateMilestones::findById($milestone->getId());
                     $params = array('msg' => lang('success add milestone', $milestone->getObjectName()), 'milestone' => $milestone->getArrayInfo(), 'reload' => array_var($_REQUEST, 'reload'));
                     if ($milestone instanceof TemplateMilestone) {
                         $params = $object;
                     }
                     print_modal_json_response($params, true, array_var($_REQUEST, 'use_ajx'));
                 } else {
                     $milestone = ProjectMilestones::findById($milestone->getId());
                     flash_success(lang('success add milestone', $milestone->getObjectName()));
                     evt_add("reload current panel");
                 }
             } else {
                 if ($milestone instanceof TemplateMilestone) {
                     flash_success(lang('success add template', $milestone->getObjectName()));
                 } else {
                     flash_success(lang('success add milestone', $milestone->getObjectName()));
                 }
                 if (array_var($task_data, 'inputtype') != 'taskview') {
                     ajx_current("back");
                 } else {
                     ajx_current("reload");
                 }
             }
         } catch (Exception $e) {
             DB::rollback();
             if (array_var($_REQUEST, 'modal')) {
                 $this->setLayout("json");
                 $this->setTemplate(get_template_path("empty"));
                 print_modal_json_response(array('errorCode' => 1, 'errorMessage' => $e->getMessage()));
             } else {
                 flash_error($e->getMessage());
             }
             ajx_current("empty");
         }
         // try
     }
     // if
 }