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 (!ttAccessCheck(right_data_entry)) {
 
  46   header('Location: access_denied.php');
 
  50 // Initialize and store date in session.
 
  51 $cl_date = $request->getParameter('date', @$_SESSION['date']);
 
  52 $selected_date = new DateAndTime(DB_DATEFORMAT, $cl_date);
 
  53 if($selected_date->isError())
 
  54   $selected_date = new DateAndTime(DB_DATEFORMAT);
 
  56   $cl_date = $selected_date->toString(DB_DATEFORMAT);
 
  57 $_SESSION['date'] = $cl_date;
 
  59 // Use custom fields plugin if it is enabled.
 
  60 if ($user->isPluginEnabled('cf')) {
 
  61   require_once('plugins/CustomFields.class.php');
 
  62   $custom_fields = new CustomFields($user->team_id);
 
  63   $smarty->assign('custom_fields', $custom_fields);
 
  66 if ($user->isPluginEnabled('mq')){
 
  67   require_once('plugins/MonthlyQuota.class.php');
 
  68   $quota = new MonthlyQuota();
 
  69   $monthlyQuota = $quota->get($selected_date->mYear, $selected_date->mMonth);
 
  70   $month_total = ttTimeHelper::getTimeForMonth($user->getActiveUser(), $selected_date);
 
  71   $minutesLeft = ttTimeHelper::toMinutes($monthlyQuota) - ttTimeHelper::toMinutes($month_total);
 
  73   $smarty->assign('month_total', $month_total);
 
  74   $smarty->assign('month_left', ttTimeHelper::fromMinutes($minutesLeft));
 
  77 // Initialize variables.
 
  78 $cl_start = trim($request->getParameter('start'));
 
  79 $cl_finish = trim($request->getParameter('finish'));
 
  80 $cl_duration = trim($request->getParameter('duration'));
 
  81 $cl_note = trim($request->getParameter('note'));
 
  83 $cl_cf_1 = trim($request->getParameter('cf_1', ($request->getMethod()=='POST'? null : @$_SESSION['cf_1'])));
 
  84 $_SESSION['cf_1'] = $cl_cf_1;
 
  86 if ($user->isPluginEnabled('iv')) {
 
  87   if ($request->isPost()) {
 
  88     $cl_billable = $request->getParameter('billable');
 
  89     $_SESSION['billable'] = (int) $cl_billable;
 
  91     if (isset($_SESSION['billable']))
 
  92       $cl_billable = $_SESSION['billable'];
 
  94 $on_behalf_id = $request->getParameter('onBehalfUser', (isset($_SESSION['behalf_id'])? $_SESSION['behalf_id'] : $user->id));
 
  95 $cl_client = $request->getParameter('client', ($request->getMethod()=='POST'? null : @$_SESSION['client']));
 
  96 $_SESSION['client'] = $cl_client;
 
  97 $cl_project = $request->getParameter('project', ($request->getMethod()=='POST'? null : @$_SESSION['project']));
 
  98 $_SESSION['project'] = $cl_project;
 
  99 $cl_task = $request->getParameter('task', ($request->getMethod()=='POST'? null : @$_SESSION['task']));
 
 100 $_SESSION['task'] = $cl_task;
 
 102 // Elements of timeRecordForm.
 
 103 $form = new Form('timeRecordForm');
 
 105 if ($user->canManageTeam()) {
 
 106   $user_list = ttTeamHelper::getActiveUsers(array('putSelfFirst'=>true));
 
 107   if (count($user_list) > 1) {
 
 108     $form->addInput(array('type'=>'combobox',
 
 109       'onchange'=>'this.form.submit();',
 
 110       'name'=>'onBehalfUser',
 
 111       'style'=>'width: 250px;',
 
 112       'value'=>$on_behalf_id,
 
 114       'datakeys'=>array('id','name')));
 
 115     $smarty->assign('on_behalf_control', 1);
 
 119 // Dropdown for clients in MODE_TIME. Use all active clients.
 
 120 if (MODE_TIME == $user->tracking_mode && $user->isPluginEnabled('cl')) {
 
 121   $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
 
 122   $form->addInput(array('type'=>'combobox',
 
 123     'onchange'=>'fillProjectDropdown(this.value);',
 
 125     'style'=>'width: 250px;',
 
 127     'data'=>$active_clients,
 
 128     'datakeys'=>array('id', 'name'),
 
 129     'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
 
 130   // Note: in other modes the client list is filtered to relevant clients only. See below.
 
 133 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
 
 134   // Dropdown for projects assigned to user.
 
 135   $project_list = $user->getAssignedProjects();
 
 136   $form->addInput(array('type'=>'combobox',
 
 137     'onchange'=>'fillTaskDropdown(this.value);',
 
 139     'style'=>'width: 250px;',
 
 140     'value'=>$cl_project,
 
 141     'data'=>$project_list,
 
 142     'datakeys'=>array('id','name'),
 
 143     'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
 
 145   // Dropdown for clients if the clients plugin is enabled.
 
 146   if ($user->isPluginEnabled('cl')) {
 
 147     $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
 
 148     // We need an array of assigned project ids to do some trimming. 
 
 149     foreach($project_list as $project)
 
 150       $projects_assigned_to_user[] = $project['id'];
 
 152     // Build a client list out of active clients. Use only clients that are relevant to user.
 
 153     // Also trim their associated project list to only assigned projects (to user).
 
 154     foreach($active_clients as $client) {
 
 155       $projects_assigned_to_client = explode(',', $client['projects']);
 
 156       if (is_array($projects_assigned_to_client) && is_array($projects_assigned_to_user))
 
 157         $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
 
 159         $client['projects'] = implode(',', $intersection);
 
 160         $client_list[] = $client;
 
 163     $form->addInput(array('type'=>'combobox',
 
 164       'onchange'=>'fillProjectDropdown(this.value);',
 
 166       'style'=>'width: 250px;',
 
 168       'data'=>$client_list,
 
 169       'datakeys'=>array('id', 'name'),
 
 170       'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
 
 174 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
 
 175   $task_list = ttTeamHelper::getActiveTasks($user->team_id);
 
 176   $form->addInput(array('type'=>'combobox',
 
 178     'style'=>'width: 250px;',
 
 181     'datakeys'=>array('id','name'),
 
 182     'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
 
 185 // Add other controls.
 
 186 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
 
 187   $form->addInput(array('type'=>'text','name'=>'start','value'=>$cl_start,'onchange'=>"formDisable('start');"));
 
 188   $form->addInput(array('type'=>'text','name'=>'finish','value'=>$cl_finish,'onchange'=>"formDisable('finish');"));
 
 190 if (!$user->canManageTeam() && defined('READONLY_START_FINISH') && isTrue(READONLY_START_FINISH)) {
 
 191   // Make the start and finish fields read-only.
 
 192   $form->getElement('start')->setEnable(false);
 
 193   $form->getElement('finish')->setEnable(false);
 
 195 if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
 
 196   $form->addInput(array('type'=>'text','name'=>'duration','value'=>$cl_duration,'onchange'=>"formDisable('duration');"));
 
 197 $form->addInput(array('type'=>'textarea','name'=>'note','style'=>'width: 600px; height: 40px;','value'=>$cl_note));
 
 198 $form->addInput(array('type'=>'calendar','name'=>'date','value'=>$cl_date)); // calendar
 
 199 if ($user->isPluginEnabled('iv'))
 
 200   $form->addInput(array('type'=>'checkbox','name'=>'billable','data'=>1,'value'=>$cl_billable));
 
 201 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
 
 202 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->getKey('button.submit')));
 
 204 // If we have custom fields - add controls for them.
 
 205 if ($custom_fields && $custom_fields->fields[0]) {
 
 206   // Only one custom field is supported at this time.
 
 207   if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT) {
 
 208     $form->addInput(array('type'=>'text','name'=>'cf_1','value'=>$cl_cf_1));
 
 209   } elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
 
 210     $form->addInput(array('type'=>'combobox','name'=>'cf_1',
 
 211       'style'=>'width: 250px;',
 
 213       'data'=>$custom_fields->options,
 
 214       'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
 
 219 if ($request->isPost()) {
 
 220   if ($request->getParameter('btn_submit')) {
 
 222     // Validate user input.
 
 223     if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
 
 224       $err->add($i18n->getKey('error.client'));
 
 225     if ($custom_fields) {
 
 226       if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $err->add($i18n->getKey('error.field'), $custom_fields->fields[0]['label']);
 
 228     if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
 
 229       if (!$cl_project) $err->add($i18n->getKey('error.project'));
 
 231     if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
 
 232       if (!$cl_task) $err->add($i18n->getKey('error.task'));
 
 235       if ('0' == $cl_duration)
 
 236         $err->add($i18n->getKey('error.field'), $i18n->getKey('label.duration'));
 
 237       elseif ($cl_start || $cl_finish) {
 
 238         if (!ttTimeHelper::isValidTime($cl_start))
 
 239           $err->add($i18n->getKey('error.field'), $i18n->getKey('label.start'));
 
 241           if (!ttTimeHelper::isValidTime($cl_finish))
 
 242             $err->add($i18n->getKey('error.field'), $i18n->getKey('label.finish'));
 
 243           if (!ttTimeHelper::isValidInterval($cl_start, $cl_finish))
 
 244             $err->add($i18n->getKey('error.interval'), $i18n->getKey('label.finish'), $i18n->getKey('label.start'));
 
 247         if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
 
 248           $err->add($i18n->getKey('error.empty'), $i18n->getKey('label.start'));
 
 249           $err->add($i18n->getKey('error.empty'), $i18n->getKey('label.finish'));
 
 251         if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
 
 252           $err->add($i18n->getKey('error.empty'), $i18n->getKey('label.duration'));
 
 255       if (!ttTimeHelper::isValidDuration($cl_duration))
 
 256         $err->add($i18n->getKey('error.field'), $i18n->getKey('label.duration'));
 
 258     if (!ttValidString($cl_note, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.note'));
 
 259     // Finished validating user input.
 
 261     // Prohibit creating entries in future.
 
 262     if (defined('FUTURE_ENTRIES') && !isTrue(FUTURE_ENTRIES)) {
 
 263       $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
 
 264       if ($selected_date->after($browser_today))
 
 265         $err->add($i18n->getKey('error.future_date'));
 
 268     // Prohibit creating entries in locked range.
 
 269     if ($user->isDateLocked($selected_date))
 
 270       $err->add($i18n->getKey('error.range_locked'));
 
 272     // Prohibit creating another uncompleted record.
 
 274       if (($not_completed_rec = ttTimeHelper::getUncompleted($user->getActiveUser())) && (($cl_finish == '') && ($cl_duration == '')))
 
 275         $err->add($i18n->getKey('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->getKey('error.goto_uncompleted')."</a>");
 
 278     // Prohibit creating an overlapping record.
 
 280       if (ttTimeHelper::overlaps($user->getActiveUser(), $cl_date, $cl_start, $cl_finish))
 
 281         $err->add($i18n->getKey('error.overlap'));
 
 286       $id = ttTimeHelper::insert(array(
 
 288         'user_id' => $user->getActiveUser(),
 
 289         'client' => $cl_client,
 
 290         'project' => $cl_project,
 
 292         'start' => $cl_start,
 
 293         'finish' => $cl_finish,
 
 294         'duration' => $cl_duration,
 
 296         'billable' => $cl_billable));
 
 298       // Insert a custom field if we have it.
 
 300       if ($id && $custom_fields && $cl_cf_1) {
 
 301         if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
 
 302           $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
 
 303         elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
 
 304           $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
 
 306       if ($id && $result) {
 
 307         header('Location: time.php');
 
 310       $err->add($i18n->getKey('error.db'));
 
 312   } elseif ($request->getParameter('btn_stop')) {
 
 313     // Stop button pressed to finish an uncompleted record.
 
 314     $record_id = $request->getParameter('record_id');
 
 315     $record = ttTimeHelper::getRecord($record_id, $user->getActiveUser());
 
 316     $browser_date = $request->getParameter('browser_date');
 
 317     $browser_time = $request->getParameter('browser_time');
 
 319     // Can we complete this record?
 
 320     if ($record['date'] == $browser_date                                // closing today's record
 
 321       && ttTimeHelper::isValidInterval($record['start'], $browser_time) // finish time is greater than start time
 
 322       && !ttTimeHelper::overlaps($user->getActiveUser(), $browser_date, $record['start'], $browser_time)) { // no overlap
 
 323       $res = ttTimeHelper::update(array(
 
 325           'date'=>$record['date'],
 
 326           'user_id'=>$user->getActiveUser(),
 
 327           'client'=>$record['client_id'],
 
 328           'project'=>$record['project_id'],
 
 329           'task'=>$record['task_id'],
 
 330           'start'=>$record['start'],
 
 331           'finish'=>$browser_time,
 
 332           'note'=>$record['comment'],
 
 333           'billable'=>$record['billable']));
 
 335         $err->add($i18n->getKey('error.db'));
 
 337       // Cannot complete, redirect for manual edit.
 
 338       header('Location: time_edit.php?id='.$record_id);
 
 342   elseif ($request->getParameter('onBehalfUser')) {
 
 343     if($user->canManageTeam()) {
 
 344       unset($_SESSION['behalf_id']);
 
 345       unset($_SESSION['behalf_name']);
 
 347       if($on_behalf_id != $user->id) {
 
 348         $_SESSION['behalf_id'] = $on_behalf_id;
 
 349         $_SESSION['behalf_name'] = ttUserHelper::getUserName($on_behalf_id);
 
 351       header('Location: time.php');
 
 357 $week_total = ttTimeHelper::getTimeForWeek($user->getActiveUser(), $selected_date);
 
 359 $smarty->assign('week_total', $week_total);
 
 360 $smarty->assign('day_total', ttTimeHelper::getTimeForDay($user->getActiveUser(), $cl_date));
 
 361 $smarty->assign('time_records', ttTimeHelper::getRecords($user->getActiveUser(), $cl_date));
 
 362 $smarty->assign('client_list', $client_list);
 
 363 $smarty->assign('project_list', $project_list);
 
 364 $smarty->assign('task_list', $task_list);
 
 365 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
 
 366 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
 
 367 $smarty->assign('timestring', $selected_date->toString($user->date_format));
 
 368 $smarty->assign('title', $i18n->getKey('title.time'));
 
 369 $smarty->assign('content_page_name', 'time.tpl');
 
 370 $smarty->display('index.tpl');