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('ttGroupHelper');
 
  33 import('ttTeamHelper');
 
  34 import('ttClientHelper');
 
  35 import('ttTimeHelper');
 
  36 import('DateAndTime');
 
  39 if (!(ttAccessAllowed('track_own_time') || ttAccessAllowed('track_time'))) {
 
  40   header('Location: access_denied.php');
 
  43 if ($user->behalf_id && (!$user->can('track_time') || !$user->checkBehalfId())) {
 
  44   header('Location: access_denied.php'); // Trying on behalf, but no right or wrong user.
 
  47 if (!$user->behalf_id && !$user->can('track_own_time') && !$user->adjustBehalfId()) {
 
  48   header('Location: access_denied.php'); // Trying as self, but no right for self, and noone to work on behalf.
 
  51 if ($request->isPost()) {
 
  52   $groupChanged = $request->getParameter('group_changed'); // Reused in multiple places below.
 
  53   if ($groupChanged && !($user->can('manage_subgroups') && $user->isGroupValid($request->getParameter('group')))) {
 
  54     header('Location: access_denied.php'); // Group changed, but no rght or wrong group id.
 
  57   $userChanged = $request->getParameter('user_changed'); // Reused in multiple places below.
 
  58   if ($userChanged && !($user->can('track_time') && $user->isUserValid($request->getParameter('user')))) {
 
  59     header('Location: access_denied.php'); // Group changed, but no rght or wrong user id.
 
  63 // End of access checks.
 
  65 // Determine group for which we display this page.
 
  66 if ($request->isPost() && $groupChanged) {
 
  67   $group_id = $request->getParameter('group');
 
  68   $user->setOnBehalfGroup($group_id);
 
  70   $group_id = $user->getGroup();
 
  72 // Determine user for which we display this page.
 
  73 if ($request->isPost() && $userChanged) {
 
  74   $user_id = $request->getParameter('user');
 
  75   $user->setOnBehalfUser($user_id);
 
  77   $user_id = $user->getUser();
 
  80 // Initialize and store date in session.
 
  81 $cl_date = $request->getParameter('date', @$_SESSION['date']);
 
  82 $selected_date = new DateAndTime(DB_DATEFORMAT, $cl_date);
 
  83 if($selected_date->isError())
 
  84   $selected_date = new DateAndTime(DB_DATEFORMAT);
 
  86   $cl_date = $selected_date->toString(DB_DATEFORMAT);
 
  87 $_SESSION['date'] = $cl_date;
 
  89 // Use custom fields plugin if it is enabled.
 
  90 if ($user->isPluginEnabled('cf')) {
 
  91   require_once('plugins/CustomFields.class.php');
 
  92   $custom_fields = new CustomFields();
 
  93   $smarty->assign('custom_fields', $custom_fields);
 
  96 if ($user->isPluginEnabled('mq')){
 
  97   require_once('plugins/MonthlyQuota.class.php');
 
  98   $quota = new MonthlyQuota();
 
  99   $month_quota_minutes = $quota->get($selected_date->mYear, $selected_date->mMonth);
 
 100   $month_total = ttTimeHelper::getTimeForMonth($user_id, $selected_date);
 
 101   $minutes_left = $month_quota_minutes - ttTimeHelper::toMinutes($month_total);
 
 103   $smarty->assign('month_total', $month_total);
 
 104   $smarty->assign('over_quota', $minutes_left < 0);
 
 105   $smarty->assign('quota_remaining', ttTimeHelper::toAbsDuration($minutes_left));
 
 108 // Initialize variables.
 
 109 $cl_start = trim($request->getParameter('start'));
 
 110 $cl_finish = trim($request->getParameter('finish'));
 
 111 $cl_duration = trim($request->getParameter('duration'));
 
 112 $cl_note = trim($request->getParameter('note'));
 
 114 $cl_cf_1 = trim($request->getParameter('cf_1', ($request->isPost() ? null : @$_SESSION['cf_1'])));
 
 115 $_SESSION['cf_1'] = $cl_cf_1;
 
 117 if ($user->isPluginEnabled('iv')) {
 
 118   if ($request->isPost()) {
 
 119     $cl_billable = $request->getParameter('billable');
 
 120     $_SESSION['billable'] = (int) $cl_billable;
 
 122     if (isset($_SESSION['billable']))
 
 123       $cl_billable = $_SESSION['billable'];
 
 125 //$on_behalf_id = $request->getParameter('onBehalfUser', (isset($_SESSION['behalf_id'])? $_SESSION['behalf_id'] : $user->id));
 
 126 //$on_behalf_group_id = $request->getParameter('onBehalfGroup', (isset($_SESSION['behalf_group_id'])? $_SESSION['behalf_group_id'] : $user->group_id));
 
 127 $cl_client = $request->getParameter('client', ($request->isPost() ? null : @$_SESSION['client']));
 
 128 $_SESSION['client'] = $cl_client;
 
 129 $cl_project = $request->getParameter('project', ($request->isPost() ? null : @$_SESSION['project']));
 
 130 $_SESSION['project'] = $cl_project;
 
 131 $cl_task = $request->getParameter('task', ($request->isPost() ? null : @$_SESSION['task']));
 
 132 $_SESSION['task'] = $cl_task;
 
 134 // Elements of timeRecordForm.
 
 135 $form = new Form('timeRecordForm');
 
 137 if ($user->can('manage_subgroups')) {
 
 138   $groups = $user->getGroupsForDropdown();
 
 139   if (count($groups) > 1) {
 
 140     $form->addInput(array('type'=>'combobox',
 
 141       'onchange'=>'document.timeRecordForm.group_changed.value=1;document.timeRecordForm.submit();',
 
 143       'style'=>'width: 250px;',
 
 146       'datakeys'=>array('id','name')));
 
 147     $form->addInput(array('type'=>'hidden','name'=>'group_changed'));
 
 148     $smarty->assign('group_dropdown', 1);
 
 151 if ($user->can('track_time')) {
 
 152   $rank = $user->getMaxRankForGroup($group_id);
 
 153   if ($user->can('track_own_time'))
 
 154     $options = array('group_id'=>$group_id,'status'=>ACTIVE,'max_rank'=>$rank,'include_self'=>true,'self_first'=>true);
 
 156     $options = array('group_id'=>$group_id,'status'=>ACTIVE,'max_rank'=>$rank);
 
 157   $user_list = $user->getUsers($options);
 
 158   if (count($user_list) >= 1) {
 
 159     $form->addInput(array('type'=>'combobox',
 
 160       'onchange'=>'document.timeRecordForm.user_changed.value=1;document.timeRecordForm.submit();',
 
 162       'style'=>'width: 250px;',
 
 165       'datakeys'=>array('id','name')));
 
 166     $form->addInput(array('type'=>'hidden','name'=>'user_changed'));
 
 167     $smarty->assign('user_dropdown', 1);
 
 171 // Dropdown for clients in MODE_TIME. Use all active clients.
 
 172 if (MODE_TIME == $user->getTrackingMode() && $user->isPluginEnabled('cl')) {
 
 173   $active_clients = ttGroupHelper::getActiveClients(true);
 
 174   $form->addInput(array('type'=>'combobox',
 
 175     'onchange'=>'fillProjectDropdown(this.value);',
 
 177     'style'=>'width: 250px;',
 
 179     'data'=>$active_clients,
 
 180     'datakeys'=>array('id', 'name'),
 
 181     'empty'=>array(''=>$i18n->get('dropdown.select'))));
 
 182   // Note: in other modes the client list is filtered to relevant clients only. See below.
 
 185 if (MODE_PROJECTS == $user->getTrackingMode() || MODE_PROJECTS_AND_TASKS == $user->getTrackingMode()) {
 
 186   // Dropdown for projects assigned to user.
 
 187   $project_list = $user->getAssignedProjects();
 
 188   $form->addInput(array('type'=>'combobox',
 
 189     'onchange'=>'fillTaskDropdown(this.value);',
 
 191     'style'=>'width: 250px;',
 
 192     'value'=>$cl_project,
 
 193     'data'=>$project_list,
 
 194     'datakeys'=>array('id','name'),
 
 195     'empty'=>array(''=>$i18n->get('dropdown.select'))));
 
 197   // Dropdown for clients if the clients plugin is enabled.
 
 198   if ($user->isPluginEnabled('cl')) {
 
 199     $active_clients = ttGroupHelper::getActiveClients(true);
 
 200     // We need an array of assigned project ids to do some trimming.
 
 201     foreach($project_list as $project)
 
 202       $projects_assigned_to_user[] = $project['id'];
 
 204     // Build a client list out of active clients. Use only clients that are relevant to user.
 
 205     // Also trim their associated project list to only assigned projects (to user).
 
 206     foreach($active_clients as $client) {
 
 207       $projects_assigned_to_client = explode(',', $client['projects']);
 
 208       if (is_array($projects_assigned_to_client) && is_array($projects_assigned_to_user))
 
 209         $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
 
 211         $client['projects'] = implode(',', $intersection);
 
 212         $client_list[] = $client;
 
 215     $form->addInput(array('type'=>'combobox',
 
 216       'onchange'=>'fillProjectDropdown(this.value);',
 
 218       'style'=>'width: 250px;',
 
 220       'data'=>$client_list,
 
 221       'datakeys'=>array('id', 'name'),
 
 222       'empty'=>array(''=>$i18n->get('dropdown.select'))));
 
 226 if (MODE_PROJECTS_AND_TASKS == $user->getTrackingMode()) {
 
 227   $task_list = ttTeamHelper::getActiveTasks($group_id);
 
 228   $form->addInput(array('type'=>'combobox',
 
 230     'style'=>'width: 250px;',
 
 233     'datakeys'=>array('id','name'),
 
 234     'empty'=>array(''=>$i18n->get('dropdown.select'))));
 
 237 // Add other controls.
 
 238 if ((TYPE_START_FINISH == $user->getRecordType()) || (TYPE_ALL == $user->getRecordType())) {
 
 239   $form->addInput(array('type'=>'text','name'=>'start','value'=>$cl_start,'onchange'=>"formDisable('start');"));
 
 240   $form->addInput(array('type'=>'text','name'=>'finish','value'=>$cl_finish,'onchange'=>"formDisable('finish');"));
 
 241   if ($user->punch_mode && !$user->canOverridePunchMode()) {
 
 242     // Make the start and finish fields read-only.
 
 243     $form->getElement('start')->setEnabled(false);
 
 244     $form->getElement('finish')->setEnabled(false);
 
 247 if ((TYPE_DURATION == $user->getRecordType()) || (TYPE_ALL == $user->getRecordType()))
 
 248   $form->addInput(array('type'=>'text','name'=>'duration','value'=>$cl_duration,'onchange'=>"formDisable('duration');"));
 
 249 if (!defined('NOTE_INPUT_HEIGHT'))
 
 250   define('NOTE_INPUT_HEIGHT', 40);
 
 251 $form->addInput(array('type'=>'textarea','name'=>'note','style'=>'width: 600px; height:'.NOTE_INPUT_HEIGHT.'px;','value'=>$cl_note));
 
 252 $form->addInput(array('type'=>'calendar','name'=>'date','value'=>$cl_date)); // calendar
 
 253 if ($user->isPluginEnabled('iv'))
 
 254   $form->addInput(array('type'=>'checkbox','name'=>'billable','value'=>$cl_billable));
 
 255 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
 
 256 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->get('button.submit')));
 
 258 // If we have custom fields - add controls for them.
 
 259 if ($custom_fields && $custom_fields->fields[0]) {
 
 260   // Only one custom field is supported at this time.
 
 261   if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT) {
 
 262     $form->addInput(array('type'=>'text','name'=>'cf_1','value'=>$cl_cf_1));
 
 263   } elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
 
 264     $form->addInput(array('type'=>'combobox','name'=>'cf_1',
 
 265       'style'=>'width: 250px;',
 
 267       'data'=>$custom_fields->options,
 
 268       'empty'=>array(''=>$i18n->get('dropdown.select'))));
 
 273 if ($request->isPost()) {
 
 274   if ($request->getParameter('btn_submit')) {
 
 276     // Validate user input.
 
 277     if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
 
 278       $err->add($i18n->get('error.client'));
 
 279     if ($custom_fields) {
 
 280       if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $err->add($i18n->get('error.field'), $custom_fields->fields[0]['label']);
 
 282     if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
 
 283       if (!$cl_project) $err->add($i18n->get('error.project'));
 
 285     if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode && $user->task_required) {
 
 286       if (!$cl_task) $err->add($i18n->get('error.task'));
 
 288     if (strlen($cl_duration) == 0) {
 
 289       if ($cl_start || $cl_finish) {
 
 290         if (!ttTimeHelper::isValidTime($cl_start))
 
 291           $err->add($i18n->get('error.field'), $i18n->get('label.start'));
 
 293           if (!ttTimeHelper::isValidTime($cl_finish))
 
 294             $err->add($i18n->get('error.field'), $i18n->get('label.finish'));
 
 295           if (!ttTimeHelper::isValidInterval($cl_start, $cl_finish))
 
 296             $err->add($i18n->get('error.interval'), $i18n->get('label.finish'), $i18n->get('label.start'));
 
 299         if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
 
 300           $err->add($i18n->get('error.empty'), $i18n->get('label.start'));
 
 301           $err->add($i18n->get('error.empty'), $i18n->get('label.finish'));
 
 303         if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
 
 304           $err->add($i18n->get('error.empty'), $i18n->get('label.duration'));
 
 307       if (false === ttTimeHelper::postedDurationToMinutes($cl_duration))
 
 308         $err->add($i18n->get('error.field'), $i18n->get('label.duration'));
 
 310     if (!ttValidString($cl_note, true)) $err->add($i18n->get('error.field'), $i18n->get('label.note'));
 
 311     // Finished validating user input.
 
 313     // Prohibit creating entries in future.
 
 314     if (!$user->future_entries) {
 
 315       $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
 
 316       if ($selected_date->after($browser_today))
 
 317         $err->add($i18n->get('error.future_date'));
 
 320     // Prohibit creating entries in locked range.
 
 321     if ($user->isDateLocked($selected_date))
 
 322       $err->add($i18n->get('error.range_locked'));
 
 324     // Prohibit creating another uncompleted record.
 
 326       if (($not_completed_rec = ttTimeHelper::getUncompleted($user_id)) && (($cl_finish == '') && ($cl_duration == '')))
 
 327         $err->add($i18n->get('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->get('error.goto_uncompleted')."</a>");
 
 330     // Prohibit creating an overlapping record.
 
 332       if (ttTimeHelper::overlaps($user_id, $cl_date, $cl_start, $cl_finish))
 
 333         $err->add($i18n->get('error.overlap'));
 
 338       $id = ttTimeHelper::insert(array(
 
 340         'user_id' => $user_id,
 
 341         'group_id' => $group_id,
 
 342         'org_id' => $user->org_id,
 
 343         'client' => $cl_client,
 
 344         'project' => $cl_project,
 
 346         'start' => $cl_start,
 
 347         'finish' => $cl_finish,
 
 348         'duration' => $cl_duration,
 
 350         'billable' => $cl_billable));
 
 352       // Insert a custom field if we have it.
 
 354       if ($id && $custom_fields && $cl_cf_1) {
 
 355         if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
 
 356           $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
 
 357         elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
 
 358           $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
 
 360       if ($id && $result) {
 
 361         header('Location: time.php');
 
 364       $err->add($i18n->get('error.db'));
 
 366   } elseif ($request->getParameter('btn_stop')) {
 
 367     // Stop button pressed to finish an uncompleted record.
 
 368     $record_id = $request->getParameter('record_id');
 
 369     $record = ttTimeHelper::getRecord($record_id, $user_id);
 
 370     $browser_date = $request->getParameter('browser_date');
 
 371     $browser_time = $request->getParameter('browser_time');
 
 373     // Can we complete this record?
 
 374     if ($record['date'] == $browser_date                                // closing today's record
 
 375       && ttTimeHelper::isValidInterval($record['start'], $browser_time) // finish time is greater than start time
 
 376       && !ttTimeHelper::overlaps($user_id, $browser_date, $record['start'], $browser_time)) { // no overlap
 
 377       $res = ttTimeHelper::update(array(
 
 379           'date'=>$record['date'],
 
 381           'client'=>$record['client_id'],
 
 382           'project'=>$record['project_id'],
 
 383           'task'=>$record['task_id'],
 
 384           'start'=>$record['start'],
 
 385           'finish'=>$browser_time,
 
 386           'note'=>$record['comment'],
 
 387           'billable'=>$record['billable']));
 
 389         $err->add($i18n->get('error.db'));
 
 391       // Cannot complete, redirect for manual edit.
 
 392       header('Location: time_edit.php?id='.$record_id);
 
 398 $week_total = ttTimeHelper::getTimeForWeek($user_id, $selected_date);
 
 400 $smarty->assign('selected_date', $selected_date);
 
 401 $smarty->assign('week_total', $week_total);
 
 402 $smarty->assign('day_total', ttTimeHelper::getTimeForDay($user_id, $cl_date));
 
 403 $smarty->assign('time_records', ttTimeHelper::getRecords($user_id, $cl_date));
 
 404 $smarty->assign('client_list', $client_list);
 
 405 $smarty->assign('project_list', $project_list);
 
 406 $smarty->assign('task_list', $task_list);
 
 407 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
 
 408 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
 
 409 $smarty->assign('timestring', $selected_date->toString($user->date_format));
 
 410 $smarty->assign('title', $i18n->get('title.time'));
 
 411 $smarty->assign('content_page_name', 'time.tpl');
 
 412 $smarty->display('index.tpl');