/**
  * Update an existing comment
  *
  * @param void
  * @return null
  */
 function edit()
 {
     $this->wireframe->print_button = false;
     if ($this->active_comment->isNew()) {
         $this->httpError(HTTP_ERR_NOT_FOUND);
     }
     // if
     if (!$this->active_comment->canEdit($this->logged_user)) {
         $this->httpError(HTTP_ERR_FORBIDDEN);
     }
     // if
     $parent = $this->active_comment->getParent();
     if (!instance_of($parent, 'ProjectObject')) {
         $this->httpError(HTTP_ERR_NOT_FOUND);
     }
     // if
     $parent->prepareProjectSectionBreadcrumb($this->wireframe);
     $this->wireframe->addBreadCrumb($parent->getName(), $parent->getViewUrl());
     $comment_data = $this->request->post('comment');
     if (!is_array($comment_data)) {
         $comment_data = array('body' => $this->active_comment->getBody());
     }
     // if
     $this->smarty->assign('comment_data', $comment_data);
     //BOF:task_1260
     $active_object = ProjectObjects::findById($this->active_comment->getParentId());
     $this->smarty->assign('subscribers', $active_object->getSubscribers());
     $link = mysql_connect(DB_HOST, DB_USER, DB_PASS);
     mysql_select_db(DB_NAME);
     //$query = "select * from healingcrystals_assignments_action_request where comment_id='" . $this->active_comment->getId() . "' and selected_by_user_id='" . $this->logged_user->getId() . "'";
     $query = "select * from healingcrystals_assignments_action_request where comment_id='" . $this->active_comment->getId() . "'";
     $request = mysql_query($query);
     $fyi_users = array();
     $action_request_users = array();
     while ($entry = mysql_fetch_array($request)) {
         //BOF:mod 20130429
         /*
         //EOF:mod 20130429
         	  	if ($entry['is_action_request']=='1'){
         //BOF:mod 20130429
         */
         if (!empty($entry['is_action_request'])) {
             //EOF:mod 20130429
             $action_request_users[] = $entry['user_id'];
         }
         //BOF:mod 20130429
         /*
         //EOF:mod 20130429
         	  	if ($entry['is_fyi']=='1'){
         	  		$fyi_users[] = $entry['user_id'];
         	  	}
         //BOF:mod 20130429
         */
         //EOF:mod 20130429
     }
     $this->smarty->assign('fyi_users', $fyi_users);
     $this->smarty->assign('action_request_users', $action_request_users);
     $this->smarty->assign('logged_user', $this->logged_user);
     //EOF:task_1260
     if ($this->request->isSubmitted()) {
         $this->active_comment->setAttributes($comment_data);
         $save = $this->active_comment->save();
         if ($save && !is_error($save)) {
             //BOF:task_1260
             //$subscribers_to_notify = array_var($comment_data, 'subscribers_to_notify');
             $action_request_user_id = array_var($comment_data, 'action_request');
             //mysql_query("update healingcrystals_assignments_action_request set is_action_request='0', is_fyi='0' where comment_id='" . $this->active_comment->getId() . "' and selected_by_user_id='" . $this->logged_user->getId() . "' and is_action_request<>'-1' and is_fyi<>'-1'");
             //mysql_query("update healingcrystals_assignments_action_request set is_action_request='0', is_fyi='0' where comment_id='" . $this->active_comment->getId() . "' and selected_by_user_id='" . $this->logged_user->getId() . "' and is_action_request<>'-1' and is_fyi<>'-1'");
             /*if (!empty($subscribers_to_notify)){
                 foreach ($subscribers_to_notify as $id){
                     $query = "select * from healingcrystals_assignments_action_request where comment_id='" . $this->active_comment->getId() . "' and selected_by_user_id='" . $this->logged_user->getId() . "' and user_id='" . $id . "'";
                     $result = mysql_query($query);
                     if (mysql_num_rows($result)){
                         $query = "update healingcrystals_assignments_action_request set is_fyi='1' where comment_id='" . $this->active_comment->getId() . "' and selected_by_user_id='" . $this->logged_user->getId() . "' and user_id='" . $id . "'";
                         mysql_query($query);
                     } else {
                         $query = "insert into healingcrystals_assignments_action_request (user_id, is_action_request, is_fyi, selected_by_user_id, comment_id, date_added) values ('" . $id . "', '0', '1', '" . $this->logged_user->getId() . "', '" . $this->active_comment->getId() . "', now())";
                         mysql_query($query);
                     }
                 }
               }*/
             $existing_ar_users = array();
             $new_ar_users = array();
             if (!empty($action_request_user_id)) {
                 foreach ($action_request_user_id as $id) {
                     $query = "select * from healingcrystals_assignments_action_request where comment_id='" . $this->active_comment->getId() . "' and user_id='" . $id . "'";
                     $result = mysql_query($query);
                     if (mysql_num_rows($result)) {
                         $info = mysql_fetch_assoc($result);
                         if ($info['is_action_request'] == '1') {
                             $existing_ar_users[] = $id;
                         } else {
                             $query = "update healingcrystals_assignments_action_request set is_action_request='1' where comment_id='" . $this->active_comment->getId() . "' and user_id='" . $id . "'";
                             mysql_query($query);
                             $new_ar_users[] = $id;
                         }
                     } else {
                         $query = "insert into healingcrystals_assignments_action_request (user_id, is_action_request, is_fyi, selected_by_user_id, comment_id, date_added) values ('" . $id . "', '1', '0', '" . $this->logged_user->getId() . "', '" . $this->active_comment->getId() . "', now())";
                         mysql_query($query);
                         $new_ar_users[] = $id;
                     }
                 }
                 $query = "update healingcrystals_assignments_action_request set is_action_request='0' where comment_id='" . $this->active_comment->getId() . "' and user_id not in (" . implode(', ', $action_request_user_id) . ")";
                 mysql_query($query);
             } else {
                 $query = "update healingcrystals_assignments_action_request set is_action_request='0' where comment_id='" . $this->active_comment->getId() . "'";
                 mysql_query($query);
             }
             mysql_query("delete from healingcrystals_assignments_action_request where comment_id='" . $this->active_comment->getId() . "' and is_action_request='0' and is_fyi='0' and marked_for_email='0'");
             //EOF:task_1260
             foreach ($action_request_users as $id) {
                 if (!in_array($id, $existing_ar_users)) {
                     //unassign
                     $query = "select object_id from actionrequests_to_tasklist where comment_id='" . $this->active_comment->getId() . "' and user_id='" . $id . "' and type='Task'";
                     $result = mysql_query($query);
                     if (mysql_num_rows($result)) {
                         $info = mysql_fetch_assoc($result);
                         $task = new Task($info['object_id']);
                         $task->delete();
                         mysql_query("delete from actionrequests_to_tasklist where comment_id='" . $this->active_comment->getId() . "' and user_id='" . $id . "' and type='Task'");
                     }
                 }
             }
             foreach ($new_ar_users as $id) {
                 //assign
                 //BOF:mod 20130429
                 /*
                 //EOF:mod 20130429
                 			$priority = '0';
                       $query = "select * from healingcrystals_assignments_action_request where comment_id='" . $this->active_comment->getId() . "' and user_id='" . $id . "'";
                 			$result = mysql_query($query, $link);
                       if (mysql_num_rows($result)){
                 				$query1 = "update healingcrystals_assignments_action_request set is_action_request='1', priority_actionrequest='" . $priority . "' where comment_id='" . $this->active_comment->getId() . "' and user_id='" . $id . "'";
                 				mysql_query($query1, $link);
                 			} else {
                 				$query1 = "insert into healingcrystals_assignments_action_request (user_id, is_action_request, is_fyi, selected_by_user_id, comment_id, date_added, priority_actionrequest) values ('" . $id . "', '1', '0', '" . $this->logged_user->getId() . "', '" . $this->active_comment->getId() . "', now(), '" . $priority . "')";
                 				mysql_query($query1, $link);
                 			}
                 //BOF:mod 20130429
                 */
                 //EOF:mod 20130429
                 $task = new Task();
                 $task->setProjectId(TASK_LIST_PROJECT_ID);
                 $task->setParentId(Page::getTaskPageIdForUser($id));
                 $task->setParentType('Page');
                 $task->setCreatedBy($this->logged_user);
                 $task->setVisibility(VISIBILITY_NORMAL);
                 $task->setState(STATE_VISIBLE);
                 $task_body = '';
                 $parent = $this->active_comment->getParent();
                 $url = $parent->getViewUrl() . '#comment' . $this->active_comment->getId();
                 $comment_body = $this->active_comment->getBody();
                 $comment_body = strip_tags($comment_body);
                 if (strlen($comment_body) > 525) {
                     $task_body .= substr($comment_body, 0, 525) . '..';
                 } else {
                     $task_body .= $comment_body;
                 }
                 $task_body .= '<br/><a href="' . $url . '">View Task in Full</a>';
                 $attachments = $this->active_comment->getAttachments();
                 if (is_foreachable($attachments)) {
                     $task_body .= '<br/>Attachments:<br/>';
                     foreach ($attachments as $attachment) {
                         $task_body .= '<a href="' . $attachment->getViewUrl() . '">' . $attachment->getName() . '</a><br/>';
                     }
                 }
                 $task->setBody($task_body);
                 $savetask = $task->save();
                 if ($savetask && !is_error($savetask)) {
                     $task->ready();
                     mysql_query("insert into actionrequests_to_tasklist (comment_id, user_id, type, object_id) values ('" . $this->active_comment->getId() . "', '" . $id . "', 'Task', '" . $task->getId() . "')");
                 }
             }
             if ($this->request->getFormat() == FORMAT_HTML) {
                 flash_success('Comment has been updated');
                 $this->redirectToUrl($this->active_comment->getRealViewUrl());
             } else {
                 $this->serveData($this->active_comment, 'comment');
             }
             // if
         } else {
             if ($this->request->getFormat() == FORMAT_HTML) {
                 $this->smarty->assign('errors', $save);
             } else {
                 $this->serveData($save);
             }
             // if
         }
         // if
     }
     // if
     //BOF:task_1260
     //mysql_close($link);
     //EOF:task_1260
 }
 /**
  * Show and process add task form
  *
  * @param void
  * @return null
  */
 function add()
 {
     $this->wireframe->print_button = false;
     if (!instance_of($this->active_task_parent, 'ProjectObject')) {
         $this->httpError(HTTP_ERR_NOT_FOUND);
     }
     // if
     if (!$this->active_task_parent->canSubtask($this->logged_user)) {
         $this->httpError(HTTP_ERR_FORBIDDEN);
     }
     // if
     $task_data = $this->request->post('task');
     $this->smarty->assign(array('task_data' => $task_data, 'page_tab' => $this->active_task_parent->getProjectTab()));
     if ($this->request->isSubmitted()) {
         db_begin_work();
         $this->active_task = new Task();
         // just in case...
         $this->active_task->log_activities = false;
         $this->active_task->setAttributes($task_data);
         $this->active_task->setParent($this->active_task_parent);
         $this->active_task->setProjectId($this->active_project->getId());
         if (trim($this->active_task->getCreatedByName()) == '' || trim($this->active_task->getCreatedByEmail()) == '') {
             $this->active_task->setCreatedBy($this->logged_user);
         }
         // if
         $this->active_task->setState(STATE_VISIBLE);
         $this->active_task->setVisibility($this->active_task_parent->getVisibility());
         $save = $this->active_task->save();
         if ($save && !is_error($save)) {
             $subscribers = array($this->logged_user->getId());
             if (is_foreachable(array_var($task_data['assignees'], 0))) {
                 $subscribers = array_merge($subscribers, array_var($task_data['assignees'], 0));
             } else {
                 $subscribers[] = $this->active_project->getLeaderId();
             }
             // if
             if (!in_array($this->active_project->getLeaderId(), $subscribers)) {
                 $subscribers[] = $this->active_project->getLeaderId();
             }
             // if
             Subscriptions::subscribeUsers($subscribers, $this->active_task);
             $activity = new NewTaskActivityLog();
             $activity->log($this->active_task, $this->logged_user);
             db_commit();
             $this->active_task->ready();
             //BOF:mod
             $recurring_flag = $task_data['recurring_flag'];
             $recurring_period = $task_data['recurring_period'];
             $recurring_period_type = $task_data['recurring_period_type'];
             $recurring_period_condition = $task_data['recurring_period_condition'];
             //$recurring_end_date         = str_replace('/', '-', $task_data['recurring_end_date']);
             ///    $reminder                   = str_replace('/', '-', $task_data['reminder']);
             $recurring_end_date = dateval($task_data['recurring_end_date']);
             /*$reminder                   = dateval($task_data['reminder']);
                          $reminderhours              = (int)$task_data['reminderhours'];
                          $reminderminutes            = (int)$task_data['reminderminutes'];
                          $remindermeridian           = $task_data['remindermeridian'];
                          if (!empty($reminder)){
                              if (!empty($remindermeridian) && $remindermeridian=='PM' && $reminderhours<12){
                                  $reminderhours += 12;
                              } elseif (!empty($remindermeridian) && $remindermeridian=='AM' && $reminderhours==12){
             			$reminderhours = 0;
             		}
                              $reminder               = $reminder . ' ' . $reminderhours . ':' . $reminderminutes;
                          }*/
             $email_flag = empty($task_data['email_flag']) ? '0' : '1';
             if ($email_flag == '1') {
                 $email_reminder_period = (int) $task_data['figure_before_due_date'];
                 $email_reminder_unit = empty($task_data['unit_before_due_date']) ? 'D' : $task_data['unit_before_due_date'];
                 $email_reminder_hours = empty($task_data['reminderhours']) ? '6' : $task_data['reminderhours'];
                 $email_reminder_minutes = (int) $task_data['reminderminutes'];
                 $email_reminder_meridian = empty($task_data['remindermeridian']) ? 'AM' : $task_data['remindermeridian'];
                 $email_reminder_time = '';
                 if ($email_reminder_meridian == 'PM' && $email_reminder_hours < 12) {
                     $email_reminder_time = $email_reminder_hours + 12 . ':';
                 } elseif ($email_reminder_meridian == 'AM' && $email_reminder_hours == 12) {
                     $email_reminder_time = '00:';
                 } else {
                     $email_reminder_time = str_pad($email_reminder_hours, 2, '0', STR_PAD_LEFT) . ':';
                 }
                 $email_reminder_time .= $email_reminder_minutes;
             }
             $link = mysql_connect(DB_HOST, DB_USER, DB_PASS);
             mysql_select_db(DB_NAME);
             //if (empty($recurring_flag) && empty($reminder) && empty($email_flag)){
             if (empty($recurring_flag) && empty($email_flag)) {
                 $query = "delete from healingcrystals_project_object_misc where object_id='" . $this->active_task->getId() . "'";
                 mysql_query($query, $link);
             } else {
                 if (empty($recurring_flag)) {
                     $recurring_period = '';
                     $recurring_period_type = '';
                     $recurring_period_condition = '';
                     $recurring_end_date = '';
                 } else {
                     if (empty($recurring_period) || (int) $recurring_period == 0) {
                         $recurring_period = '7';
                     }
                 }
                 $query = "select * from healingcrystals_project_object_misc where object_id='" . $this->active_task->getId() . "'";
                 $result = mysql_query($query, $link);
                 if (mysql_num_rows($result)) {
                     $query01 = "update healingcrystals_project_object_misc set\n                                    recurring_period='" . $recurring_period . "',\n                                    recurring_period_type='" . $recurring_period_type . "',\n                                    recurring_period_condition='" . $recurring_period_condition . "',\n                                    recurring_end_date='" . $recurring_end_date . "', " . "auto_email_status='" . $email_flag . "',\n\t\t\t\t\t\t\t\t\temail_reminder_period=" . ($email_flag == '1' ? "'" . $email_reminder_period . "'" : "null") . ", \n\t\t\t\t\t\t\t\t\temail_reminder_unit=" . ($email_flag == '1' ? "'" . $email_reminder_unit . "'" : "null") . ", \n\t\t\t\t\t\t\t\t\temail_reminder_time=" . ($email_flag == '1' ? "'" . $email_reminder_time . "'" : "null") . ", \n                                    last_modified=now() where object_id='" . $this->active_task->getId() . "'";
                     mysql_query($query01, $link);
                 } else {
                     $query01 = "insert into healingcrystals_project_object_misc\n                                    (object_id, " . "recurring_period,\n                                     recurring_period_type,\n                                     recurring_period_condition,\n                                     recurring_end_date,\n                                     date_added,\n                                     auto_email_status, \n\t\t\t\t\t\t\t\t\t email_reminder_period, \n\t\t\t\t\t\t\t\t\t email_reminder_unit, \n\t\t\t\t\t\t\t\t\t email_reminder_time) values\n                                     ('" . $this->active_task->getId() . "', " . "'" . $recurring_period . "',\n                                      '" . $recurring_period_type . "',\n                                      '" . $recurring_period_condition . "',\n                                      '" . $recurring_end_date . "',\n                                      now(),\n                                      '" . $email_flag . "', \n\t\t\t\t\t\t\t\t\t  " . ($email_flag == '1' ? "'" . $email_reminder_period . "'" : "null") . ", \n\t\t\t\t\t\t\t\t\t  " . ($email_flag == '1' ? "'" . $email_reminder_unit . "'" : "null") . ", \n\t\t\t\t\t\t\t\t\t  " . ($email_flag == '1' ? "'" . $email_reminder_time . "'" : "null") . ")";
                     mysql_query($query01, $link);
                 }
             }
             mysql_close($link);
             //EOF:mod
             if ($this->request->isApiCall()) {
                 $this->serveData($this->active_task, 'task');
             } elseif ($this->request->isAsyncCall()) {
                 $this->smarty->assign(array('_object_task' => $this->active_task));
                 print tpl_fetch(get_template_path('_task_opened_row', $this->controller_name, RESOURCES_MODULE));
                 die;
             } else {
                 //flash_success('Task ":name" has been added', array('name' => str_excerpt($this->active_task->getBody(), 80, '...')), false, false);
                 //bof:mod
                 flash_success('Task ":name" has been added', array('name' => str_excerpt(strip_tags($this->active_task->getBody()), 80, '...')), false, false);
                 //eof:mod
                 $this->redirectToUrl($this->active_task_parent->getViewUrl());
             }
             // if
         } else {
             db_rollback();
             if ($this->request->isApiCall() || $this->request->isAsyncCall()) {
                 $this->serveData($save);
             } else {
                 $this->smarty->assign('errors', $save);
                 //$this->smarty->assign('add_content', '');
             }
             // if
         }
         // if
     } else {
         if ($this->request->isApiCall()) {
             $this->httpError(HTTP_ERR_BAD_REQUEST);
         }
         // if
     }
     // if
 }
 /**
  * Mark this object as completed
  *
  * @param User $by
  * @param string $comment
  * @return boolean
  */
 function complete($by, $comment = null)
 {
     if ($this->isCompleted()) {
         return true;
         // already completed
     }
     // if
     if (!instance_of($by, 'User') && !instance_of($by, 'AnonymousUser')) {
         return new InvalidParamError('by', $by, '$by is expected to be an User or AnonymousUser instance', true);
     }
     // if
     if (instance_of($this, 'Ticket')) {
         $recurring_flag = false;
         $link = mysql_connect(DB_HOST, DB_USER, DB_PASS);
         mysql_select_db(DB_NAME);
         $query = "select recurring_period, recurring_period_type from healingcrystals_project_object_misc where object_id='" . $this->getId() . "'";
         $result = mysql_query($query, $link);
         if (mysql_num_rows($result)) {
             $info = mysql_fetch_assoc($result);
             $recurring_period = $info['recurring_period'];
             $recurring_period_type = $info['recurring_period_type'];
             switch ($recurring_period_type) {
                 case 'D':
                     $recurring_period_type = 'day' . ($recurring_period == 1 ? '' : 's');
                     break;
                 case 'W':
                     $recurring_period_type = 'week' . ($recurring_period == 1 ? '' : 's');
                     break;
                 case 'M':
                     $recurring_period_type = 'month' . ($recurring_period == 1 ? '' : 's');
                     break;
                 case 'Y':
                     $recurring_period_type = 'year' . ($recurring_period == 1 ? '' : 's');
                     break;
             }
             if (!empty($recurring_period) && !empty($recurring_period_type)) {
                 $recurring_flag = true;
             }
         }
         mysql_close();
     }
     if (instance_of($this, 'Ticket') && $recurring_flag) {
         $datetime = new DateTime();
         $datetime->modify('+' . $recurring_period . ' ' . $recurring_period_type);
         $this->setDueOn($datetime->format('Y-m-d'));
         $save = $this->save();
         if ($save && !is_error($save)) {
             $activity_log = new TaskCompletedActivityLog();
             $activity_log->log($this, $by);
         }
         // if
         return $save;
     } else {
         $this->setCompletedBy($by);
         $this->setCompletedOn(new DateTimeValue());
         $save = $this->save();
         if ($save && !is_error($save)) {
             event_trigger('on_project_object_completed', array(&$this, &$by, $comment));
             $activity_log = new TaskCompletedActivityLog();
             $activity_log->log($this, $by);
             if (instance_of($this, 'Task')) {
                 $link = mysql_connect(DB_HOST, DB_USER, DB_PASS);
                 mysql_select_db(DB_NAME);
                 $query = "select * from healingcrystals_project_object_misc where object_id='" . $this->getId() . "'";
                 $result = mysql_query($query, $link);
                 $flag_continue = true;
                 if (mysql_num_rows($result)) {
                     $info = mysql_fetch_assoc($result);
                     if (empty($info['recurring_period'])) {
                         $flag_continue = false;
                     }
                     if ($flag_continue) {
                         $recurring_period = $info['recurring_period'];
                         $recurring_period_type = $info['recurring_period_type'];
                         $recurring_period_condition = $info['recurring_period_condition'];
                         $recurring_end_date = $info['recurring_end_date'];
                         $auto_email_status = $info['auto_email_status'];
                         $email_reminder_period = $info['email_reminder_period'];
                         $email_reminder_unit = $info['email_reminder_unit'];
                         $email_reminder_time = $info['email_reminder_time'];
                         if ($recurring_period_condition == 'after_due_date') {
                             $due_date = new DateTime($this->getDueOn());
                         } else {
                             $due_date = new DateTime();
                         }
                         switch ($recurring_period_type) {
                             case 'D':
                                 $recurring_period_type = 'day' . ($recurring_period == 1 ? '' : 's');
                                 break;
                             case 'W':
                                 $recurring_period_type = 'week' . ($recurring_period == 1 ? '' : 's');
                                 break;
                             case 'M':
                                 $recurring_period_type = 'month' . ($recurring_period == 1 ? '' : 's');
                                 break;
                         }
                         $due_date->modify('+' . $recurring_period . ' ' . $recurring_period_type);
                         if (!empty($recurring_end_date) && $recurring_end_date != '0000-00-00') {
                             if (strtotime($due_date->format('Y-m-d')) - strtotime($recurring_end_date) > 0) {
                                 $flag_continue = false;
                             }
                         }
                     }
                     if ($flag_continue) {
                         $new_task = new Task();
                         $new_task->setSource($this->getSource());
                         $new_task->setType($this->getType());
                         $new_task->setModule('resources');
                         $new_task->setProjectId($this->getProjectId());
                         $new_task->setMilestoneId($this->getMilestoneId());
                         $new_task->setParentId($this->getParentId());
                         $new_task->setParentType($this->getParentType());
                         //$new_task->setName($this->getName());
                         $new_task->setBody($this->getBody());
                         $new_task->setState($this->getState());
                         $new_task->setVisibility($this->getVisibility());
                         $new_task->setPriority($this->getPriority());
                         $new_task->setCreatedOn(date('Y-m-d H:i:s'));
                         $new_task->setCreatedById($this->getCreatedById());
                         $new_task->setCreatedByName($this->getCreatedByName());
                         $new_task->setCreatedByEmail($this->getCreatedByEmail());
                         $new_task->setDueOn($due_date->format('Y-m-d'));
                         $query02 = "insert into healingcrystals_project_objects (source, type, module, project_id, milestone_id, parent_id, parent_type, body, state, visibility, priority, created_on, created_by_id, created_by_name, created_by_email, due_on) values ('{$new_task->getSource()}' , '{$new_task->getType()}', '{$new_task->getModule()}', '{$new_task->getProjectId()}', '{$new_task->getMilestoneId()}', '{$new_task->getParentId()}', '{$new_task->getParentType()}', '" . mysql_real_escape_string($new_task->getBody()) . "', '{$new_task->getState()}', '{$new_task->getVisibility()}', '{$new_task->getPriority()}', '{$new_task->getCreatedOn()}', '{$new_task->getCreatedById()}', '{$new_task->getCreatedByName()}', '{$new_task->getCreatedByEmail()}',  '{$new_task->getDueOn()}')";
                         mysql_query($query02);
                         $new_task_id = mysql_insert_id($link);
                         if ($new_task_id) {
                             $_SESSION['new_recurring_task_id'] = $new_task_id;
                             $query02 = "select * from healingcrystals_assignments where object_id='" . $this->getId() . "'";
                             $result02 = mysql_query($query02, $link);
                             if (mysql_num_rows($result02)) {
                                 $query03 = "insert into healingcrystals_assignments \n\t\t\t\t\t\t\t\t\t\t\tselect user_id, '{$new_task_id}', is_owner from healingcrystals_assignments where object_id='" . $this->getId() . "'";
                                 mysql_query($query03);
                             }
                             $query02 = "insert into healingcrystals_project_object_misc \n\t\t\t\t\t\t\t\t\t\tselect '{$new_task_id}', reminder_date, recurring_period, recurring_period_type, recurring_period_condition, recurring_end_date, now(), null, auto_email_status, email_reminder_period, email_reminder_unit, email_reminder_time, null from healingcrystals_project_object_misc where object_id='" . $this->getId() . "'";
                             mysql_query($query02);
                         }
                     }
                 }
                 $query = "select comment_id, user_id from actionrequests_to_tasklist where object_id='" . $this->getId() . "'";
                 $result = mysql_query($query);
                 if (mysql_num_rows($result)) {
                     $info = mysql_fetch_assoc($result);
                     $query = "update healingcrystals_assignments_action_request set is_action_request='-1', last_modified=now() where is_action_request='1' and user_id='" . $info['user_id'] . "' and comment_id='" . $info['comment_id'] . "'";
                     mysql_query($query);
                 }
                 //mysql_close();
             }
         }
         // if
         return $save;
     }
 }
 /**
  * Quick add checklist
  *
  * @param void
  * @return null
  */
 function quick_add()
 {
     if (!Checklist::canAdd($this->logged_user, $this->active_project)) {
         $this->httpError(HTTP_ERR_FORBIDDEN, lang("You don't have permission for this action"), true, true);
     }
     // if
     $this->skip_layout = true;
     $checklist_data = $this->request->post('checklist');
     if (!is_array($checklist_data)) {
         $checklist_data = array('visibility' => $this->active_project->getDefaultVisibility());
     }
     //if
     $this->smarty->assign(array('checklist_data' => $checklist_data, 'quick_add_url' => assemble_url('project_checklists_quick_add', array('project_id' => $this->active_project->getId()))));
     if ($this->request->isSubmitted()) {
         db_begin_work();
         $this->active_checklist = new Checklist();
         $this->active_checklist->setAttributes($checklist_data);
         $this->active_checklist->setProjectId($this->active_project->getId());
         $this->active_checklist->setCreatedBy($this->logged_user);
         $this->active_checklist->setState(STATE_VISIBLE);
         $subscribers = array($this->logged_user->getId());
         if (is_foreachable(array_var($checklist_data['assignees'], 0))) {
             $subscribers = array_merge($subscribers, array_var($checklist_data['assignees'], 0));
         } else {
             $subscribers[] = $this->active_project->getLeaderId();
         }
         // if
         Subscriptions::subscribeUsers($subscribers, $this->active_checklist);
         $this->active_checklist->ready();
         // ready
         $save = $this->active_checklist->save();
         if ($save && !is_error($save)) {
             if (isset($checklist_data['tasks']) && is_foreachable($checklist_data['tasks'])) {
                 foreach ($checklist_data['tasks'] as $task_text) {
                     $task_text = trim($task_text);
                     if ($task_text != '') {
                         $task = new Task();
                         $task->setBody($task_text);
                         $task->setPriority(PRIORITY_NORMAL);
                         $task->setProjectId($this->active_project->getId());
                         $task->setParent($this->active_checklist);
                         $task->setCreatedBy($this->logged_user);
                         $task->setState(STATE_VISIBLE);
                         $task->setVisibility(VISIBILITY_NORMAL);
                         $task->new_assignees = $checklist_data['assignees'];
                         $task->save();
                         Subscriptions::subscribeUsers($subscribers, $task);
                         $task->ready();
                     }
                     // if
                 }
                 // if
             }
             // if
             db_commit();
             $this->smarty->assign(array('active_checklist' => $this->active_checklist, 'checklist_data' => array('visibility' => $this->active_project->getDefaultVisibility()), 'project_id' => $this->active_project->getId()));
         } else {
             db_rollback();
             $this->httpError(HTTP_ERR_OPERATION_FAILED, $save->getErrorsAsString(), true, true);
         }
         // if
     }
     // if
 }
 function importPendingEmailToTaskList(&$incoming_mail, &$project, &$user, $page_id, $comment)
 {
     //$link = mysql_connect(DB_HOST, DB_USER, DB_PASS);
     //mysql_select_db(DB_NAME, $link);
     //mysql_query("insert into testing (date_added, content) values (now(), 'in task list adde func:Page " . $page_id . " / comment: " . $comment->getId() . "')");
     //mysql_query("insert into testing (date_added, content) values (now(), '" . mysql_real_escape_string($incoming_mail->getSubject()) . "')");
     //EOF:mod 20120820
     list($user_name, $priority) = explode('-', $incoming_mail->getSubject());
     $user_name = trim($user_name);
     $priority = trim($priority);
     $name_parts = explode(' ', $user_name);
     list($first_name, $last_name) = $name_parts;
     //mysql_query("insert into testing (date_added, content) values (now(), '" . mysql_real_escape_string($priority) . "')");
     //BOF:mod 20120820
     /*
     //EOF:mod 20120820
     $link = mysql_connect(DB_HOST, DB_USER, DB_PASS);
     mysql_select_db(DB_NAME, $link);
     $query = "select id from healingcrystals_users where first_name='" . mysql_real_escape_string($first_name) . "' " . (!empty($last_name) ? " and last_name='" . mysql_real_escape_string($last_name) . "' " : "");
     $result = mysql_query($query, $link);
     if (mysql_num_rows($result)){
     		$info = mysql_fetch_assoc($result);
     		$user_id = $info['id'];
     		$task_meant_for_user = Users::findById($user_id);
     		$page_title = $task_meant_for_user->getName() . ' - Task List';
     		//echo $page_title . '<br/>';
     		$query2 = "select id from healingcrystals_project_objects where type='Page' and project_id='" . TASK_LIST_PROJECT_ID . "' and name='" . mysql_real_escape_string($page_title) . "'";
     		//echo $query2 . '<br/>';
     		$result2 = mysql_query($query2,$link);
     		if (mysql_num_rows($result2)){
     			$info2 = mysql_fetch_assoc($result2);
     			$page_id = $info2['id'];
     		} else {
     			$query3 = "select id from healingcrystals_project_objects where type='Category' and module='pages' and project_id='" . TASK_LIST_PROJECT_ID . "' and name='General'";
     			//echo $query3 . '<br/>';
     			$page_category = mysql_query($query3, $link);
     			$page_category_info = mysql_fetch_assoc($page_category);
     			$category_id = $page_category_info['id'];
     			//echo $category_id . '<br/>';
     
     			$page_data = array(
     				'type' => 'Page',
     				'module' => 'pages',
     				'visibility' => VISIBILITY_NORMAL,
     				'name' => $page_title,
     				'body' => 'Auto-generated Task List Page',
     				'integer_field_1' => '1',
     			);
     			//print_r($page_data);
     			//db_begin_work();
     			$page = new Page();
     			$page->setAttributes($page_data);
     			$page->setProjectId(TASK_LIST_PROJECT_ID);
     			$page->setCreatedBy($user);
     			$page->setState(STATE_VISIBLE);
     			$page->setParentId($category_id);
     			$page->save();
     			$page->ready();
     			$page_id = $page->getId();
     		}
     }
     mysql_close($link);
     //BOF:mod 20120820
     */
     //EOF:mod 20120820
     $task = new Task();
     $task->setProjectId($project->getId());
     $task->setParentId($page_id);
     $task->setParentType('Page');
     $task->setCreatedBy($user);
     $task->setCreatedOn($incoming_mail->getCreatedOn());
     $task->setVisibility(VISIBILITY_NORMAL);
     $task->setState(STATE_VISIBLE);
     $task->setSource(OBJECT_SOURCE_EMAIL);
     if (!empty($priority)) {
         $constant_name = 'PRIORITY_' . strtoupper(str_replace(' ', '', $priority));
         $priority_val = '';
         $priority_val_set = false;
         if (defined($constant_name)) {
             switch ($constant_name) {
                 case 'PRIORITY_HOLD':
                     $priority_val = '-4';
                     $priority_val_set = true;
                     break;
                     //BOF:mod 20121107
                     /*
                     //EOF:mod 20121107
                     case 'PRIORITY_ONGOING':
                     	$priority_val = '-3';
                     	$priority_val_set = true;
                     	break;
                     //BOF:mod 20121107
                     */
                     //EOF:mod 20121107
                 //BOF:mod 20121107
                 /*
                 //EOF:mod 20121107
                 case 'PRIORITY_ONGOING':
                 	$priority_val = '-3';
                 	$priority_val_set = true;
                 	break;
                 //BOF:mod 20121107
                 */
                 //EOF:mod 20121107
                 case 'PRIORITY_LOWEST':
                     $priority_val = '-2';
                     $priority_val_set = true;
                     break;
                 case 'PRIORITY_LOW':
                     $priority_val = '-1';
                     $priority_val_set = true;
                     break;
                 case 'PRIORITY_NORMAL':
                     $priority_val = '0';
                     $priority_val_set = true;
                     break;
                 case 'PRIORITY_HIGH':
                     $priority_val = '1';
                     $priority_val_set = true;
                     break;
                 case 'PRIORITY_HIGHEST':
                     $priority_val = '2';
                     $priority_val_set = true;
                     break;
                     //BOF:mod 20121107
                 //BOF:mod 20121107
                 case 'PRIORITY_URGENT':
                     $priority_val = '3';
                     $priority_val_set = true;
                     break;
                     //EOF:mod 20121107
             }
             if ($priority_val_set) {
                 $task->setPriority($priority_val);
             }
         }
     }
     $task->setName($incoming_mail->getSubject());
     $amended_body = '';
     //BOF:mod 20120820
     if (empty($comment)) {
         if (strpos($incoming_mail->getSubject(), '{') !== false) {
             $amended_body .= substr($incoming_mail->getSubject(), 0, strrpos($incoming_mail->getSubject(), '{'));
         } else {
             $amended_body .= $incoming_mail->getBody();
         }
     } else {
         if (strpos($incoming_mail->getSubject(), '{') !== false) {
             $parent = $comment->getParent();
             $url = $parent->getViewUrl() . '#comment' . $comment->getId();
             $amended_body .= substr($incoming_mail->getSubject(), 0, strrpos($incoming_mail->getSubject(), '{')) . '<br/><a href="' . $url . '">View Task in Full</a>';
         } else {
             $temp_body = strip_tags($incoming_mail->getBody());
             $chars_len = strlen($temp_body);
             //if ($chars_len > 150){
             if ($chars_len > 525) {
                 //mysql_query("insert into testing (date_added, content) values (now(), 'greater than 150 block')");
                 //$url = $comment->getRealViewUrl();
                 $parent = $comment->getParent();
                 $url = $parent->getViewUrl() . '#comment' . $comment->getId();
                 //mysql_query("insert into testing (date_added, content) values (now(), '" . mysql_real_escape_string($url) . "')");
                 //$amended_body .= substr($temp_body, 0, 150) . '.. <br/><a href="' . $url . '">View Task in Full</a>';
                 $amended_body .= substr($temp_body, 0, 525) . '.. <br/><a href="' . $url . '">View Task in Full</a>';
             } else {
                 $amended_body .= $incoming_mail->getBody();
             }
         }
         //mysql_query("insert into testing (date_added, content) values (now(), '" . mysql_real_escape_string($amended_body) . "')");
         $attachments = $comment->getAttachments();
         if (is_foreachable($attachments)) {
             $amended_body .= '<br/>Attachments:<br/>';
             foreach ($attachments as $attachment) {
                 $amended_body .= '<a href="' . $attachment->getViewUrl() . '">' . $attachment->getName() . '</a><br/>';
             }
         }
     }
     $task->setBody($amended_body);
     //IncomingMailImporter::attachFilesToProjectObject($incoming_mail, $task);
     $save = $task->save();
     if ($save && !is_error($save)) {
         //$subscibed_users = array($project->getLeaderId());
         //if (instance_of($user, 'User')) {
         // $subscibed_users[] = $user->getId();
         //} // if
         //Subscriptions::subscribeUsers($subscibed_users, $ticket);
         $task->ready();
         //mysql_query("insert into testing (date_added, content) values (now(), 'in task list adde func end: " . $task->getId() . "')");
         return $task;
     }
     // if
     return $save;
     //mysql_close($link);
 }