/**
  * Constructor
  *
  * @param Request $request
  * @return ProjectObjectsController
  */
 function __construct($request)
 {
     parent::__construct($request);
     $object_id = (int) $this->request->getId('object_id');
     if ($object_id) {
         $this->active_object = ProjectObjects::findById($object_id);
     }
     // if
     if (instance_of($this->active_object, 'ProjectObject')) {
         $this->active_object->prepareProjectSectionBreadcrumb($this->wireframe);
         $this->wireframe->addBreadCrumb($this->active_object->getName(), $this->active_object->getViewUrl());
     } else {
         $this->httpError(HTTP_ERR_NOT_FOUND);
     }
     // if
     $this->smarty->assign(array('active_object' => $this->active_object, 'page_tab' => $this->active_object->getProjectTab()));
 }
 /**
  * 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
 }