2 // +----------------------------------------------------------------------+
 
   3 // | Anuko Time Tracker
 
   4 // +----------------------------------------------------------------------+
 
   5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
 
   6 // +----------------------------------------------------------------------+
 
   7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
 
   8 // | by anyone for any purpose, and freely redistributed alone or in
 
   9 // | combination with other software, provided that the license is obeyed.
 
  11 // | There are only two ways to violate the license:
 
  13 // | 1. To redistribute this code in source form, with the copyright
 
  14 // |    notice or license removed or altered. (Distributing in compiled
 
  15 // |    forms without embedded copyright notices is permitted).
 
  17 // | 2. To redistribute modified versions of this code in *any* form
 
  18 // |    that bears insufficient indications that the modifications are
 
  19 // |    not the work of the original author(s).
 
  21 // | This license applies to this document only, not any other software
 
  22 // | that it may be combined with.
 
  24 // +----------------------------------------------------------------------+
 
  26 // | https://www.anuko.com/time_tracker/credits.htm
 
  27 // +----------------------------------------------------------------------+
 
  29 require_once('initialize.php');
 
  31 import('ttUserHelper');
 
  32 import('ttTeamHelper');
 
  33 import('ttClientHelper');
 
  34 import('ttTimeHelper');
 
  35 import('DateAndTime');
 
  37 // This is a now removed check whether user browser supports cookies.
 
  38 // if (!isset($_COOKIE['tt_PHPSESSID'])) {
 
  39   // This test gives a false-positive if user goes directly to this page
 
  40   // as from a desktop shortcut (on first request only).
 
  41   // die ("Your browser's cookie functionality is turned off. Please turn it on.");
 
  45 if (!(ttAccessAllowed('track_own_time') || ttAccessAllowed('track_time'))) {
 
  46   header('Location: access_denied.php');
 
  49 if ($user->behalf_id && (!$user->can('track_time') || !$user->checkBehalfId())) {
 
  50   header('Location: access_denied.php'); // Trying on behalf, but no right or wrong user.
 
  53 if (!$user->behalf_id && !$user->can('track_own_time') && !$user->adjustBehalfId()) {
 
  54   header('Location: access_denied.php'); // Trying as self, but no right for self, and noone to work on behalf.
 
  58 // Initialize and store date in session.
 
  59 $cl_date = $request->getParameter('date', @$_SESSION['date']);
 
  60 $selected_date = new DateAndTime(DB_DATEFORMAT, $cl_date);
 
  61 if($selected_date->isError())
 
  62   $selected_date = new DateAndTime(DB_DATEFORMAT);
 
  64   $cl_date = $selected_date->toString(DB_DATEFORMAT);
 
  65 $_SESSION['date'] = $cl_date;
 
  67 // Use custom fields plugin if it is enabled.
 
  68 if ($user->isPluginEnabled('cf')) {
 
  69   require_once('plugins/CustomFields.class.php');
 
  70   $custom_fields = new CustomFields($user->team_id);
 
  71   $smarty->assign('custom_fields', $custom_fields);
 
  74 if ($user->isPluginEnabled('mq')){
 
  75   require_once('plugins/MonthlyQuota.class.php');
 
  76   $quota = new MonthlyQuota();
 
  77   $month_quota_minutes = $quota->get($selected_date->mYear, $selected_date->mMonth);
 
  78   $month_total = ttTimeHelper::getTimeForMonth($user->getActiveUser(), $selected_date);
 
  79   $minutes_left = $month_quota_minutes - ttTimeHelper::toMinutes($month_total);
 
  81   $smarty->assign('month_total', $month_total);
 
  82   $smarty->assign('over_quota', $minutes_left < 0);
 
  83   $smarty->assign('quota_remaining', ttTimeHelper::toAbsDuration($minutes_left));
 
  86 // Initialize variables.
 
  87 $cl_start = trim($request->getParameter('start'));
 
  88 $cl_finish = trim($request->getParameter('finish'));
 
  89 $cl_duration = trim($request->getParameter('duration'));
 
  90 $cl_note = trim($request->getParameter('note'));
 
  92 $cl_cf_1 = trim($request->getParameter('cf_1', ($request->isPost() ? null : @$_SESSION['cf_1'])));
 
  93 $_SESSION['cf_1'] = $cl_cf_1;
 
  95 if ($user->isPluginEnabled('iv')) {
 
  96   if ($request->isPost()) {
 
  97     $cl_billable = $request->getParameter('billable');
 
  98     $_SESSION['billable'] = (int) $cl_billable;
 
 100     if (isset($_SESSION['billable']))
 
 101       $cl_billable = $_SESSION['billable'];
 
 103 $on_behalf_id = $request->getParameter('onBehalfUser', (isset($_SESSION['behalf_id'])? $_SESSION['behalf_id'] : $user->id));
 
 104 $cl_client = $request->getParameter('client', ($request->isPost() ? null : @$_SESSION['client']));
 
 105 $_SESSION['client'] = $cl_client;
 
 106 $cl_project = $request->getParameter('project', ($request->isPost() ? null : @$_SESSION['project']));
 
 107 $_SESSION['project'] = $cl_project;
 
 108 $cl_task = $request->getParameter('task', ($request->isPost() ? null : @$_SESSION['task']));
 
 109 $_SESSION['task'] = $cl_task;
 
 111 // Elements of timeRecordForm.
 
 112 $form = new Form('timeRecordForm');
 
 114 if ($user->can('track_time')) {
 
 115   if ($user->can('track_own_time'))
 
 116     $options = array('status'=>ACTIVE,'max_rank'=>$user->rank-1,'include_self'=>true,'self_first'=>true);
 
 118     $options = array('status'=>ACTIVE,'max_rank'=>$user->rank-1);
 
 119   $user_list = $user->getUsers($options);
 
 120   if (count($user_list) >= 1) {
 
 121     $form->addInput(array('type'=>'combobox',
 
 122       'onchange'=>'this.form.submit();',
 
 123       'name'=>'onBehalfUser',
 
 124       'style'=>'width: 250px;',
 
 125       'value'=>$on_behalf_id,
 
 127       'datakeys'=>array('id','name')));
 
 128     $smarty->assign('on_behalf_control', 1);
 
 132 // Dropdown for clients in MODE_TIME. Use all active clients.
 
 133 if (MODE_TIME == $user->tracking_mode && $user->isPluginEnabled('cl')) {
 
 134   $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
 
 135   $form->addInput(array('type'=>'combobox',
 
 136     'onchange'=>'fillProjectDropdown(this.value);',
 
 138     'style'=>'width: 250px;',
 
 140     'data'=>$active_clients,
 
 141     'datakeys'=>array('id', 'name'),
 
 142     'empty'=>array(''=>$i18n->get('dropdown.select'))));
 
 143   // Note: in other modes the client list is filtered to relevant clients only. See below.
 
 146 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
 
 147   // Dropdown for projects assigned to user.
 
 148   $project_list = $user->getAssignedProjects();
 
 149   $form->addInput(array('type'=>'combobox',
 
 150     'onchange'=>'fillTaskDropdown(this.value);',
 
 152     'style'=>'width: 250px;',
 
 153     'value'=>$cl_project,
 
 154     'data'=>$project_list,
 
 155     'datakeys'=>array('id','name'),
 
 156     'empty'=>array(''=>$i18n->get('dropdown.select'))));
 
 158   // Dropdown for clients if the clients plugin is enabled.
 
 159   if ($user->isPluginEnabled('cl')) {
 
 160     $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
 
 161     // We need an array of assigned project ids to do some trimming.
 
 162     foreach($project_list as $project)
 
 163       $projects_assigned_to_user[] = $project['id'];
 
 165     // Build a client list out of active clients. Use only clients that are relevant to user.
 
 166     // Also trim their associated project list to only assigned projects (to user).
 
 167     foreach($active_clients as $client) {
 
 168       $projects_assigned_to_client = explode(',', $client['projects']);
 
 169       if (is_array($projects_assigned_to_client) && is_array($projects_assigned_to_user))
 
 170         $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
 
 172         $client['projects'] = implode(',', $intersection);
 
 173         $client_list[] = $client;
 
 176     $form->addInput(array('type'=>'combobox',
 
 177       'onchange'=>'fillProjectDropdown(this.value);',
 
 179       'style'=>'width: 250px;',
 
 181       'data'=>$client_list,
 
 182       'datakeys'=>array('id', 'name'),
 
 183       'empty'=>array(''=>$i18n->get('dropdown.select'))));
 
 187 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
 
 188   $task_list = ttTeamHelper::getActiveTasks($user->team_id);
 
 189   $form->addInput(array('type'=>'combobox',
 
 191     'style'=>'width: 250px;',
 
 194     'datakeys'=>array('id','name'),
 
 195     'empty'=>array(''=>$i18n->get('dropdown.select'))));
 
 198 // Add other controls.
 
 199 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
 
 200   $form->addInput(array('type'=>'text','name'=>'start','value'=>$cl_start,'onchange'=>"formDisable('start');"));
 
 201   $form->addInput(array('type'=>'text','name'=>'finish','value'=>$cl_finish,'onchange'=>"formDisable('finish');"));
 
 202   if ($user->punch_mode && !$user->canOverridePunchMode()) {
 
 203     // Make the start and finish fields read-only.
 
 204     $form->getElement('start')->setEnabled(false);
 
 205     $form->getElement('finish')->setEnabled(false);
 
 208 if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
 
 209   $form->addInput(array('type'=>'text','name'=>'duration','value'=>$cl_duration,'onchange'=>"formDisable('duration');"));
 
 210 if (!defined('NOTE_INPUT_HEIGHT'))
 
 211   define('NOTE_INPUT_HEIGHT', 40);
 
 212 $form->addInput(array('type'=>'textarea','name'=>'note','style'=>'width: 600px; height:'.NOTE_INPUT_HEIGHT.'px;','value'=>$cl_note));
 
 213 $form->addInput(array('type'=>'calendar','name'=>'date','value'=>$cl_date)); // calendar
 
 214 if ($user->isPluginEnabled('iv'))
 
 215   $form->addInput(array('type'=>'checkbox','name'=>'billable','value'=>$cl_billable));
 
 216 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
 
 217 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->get('button.submit')));
 
 219 // If we have custom fields - add controls for them.
 
 220 if ($custom_fields && $custom_fields->fields[0]) {
 
 221   // Only one custom field is supported at this time.
 
 222   if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT) {
 
 223     $form->addInput(array('type'=>'text','name'=>'cf_1','value'=>$cl_cf_1));
 
 224   } elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
 
 225     $form->addInput(array('type'=>'combobox','name'=>'cf_1',
 
 226       'style'=>'width: 250px;',
 
 228       'data'=>$custom_fields->options,
 
 229       'empty'=>array(''=>$i18n->get('dropdown.select'))));
 
 234 if ($request->isPost()) {
 
 235   if ($request->getParameter('btn_submit')) {
 
 237     // Validate user input.
 
 238     if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
 
 239       $err->add($i18n->get('error.client'));
 
 240     if ($custom_fields) {
 
 241       if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $err->add($i18n->get('error.field'), $custom_fields->fields[0]['label']);
 
 243     if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
 
 244       if (!$cl_project) $err->add($i18n->get('error.project'));
 
 246     if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode && $user->task_required) {
 
 247       if (!$cl_task) $err->add($i18n->get('error.task'));
 
 249     if (strlen($cl_duration) == 0) {
 
 250       if ($cl_start || $cl_finish) {
 
 251         if (!ttTimeHelper::isValidTime($cl_start))
 
 252           $err->add($i18n->get('error.field'), $i18n->get('label.start'));
 
 254           if (!ttTimeHelper::isValidTime($cl_finish))
 
 255             $err->add($i18n->get('error.field'), $i18n->get('label.finish'));
 
 256           if (!ttTimeHelper::isValidInterval($cl_start, $cl_finish))
 
 257             $err->add($i18n->get('error.interval'), $i18n->get('label.finish'), $i18n->get('label.start'));
 
 260         if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
 
 261           $err->add($i18n->get('error.empty'), $i18n->get('label.start'));
 
 262           $err->add($i18n->get('error.empty'), $i18n->get('label.finish'));
 
 264         if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
 
 265           $err->add($i18n->get('error.empty'), $i18n->get('label.duration'));
 
 268       if (false === ttTimeHelper::postedDurationToMinutes($cl_duration))
 
 269         $err->add($i18n->get('error.field'), $i18n->get('label.duration'));
 
 271     if (!ttValidString($cl_note, true)) $err->add($i18n->get('error.field'), $i18n->get('label.note'));
 
 272     // Finished validating user input.
 
 274     // Prohibit creating entries in future.
 
 275     if (!$user->future_entries) {
 
 276       $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
 
 277       if ($selected_date->after($browser_today))
 
 278         $err->add($i18n->get('error.future_date'));
 
 281     // Prohibit creating entries in locked range.
 
 282     if ($user->isDateLocked($selected_date))
 
 283       $err->add($i18n->get('error.range_locked'));
 
 285     // Prohibit creating another uncompleted record.
 
 287       if (($not_completed_rec = ttTimeHelper::getUncompleted($user->getActiveUser())) && (($cl_finish == '') && ($cl_duration == '')))
 
 288         $err->add($i18n->get('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->get('error.goto_uncompleted')."</a>");
 
 291     // Prohibit creating an overlapping record.
 
 293       if (ttTimeHelper::overlaps($user->getActiveUser(), $cl_date, $cl_start, $cl_finish))
 
 294         $err->add($i18n->get('error.overlap'));
 
 299       $id = ttTimeHelper::insert(array(
 
 301         'user_id' => $user->getActiveUser(),
 
 302         'client' => $cl_client,
 
 303         'project' => $cl_project,
 
 305         'start' => $cl_start,
 
 306         'finish' => $cl_finish,
 
 307         'duration' => $cl_duration,
 
 309         'billable' => $cl_billable));
 
 311       // Insert a custom field if we have it.
 
 313       if ($id && $custom_fields && $cl_cf_1) {
 
 314         if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
 
 315           $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
 
 316         elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
 
 317           $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
 
 319       if ($id && $result) {
 
 320         header('Location: time.php');
 
 323       $err->add($i18n->get('error.db'));
 
 325   } elseif ($request->getParameter('btn_stop')) {
 
 326     // Stop button pressed to finish an uncompleted record.
 
 327     $record_id = $request->getParameter('record_id');
 
 328     $record = ttTimeHelper::getRecord($record_id, $user->getActiveUser());
 
 329     $browser_date = $request->getParameter('browser_date');
 
 330     $browser_time = $request->getParameter('browser_time');
 
 332     // Can we complete this record?
 
 333     if ($record['date'] == $browser_date                                // closing today's record
 
 334       && ttTimeHelper::isValidInterval($record['start'], $browser_time) // finish time is greater than start time
 
 335       && !ttTimeHelper::overlaps($user->getActiveUser(), $browser_date, $record['start'], $browser_time)) { // no overlap
 
 336       $res = ttTimeHelper::update(array(
 
 338           'date'=>$record['date'],
 
 339           'user_id'=>$user->getActiveUser(),
 
 340           'client'=>$record['client_id'],
 
 341           'project'=>$record['project_id'],
 
 342           'task'=>$record['task_id'],
 
 343           'start'=>$record['start'],
 
 344           'finish'=>$browser_time,
 
 345           'note'=>$record['comment'],
 
 346           'billable'=>$record['billable']));
 
 348         $err->add($i18n->get('error.db'));
 
 350       // Cannot complete, redirect for manual edit.
 
 351       header('Location: time_edit.php?id='.$record_id);
 
 355   elseif ($request->getParameter('onBehalfUser')) {
 
 356     if($user->canManageTeam()) {
 
 357       unset($_SESSION['behalf_id']);
 
 358       unset($_SESSION['behalf_name']);
 
 360       if($on_behalf_id != $user->id) {
 
 361         $_SESSION['behalf_id'] = $on_behalf_id;
 
 362         $_SESSION['behalf_name'] = ttUserHelper::getUserName($on_behalf_id);
 
 364       header('Location: time.php');
 
 370 $week_total = ttTimeHelper::getTimeForWeek($user->getActiveUser(), $selected_date);
 
 372 $smarty->assign('selected_date', $selected_date);
 
 373 $smarty->assign('week_total', $week_total);
 
 374 $smarty->assign('day_total', ttTimeHelper::getTimeForDay($user->getActiveUser(), $cl_date));
 
 375 $smarty->assign('time_records', ttTimeHelper::getRecords($user->getActiveUser(), $cl_date));
 
 376 $smarty->assign('client_list', $client_list);
 
 377 $smarty->assign('project_list', $project_list);
 
 378 $smarty->assign('task_list', $task_list);
 
 379 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
 
 380 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
 
 381 $smarty->assign('timestring', $selected_date->toString($user->date_format));
 
 382 $smarty->assign('title', $i18n->get('title.time'));
 
 383 $smarty->assign('content_page_name', 'time.tpl');
 
 384 $smarty->display('index.tpl');