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('ttGroupHelper');
 
  34 import('ttClientHelper');
 
  35 import('ttTimeHelper');
 
  36 import('DateAndTime');
 
  39 if (!ttAccessAllowed('track_own_time')) {
 
  40   header('Location: access_denied.php');
 
  44 // Initialize and store date in session.
 
  45 $cl_date  = $request->getParameter('date', @$_SESSION['date']);
 
  46 $selected_date = new DateAndTime(DB_DATEFORMAT, $cl_date);
 
  47 if($selected_date->isError())
 
  48   $selected_date = new DateAndTime(DB_DATEFORMAT);
 
  50   $cl_date = $selected_date->toString(DB_DATEFORMAT);
 
  51 $_SESSION['date'] = $cl_date;
 
  52 // TODO: for time page we may limit the day to today only.
 
  54 // Use custom fields plugin if it is enabled.
 
  55 if ($user->isPluginEnabled('cf')) {
 
  56   require_once('../plugins/CustomFields.class.php');
 
  57   $custom_fields = new CustomFields();
 
  58   $smarty->assign('custom_fields', $custom_fields);
 
  61 // Initialize variables.
 
  62 $cl_start = trim($request->getParameter('browser_time'));
 
  63 $cl_finish = trim($request->getParameter('browser_time'));
 
  65 $cl_cf_1 = trim($request->getParameter('cf_1', ($request->isPost() ? null : @$_SESSION['cf_1'])));
 
  66 $_SESSION['cf_1'] = $cl_cf_1;
 
  68 if ($user->isPluginEnabled('iv')) {
 
  69   if ($request->isPost()) {
 
  70     $cl_billable = $request->getParameter('billable');
 
  71     $_SESSION['billable'] = (int) $cl_billable;
 
  73     if (isset($_SESSION['billable']))
 
  74       $cl_billable = $_SESSION['billable'];
 
  76 $cl_client = $request->getParameter('client', @$_SESSION['client']);
 
  77 $_SESSION['client'] = $cl_client;
 
  78 $cl_project = $request->getParameter('project', @$_SESSION['project']);
 
  79 $_SESSION['project'] = $cl_project;
 
  80 $cl_task = $request->getParameter('task', @$_SESSION['task']);
 
  81 $_SESSION['task'] = $cl_task;
 
  83 // Obtain uncompleted record. Assumtion is that only 1 uncompleted record is allowed.
 
  84 $uncompleted = ttTimeHelper::getUncompleted($user->getUser());
 
  85 $enable_controls = ($uncompleted == null);
 
  87 // Elements of timeRecordForm.
 
  88 $form = new Form('timeRecordForm');
 
  90 // Dropdown for clients in MODE_TIME. Use all active clients.
 
  91 if (MODE_TIME == $user->tracking_mode && $user->isPluginEnabled('cl')) {
 
  92     $active_clients = ttGroupHelper::getActiveClients(true);
 
  93     $form->addInput(array('type'=>'combobox',
 
  94       'onchange'=>'fillProjectDropdown(this.value);',
 
  96       'style'=>'width: 250px;',
 
  97       'enable'=>$enable_controls,
 
  99       'data'=>$active_clients,
 
 100       'datakeys'=>array('id', 'name'),
 
 101       'empty'=>array(''=>$i18n->get('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->get('dropdown.select'))));
 
 118   // Dropdown for clients if the clients plugin is enabled.
 
 119   if ($user->isPluginEnabled('cl')) {
 
 120     $active_clients = ttGroupHelper::getActiveClients(true);
 
 121     // We need an array of assigned project ids to do some trimming. 
 
 122     foreach($project_list as $project)
 
 123       $projects_assigned_to_user[] = $project['id'];
 
 125     // Build a client list out of active clients. Use only clients that are relevant to user.
 
 126     // Also trim their associated project list to only assigned projects (to user).
 
 127     foreach($active_clients as $client) {
 
 128       $projects_assigned_to_client = explode(',', $client['projects']);
 
 129       $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
 
 131         $client['projects'] = implode(',', $intersection);
 
 132         $client_list[] = $client;
 
 135     $form->addInput(array('type'=>'combobox',
 
 136       'onchange'=>'fillProjectDropdown(this.value);',
 
 138       'style'=>'width: 250px;',
 
 139       'enable'=>$enable_controls,
 
 141       'data'=>$client_list,
 
 142       'datakeys'=>array('id', 'name'),
 
 143       'empty'=>array(''=>$i18n->get('dropdown.select'))));
 
 147 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
 
 148   $task_list = ttTeamHelper::getActiveTasks($user->group_id);
 
 149   $form->addInput(array('type'=>'combobox',
 
 151     'style'=>'width: 250px;',
 
 152     'enable'=>$enable_controls,
 
 155     'datakeys'=>array('id','name'),
 
 156     'empty'=>array(''=>$i18n->get('dropdown.select'))));
 
 158 if ($user->isPluginEnabled('iv'))
 
 159   $form->addInput(array('type'=>'checkbox','name'=>'billable','value'=>$cl_billable,'enable'=>$enable_controls));
 
 160 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on button click.
 
 161 $form->addInput(array('type'=>'hidden','name'=>'browser_time','value'=>''));  // User current time, which gets filled in on button click.
 
 162 $enable_start = $uncompleted ? false : true;
 
 164   $form->addInput(array('type'=>'submit','name'=>'btn_start','onclick'=>'browser_time.value=get_time()','value'=>$i18n->get('label.start'),'enable'=>$enable_start));
 
 166   $form->addInput(array('type'=>'submit','name'=>'btn_stop','onclick'=>'browser_time.value=get_time()','value'=>$i18n->get('label.finish'),'enable'=>!$enable_start));
 
 168 // If we have custom fields - add controls for them.
 
 169 if ($custom_fields && $custom_fields->fields[0]) {
 
 170   // Only one custom field is supported at this time.
 
 171   if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT) {
 
 172     $form->addInput(array('type'=>'text','name'=>'cf_1','value'=>$cl_cf_1));
 
 173   } elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
 
 174     $form->addInput(array('type'=>'combobox','name'=>'cf_1',
 
 175       'style'=>'width: 250px;',
 
 177       'data'=>$custom_fields->options,
 
 178       'empty'=>array(''=>$i18n->get('dropdown.select'))
 
 184 if ($request->isPost()) {
 
 185   if ($request->getParameter('btn_start')) {
 
 186     // Start button clicked. We need to create a new uncompleted record with only the start time.
 
 189     // Validate user input.
 
 190     if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
 
 191       $err->add($i18n->get('error.client'));
 
 192     if ($custom_fields) {
 
 193       if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $err->add($i18n->get('error.field'), $custom_fields->fields[0]['label']);
 
 195     if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
 
 196       if (!$cl_project) $err->add($i18n->get('error.project'));
 
 198     if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
 
 199       if (!$cl_task) $err->add($i18n->get('error.task'));
 
 201     // Finished validating user input.
 
 203     // Prohibit creating entries in future.
 
 204     if (!$user->future_entries) {
 
 205       $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
 
 206       if ($selected_date->after($browser_today))
 
 207         $err->add($i18n->get('error.future_date'));
 
 210     // Prohibit creating time entries in locked interval.
 
 211     if ($user->isDateLocked($selected_date))
 
 212       $err->add($i18n->get('error.range_locked'));
 
 214     // Prohibit creating another uncompleted record.
 
 215     if ($err->no() && $uncompleted) {
 
 216       $err->add($i18n->get('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->get('error.goto_uncompleted')."</a>");
 
 219     // Prohibit creating an overlapping record.
 
 221       if (ttTimeHelper::overlaps($user->getUser(), $cl_date, $cl_start, $cl_finish))
 
 222         $err->add($i18n->get('error.overlap'));
 
 226       $id = ttTimeHelper::insert(array(
 
 228         'user_id' => $user->getUser(),
 
 229         'group_id' => $user->getGroup(),
 
 230         'org_id' => $user->org_id,
 
 231         'client' => $cl_client,
 
 232         'project' => $cl_project,
 
 234         'start' => $cl_start,
 
 235         'finish' => $cl_finish,
 
 236         'duration' => $cl_duration,
 
 238         'billable' => $cl_billable));
 
 240       // Insert a custom field if we have it.
 
 242       if ($id && $custom_fields && $cl_cf_1) {
 
 243         if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
 
 244           $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
 
 245         elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
 
 246           $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
 
 249       if ($id && $result) {
 
 250         header('Location: timer.php');
 
 253       $err->add($i18n->get('error.db'));
 
 256   if ($request->getParameter('btn_stop')) {
 
 257     // Stop button clicked. We need to finish an uncompleted record in progress.
 
 258     $record = ttTimeHelper::getRecord($uncompleted['id'], $user->getUser());
 
 260     // Can we complete this record?
 
 261     if (ttTimeHelper::isValidInterval($record['start'], $cl_finish) // finish time is greater than start time
 
 262       && !ttTimeHelper::overlaps($user->getUser(), $cl_date, $record['start'], $cl_finish)) { // no overlap
 
 263       $res = ttTimeHelper::update(array(
 
 266         'user_id'=>$user->getUser(),
 
 267         'client'=>$record['client_id'],
 
 268         'project'=>$record['project_id'],
 
 269         'task'=>$record['task_id'],
 
 270         'start'=>$record['start'],
 
 271         'finish'=>$cl_finish,
 
 272         'note'=>$record['comment'],
 
 273         'billable'=>$record['billable']));
 
 275         header('Location: timer.php');
 
 278         $err->add($i18n->get('error.db'));
 
 280       // Cannot complete, redirect for manual edit.
 
 281       header('Location: time_edit.php?id='.$record['id']);
 
 287 $week_total = ttTimeHelper::getTimeForWeek($user->getUser(), $cl_date);
 
 288 $smarty->assign('week_total', $week_total);
 
 290 $smarty->assign('uncompleted', $uncompleted);
 
 294 $smarty->assign('time_records', ttTimeHelper::getRecords($user->getUser(), $cl_date));
 
 295 $smarty->assign('day_total', ttTimeHelper::getTimeForDay($user->getUser(), $cl_date));
 
 296 $smarty->assign('client_list', $client_list);
 
 297 $smarty->assign('project_list', $project_list);
 
 298 $smarty->assign('task_list', $task_list);
 
 299 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
 
 300 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
 
 301 $smarty->assign('timestring', $selected_date->toString($user->date_format));
 
 302 $smarty->assign('title', $i18n->get('title.time'));
 
 303 $smarty->assign('content_page_name', 'mobile/timer.tpl');
 
 304 $smarty->display('mobile/index.tpl');