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 (!ttAccessAllowed('track_own_time')) {
 
  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 ($user->isPluginEnabled('cf')) {
 
  55   require_once('../plugins/CustomFields.class.php');
 
  56   $custom_fields = new CustomFields($user->group_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->isPost() ? null : @$_SESSION['cf_1'])));
 
  65 $_SESSION['cf_1'] = $cl_cf_1;
 
  67 if ($user->isPluginEnabled('iv')) {
 
  68   if ($request->isPost()) {
 
  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('timeRecordForm');
 
  89 // Dropdown for clients in MODE_TIME. Use all active clients.
 
  90 if (MODE_TIME == $user->tracking_mode && $user->isPluginEnabled('cl')) {
 
  91     $active_clients = ttTeamHelper::getActiveClients($user->group_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->get('dropdown.select'))));
 
 101   // Note: in other modes the client list is filtered to relevant clients only. See below.
 
 104 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
 
 105   // Dropdown for projects assigned to user.
 
 106   $project_list = $user->getAssignedProjects();
 
 107   $form->addInput(array('type'=>'combobox',
 
 108     'onchange'=>'fillTaskDropdown(this.value);',
 
 110     'style'=>'width: 250px;',
 
 111     'enable'=>$enable_controls,
 
 112     'value'=>$cl_project,
 
 113     'data'=>$project_list,
 
 114     'datakeys'=>array('id','name'),
 
 115     'empty'=>array(''=>$i18n->get('dropdown.select'))));
 
 117   // Dropdown for clients if the clients plugin is enabled.
 
 118   if ($user->isPluginEnabled('cl')) {
 
 119     $active_clients = ttTeamHelper::getActiveClients($user->group_id, true);
 
 120     // We need an array of assigned project ids to do some trimming. 
 
 121     foreach($project_list as $project)
 
 122       $projects_assigned_to_user[] = $project['id'];
 
 124     // Build a client list out of active clients. Use only clients that are relevant to user.
 
 125     // Also trim their associated project list to only assigned projects (to user).
 
 126     foreach($active_clients as $client) {
 
 127       $projects_assigned_to_client = explode(',', $client['projects']);
 
 128       $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
 
 130         $client['projects'] = implode(',', $intersection);
 
 131         $client_list[] = $client;
 
 134     $form->addInput(array('type'=>'combobox',
 
 135       'onchange'=>'fillProjectDropdown(this.value);',
 
 137       'style'=>'width: 250px;',
 
 138       'enable'=>$enable_controls,
 
 140       'data'=>$client_list,
 
 141       'datakeys'=>array('id', 'name'),
 
 142       'empty'=>array(''=>$i18n->get('dropdown.select'))));
 
 146 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
 
 147   $task_list = ttTeamHelper::getActiveTasks($user->group_id);
 
 148   $form->addInput(array('type'=>'combobox',
 
 150     'style'=>'width: 250px;',
 
 151     'enable'=>$enable_controls,
 
 154     'datakeys'=>array('id','name'),
 
 155     'empty'=>array(''=>$i18n->get('dropdown.select'))));
 
 157 if ($user->isPluginEnabled('iv'))
 
 158   $form->addInput(array('type'=>'checkbox','name'=>'billable','value'=>$cl_billable,'enable'=>$enable_controls));
 
 159 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on button click.
 
 160 $form->addInput(array('type'=>'hidden','name'=>'browser_time','value'=>''));  // User current time, which gets filled in on button click.
 
 161 $enable_start = $uncompleted ? false : true;
 
 163   $form->addInput(array('type'=>'submit','name'=>'btn_start','onclick'=>'browser_time.value=get_time()','value'=>$i18n->get('label.start'),'enable'=>$enable_start));
 
 165   $form->addInput(array('type'=>'submit','name'=>'btn_stop','onclick'=>'browser_time.value=get_time()','value'=>$i18n->get('label.finish'),'enable'=>!$enable_start));
 
 167 // If we have custom fields - add controls for them.
 
 168 if ($custom_fields && $custom_fields->fields[0]) {
 
 169   // Only one custom field is supported at this time.
 
 170   if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT) {
 
 171     $form->addInput(array('type'=>'text','name'=>'cf_1','value'=>$cl_cf_1));
 
 172   } elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
 
 173     $form->addInput(array('type'=>'combobox','name'=>'cf_1',
 
 174       'style'=>'width: 250px;',
 
 176       'data'=>$custom_fields->options,
 
 177       'empty'=>array(''=>$i18n->get('dropdown.select'))
 
 183 if ($request->isPost()) {
 
 184   if ($request->getParameter('btn_start')) {
 
 185     // Start button clicked. We need to create a new uncompleted record with only the start time.
 
 188     // Validate user input.
 
 189     if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
 
 190       $err->add($i18n->get('error.client'));
 
 191     if ($custom_fields) {
 
 192       if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $err->add($i18n->get('error.field'), $custom_fields->fields[0]['label']);
 
 194     if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
 
 195       if (!$cl_project) $err->add($i18n->get('error.project'));
 
 197     if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
 
 198       if (!$cl_task) $err->add($i18n->get('error.task'));
 
 200     // Finished validating user input.
 
 202     // Prohibit creating entries in future.
 
 203     if (!$user->future_entries) {
 
 204       $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
 
 205       if ($selected_date->after($browser_today))
 
 206         $err->add($i18n->get('error.future_date'));
 
 209     // Prohibit creating time entries in locked interval.
 
 210     if ($user->isDateLocked($selected_date))
 
 211       $err->add($i18n->get('error.range_locked'));
 
 213     // Prohibit creating another uncompleted record.
 
 214     if ($err->no() && $uncompleted) {
 
 215       $err->add($i18n->get('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->get('error.goto_uncompleted')."</a>");
 
 218     // Prohibit creating an overlapping record.
 
 220       if (ttTimeHelper::overlaps($user->getActiveUser(), $cl_date, $cl_start, $cl_finish))
 
 221         $err->add($i18n->get('error.overlap'));
 
 225       $id = ttTimeHelper::insert(array(
 
 227         'user_id' => $user->getActiveUser(),
 
 228         'client' => $cl_client,
 
 229         'project' => $cl_project,
 
 231         'start' => $cl_start,
 
 232         'finish' => $cl_finish,
 
 233         'duration' => $cl_duration,
 
 235         'billable' => $cl_billable));
 
 237       // Insert a custom field if we have it.
 
 239       if ($id && $custom_fields && $cl_cf_1) {
 
 240         if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
 
 241           $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
 
 242         elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
 
 243           $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
 
 246       if ($id && $result) {
 
 247         header('Location: timer.php');
 
 250       $err->add($i18n->get('error.db'));
 
 253   if ($request->getParameter('btn_stop')) {
 
 254     // Stop button clicked. We need to finish an uncompleted record in progress.
 
 255     $record = ttTimeHelper::getRecord($uncompleted['id'], $user->getActiveUser());
 
 257     // Can we complete this record?
 
 258     if (ttTimeHelper::isValidInterval($record['start'], $cl_finish) // finish time is greater than start time
 
 259       && !ttTimeHelper::overlaps($user->getActiveUser(), $cl_date, $record['start'], $cl_finish)) { // no overlap
 
 260       $res = ttTimeHelper::update(array(
 
 263         'user_id'=>$user->getActiveUser(),
 
 264         'client'=>$record['client_id'],
 
 265         'project'=>$record['project_id'],
 
 266         'task'=>$record['task_id'],
 
 267         'start'=>$record['start'],
 
 268         'finish'=>$cl_finish,
 
 269         'note'=>$record['comment'],
 
 270         'billable'=>$record['billable']));
 
 272         header('Location: timer.php');
 
 275         $err->add($i18n->get('error.db'));
 
 277       // Cannot complete, redirect for manual edit.
 
 278       header('Location: time_edit.php?id='.$record['id']);
 
 284 $week_total = ttTimeHelper::getTimeForWeek($user->getActiveUser(), $cl_date);
 
 285 $smarty->assign('week_total', $week_total);
 
 287 $smarty->assign('uncompleted', $uncompleted);
 
 291 $smarty->assign('time_records', ttTimeHelper::getRecords($user->getActiveUser(), $cl_date));
 
 292 $smarty->assign('day_total', ttTimeHelper::getTimeForDay($user->getActiveUser(), $cl_date));
 
 293 $smarty->assign('client_list', $client_list);
 
 294 $smarty->assign('project_list', $project_list);
 
 295 $smarty->assign('task_list', $task_list);
 
 296 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
 
 297 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
 
 298 $smarty->assign('timestring', $selected_date->toString($user->date_format));
 
 299 $smarty->assign('title', $i18n->get('title.time'));
 
 300 $smarty->assign('content_page_name', 'mobile/timer.tpl');
 
 301 $smarty->display('mobile/index.tpl');