/**
  * Show and process edit time form
  *
  * @access public
  * @param void
  * @return null
  */
 function edit()
 {
     $this->setTemplate('add_time');
     $time = ProjectTimes::findById(get_id());
     if (!$time instanceof ProjectTime) {
         flash_error(lang('time dnx'));
         $this->redirectTo('time', 'index');
     }
     // if
     if (!$time->canEdit(logged_user())) {
         flash_error(lang('no access permissions'));
         $this->redirectToReferer(get_url('time'));
     }
     $time_data = array_var($_POST, 'time');
     if (!is_array($time_data)) {
         $time_data = array('name' => $time->getName(), 'hours' => $time->getHours(), 'is_billable' => $time->getBillable(), 'done_date' => $time->getDoneDate(), 'description' => $time->getDescription(), 'assigned_to' => $time->getAssignedToCompanyId() . ':' . $time->getAssignedToUserId(), 'is_private' => $time->isPrivate(), 'task_id' => $time->getTaskId(), 'task_list_id' => $time->getTaskListId());
         // array
     }
     // if
     tpl_assign('time_data', $time_data);
     tpl_assign('time', $time);
     // Attiks - BEGIN
     tpl_assign('open_task_lists', active_project()->getOpenTaskLists());
     // Attiks - END
     if (is_array(array_var($_POST, 'time'))) {
         $old_owner = $time->getAssignedTo();
         // remember the old owner
         if (isset($_POST['time_done_date'])) {
             $time_data['done_date'] = DateTimeValueLib::makeFromString($_POST['time_done_date']);
         } else {
             $time_data['done_date'] = DateTimeValueLib::make(0, 0, 0, array_var($_POST, 'time_done_date_month', 1), array_var($_POST, 'time_done_date_day', 1), array_var($_POST, 'time_done_date_year', 1970));
         }
         // Attiks - BEGIN
         if (isset($time_data['task_id'])) {
             if (substr($time_data['task_id'], 0, 5) == 'task_') {
                 $time_data['task_id'] = substr($time_data['task_id'], 5);
                 $t = ProjectTasks::findById($time_data['task_id']);
                 if (!$t instanceof ProjectTask) {
                     flash_error(lang('task dnx'));
                     $this->redirectTo('task');
                 }
                 // if
                 $time_data['task_list_id'] = $t->getTaskListId();
             } else {
                 $time_data['task_list_id'] = $time_data['task_id'];
                 $time_data['task_id'] = null;
             }
         }
         // Attiks - END
         $assigned_to = explode(':', array_var($time_data, 'assigned_to', ''));
         $old_is_private = $time->isPrivate();
         $time->setFromAttributes($time_data);
         if (!logged_user()->isMemberOfOwnerCompany()) {
             $time->setIsPrivate($old_is_private);
         }
         $time->setProjectId(active_project()->getId());
         $time->setAssignedToCompanyId(array_var($assigned_to, 0, 0));
         $time->setAssignedToUserId(array_var($assigned_to, 1, 0));
         try {
             DB::beginWork();
             $time->save();
             ApplicationLogs::createLog($time, active_project(), ApplicationLogs::ACTION_EDIT);
             DB::commit();
             flash_success(lang('success edit time', $time->getName()));
             $this->redirectTo('time');
         } catch (Exception $e) {
             DB::rollback();
             tpl_assign('error', $e);
         }
         // try
     }
     // if
 }
 /**
  * This function will return true if this datetime is yesterday
  *
  * @param void
  * @return boolean
  */
 function isYesterday()
 {
     $yesterday = DateTimeValueLib::makeFromString('yesterday');
     return $this->getDay() == $yesterday->getDay() && $this->getMonth() == $yesterday->getMonth() && $this->getYear() == $yesterday->getYear();
 }
 function deleteMailsFromServer(MailAccount $account)
 {
     $count = 0;
     if ($account->getDelFromServer() > 0) {
         $max_date = DateTimeValueLib::now();
         $max_date->add('d', -1 * $account->getDelFromServer());
         if ($account->getIsImap()) {
             if ($account->getIncomingSsl()) {
                 $imap = new Net_IMAP($ret, "ssl://" . $account->getServer(), $account->getIncomingSslPort());
             } else {
                 $imap = new Net_IMAP($ret, "tcp://" . $account->getServer());
             }
             if (PEAR::isError($ret)) {
                 Logger::log($ret->getMessage());
                 throw new Exception($ret->getMessage());
             }
             $ret = $imap->login($account->getEmail(), self::ENCRYPT_DECRYPT($account->getPassword()));
             $result = array();
             if ($ret === true) {
                 $mailboxes = MailAccountImapFolders::getMailAccountImapFolders($account->getId());
                 if (is_array($mailboxes)) {
                     foreach ($mailboxes as $box) {
                         if ($box->getCheckFolder()) {
                             $numMessages = $imap->getNumberOfMessages(utf8_decode($box->getFolderName()));
                             for ($i = 1; $i <= $numMessages; $i++) {
                                 $summary = $imap->getSummary($i);
                                 if (is_array($summary)) {
                                     $m_date = DateTimeValueLib::makeFromString($summary[0]['INTERNALDATE']);
                                     if ($m_date instanceof DateTimeValue && $max_date->getTimestamp() > $m_date->getTimestamp()) {
                                         if (MailContents::mailRecordExists($account->getId(), $summary[0]['UID'], $box->getFolderName(), null)) {
                                             $imap->deleteMessages($i);
                                             $count++;
                                         }
                                     } else {
                                         break;
                                     }
                                 }
                             }
                             $imap->expunge();
                         }
                     }
                 }
             }
         } else {
             //require_once "Net/POP3.php";
             $pop3 = new Net_POP3();
             // Connect to mail server
             if ($account->getIncomingSsl()) {
                 $pop3->connect("ssl://" . $account->getServer(), $account->getIncomingSslPort());
             } else {
                 $pop3->connect($account->getServer());
             }
             if (PEAR::isError($ret = $pop3->login($account->getEmail(), self::ENCRYPT_DECRYPT($account->getPassword()), 'USER'))) {
                 throw new Exception($ret->getMessage());
             }
             $emails = $pop3->getListing();
             foreach ($emails as $email) {
                 if (MailContents::mailRecordExists($account->getId(), $email['uidl'], null, null)) {
                     $headers = $pop3->getParsedHeaders($email['msg_id']);
                     $date = DateTimeValueLib::makeFromString(array_var($headers, 'Date'));
                     if ($date instanceof DateTimeValue && $max_date->getTimestamp() > $date->getTimestamp()) {
                         $pop3->deleteMsg($email['msg_id']);
                         $count++;
                     }
                 }
             }
             $pop3->disconnect();
         }
     }
     return $count;
 }
 /**
  * Read the values from XML node
  *
  * @param SimpleXMLElement $node
  * @return boolean
  */
 function read(SimpleXMLElement $node)
 {
     $this->setVersionNumber($node['number']);
     $this->setIsStable(isset($node['stable']) && $node['stable'] == 'stable');
     $this->setDetailsUrl($node->details_url);
     $this->setSignature($node->signature);
     $this->setReleaseNotes($node->release_notes);
     $this->setReleaseDate(DateTimeValueLib::makeFromString((string) $node->release_date));
     $this->setChangeLogUrl($node->change_log);
     if ($node->download_links->children() instanceof SimpleXMLElement) {
         $download_links = array();
         foreach ($node->download_links->children() as $link_node) {
             $download_link = new VersionsFeedDownloadLink($link_node);
             if ($download_link->isLoaded()) {
                 $download_links[] = $download_link;
             }
         }
         // foreach
         $this->download_links = count($download_links) ? $download_links : null;
     }
     // if
     return true;
 }
 /**
  * Convert raw value from database to PHP value
  *
  * @access public
  * @param mixed $value
  * @param string $type
  * @return mixed
  */
 function rawToPHP($value, $type = DATA_TYPE_STRING)
 {
     // NULL!
     if (is_null($value)) {
         return null;
     }
     // Switch type...
     switch ($type) {
         // String
         case DATA_TYPE_STRING:
             return strval($value);
             // Integer
         // Integer
         case DATA_TYPE_INTEGER:
             return intval($value);
             // Float
         // Float
         case DATA_TYPE_FLOAT:
             return floatval($value);
             // Boolean
         // Boolean
         case DATA_TYPE_BOOLEAN:
             return (bool) $value;
             // Date and time
         // Date and time
         case DATA_TYPE_DATETIME:
         case DATA_TYPE_DATE:
         case DATA_TYPE_TIME:
             if ($value instanceof DateTimeValue) {
                 return $value;
             } else {
                 if ($type == DATA_TYPE_DATETIME && ($value == EMPTY_DATETIME || $value == EMPTY_DATE) || $type == DATA_TYPE_DATE && $value == EMPTY_DATE || $type == DATA_TYPE_TIME && $value == EMPTY_TIME) {
                     return null;
                 }
                 return DateTimeValueLib::makeFromString($value);
             }
             // if
     }
     // switch
 }
 /**
 * Convert raw value to php
 *
 * @param string $value
 * @return mixed
 */
 protected function rawToPhp($value) {
   $from_value = trim($value) ? $value : EMPTY_DATETIME;
   return DateTimeValueLib::makeFromString($from_value);
 } // rawToPhp
 private function getDateGroupsConditions($date_field)
 {
     $unknown_group_name = $date_field;
     $date_field = "`" . $date_field . "`";
     $date_groups = array();
     $current = DateTimeValueLib::now();
     $current->advance(logged_user()->getTimezone() * 3600);
     //Relative dates starts
     $relative = array();
     $relative['first_day_of_year'] = strToTime('first day of January this year', $current->getTimestamp());
     $relative['first_day_of_last_month'] = strToTime('first day of previous month', $current->getTimestamp());
     $relative['first_day_of_this_month'] = strToTime('first day of this month', $current->getTimestamp());
     $relative['first_day_of_last_week'] = strToTime('-1 days monday last week', $current->getTimestamp());
     $relative['first_day_of_this_week'] = strToTime('-1 days monday this week', $current->getTimestamp());
     $relative['yesterday'] = strToTime('yesterday', $current->getTimestamp());
     $relative['today'] = strToTime('today', $current->getTimestamp());
     $relative['tomorrow'] = strToTime('tomorrow', $current->getTimestamp());
     $relative['last_day_of_this_week'] = strToTime('saturday this week', $current->getTimestamp());
     $relative['first_day_of_next_week'] = strToTime('-1 days monday next week', $current->getTimestamp());
     $relative['last_day_of_next_week'] = strToTime('saturday next week', $current->getTimestamp());
     $relative['last_day_of_this_month'] = strToTime('last day of this month', $current->getTimestamp());
     $relative['last_day_of_next_month'] = strToTime('last day of next month', $current->getTimestamp());
     $relative['last_day_of_next_3_months'] = strToTime('last day of +3 month', $current->getTimestamp());
     $relative['last_day_of_this_year'] = strToTime('last day of december this year', $current->getTimestamp());
     foreach ($relative as $key => &$value) {
         $value = DateTimeValueLib::makeFromString(date(DATE_MYSQL, $value));
         $value->beginningOfDay();
     }
     //Relative dates ends
     $relative_ends = array();
     foreach ($relative as $key => &$value) {
         $new_value = clone $value;
         $relative_ends[$key] = $new_value->endOfDay();
     }
     //if use_time_in_task_dates today condition is `due_date` >= '2015-03-04 02:00:00' AND `due_date` <= '2015-03-05 01:59:59' in GMT - 2
     //else today condition is `due_date` >= '2015-03-04 00:00:00' AND `due_date` <= '2015-03-04 23:59:59'
     //example date 04/03/2015
     if (config_option('use_time_in_task_dates') || $date_field == "`created_on`") {
         foreach ($relative as $key => &$value) {
             $value->advance(-logged_user()->getTimezone() * 3600);
         }
         foreach ($relative_ends as $key => &$value) {
             $value->advance(-logged_user()->getTimezone() * 3600);
         }
     }
     $not_empty_date = " AND " . $date_field . " <> '0000-00-00 00:00:00'";
     //before this year //check if last month is on last year
     $group_1 = array();
     $group_1['group_name'] = lang('before this year');
     $group_1['group_order'] = 1;
     $group_1['id'] = 'group_before_this_year';
     if ($relative['first_day_of_year'] < $relative['first_day_of_last_month']) {
         $group_1['conditions'] = $date_field . " < '" . $relative['first_day_of_year']->toMySQL() . "'" . $not_empty_date;
     } else {
         $group_1['conditions'] = $date_field . " < '" . $relative['first_day_of_last_month']->toMySQL() . "'" . $not_empty_date;
     }
     $date_groups[] = $group_1;
     //this year (before last month)
     if ($relative['first_day_of_year'] < $relative['first_day_of_last_month']) {
         $group_2 = array();
         $group_2['group_name'] = lang('this year (before last month)');
         $group_2['group_order'] = 2;
         $group_2['id'] = 'group_this_year_before_last_month';
         $condition = $date_field . " >= '" . $relative['first_day_of_year']->toMySQL() . "'";
         $condition .= " AND " . $date_field . " < '" . $relative['first_day_of_last_month']->toMySQL() . "'";
         $group_2['conditions'] = $condition;
         $date_groups[] = $group_2;
     }
     //last month
     $group_3 = array();
     $group_3['group_name'] = lang('last month');
     $group_3['group_order'] = 3;
     $group_3['id'] = 'group_last_month';
     $condition = $date_field . " >= '" . $relative['first_day_of_last_month']->toMySQL() . "'";
     if ($relative['first_day_of_this_month'] < $relative['first_day_of_last_week']) {
         $condition .= " AND " . $date_field . " < '" . $relative['first_day_of_this_month']->toMySQL() . "'";
     } else {
         $condition .= " AND " . $date_field . " < '" . $relative['first_day_of_last_week']->toMySQL() . "'";
     }
     $group_3['conditions'] = $condition;
     $date_groups[] = $group_3;
     //this month(before last week)
     if ($relative['first_day_of_this_month'] < $relative['first_day_of_last_week']) {
         $group_4 = array();
         $group_4['group_name'] = lang('this month(before last week)');
         $group_4['group_order'] = 4;
         $group_4['id'] = 'group_this_month_before_last_week';
         $condition = $date_field . " >= '" . $relative['first_day_of_this_month']->toMySQL() . "'";
         $condition .= " AND " . $date_field . " < '" . $relative['first_day_of_last_week']->toMySQL() . "'";
         $group_4['conditions'] = $condition;
         $date_groups[] = $group_4;
     }
     //last week (week start on sunday and finish on saturday)
     $group_5 = array();
     $group_5['group_name'] = lang('last week');
     $group_5['group_order'] = 5;
     $group_5['id'] = 'group_last_week';
     $condition = $date_field . " >= '" . $relative['first_day_of_last_week']->toMySQL() . "'";
     $condition .= " AND " . $date_field . " < '" . $relative['first_day_of_this_week']->toMySQL() . "'";
     $group_5['conditions'] = $condition;
     $date_groups[] = $group_5;
     //this week(before yesterday)
     if ($relative['first_day_of_this_week'] < $relative['yesterday']) {
         $group_6 = array();
         $group_6['group_name'] = lang('this week(before yesterday)');
         $group_6['group_order'] = 6;
         $group_6['id'] = 'group_this_week_before_yesterday';
         $condition = $date_field . " >= '" . $relative['first_day_of_this_week']->toMySQL() . "'";
         $condition .= " AND " . $date_field . " < '" . $relative['yesterday']->toMySQL() . "'";
         $group_6['conditions'] = $condition;
         $date_groups[] = $group_6;
     }
     //yesterday
     $group_7 = array();
     $group_7['group_name'] = lang('yesterday');
     $group_7['group_order'] = 7;
     $group_7['id'] = 'group_yesterday';
     $condition = $date_field . " >= '" . $relative['yesterday']->toMySQL() . "'";
     $condition .= " AND " . $date_field . " <= '" . $relative_ends['yesterday']->toMySQL() . "'";
     $group_7['conditions'] = $condition;
     $date_groups[] = $group_7;
     //today
     $group_8 = array();
     $group_8['group_name'] = lang('today');
     $group_8['group_order'] = 8;
     $group_8['id'] = 'group_today';
     $condition = $date_field . " >= '" . $relative['today']->toMySQL() . "'";
     $condition .= " AND " . $date_field . " <= '" . $relative_ends['today']->toMySQL() . "'";
     $group_8['conditions'] = $condition;
     $date_groups[] = $group_8;
     //tomorrow
     $group_9 = array();
     $group_9['group_name'] = lang('tomorrow');
     $group_9['group_order'] = 9;
     $group_9['id'] = 'group_tomorrow';
     $condition = $date_field . " >= '" . $relative['tomorrow']->toMySQL() . "'";
     $condition .= " AND " . $date_field . " <= '" . $relative_ends['tomorrow']->toMySQL() . "'";
     $group_9['conditions'] = $condition;
     $date_groups[] = $group_9;
     //this week(later tomorrow)
     if ($relative['tomorrow'] < $relative['last_day_of_this_week']) {
         $group_10 = array();
         $group_10['group_name'] = lang('this week(later tomorrow)');
         $group_10['group_order'] = 10;
         $group_10['id'] = 'group_this_week_later_tomorrow';
         $condition = $date_field . " > '" . $relative_ends['tomorrow']->toMySQL() . "'";
         $condition .= " AND " . $date_field . " <= '" . $relative_ends['last_day_of_this_week']->toMySQL() . "'";
         $group_10['conditions'] = $condition;
         $date_groups[] = $group_10;
     }
     //next week
     $group_11 = array();
     $group_11['group_name'] = lang('next week');
     $group_11['group_order'] = 11;
     $group_11['id'] = 'group_next_week';
     $condition = $date_field . " >= '" . $relative['first_day_of_next_week']->toMySQL() . "'";
     $condition .= " AND " . $date_field . " <= '" . $relative_ends['last_day_of_next_week']->toMySQL() . "'";
     $group_11['conditions'] = $condition;
     $date_groups[] = $group_11;
     //this month(after next week)
     if ($relative['last_day_of_next_week'] < $relative['last_day_of_this_month']) {
         $group_12 = array();
         $group_12['group_name'] = lang('this month(after next week)');
         $group_12['group_order'] = 12;
         $group_12['id'] = 'group_this_month_after_next_week';
         $condition = $date_field . " > '" . $relative_ends['last_day_of_next_week']->toMySQL() . "'";
         $condition .= " AND " . $date_field . " <= '" . $relative_ends['last_day_of_this_month']->toMySQL() . "'";
         $group_12['conditions'] = $condition;
         $date_groups[] = $group_12;
     }
     //next month  //before next week
     $group_13 = array();
     $group_13['group_name'] = lang('next month');
     $group_13['group_order'] = 13;
     $group_13['id'] = 'group_next_month';
     if ($relative['last_day_of_next_week'] < $relative['last_day_of_this_month']) {
         $condition = $date_field . " > '" . $relative_ends['last_day_of_this_month']->toMySQL() . "'";
         $condition .= " AND " . $date_field . " <= '" . $relative_ends['last_day_of_next_month']->toMySQL() . "'";
         $group_13['conditions'] = $condition;
     } else {
         $condition = $date_field . " > '" . $relative_ends['last_day_of_next_week']->toMySQL() . "'";
         $condition .= " AND " . $date_field . " <= '" . $relative_ends['last_day_of_next_month']->toMySQL() . "'";
         $group_13['conditions'] = $condition;
     }
     $date_groups[] = $group_13;
     //next three months(after next month)
     $group_14 = array();
     $group_14['group_name'] = lang('next three months(after next month)');
     $group_14['group_order'] = 14;
     $group_14['id'] = 'group_next_three_months_after_next_month';
     $condition = $date_field . " > '" . $relative_ends['last_day_of_next_month']->toMySQL() . "'";
     $condition .= " AND " . $date_field . " <= '" . $relative_ends['last_day_of_next_3_months']->toMySQL() . "'";
     $group_14['conditions'] = $condition;
     $date_groups[] = $group_14;
     //this year
     if ($relative['last_day_of_next_3_months'] < $relative['last_day_of_this_year']) {
         $group_15 = array();
         $group_15['group_name'] = lang('this year');
         $group_15['group_order'] = 15;
         $group_15['id'] = 'group_this_year';
         $condition = $date_field . " > '" . $relative_ends['last_day_of_next_3_months']->toMySQL() . "'";
         $condition .= " AND " . $date_field . " <= '" . $relative_ends['last_day_of_this_year']->toMySQL() . "'";
         $group_15['conditions'] = $condition;
         $date_groups[] = $group_15;
     }
     //after this year //before next three months
     $group_16 = array();
     $group_16['group_name'] = lang('after this year');
     $group_16['group_order'] = 16;
     $group_16['id'] = 'group_after_this_year';
     if ($relative['last_day_of_next_3_months'] < $relative['last_day_of_this_year']) {
         $condition = $date_field . " > '" . $relative_ends['last_day_of_this_year']->toMySQL() . "'" . $not_empty_date;
         $group_16['conditions'] = $condition;
     } else {
         $condition = $date_field . " > '" . $relative_ends['last_day_of_next_3_months']->toMySQL() . "'" . $not_empty_date;
         $group_16['conditions'] = $condition;
     }
     $date_groups[] = $group_16;
     //no date EMPTY_DATETIME
     if ($unknown_group_name == 'due_date') {
         $unknown_group_name = 'without due date';
     } elseif ($unknown_group_name == 'start_date') {
         $unknown_group_name = 'without start date';
     } else {
         $unknown_group_name = 'without date';
     }
     $group_17 = array();
     $group_17['group_name'] = lang($unknown_group_name);
     $group_17['group_order'] = 17;
     $group_17['id'] = 'group_undefined';
     $group_17['conditions'] = $date_field . " = '" . EMPTY_DATETIME . "'";
     $date_groups[] = $group_17;
     return $date_groups;
 }
 /**
  * Edit task
  *
  * @access public
  * @param void
  * @return null
  */
 function edit_task()
 {
     $this->setTemplate('add_task');
     $task = ProjectTasks::findById(get_id());
     if (!$task instanceof ProjectTask) {
         flash_error(lang('task dnx'));
         $this->redirectTo('task');
     }
     // if
     $task_list = $task->getTaskList();
     if (!$task_list instanceof ProjectTaskList) {
         flash_error('task list dnx');
         $this->redirectTo('task');
     }
     // if
     if (!$task->canEdit(logged_user())) {
         flash_error(lang('no access permissions'));
         $this->redirectTo('task');
     }
     // if
     $task_data = array_var($_POST, 'task');
     if (!is_array($task_data)) {
         $task_data = array('text' => $task->getText(), 'start_date' => $task->getStartDate(), 'due_date' => $task->getDueDate(), 'task_list_id' => $task->getTaskListId(), 'assigned_to' => $task->getAssignedToCompanyId() . ':' . $task->getAssignedToUserId(), 'send_notification' => config_option('send_notification_default', '0'));
         // array
     }
     // if
     tpl_assign('task', $task);
     tpl_assign('task_list', $task_list);
     tpl_assign('task_data', $task_data);
     if (is_array(array_var($_POST, 'task'))) {
         $old_owner = $task->getAssignedTo();
         //$task_data['due_date'] = DateTimeValueLib::make(0, 0, 0, array_var($_POST, 'task_due_date_month', 1), array_var($_POST, 'task_due_date_day', 1), array_var($_POST, 'task_due_date_year', 1970));
         if (isset($_POST['task_start_date'])) {
             $task_data['start_date'] = DateTimeValueLib::makeFromString($_POST['task_start_date']);
         } else {
             $task_data['start_date'] = DateTimeValueLib::make(0, 0, 0, array_var($_POST, 'task_start_date_month', 1), array_var($_POST, 'task_start_date_day', 1), array_var($_POST, 'task_start_date_year', 1970));
         }
         if (isset($_POST['task_due_date'])) {
             $task_data['due_date'] = DateTimeValueLib::makeFromString($_POST['task_due_date']);
         } else {
             $task_data['due_date'] = DateTimeValueLib::make(0, 0, 0, array_var($_POST, 'task_due_date_month', 1), array_var($_POST, 'task_due_date_day', 1), array_var($_POST, 'task_due_date_year', 1970));
         }
         $task->setFromAttributes($task_data);
         $task->setTaskListId($task_list->getId());
         // keep old task list id
         $assigned_to = explode(':', array_var($task_data, 'assigned_to', ''));
         $task->setAssignedToCompanyId(array_var($assigned_to, 0, 0));
         $task->setAssignedToUserId(array_var($assigned_to, 1, 0));
         try {
             DB::beginWork();
             $task->save();
             // Move?
             $new_task_list_id = (int) array_var($task_data, 'task_list_id');
             if ($new_task_list_id && $task->getTaskListId() != $new_task_list_id) {
                 // Move!
                 $new_task_list = ProjectTaskLists::findById($new_task_list_id);
                 if ($new_task_list instanceof ProjectTaskList) {
                     $task_list->detachTask($task, $new_task_list);
                     // detach from old and attach to new list
                 }
                 // if
             }
             // if
             ApplicationLogs::createLog($task, active_project(), ApplicationLogs::ACTION_EDIT);
             DB::commit();
             trace(__FILE__, 'edit_task: notify user');
             // notify user
             if (array_var($task_data, 'send_notification') == 'checked') {
                 try {
                     if (Notifier::notifyNeeded($task->getAssignedTo(), $old_owner)) {
                         Notifier::taskAssigned($task);
                     }
                 } catch (Exception $e) {
                     Logger::log("Error: Notification failed, " . $e->getMessage(), Logger::ERROR);
                 }
                 // try
             }
             // if
             flash_success(lang('success edit task'));
             // Redirect to task list. Check if we have updated task list ID first
             if (isset($new_task_list) && $new_task_list instanceof ProjectTaskList) {
                 $this->redirectToUrl($new_task_list->getViewUrl());
             } else {
                 $this->redirectToUrl($task_list->getViewUrl());
             }
             // if
         } catch (Exception $e) {
             DB::rollback();
             tpl_assign('error', $e);
         }
         // try
     }
     // if
 }
 static function updateBillingValues()
 {
     $timeslot_rows = DB::executeAll("SELECT * FROM " . TABLE_PREFIX . "timeslots WHERE `end_time` > 0 AND billing_id = 0 AND is_fixed_billing = 0");
     $users = Contacts::getAllUsers();
     $usArray = array();
     foreach ($users as $u) {
         $usArray[$u->getId()] = $u;
     }
     $count = 0;
     $categories_cache = array();
     foreach ($timeslot_rows as $ts_row) {
         if ($ts_row['start_time'] == EMPTY_DATETIME) {
             $ts_row['minutes'] = 0;
         } else {
             $startTime = DateTimeValueLib::makeFromString($ts_row['start_time']);
             if ($ts_row['start_time'] == EMPTY_DATETIME) {
                 $endTime = $ts_row['is_paused'] ? DateTimeValueLib::makeFromString($ts_row['paused_on']) : DateTimeValueLib::now();
             } else {
                 $endTime = DateTimeValueLib::makeFromString($ts_row['end_time']);
             }
             $timeDiff = DateTimeValueLib::get_time_difference($startTime->getTimestamp(), $endTime->getTimestamp(), $ts_row['subtract']);
             $ts_row['minutes'] = $timeDiff['days'] * 1440 + $timeDiff['hours'] * 60 + $timeDiff['minutes'];
         }
         $user = $usArray[$ts_row['contact_id']];
         if ($user instanceof Contact) {
             $billing_category_id = $user->getDefaultBillingId();
             if ($billing_category_id > 0) {
                 $hours = $ts_row['minutes'] / 60;
                 $billing_category = array_var($categories_cache, $billing_category_id);
                 if (!$billing_category instanceof BillingCategory) {
                     $billing_category = BillingCategories::findById($billing_category_id);
                     $categories_cache[$billing_category_id] = $billing_category;
                 }
                 if ($billing_category instanceof BillingCategory) {
                     $hourly_billing = $billing_category->getDefaultValue();
                     DB::execute("UPDATE " . TABLE_PREFIX . "timeslots SET billing_id='{$billing_category_id}', hourly_billing='{$hourly_billing}', \r\n\t\t\t\t\t\t\tfixed_billing='" . round($hourly_billing * $hours, 2) . "', is_fixed_billing=0 \r\n\t\t\t\t\t\t\tWHERE object_id=" . $ts_row['object_id']);
                     $count++;
                 }
             }
         } else {
             DB::execute("UPDATE " . TABLE_PREFIX . "timeslots SET is_fixed_billing=1 WHERE object_id=" . $ts_row['object_id']);
         }
     }
     return $count;
 }
 /**
  * Shows weekly schedule in a calendar view
  * 
  * @param void
  * @return null
  */
 function weekly_schedule()
 {
     $this->addHelper('textile');
     // Gets desired view 'detail', 'list' or 'calendar'
     // $view_type is from URL, Cookie or set to default: 'calendar'
     $view_type = array_var($_GET, 'view', Cookie::getValue('weeklyScheduleViewType', 'calendar'));
     $expiration = Cookie::getValue('remember' . TOKEN_COOKIE_NAME) ? REMEMBER_LOGIN_LIFETIME : null;
     Cookie::setValue('weeklyScheduleViewType', $view_type, $expiration);
     $monthYear = array_var($_GET, 'month');
     if (!isset($monthYear) || trim($monthYear) == '' || preg_match('/^(\\d{4})(\\d{2})$/', $monthYear, $matches) == 0) {
         $year = gmdate('Y');
         $month = gmdate('m');
     } else {
         list(, $year, $month) = $matches;
     }
     // TODO make first day of week configurable
     $from_date = DateTimeValueLib::makeFromString('monday' . (date('w') == 1 ? '' : ' last week'));
     $to_date = $from_date->advance(60 * 60 * 24 * 7 * 3, false);
     // +3 weeks
     $upcoming_milestones = ProjectMilestones::getActiveMilestonesInPeriodByUser(logged_user(), $from_date, $to_date);
     $upcoming_tickets = array();
     if (plugin_active('tickets')) {
         $upcoming_tickets = ProjectTickets::getOpenTicketsInPeriodByUser(logged_user(), $from_date, $to_date);
     }
     $active_projects = array();
     $projects_index = array();
     $counter = 1;
     if (is_array($upcoming_milestones)) {
         foreach ($upcoming_milestones as $milestone) {
             if (!isset($projects_index[$milestone->getProjectId()])) {
                 $projects_index[$milestone->getProjectId()] = $counter;
                 $active_projects[] = $milestone->getProject();
                 $counter++;
             }
             // if
         }
         // foreach
     }
     // if
     if (is_array($upcoming_tickets)) {
         foreach ($upcoming_tickets as $ticket) {
             if (!isset($projects_index[$ticket->getProjectId()])) {
                 $projects_index[$ticket->getProjectId()] = $counter;
                 $active_projects[] = $ticket->getProject();
                 $counter++;
             }
             // if
         }
         // foreach
     }
     // if
     tpl_assign('from_date', $from_date);
     tpl_assign('to_date', $to_date);
     tpl_assign('view_type', $view_type);
     tpl_assign('upcoming_tickets', $upcoming_tickets);
     tpl_assign('late_tickets', array());
     // logged_user()->getLateTickets());
     tpl_assign('upcoming_milestones', $upcoming_milestones);
     tpl_assign('late_milestones', array());
     // logged_user()->getLateMilestones());
     tpl_assign('projects', $active_projects);
     tpl_assign('projects_index', $projects_index);
 }
 /**
  * Show and process edit milestone form
  *
  * @access public
  * @param void
  * @return null
  */
 function edit()
 {
     $this->setTemplate('add_milestone');
     $milestone = ProjectMilestones::findById(get_id());
     if (!$milestone instanceof ProjectMilestone) {
         flash_error(lang('milestone dnx'));
         $this->redirectTo('milestone', 'index');
     }
     // if
     if (!$milestone->canEdit(logged_user())) {
         flash_error(lang('no access permissions'));
         $this->redirectToReferer(get_url('milestone'));
     }
     $milestone_data = array_var($_POST, 'milestone');
     if (!is_array($milestone_data)) {
         $tag_names = plugin_active('tags') ? $milestone->getTagNames() : '';
         $milestone_data = array('name' => $milestone->getName(), 'due_date' => $milestone->getDueDate(), 'description' => $milestone->getDescription(), 'assigned_to' => $milestone->getAssignedToCompanyId() . ':' . $milestone->getAssignedToUserId(), 'tags' => is_array($tag_names) ? implode(', ', $tag_names) : '', 'is_private' => $milestone->isPrivate());
         // array
     }
     // if
     tpl_assign('milestone_data', $milestone_data);
     tpl_assign('milestone', $milestone);
     if (is_array(array_var($_POST, 'milestone'))) {
         $old_owner = $milestone->getAssignedTo();
         // remember the old owner
         if (isset($_POST['milestone_due_date'])) {
             $milestone_data['due_date'] = DateTimeValueLib::makeFromString($_POST['milestone_due_date']);
         } else {
             $milestone_data['due_date'] = DateTimeValueLib::make(0, 0, 0, array_var($_POST, 'milestone_due_date_month', 1), array_var($_POST, 'milestone_due_date_day', 1), array_var($_POST, 'milestone_due_date_year', 1970));
         }
         //$milestone_data['due_date'] = DateTimeValueLib::make(0, 0, 0, array_var($_POST, 'milestone_due_date_month', 1), array_var($_POST, 'milestone_due_date_day', 1), array_var($_POST, 'milestone_due_date_year', 1970));
         $assigned_to = explode(':', array_var($milestone_data, 'assigned_to', ''));
         $old_is_private = $milestone->isPrivate();
         $milestone->setFromAttributes($milestone_data);
         if (!logged_user()->isMemberOfOwnerCompany()) {
             $milestone->setIsPrivate($old_is_private);
         }
         $milestone->setProjectId(active_project()->getId());
         $milestone->setAssignedToCompanyId(array_var($assigned_to, 0, 0));
         $milestone->setAssignedToUserId(array_var($assigned_to, 1, 0));
         try {
             DB::beginWork();
             $milestone->save();
             if (plugin_active('tags')) {
                 $milestone->setTagsFromCSV(array_var($milestone_data, 'tags'));
             }
             ApplicationLogs::createLog($milestone, active_project(), ApplicationLogs::ACTION_EDIT);
             DB::commit();
             // If owner is changed send notification but don't break submission
             try {
                 $new_owner = $milestone->getAssignedTo();
                 if (array_var($milestone_data, 'send_notification') == 'checked') {
                     if ($old_owner instanceof User) {
                         // We have a new owner and it is different than old owner
                         if ($new_owner instanceof User && $new_owner->getId() != $old_owner->getId()) {
                             Notifier::milestoneAssigned($milestone);
                         }
                     } else {
                         // We have new owner
                         if ($new_owner instanceof User) {
                             Notifier::milestoneAssigned($milestone);
                         }
                     }
                     // if
                 }
                 // if
             } catch (Exception $e) {
             }
             // try
             flash_success(lang('success edit milestone', $milestone->getName()));
             $this->redirectTo('milestone');
         } catch (Exception $e) {
             DB::rollback();
             tpl_assign('error', $e);
         }
         // try
     }
     // if
 }
 /**
  * This function will return true if this datetime is yesterday
  *
  * @param void
  * @return boolean
  */
 function isYesterday()
 {
     $yesterday = DateTimeValueLib::makeFromString('yesterday');
     /* TODO: if (logged_user() instanceof User) {
     			$date = new DateTimeValue($this->getTimestamp() + logged_user()->getTimezone() * 3600);
     		} else {
     			$date = $this;
     		}*/
     return $this->getDay() == $yesterday->getDay() && $this->getMonth() == $yesterday->getMonth() && $this->getYear() == $yesterday->getYear();
 }
 /**
  * Render form control
  *
  * @param string $control_name
  * @return string
  */
 function render($control_name)
 {
     $value = DateTimeValueLib::makeFromString($this->getValue());
     return pick_time_widget2($control_name, $value, null, null, null);
 }