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');
 
  38 if (!ttAccessCheck(right_data_entry)) {
 
  39   header('Location: access_denied.php');
 
  43 // Initialize and store date in session.
 
  44 $cl_date  = $request->getParameter('date', @$_SESSION['date']);
 
  45 $selected_date = new DateAndTime(DB_DATEFORMAT, $cl_date);
 
  46 if($selected_date->isError())
 
  47   $selected_date = new DateAndTime(DB_DATEFORMAT);
 
  49   $cl_date = $selected_date->toString(DB_DATEFORMAT);
 
  50 $_SESSION['date'] = $cl_date;
 
  51 // TODO: for time page we may limit the day to today only.
 
  53 // Use custom fields plugin if it is enabled.
 
  54 if (in_array('cf', explode(',', $user->plugins))) {
 
  55   require_once('../plugins/CustomFields.class.php');
 
  56   $custom_fields = new CustomFields($user->team_id);
 
  57   $smarty->assign('custom_fields', $custom_fields);
 
  60 // Initialize variables.
 
  61 $cl_start = trim($request->getParameter('browser_time'));
 
  62 $cl_finish = trim($request->getParameter('browser_time'));
 
  64 $cl_cf_1 = trim($request->getParameter('cf_1', ($request->getMethod()=='POST'? null : @$_SESSION['cf_1'])));
 
  65 $_SESSION['cf_1'] = $cl_cf_1;
 
  67 if (in_array('iv', explode(',', $user->plugins))) {
 
  68   if ($request->getMethod() == 'POST') {
 
  69     $cl_billable = $request->getParameter('billable');
 
  70     $_SESSION['billable'] = (int) $cl_billable;
 
  72     if (isset($_SESSION['billable']))
 
  73       $cl_billable = $_SESSION['billable'];
 
  75 $cl_client = $request->getParameter('client', @$_SESSION['client']);
 
  76 $_SESSION['client'] = $cl_client;
 
  77 $cl_project = $request->getParameter('project', @$_SESSION['project']);
 
  78 $_SESSION['project'] = $cl_project;
 
  79 $cl_task = $request->getParameter('task', @$_SESSION['task']);
 
  80 $_SESSION['task'] = $cl_task;
 
  82 // Obtain uncompleted record. Assumtion is that only 1 uncompleted record is allowed.
 
  83 $uncompleted = ttTimeHelper::getUncompleted($user->getActiveUser());
 
  84 $enable_controls = ($uncompleted == null);
 
  86 // Elements of timeRecordForm.
 
  87 $form = new Form('timerRecordForm');
 
  89 // Dropdown for clients in MODE_TIME. Use all active clients.
 
  90 if (MODE_TIME == $user->tracking_mode && in_array('cl', explode(',', $user->plugins))) {
 
  91     $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
 
  92     $form->addInput(array('type'=>'combobox',
 
  93       'onchange'=>'fillProjectDropdown(this.value);',
 
  95       'style'=>'width: 250px;',
 
  96       'enable'=>$enable_controls,
 
  98       'data'=>$active_clients,
 
  99       'datakeys'=>array('id', 'name'),
 
 100       'empty'=>array(''=>$i18n->getKey('dropdown.select'))
 
 102   // Note: in other modes the client list is filtered to relevant clients only. See below.
 
 105 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
 
 106   // Dropdown for projects assigned to user.
 
 107   $project_list = $user->getAssignedProjects();
 
 108   $form->addInput(array('type'=>'combobox',
 
 109     'onchange'=>'fillTaskDropdown(this.value);',
 
 111     'style'=>'width: 250px;',
 
 112     'enable'=>$enable_controls,
 
 113     'value'=>$cl_project,
 
 114     'data'=>$project_list,
 
 115     'datakeys'=>array('id','name'),
 
 116     'empty'=>array(''=>$i18n->getKey('dropdown.select'))
 
 119   // Dropdown for clients if the clients plugin is enabled.
 
 120   if (in_array('cl', explode(',', $user->plugins))) {
 
 121     $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
 
 122     // We need an array of assigned project ids to do some trimming. 
 
 123     foreach($project_list as $project)
 
 124       $projects_assigned_to_user[] = $project['id'];
 
 126     // Build a client list out of active clients. Use only clients that are relevant to user.
 
 127     // Also trim their associated project list to only assigned projects (to user).
 
 128     foreach($active_clients as $client) {
 
 129           $projects_assigned_to_client = explode(',', $client['projects']);
 
 130           $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
 
 132             $client['projects'] = implode(',', $intersection);
 
 133             $client_list[] = $client;
 
 136     $form->addInput(array('type'=>'combobox',
 
 137       'onchange'=>'fillProjectDropdown(this.value);',
 
 139       'style'=>'width: 250px;',
 
 140       'enable'=>$enable_controls,
 
 142       'data'=>$client_list,
 
 143       'datakeys'=>array('id', 'name'),
 
 144       'empty'=>array(''=>$i18n->getKey('dropdown.select'))
 
 149 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
 
 150   $task_list = ttTeamHelper::getActiveTasks($user->team_id);
 
 151   $form->addInput(array('type'=>'combobox',
 
 153     'style'=>'width: 250px;',
 
 154     'enable'=>$enable_controls,
 
 157     'datakeys'=>array('id','name'),
 
 158     'empty'=>array(''=>$i18n->getKey('dropdown.select'))
 
 161 if (in_array('iv', explode(',', $user->plugins)))
 
 162   $form->addInput(array('type'=>'checkbox','name'=>'billable','data'=>1,'value'=>$cl_billable,'enable'=>$enable_controls));
 
 163 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on button click.
 
 164 $form->addInput(array('type'=>'hidden','name'=>'browser_time','value'=>''));  // User current time, which gets filled in on button click.
 
 165 $enable_start = $uncompleted ? false : true;
 
 166 $enable_stop = $uncompleted ? true : false;
 
 168   $form->addInput(array('type'=>'submit','name'=>'btn_start','onclick'=>'browser_time.value=get_time()','value'=>$i18n->getKey('label.start'),'enable'=>$enable_start));
 
 170   $form->addInput(array('type'=>'submit','name'=>'btn_stop','onclick'=>'browser_time.value=get_time()','value'=>$i18n->getKey('label.finish'),'enable'=>$enable_stop));
 
 172 // If we have custom fields - add controls for them.
 
 173 if ($custom_fields && $custom_fields->fields[0]) {
 
 174   // Only one custom field is supported at this time.
 
 175   if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT) {
 
 176     $form->addInput(array('type'=>'text','name'=>'cf_1','value'=>$cl_cf_1));
 
 177   } else if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
 
 178     $form->addInput(array('type'=>'combobox','name'=>'cf_1',
 
 179       'style'=>'width: 250px;',
 
 181       'data'=>$custom_fields->options,
 
 182       'empty'=>array(''=>$i18n->getKey('dropdown.select'))
 
 187 // Determine lock date. Time entries earlier than lock date cannot be created or modified. 
 
 188 $lock_interval = $user->lock_interval;
 
 190 if ($lock_interval > 0) {
 
 191   $lockdate = new DateAndTime();
 
 192   $lockdate->decDay($lock_interval);
 
 196 if ($request->getMethod() == 'POST') {
 
 197   if ($request->getParameter('btn_start')) {
 
 198     // Start button clicked. We need to create a new uncompleted record with only the start time.
 
 201     // Validate user input.
 
 202     if (in_array('cl', explode(',', $user->plugins)) && in_array('cm', explode(',', $user->plugins)) && !$cl_client)
 
 203       $errors->add($i18n->getKey('error.client'));
 
 204     if ($custom_fields) {
 
 205       if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $errors->add($i18n->getKey('error.field'), $custom_fields->fields[0]['label']);
 
 207     if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
 
 208       if (!$cl_project) $errors->add($i18n->getKey('error.project'));
 
 210     if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
 
 211       if (!$cl_task) $errors->add($i18n->getKey('error.task'));
 
 213     // Finished validating user input.
 
 215     // Prohibit creating entries in future.
 
 216     if (defined('FUTURE_ENTRIES') && !isTrue(FUTURE_ENTRIES)) {
 
 217       $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
 
 218       if ($selected_date->after($browser_today))
 
 219         $errors->add($i18n->getKey('error.future_date'));
 
 222     // Prohibit creating time entries in locked interval.
 
 223     if($lockdate && $selected_date->before($lockdate))
 
 224       $errors->add($i18n->getKey('error.period_locked'));
 
 226     // Prohibit creating another uncompleted record.
 
 227     if ($errors->isEmpty() && $uncompleted) {
 
 228       $errors->add($i18n->getKey('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->getKey('error.goto_uncompleted')."</a>");
 
 231     // Prohibit creating an overlapping record.
 
 232     if ($errors->isEmpty()) {
 
 233       if (ttTimeHelper::overlaps($user->getActiveUser(), $cl_date, $cl_start, $cl_finish))
 
 234         $errors->add($i18n->getKey('error.overlap'));
 
 237     if ($errors->isEmpty()) {
 
 238       $id = ttTimeHelper::insert(array(
 
 240         'user_id' => $user->getActiveUser(),
 
 241         'client' => $cl_client,
 
 242         'project' => $cl_project,
 
 244         'start' => $cl_start,
 
 245         'finish' => $cl_finish,
 
 246         'duration' => $cl_duration,
 
 248         'billable' => $cl_billable));
 
 250       // Insert a custom field if we have it.
 
 252       if ($id && $custom_fields && $cl_cf_1) {
 
 253         if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
 
 254           $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
 
 255         else if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
 
 256           $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
 
 259       if ($id && $result) {
 
 260         header('Location: timer.php');
 
 263       $errors->add($i18n->getKey('error.db'));
 
 266   if ($request->getParameter('btn_stop')) {
 
 267     // Stop button clicked. We need to finish an uncompleted record in progress.
 
 268     $record = ttTimeHelper::getRecord($uncompleted['id'], $user->getActiveUser());
 
 270     // Can we complete this record?
 
 271     if (ttTimeHelper::isValidInterval($record['start'], $cl_finish) // finish time is greater than start time
 
 272       && !ttTimeHelper::overlaps($user->getActiveUser(), $cl_date, $record['start'], $cl_finish)) { // no overlap
 
 273       $res = ttTimeHelper::update(array(
 
 276         'user_id'=>$user->getActiveUser(),
 
 277         'client'=>$record['client_id'],  
 
 278         'project'=>$record['project_id'],  
 
 279         'task'=>$record['task_id'],  
 
 280         'start'=>$record['start'],  
 
 281         'finish'=>$cl_finish,
 
 282         'note'=>$record['comment'],
 
 283         'billable'=>$record['billable']));
 
 285         header('Location: timer.php');
 
 288         $errors->add($i18n->getKey('error.db'));
 
 290       // Cannot complete, redirect for manual edit.
 
 291       header('Location: time_edit.php?id='.$record['id']);
 
 297 $week_total = ttTimeHelper::getTimeForWeek($user->getActiveUser(), $cl_date);
 
 298 $smarty->assign('week_total', $week_total);
 
 300 $smarty->assign('uncompleted', $uncompleted);
 
 304 $smarty->assign('time_records', ttTimeHelper::getRecords($user->getActiveUser(), $cl_date));
 
 305 $smarty->assign('day_total', ttTimeHelper::getTimeForDay($user->getActiveUser(), $cl_date));
 
 306 $smarty->assign('client_list', $client_list);
 
 307 $smarty->assign('project_list', $project_list);
 
 308 $smarty->assign('task_list', $task_list);
 
 309 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
 
 310 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
 
 311 $smarty->assign('timestring', $selected_date->toString($user->date_format));
 
 312 $smarty->assign('title', $i18n->getKey('title.time'));
 
 313 $smarty->assign('content_page_name', 'mobile/timer.tpl');
 
 314 $smarty->display('mobile/index.tpl');