/**
  * Quick add milestone
  *
  * @param void
  * @return null
  */
 function quick_add()
 {
     if (!Milestone::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;
     $milestone_data = $this->request->post('milestone');
     if (!is_array($milestone_data)) {
         $milestone_data = array('visibility' => $this->active_project->getDefaultVisibility());
     }
     // if
     $this->smarty->assign(array('milestone_data' => $milestone_data, 'quick_add_url' => assemble_url('project_milestones_quick_add', array('project_id' => $this->active_project->getId()))));
     if ($this->request->isSubmitted()) {
         db_begin_work();
         $this->active_milestone = new Milestone();
         $this->active_milestone->setAttributes($milestone_data);
         if (!isset($milestone_data['priority'])) {
             $this->active_milestone->setPriority(PRIORITY_NORMAL);
         }
         // if
         $this->active_milestone->setProjectId($this->active_project->getId());
         $this->active_milestone->setCreatedBy($this->logged_user);
         $this->active_milestone->setState(STATE_VISIBLE);
         $this->active_milestone->setVisibility(VISIBILITY_NORMAL);
         $save = $this->active_milestone->save();
         if ($save && !is_error($save)) {
             $subscribers = array($this->logged_user->getId());
             if (is_foreachable(array_var($milestone_data['assignees'], 0))) {
                 $subscribers = array_merge($subscribers, array_var($milestone_data['assignees'], 0));
             } else {
                 $subscribers[] = $this->active_project->getLeaderId();
             }
             // if
             Subscriptions::subscribeUsers($subscribers, $this->active_milestone);
             db_commit();
             $this->active_milestone->ready();
             $this->smarty->assign(array('active_milestone' => $this->active_milestone, 'milestone_data' => array('visibility' => $this->active_project->getDefaultVisibility()), 'project_id' => $this->active_project->getId()));
             $this->skip_layout = true;
         } else {
             db_rollback();
             $this->httpError(HTTP_ERR_OPERATION_FAILED, $save->getErrorsAsString(), true, true);
         }
         // if
     }
     // if
 }
 /**
  * Upate discussion
  *
  * @param void
  * @return null
  */
 function edit()
 {
     $this->wireframe->print_button = false;
     if ($this->request->isApiCall() && !$this->request->isSubmitted()) {
         $this->httpError(HTTP_ERR_BAD_REQUEST);
     }
     // ifs
     if ($this->active_discussion->isNew()) {
         $this->httpError(HTTP_ERR_NOT_FOUND);
     }
     // if
     if (!$this->active_discussion->canEdit($this->logged_user)) {
         $this->httpError(HTTP_ERR_FORBIDDEN);
     }
     // if
     //BOF:mod 20110615
     $subscribers = $this->active_discussion->getSubscribers();
     $notify_users = array(array(), null);
     foreach ($subscribers as $subscriber) {
         $notify_users[0][] = $subscriber->getId();
     }
     $this->smarty->assign('notify_users', $notify_users);
     //EOF:mod 20110615
     $discussion_data = $this->request->post('discussion');
     if (!is_array($discussion_data)) {
         $discussion_data = array('name' => $this->active_discussion->getName(), 'body' => $this->active_discussion->getBody(), 'parent_id' => $this->active_discussion->getParentId(), 'milestone_id' => $this->active_discussion->getMilestoneId(), 'visibility' => $this->active_discussion->getVisibility(), 'tags' => $this->active_discussion->getTags());
     }
     // if
     $this->smarty->assign('discussion_data', $discussion_data);
     if ($this->request->isSubmitted()) {
         db_begin_work();
         $old_name = $this->active_discussion->getName();
         $this->active_discussion->setAttributes($discussion_data);
         $save = $this->active_discussion->save();
         if ($save && !is_error($save)) {
             db_commit();
             //BOF: mod
             $this->active_discussion->register_departments(!empty($discussion_data['departments']) ? $discussion_data['departments'] : array());
             //EOF: mod
             //BOF:mod 20110614
             $subscribers = $this->request->post('notify_users');
             if (!in_array($this->active_project->getLeaderId(), $subscribers)) {
                 $subscribers[] = $this->active_project->getLeaderId();
             }
             // if
             Subscriptions::subscribeUsers($subscribers, $this->active_discussion);
             $assignees_flag_data = $this->request->post('assignee');
             $this->active_discussion->register_assignees_flag($assignees_flag_data);
             //EOF:mod 20110614
             if ($this->request->getFormat() == FORMAT_HTML) {
                 flash_success('Discussion ":name" has been updated', array('name' => $old_name));
                 $this->redirectToUrl($this->active_discussion->getViewUrl());
             } else {
                 $this->serveData($this->active_discussion, 'discussion');
             }
             // if
         } else {
             db_rollback();
             if ($this->request->getFormat() == FORMAT_HTML) {
                 $this->smarty->assign('errors', $save);
             } else {
                 $this->serveData($save);
             }
             // if
         }
         // if
     }
     // if
 }
 /**
  * Upload single file
  *
  * @param void
  * @return null
  */
 function upload_single()
 {
     if ($this->request->isSubmitted()) {
         if (!File::canAdd($this->logged_user, $this->active_project)) {
             if ($this->request->isApiCall()) {
                 $this->httpError(HTTP_ERR_FORBIDDEN, null, true, true);
             } else {
                 die('error - upload not permitted');
             }
             // if
         }
         // if
         $file_data = $this->request->post('file');
         if (!is_array($file_data)) {
             $file_data = array('milestone_id' => $this->request->get('milestone_id'), 'visibility' => $this->active_project->getDefaultVisibility());
             if (instance_of($this->active_category, 'Category')) {
                 $file_data['parent_id'] = $this->active_category->getId();
             }
             // if
         }
         // if
         $this->smarty->assign('file_data', $file_data);
         if ($this->request->isSubmitted()) {
             db_begin_work();
             $this->active_file = new File();
             $attached = attach_from_files($this->active_file, $this->logged_user);
             // Do we have an upload error?
             if (is_error($attached) || $attached != 1) {
                 if ($this->request->isApiCall()) {
                     $this->serveData(is_error($attached) ? $attached : new Error('0 files uploaded'));
                 } else {
                     die('error - nothing uploaded');
                 }
                 // if
             }
             // if
             $this->active_file->setAttributes($file_data);
             if ($this->active_file->getName() == '') {
                 $this->active_file->setName($this->active_file->pending_files[0]['name']);
             }
             // if
             $this->active_file->setRevision(1);
             $this->active_file->setProjectId($this->active_project->getId());
             if (trim($this->active_file->getCreatedByName()) == '' || trim($this->active_file->getCreatedByEmail()) == '') {
                 $this->active_file->setCreatedBy($this->logged_user);
             }
             // if
             $this->active_file->setState(STATE_VISIBLE);
             $save = $this->active_file->save();
             if ($save && !is_error($save)) {
                 if ($this->active_file->countRevisions() > 0) {
                     $subscribers = array($this->logged_user->getId());
                     if (is_foreachable($this->request->post('notify_users'))) {
                         $subscribers = array_merge($subscribers, $this->request->post('notify_users'));
                     } 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_file);
                     db_commit();
                     $this->active_file->ready();
                     if ($this->request->isApiCall()) {
                         $this->serveData($this->active_file, 'file');
                     } else {
                         die('success');
                         // async
                     }
                     // if
                 } else {
                     if ($this->request->isApiCall()) {
                         $this->httpError(HTTP_ERR_OPERATION_FAILED, null, true, true);
                     } else {
                         die('error - unable to attach file');
                     }
                     // if
                 }
                 // if
             } else {
                 if ($this->request->isApiCall()) {
                     $this->serveData($save);
                 } else {
                     die('error - could not save file object');
                     // async
                 }
                 // if
             }
             // if
         }
         // if
     } else {
         if ($this->request->isApiCall()) {
             $this->httpError(HTTP_ERR_BAD_REQUEST, null, true, true);
         } else {
             die('error - request is not POST request');
             // async
         }
         // if
     }
     // if
 }
 /**
  * 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
 }
 /**
  * Show and process edit page form. Also, handle all other page update 
  * requests
  *
  * @param void
  * @return null
  */
 function edit()
 {
     $this->wireframe->print_button = false;
     if ($this->active_page->isNew()) {
         $this->httpError(HTTP_ERR_NOT_FOUND);
     }
     // if
     if (!$this->active_page->canEdit($this->logged_user)) {
         $this->httpError(HTTP_ERR_FORBIDDEN);
     }
     // if
     //BOF:mod 20110615
     $subscribers = $this->active_page->getSubscribers();
     $notify_users = array(array(), null);
     foreach ($subscribers as $subscriber) {
         $notify_users[0][] = $subscriber->getId();
     }
     $this->smarty->assign('notify_users', $notify_users);
     //EOF:mod 20110615
     $page_data = $this->request->post('page');
     if (!is_array($page_data)) {
         $page_data = array('name' => $this->active_page->getName(), 'body' => $this->active_page->getBody(), 'visibility' => $this->active_page->getVisibility(), 'parent_id' => $this->active_page->getParentId(), 'milestone_id' => $this->active_page->getMilestoneId(), 'tags' => $this->active_page->getTags());
     }
     // if
     //BOF:mod 20110519
     //$subscribers = $this->active_page->getSubscribers();
     //$page_data['subscribers'] = $subscribers;
     //EOF:mod 20110519
     //BOF:mod 20121116
     $options = array();
     $options[] = array('url' => 'javascript:convert_object_to_milestone(\'' . $this->active_page->getProjectId() . '\', \'' . $this->active_page->getId() . '\', \'' . $this->active_page->getType() . '\');', 'text' => 'Milestone');
     $options[] = array('url' => 'javascript:convert_object_to_ticket(\'' . $this->active_page->getProjectId() . '\', \'' . $this->active_page->getId() . '\', \'' . $this->active_page->getType() . '\');', 'text' => 'Ticket');
     $this->wireframe->addPageAction(lang('Convert To'), 'javascript://', $options);
     //EOF:mod 20121116
     $this->smarty->assign(array('page_data' => $page_data));
     if ($this->request->isSubmitted()) {
         db_begin_work();
         $old_page_name = $this->active_page->getName();
         $old_page_body = $this->active_page->getBody();
         $this->active_page->setAttributes($page_data);
         $new_version = null;
         $error = null;
         // Create a new version
         if (!array_var($page_data, 'is_minor_revision', false) && ($this->active_page->getName() != $old_page_name || $this->active_page->getBody() != $old_page_body)) {
             $new_version = $this->active_page->createVersion($this->logged_user);
             if (is_error($new_version)) {
                 $error = $new_version;
             }
             // if
         }
         // if
         // Update page properties if we don't have an error already
         if (!is_error($error)) {
             //BOF:mod 20121122
             if (!empty($page_data['new_team_id']) && $page_data['new_team_id'] != $this->active_project->getId()) {
                 $this->active_page->setProjectId($page_data['new_team_id']);
             }
             //EOF:mod 20121122
             $save = $this->active_page->save();
             if (is_error($save)) {
                 $error = $save;
             }
             // if
         }
         // if
         if (!is_error($error)) {
             if ($new_version) {
                 //BOF:mod 20010519
                 /*
                 //EOF:mod 20010519
                             event_trigger('on_new_revision', array(&$this->active_page, &$new_version, &$this->logged_user));
                 //BOF:mod 20010519
                 */
                 /*if ($page_data['subscribers_to_notify']){
                 			$link = mysql_connect(DB_HOST, DB_USER, DB_PASS);
                 			mysql_select_db(DB_NAME);
                 			foreach($page_data['subscribers_to_notify'] as $fyi_user_id){
                 				$query = "insert into healingcrystals_project_objects_to_fyi_users (fyi_user_id, selected_by_user_id, object_id) values ('" . $fyi_user_id . "', '" . $this->logged_user->getId() . "', '" . $this->active_page->getId() . "')";
                 				mysql_query($query);
                 			}				
                 			mysql_close($link);
                 		}*/
                 //EOF:mod 20010519
                 $activity_log = new NewPageVersionActivityLog();
                 $activity_log->log($this->active_page, $this->logged_user);
             }
             // if
             //BOF:mod 20110614
             $subscribers = $this->request->post('notify_users');
             if (!in_array($this->active_project->getLeaderId(), $subscribers)) {
                 $subscribers[] = $this->active_project->getLeaderId();
             }
             // if
             Subscriptions::subscribeUsers($subscribers, $this->active_page);
             //EOF:mod 20110614
             //Subscriptions::subscribe($this->logged_user, $this->active_page);
             db_commit();
             //BOF: mod
             $this->active_page->register_departments(!empty($page_data['departments']) ? $page_data['departments'] : array());
             //EOF: mod
             //BOF:mod 20110614
             $assignees_flag_data = $this->request->post('assignee');
             $this->active_page->register_assignees_flag($assignees_flag_data);
             //EOF:mod 20110614
             if ($this->request->getFormat() == FORMAT_HTML) {
                 flash_success('Page ":name" has been updated', array('name' => $old_page_name));
                 $this->redirectToUrl($this->active_page->getViewUrl());
             } else {
                 $this->serveData($this->active_page, 'page');
             }
             // if
         } else {
             db_rollback();
             if ($this->request->getFormat() == FORMAT_HTML) {
                 $this->smarty->assign('errors', $error);
             } else {
                 $this->serveData($error);
             }
             // if
         }
         // if
     }
     // if
 }
 /**
  * Save this object into the database
  *
  * @param void
  * @return boolean
  * @throws DBQueryError
  * @throws ValidationErrors
  */
 function save()
 {
     $is_new = $this->isNew();
     $modified_fields = $this->modified_fields;
     $old_values = $this->old_values;
     if ($is_new) {
         $this->setType(get_class($this));
     }
     // if
     if ($this->isModified()) {
         $this->setVersion($this->getVersion() + 1);
         // increment object version on save...
     }
     // if
     db_begin_work();
     $save = parent::save();
     if (!$save || is_error($save)) {
         db_rollback();
         return $save;
     }
     // if
     // Log activities...
     if ($this->log_activities) {
         if ($is_new) {
             if ($this->log_creation) {
                 if (instance_of($this, 'File')) {
                     $activity_log = new NewFileActivityLog();
                 } else {
                     $activity_log = new ObjectCreatedActivityLog();
                 }
                 // if
                 $activity_log->log($this, $this->getCreatedBy());
             }
             // if
         } else {
             if ($this->log_update || $this->log_move_to_trash || $this->log_restore_from_trash) {
                 $trashed = false;
                 $restored = false;
                 if (is_array($this->modified_fields) && in_array('state', $modified_fields)) {
                     if (isset($old_values['state']) && $old_values['state'] == STATE_DELETED && $this->getState() == STATE_VISIBLE) {
                         $restored = true;
                     }
                     // if
                     if (isset($old_values['state']) && $old_values['state'] == STATE_VISIBLE && $this->getState() == STATE_DELETED) {
                         $trashed = true;
                     }
                     // if
                 }
                 // if
                 if ($trashed) {
                     if ($this->log_move_to_trash) {
                         $activity_log = new ObjectTrashedActivityLog();
                         $activity_log->log($this);
                     }
                     // if
                 } elseif ($restored) {
                     if ($this->log_restore_from_trash) {
                         $activity_log = new ObjectRestoredActivityLog();
                         $activity_log->log($this);
                     }
                     // if
                 } else {
                     if ($this->log_update) {
                         $activity_log = new ObjectUpdatedActivityLog();
                         $activity_log->log($this);
                     }
                     // if
                 }
                 // if
             }
             // if
         }
         // if
     }
     // if
     // Pending files
     if ($this->can_have_attachments && is_foreachable($this->pending_files)) {
         foreach ($this->pending_files as $pending_file) {
             $attachment = new Attachment();
             $attachment->setParent($this);
             if (isset($pending_file['created_by']) && (instance_of($pending_file['created_by'], 'User') || instance_of($pending_file['created_by'], 'AnonymousUser'))) {
                 $attachment->setCreatedBy($pending_file['created_by']);
             } else {
                 $attachment->setCreatedBy($this->getCreatedBy());
             }
             // if
             $attachment->setName($pending_file['name']);
             $attachment->setLocation(substr($pending_file['location'], strlen(UPLOAD_PATH) + 1));
             $attachment->setMimeType($pending_file['type']);
             $attachment->setSize($pending_file['size']);
             if (instance_of($this, 'File')) {
                 $attachment->setAttachmentType(ATTACHMENT_TYPE_FILE_REVISION);
             }
             // if
             $save_attachment = $attachment->save();
             if (is_error($save_attachment)) {
                 db_rollback();
                 return $save_attachment;
             }
             // if
         }
         // foreach
         $this->pending_files = array();
         // no more pending files
     }
     // if
     // Set assignees
     if ($this->can_have_assignees && $this->new_assignees !== false) {
         $this->old_assignees = $is_new ? array(array(), 0) : Assignments::findAssignmentDataByObject($this);
         //BOF: mod
         list($old_subscribers, ) = $this->old_assignees;
         if (count($old_subscribers)) {
             foreach ($old_subscribers as $old_subscriber_id) {
                 Subscriptions::unsubscribe(new User($old_subscriber_id), $this);
             }
         }
         //EOF: mod
         Assignments::deleteByObject($this);
         $object_id = $this->getId();
         if (is_array($this->new_assignees)) {
             list($assignees, $owner_id) = $this->new_assignees;
             if (is_foreachable($assignees)) {
                 $user_ids = array();
                 $to_insert = array();
                 foreach ($assignees as $user_id) {
                     if (in_array($user_id, $user_ids)) {
                         continue;
                     }
                     // if
                     $is_owner = $user_id == $owner_id ? 1 : 0;
                     $to_insert[] = "({$user_id}, {$object_id}, {$is_owner})";
                     $user_ids[] = $user_id;
                 }
                 // foreach
                 // Insert assignments
                 $insert = db_execute('INSERT INTO ' . TABLE_PREFIX . 'assignments VALUES ' . implode(', ', $to_insert));
                 if (is_error($insert) && !$insert) {
                     db_rollback();
                     return $insert;
                 }
                 // if
                 // Not array... Empty...
             } else {
                 $assignees = array();
                 $owner_id = 0;
             }
             // if
             // Clean up assignments cache
             clean_assignments_cache();
             // Make sure that all assignees are subscribed
             Subscriptions::subscribeUsers($assignees, $this, false);
             // Check if object is reassigned
             if (!$is_new) {
                 $reassigned = false;
                 if (is_array($this->old_assignees)) {
                     list($old_assignees, $old_owner_id) = $this->old_assignees;
                 } else {
                     $old_assignees = array();
                     $old_owner_id = 0;
                 }
                 // if
                 if ($owner_id != $old_owner_id) {
                     $reassigned = true;
                 } else {
                     if (count($assignees) != count($old_assignees)) {
                         $reassigned = true;
                     } else {
                         foreach ($assignees as $assignee_id) {
                             if (!in_array($assignee_id, $old_assignees)) {
                                 $reassigned = true;
                             }
                             // if
                         }
                         // foerach
                     }
                     // if
                 }
                 // if
                 if ($reassigned) {
                     event_trigger('on_project_object_reassigned', array(&$this, $this->old_assignees, array($assignees, $owner_id)));
                 } else {
                     $this->old_assignees = false;
                 }
                 // if
             }
             // if
         }
         // if
         $this->new_assignees = false;
         // reset
     }
     // if
     // Search index
     if (is_foreachable($this->searchable_fields)) {
         $update_search_index = false;
         // Do we need to update search index?
         foreach ($this->searchable_fields as $field) {
             if (in_array($field, $modified_fields)) {
                 $update_search_index = true;
                 break;
             }
             // if
         }
         // foreach
         // We do... Prepare and if content is empty remove it from the index
         if ($update_search_index) {
             $content = '';
             foreach ($this->searchable_fields as $field) {
                 $value = $this->getFieldValue($field);
                 if ($value) {
                     $content .= $value . "\n\n";
                 }
                 // if
             }
             // foreach
             if ($content) {
                 search_index_set($this->getId(), 'ProjectObject', $content);
             } else {
                 search_index_remove($this->getId(), 'ProjectObject');
             }
             // if
         }
         // if
     }
     // if
     // Update properties of child elements
     if (!$is_new) {
         $properties = array();
         if (in_array('visibility', $modified_fields)) {
             $properties['setVisibility'] = $this->getVisibility();
         }
         // if
         if (in_array('milestone_id', $modified_fields)) {
             $properties['setMilestoneId'] = $this->getMilestoneId();
         }
         // if
         $types = array();
         if ($this->can_have_comments) {
             $types[] = 'Comment';
         }
         // if
         if ($this->can_have_tasks) {
             $types[] = 'Task';
         }
         // if
         if ($this->getHasTime()) {
             $types[] = 'TimeRecord';
         }
         // if
         ProjectObjects::updatePropertiesByParent($this, $properties, $types);
     }
     // if
     if (!$is_new && in_array('project_id', $modified_fields)) {
         ActivityLogs::updateProjectIdCache($this);
     }
     // if
     // Commit and done!
     db_commit();
     return true;
 }
 /**
  * Index page action
  *
  */
 function index()
 {
     if (!module_loaded('tickets')) {
         $this->redirectTo('public_submit_unavailable');
     }
     // if
     $ticket_data = $this->request->post('ticket');
     $this->smarty->assign(array('captcha_url' => ROOT_URL . '/captcha.php?id=' . md5(time()), "ticket_data" => $ticket_data));
     if ($this->request->isSubmitted()) {
         $errors = new ValidationErrors();
         if ($this->captcha_enabled) {
             //$captcha_value = array_var($_SESSION, CAPTCHA_SESSION_ID);
             if (!Captcha::Validate($ticket_data['captcha'])) {
                 $errors->addError(lang('Code you entered is not valid'), 'captcha');
                 $this->smarty->assign('errors', $errors);
             }
             // if
         }
         // if
         if (!$errors->hasErrors()) {
             $submitter = new AnonymousUser($ticket_data['created_by_name'], $ticket_data['created_by_email']);
             db_begin_work();
             $ticket = new Ticket();
             attach_from_files($ticket, $submitter);
             $ticket->setAttributes($ticket_data);
             $ticket->setProjectId($this->active_project->getId());
             $ticket->setVisibility(VISIBILITY_NORMAL);
             $ticket->setState(STATE_VISIBLE);
             $ticket->setCreatedBy($submitter);
             $save = $ticket->save();
             if (!$save || is_error($save)) {
                 unset($ticket_data['captcha']);
                 db_rollback();
                 $this->smarty->assign(array('ticket_data' => $ticket_data, 'errors' => $save));
             } else {
                 Subscriptions::subscribeUsers(array($this->active_project->getLeaderId()), $ticket);
                 db_commit();
                 $ticket->ready();
                 $this->redirectTo('public_submit_success');
             }
             // if
         }
         // if
     }
     // if
 }
 /**
  * Import pending email as discussion
  *
  * @param IncomingMail $incoming_mail
  * @param Project $project
  * @param User $user
  * @return Discussion
  */
 function importPendingEmailAsDiscussion(&$incoming_mail, &$project, &$user)
 {
     $discussion = new Discussion();
     $discussion->setProjectId($project->getId());
     $discussion->setCreatedBy($user);
     $discussion->setCreatedOn($incoming_mail->getCreatedOn());
     $discussion->setVisibility(VISIBILITY_NORMAL);
     $discussion->setState(STATE_VISIBLE);
     $discussion->setSource(OBJECT_SOURCE_EMAIL);
     $discussion->setName($incoming_mail->getSubject());
     $discussion->setBody($incoming_mail->getBody());
     IncomingMailImporter::attachFilesToProjectObject($incoming_mail, $discussion);
     $save = $discussion->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, $discussion);
         $discussion->ready();
         return $discussion;
     }
     // if
     return $save;
 }