/**
  * Call object function and server result to client
  * 
  * Most of the actions in this controller look the same. This simple 
  * implementation holds behavior that is same for almost all the actions. 
  * Copying is bad :)
  * 
  * $success_message and $error_message are language patters. Variables that 
  * are provided by this functions to the patterns are:
  * 
  * - name - object name
  * - type - object ype
  *
  * @param string $method
  * @param array $params
  * @param string $success_message
  * @param string $error_message
  * @return null
  */
 function executeOnActiveObject($method, $params = null, $success_message = '', $error_message = '')
 {
     if (empty($method) || empty($success_message) || empty($error_message)) {
         $this->httpError(HTTP_ERR_BAD_REQUEST);
     }
     // if
     if ($this->request->isSubmitted()) {
         db_begin_work();
         if (is_array($params)) {
             $action = call_user_func_array(array(&$this->active_object, $method), $params);
         } else {
             $action = call_user_func(array(&$this->active_object, $method));
         }
         // if
         if ($action && !is_error($action)) {
             db_commit();
             if ($this->request->getFormat() == FORMAT_HTML) {
                 if ($this->request->get('async')) {
                     $this->httpOk();
                 }
                 // if
                 flash_success($success_message, null, true);
                 $this->redirectToReferer($this->active_object->getViewUrl());
             } else {
                 $this->serveData($this->active_object, strtolower($this->active_object->getType()));
             }
             // if
         } else {
             db_rollback();
             if ($this->request->getFormat() == FORMAT_HTML) {
                 flash_error($error_message, null, true);
                 $this->redirectToReferer($this->active_object->getViewUrl());
             } else {
                 $this->httpError(HTTP_ERR_OPERATION_FAILED, null, true, $this->request->isApiCall());
             }
             // if
         }
         // if
     } else {
         $this->httpError(HTTP_ERR_BAD_REQUEST, null, true, $this->request->isApiCall());
     }
     // if
     die;
     // just in case! :)
 }
 /**
  * Show timetracking module homepage
  *
  * @param void
  * @return null
  */
 function index()
 {
     if ($this->request->isApiCall()) {
         $this->serveData(TimeRecords::findByProject($this->active_project, STATE_VISIBLE, $this->logged_user->getVisibility()), 'time_records');
     } else {
         // Content for widget popup
         if ($this->request->get('for_popup_dialog')) {
             $this->_render_popup_content();
             // Classic page
         } else {
             if (instance_of($this->active_object, 'ProjectObject')) {
                 $this->wireframe->addPageMessage(lang('Time spent on <a href=":url">:name</a> :type', array('url' => $this->active_object->getViewUrl(), 'name' => $this->active_object->getName(), 'type' => $this->active_object->getVerboseType(true))), 'info');
             }
             // if
             $timetracking_data = array('record_date' => new DateValue(time() + get_user_gmt_offset($this->logged_user)), 'user_id' => $this->logged_user->getId());
             $per_page = 20;
             $page = (int) $this->request->get('page');
             if ($page < 1) {
                 $page = 1;
             }
             // if
             if (instance_of($this->active_object, 'ProjectObject')) {
                 list($timerecords, $pagination) = TimeRecords::paginateByObject($this->active_object, $page, $per_page, STATE_VISIBLE, $this->logged_user->getVisibility());
             } else {
                 list($timerecords, $pagination) = TimeRecords::paginateByProject($this->active_project, $page, $per_page, STATE_VISIBLE, $this->logged_user->getVisibility());
             }
             // if
             // Mark this objects as read
             if (is_foreachable($timerecords)) {
                 foreach ($timerecords as $timerecord) {
                     ProjectObjectViews::log($timerecord, $this->logged_user);
                 }
                 // foreach
             }
             // if
             $this->smarty->assign(array('timetracking_data' => $timetracking_data, 'timerecords' => $timerecords, 'pagination' => $pagination, 'can_add' => TimeRecord::canAdd($this->logged_user, $this->active_project)));
             js_assign('mass_update_url', assemble_url('project_time_mass_update', array('project_id' => $this->active_project->getId())));
         }
         // if
     }
     // if
 }
 /**
  * Show and process edit task form
  *
  * @param void
  * @return null
  */
 function edit()
 {
     $this->wireframe->print_button = false;
     if ($this->active_task->isNew()) {
         $this->httpError(HTTP_ERR_NOT_FOUND, null, true, $this->request->isApiCall());
     }
     // if
     if (empty($this->active_task_parent)) {
         $this->httpError(HTTP_ERR_NOT_FOUND, null, true, $this->request->isApiCall());
     }
     // if
     if (!$this->active_task->canEdit($this->logged_user) && $this->active_task->getProjectId() != TASK_LIST_PROJECT_ID) {
         $this->httpError(HTTP_ERR_FORBIDDEN, null, true, $this->request->isApiCall());
     }
     // if
     //BOF:mod
     $this->wireframe->addPageAction(lang('Convert Task to a Ticket'), tickets_module_url($this->active_project) . '&task_id=' . $this->active_task->getId());
     //EOF:mod
     $task_data = $this->request->post('task');
     if (!is_array($task_data)) {
         $task_data = array('body' => $this->active_task->getBody(), 'priority' => $this->active_task->getPriority(), 'due_on' => $this->active_task->getDueOn(), 'assignees' => Assignments::findAssignmentDataByObject($this->active_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->active_task->getId() . "'";
         $result = mysql_query($query, $link);
         if (mysql_num_rows($result)) {
             $info = mysql_fetch_assoc($result);
             $task_data['recurring_flag'] = '1';
             $task_data['recurring_period'] = $info['recurring_period'];
             if (empty($task_data['recurring_period'])) {
                 $task_data['recurring_flag'] = '0';
             }
             $task_data['recurring_period_type'] = $info['recurring_period_type'];
             $task_data['recurring_period_condition'] = $info['recurring_period_condition'];
             $task_data['recurring_end_date'] = empty($info['recurring_end_date']) || $info['recurring_end_date'] == '0000-00-00' ? '' : dateval($info['recurring_end_date']);
             /*if (!empty($info['reminder_date']) && $info['reminder_date']!='0000-00-00 00:00:00'){
                              list($date, $time) = explode(' ', $info['reminder_date']);
                              list($h, $m, $s) = explode(':', $time);
                              //$date = str_replace('-', '/', $date);
                              $date = dateval($date);
             		$task_data['reminder'] = $date;
                          } else {*/
             $task_data['reminder'] = '';
             /*}
               $task_data['remindermeridian'] = $h>=12 ? 'PM' : 'AM';
               $task_data['reminderhours'] = $h>12 ? $h-12 : ($h!=0 ? $h : '12');
               $task_data['reminderminutes'] = $m;*/
             //BOF:mod 20120703
             $task_data['auto_email_status'] = $info['auto_email_status'];
             //EOF:mod 20120703
             if ($info['auto_email_status'] == '1') {
                 $task_data['figure_before_due_date'] = empty($info['email_reminder_period']) ? '0' : $info['email_reminder_period'];
                 $task_data['unit_before_due_date'] = empty($info['email_reminder_unit']) ? 'D' : $info['email_reminder_unit'];
                 $email_reminder_time = $info['email_reminder_time'];
                 if (!empty($email_reminder_time)) {
                     list($h, $m, ) = explode(':', $email_reminder_time);
                     $task_data['remindermeridian'] = $h >= 12 ? 'PM' : 'AM';
                     $task_data['reminderhours'] = $h > 12 ? $h - 12 : ($h != 0 ? $h : '12');
                     $task_data['reminderminutes'] = $m;
                 } else {
                     $task_data['remindermeridian'] = 'AM';
                     $task_data['reminderhours'] = '6';
                     $task_data['reminderminutes'] = '0';
                 }
             }
         } else {
             $task_data['recurring_flag'] = '0';
             $task_data['recurring_period'] = '';
             $task_data['recurring_period_type'] = 'D';
             $task_data['recurring_period_condition'] = 'after_due_date';
             $task_data['recurring_end_date'] = '';
             $task_data['reminder'] = '';
             $task_data['reminderhours'] = '6';
             $task_data['reminderminutes'] = '0';
             $task_data['remindermeridian'] = 'AM';
             //BOF:mod 20120703
             $task_data['auto_email_status'] = '';
             //EOF:mod 20120703
             $task_data['figure_before_due_date'] = '0';
             $task_data['unit_before_due_date'] = 'D';
         }
         mysql_close($link);
     }
     // if
     $this->smarty->assign('task_data', $task_data);
     if ($this->request->isSubmitted()) {
         if (!isset($task_data['assignees'])) {
             $task_data['assignees'] = array(array(), 0);
         }
         // if
         db_begin_work();
         $old_name = $this->active_task->getBody();
         $this->active_task->setAttributes($task_data);
         $save = $this->active_task->save();
         if ($save && !is_error($save)) {
             db_commit();
             //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)) {
                 $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 = '';
                 }
                 $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 " . " 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 . "',\n                                    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\temail_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');
             } else {
                 //flash_success('Task ":name" has been updated', array('name' => str_excerpt($old_name, 80, '...')), false, false);
                 //bof:mod
                 flash_success('Task ":name" has been updated', array('name' => str_excerpt(strip_tags($old_name), 80, '...')), false, false);
                 //eof:mod
                 //BOF:mod 20120904
                 /*
                 //EOF:mod 20120904
                             $this->redirectToUrl($this->active_task_parent->getViewUrl());
                 //BOF:mod 20120904
                 */
                 $this->redirectToUrl($this->active_task_parent->getViewUrl() . '#task' . $this->active_task->getId());
                 //EOF:mod 20120904
             }
             // if
         } else {
             db_rollback();
             if ($this->request->isApiCall()) {
                 $this->serveData($save);
             } else {
                 $this->smarty->assign('errors', $save);
             }
             // if
         }
         // if
     } else {
         if ($this->request->isApiCall()) {
             $this->httpError(HTTP_ERR_BAD_REQUEST, null, true, true);
         }
         // if
     }
     // if
 }