/**
  * Copy a project task to a template task
  *
  * @access public
  * @param $project_task ProjectTask project task
  * @param $template_id int the template id
  * @param $parent_id int the parent id if is a subtask
  * @return TemplateTask
  */
 function copyFromProjectTask($project_task, $template_id, $parent_id = 0, $milestone_id = 0)
 {
     //param
     $parent_subtask = 0;
     $new_st_date = '';
     $new_due_date = '';
     $new_task = new TemplateTask();
     $new_task->setObjectName($project_task->getObjectName());
     $new_task->setText($project_task->getText());
     $new_task->setAssignedToContactId($project_task->getAssignedToContactId());
     $new_task->setAssignedOn($project_task->getAssignedOn());
     $new_task->setAssignedById($project_task->getAssignedById());
     $new_task->setTimeEstimate($project_task->getTimeEstimate());
     $new_task->setStartedOn($project_task->getStartedOn());
     $new_task->setStartedById($project_task->getStartedById());
     $new_task->setPriority($project_task->getPriority());
     $new_task->setOrder($project_task->getOrder());
     $new_task->setUseStartTime($project_task->getUseStartTime());
     $new_task->setUseDueTime($project_task->getUseDueTime());
     $new_task->setTypeContent($project_task->getTypeContent());
     $new_task->setParentId($project_task->getParentId());
     $new_task->setOriginalTaskId($project_task->getId());
     $new_task->setTemplateId($template_id);
     $new_task->setSessionId(null);
     $new_task->setParentId($parent_id);
     $new_task->setMilestoneId($milestone_id);
     if ($project_task->getDueDate() instanceof DateTimeValue) {
         $new_task->setDueDate(new DateTimeValue($project_task->getDueDate()->getTimestamp()));
     }
     if ($project_task->getStartDate() instanceof DateTimeValue) {
         $new_task->setStartDate(new DateTimeValue($project_task->getStartDate()->getTimestamp()));
     }
     if ($new_st_date != "") {
         if ($new_task->getStartDate() instanceof DateTimeValue) {
             $new_task->setStartDate($new_st_date);
         }
     }
     if ($new_due_date != "") {
         if ($new_task->getDueDate() instanceof DateTimeValue) {
             $new_task->setDueDate($new_due_date);
         }
     }
     $new_task->save();
     // Copy members, linked_objects, custom_properties, subscribers, reminders and comments
     copy_additional_object_data($project_task, $new_task);
     // Ensure that assigned user is subscribed
     if ($new_task->getAssignedTo() instanceof Contact) {
         $new_task->subscribeUser($new_task->getAssignedTo());
     }
     return $new_task;
 }
 function cloneTask($new_st_date = '', $new_due_date = '', $copy_status = false, $copy_repeat_options = true, $parent_subtask = 0)
 {
     $new_task = new ProjectTask();
     if ($parent_subtask != 0) {
         $new_task->setParentId($parent_subtask);
     } else {
         $new_task->setParentId($this->getParentId());
     }
     $new_task->setObjectName($this->getObjectName());
     $new_task->setText($this->getText());
     $new_task->setAssignedToContactId($this->getAssignedToContactId());
     $new_task->setAssignedOn($this->getAssignedOn());
     $new_task->setAssignedById($this->getAssignedById());
     $new_task->setTimeEstimate($this->getTimeEstimate());
     $new_task->setStartedOn($this->getStartedOn());
     $new_task->setStartedById($this->getStartedById());
     $new_task->setPriority($this->getPriority());
     $new_task->setState($this->getState());
     $new_task->setOrder($this->getOrder());
     $new_task->setMilestoneId($this->getMilestoneId());
     $new_task->setFromTemplateId($this->getFromTemplateId());
     $new_task->setUseStartTime($this->getUseStartTime());
     $new_task->setUseDueTime($this->getUseDueTime());
     $new_task->setTypeContent($this->getTypeContent());
     if ($this->getParentId() == 0) {
         //if not subtask
         if ($this->getOriginalTaskId() == 0) {
             $new_task->setOriginalTaskId($this->getObjectId());
         } else {
             $new_task->setOriginalTaskId($this->getOriginalTaskId());
         }
     }
     if ($this->getDueDate() instanceof DateTimeValue) {
         $new_task->setDueDate(new DateTimeValue($this->getDueDate()->getTimestamp()));
     }
     if ($this->getStartDate() instanceof DateTimeValue) {
         $new_task->setStartDate(new DateTimeValue($this->getStartDate()->getTimestamp()));
     }
     if ($copy_status) {
         $new_task->setCompletedById($this->getCompletedById());
         $new_task->setCompletedOn($this->getCompletedOn());
     }
     if ($copy_repeat_options) {
         $new_task->setRepeatEnd($this->getRepeatEnd());
         $new_task->setRepeatForever($this->getRepeatForever());
         $new_task->setRepeatNum($this->getRepeatNum());
         $new_task->setRepeatBy($this->getRepeatBy());
         $new_task->setRepeatD($this->getRepeatD());
         $new_task->setRepeatM($this->getRepeatM());
         $new_task->setRepeatY($this->getRepeatY());
     }
     if ($new_st_date != "") {
         if ($new_task->getStartDate() instanceof DateTimeValue) {
             $new_task->setStartDate($new_st_date);
         }
     }
     if ($new_due_date != "") {
         if ($new_task->getDueDate() instanceof DateTimeValue) {
             $new_task->setDueDate($new_due_date);
         }
     }
     $new_task->save();
     // Copy members, linked_objects, custom_properties, subscribers, reminders and comments
     copy_additional_object_data($this, $new_task);
     // Ensure that assigned user is subscribed
     if ($new_task->getAssignedTo() instanceof Contact) {
         $new_task->subscribeUser($new_task->getAssignedTo());
     }
     $sub_tasks = $this->getAllSubTasks();
     foreach ($sub_tasks as $st) {
         $new_dates = $this->getNextRepetitionDatesSubtask($st, $new_task, $new_st_date, $new_due_date);
         if ($st->getParentId() == $this->getId()) {
             $new_st = $st->cloneTask(array_var($new_dates, 'st'), array_var($new_dates, 'due'), $copy_status, $copy_repeat_options, $new_task->getId());
             if ($copy_status) {
                 $new_st->setCompletedById($st->getCompletedById());
                 $new_st->setCompletedOn($st->getCompletedOn());
                 $new_st->save();
             }
             $new_task->attachTask($new_st);
         }
     }
     return $new_task;
 }