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 // Use custom fields plugin if it is enabled.
 
  44 if (in_array('cf', explode(',', $user->plugins))) {
 
  45   require_once('../plugins/CustomFields.class.php');
 
  46   $custom_fields = new CustomFields($user->team_id);
 
  47   $smarty->assign('custom_fields', $custom_fields);
 
  50 $cl_id = $request->getParameter('id');
 
  52 // Get the time record we are editing.
 
  53 $time_rec = ttTimeHelper::getRecord($cl_id, $user->getActiveUser());
 
  55 // Prohibit editing invoiced records.
 
  56 if ($time_rec['invoice_id']) die($i18n->getKey('error.sys'));
 
  58 $item_date = new DateAndTime(DB_DATEFORMAT, $time_rec['date']);
 
  60 // Initialize variables.
 
  61 $cl_start = $cl_finish = $cl_duration = $cl_date = $cl_note = $cl_project = $cl_task = $cl_billable = null;
 
  62 if ($request->getMethod() == 'POST') {
 
  63   $cl_start = trim($request->getParameter('start'));
 
  64   $cl_finish = trim($request->getParameter('finish'));
 
  65   $cl_duration = trim($request->getParameter('duration'));
 
  66   $cl_date = $request->getParameter('date');
 
  67   $cl_note = trim($request->getParameter('note'));
 
  68   $cl_cf_1 = trim($request->getParameter('cf_1'));
 
  69   $cl_client = $request->getParameter('client');
 
  70   $cl_project = $request->getParameter('project');
 
  71   $cl_task = $request->getParameter('task');
 
  73   if (in_array('iv', explode(',', $user->plugins)))
 
  74     $cl_billable = $request->getParameter('billable');
 
  76   $cl_client = $time_rec['client_id'];
 
  77   $cl_project = $time_rec['project_id'];
 
  78   $cl_task = $time_rec['task_id'];
 
  79   $cl_start = $time_rec['start'];
 
  80   $cl_finish = $time_rec['finish'];
 
  81   $cl_duration = $time_rec['duration'];
 
  82   $cl_date      = $item_date->toString($user->date_format);    
 
  83   $cl_note = $time_rec['comment'];
 
  85   // If we have custom fields - obtain values for them.
 
  87     // Get custom field value for time record.
 
  88         $fields = $custom_fields->get($time_rec['id']);
 
  89         if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
 
  90       $cl_cf_1 = $fields[0]['value'];
 
  91     else if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
 
  92       $cl_cf_1 = $fields[0]['option_id'];
 
  95   $cl_billable = $time_rec['billable'];
 
  97   // Add an info message to the form if we are editing an uncompleted record.
 
  98   if (($cl_start == $cl_finish) && ($cl_duration == '0:00')) {
 
 101     $messages->add($i18n->getKey('form.time_edit.uncompleted'));
 
 105 // Initialize elements of 'timeRecordForm'.
 
 106 $form = new Form('timeRecordForm');
 
 108 // Dropdown for clients in MODE_TIME. Use all active clients.
 
 109 if (MODE_TIME == $user->tracking_mode && in_array('cl', explode(',', $user->plugins))) {
 
 110     $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
 
 111     $form->addInput(array('type'=>'combobox',
 
 112       'onchange'=>'fillProjectDropdown(this.value);',
 
 114       'style'=>'width: 250px;',
 
 116       'data'=>$active_clients,
 
 117       'datakeys'=>array('id', 'name'),
 
 118       'empty'=>array(''=>$i18n->getKey('dropdown.select'))
 
 120   // Note: in other modes the client list is filtered to relevant clients only. See below.
 
 123 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
 
 124   // Dropdown for projects assigned to user.
 
 125   $project_list = $user->getAssignedProjects();
 
 126   $form->addInput(array('type'=>'combobox',
 
 127     'onchange'=>'fillTaskDropdown(this.value);',
 
 129     'style'=>'width: 250px;',
 
 130     'value'=>$cl_project,
 
 131     'data'=>$project_list,
 
 132     'datakeys'=>array('id','name'),
 
 133     'empty'=>array(''=>$i18n->getKey('dropdown.select'))
 
 136   // Dropdown for clients if the clients plugin is enabled.
 
 137   if (in_array('cl', explode(',', $user->plugins))) {
 
 138     $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
 
 139     // We need an array of assigned project ids to do some trimming. 
 
 140     foreach($project_list as $project)
 
 141       $projects_assigned_to_user[] = $project['id'];
 
 143     // Build a client list out of active clients. Use only clients that are relevant to user.
 
 144     // Also trim their associated project list to only assigned projects (to user).
 
 145     foreach($active_clients as $client) {
 
 146           $projects_assigned_to_client = explode(',', $client['projects']);
 
 147           $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
 
 149             $client['projects'] = implode(',', $intersection);
 
 150             $client_list[] = $client;
 
 153     $form->addInput(array('type'=>'combobox',
 
 154       'onchange'=>'fillProjectDropdown(this.value);',
 
 156       'style'=>'width: 250px;',
 
 158       'data'=>$client_list,
 
 159       'datakeys'=>array('id', 'name'),
 
 160       'empty'=>array(''=>$i18n->getKey('dropdown.select'))
 
 165 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
 
 166   $task_list = ttTeamHelper::getActiveTasks($user->team_id);
 
 167   $form->addInput(array('type'=>'combobox',
 
 169     'style'=>'width: 250px;',
 
 172     'datakeys'=>array('id','name'),
 
 173     'empty'=>array(''=>$i18n->getKey('dropdown.select'))
 
 177 // Add other controls.
 
 178 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
 
 179   $form->addInput(array('type'=>'text','name'=>'start','value'=>$cl_start,'onchange'=>"formDisable('start');"));
 
 180   $form->addInput(array('type'=>'text','name'=>'finish','value'=>$cl_finish,'onchange'=>"formDisable('finish');"));
 
 182 if (!$user->canManageTeam() && defined('READONLY_START_FINISH') && isTrue(READONLY_START_FINISH)) {
 
 183   // Make the start and finish fields read-only.
 
 184   $form->getElement('start')->setEnable(false);
 
 185   $form->getElement('finish')->setEnable(false);        
 
 187 if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
 
 188   $form->addInput(array('type'=>'text','name'=>'duration','value'=>$cl_duration,'onchange'=>"formDisable('duration');"));
 
 189 $form->addInput(array('type'=>'datefield','name'=>'date','maxlength'=>'20','value'=>$cl_date));
 
 190 $form->addInput(array('type'=>'textarea','name'=>'note','style'=>'width: 250px; height: 60px;','value'=>$cl_note));
 
 191 // If we have custom fields - add controls for them.
 
 192 if ($custom_fields && $custom_fields->fields[0]) {
 
 193   // Only one custom field is supported at this time.
 
 194   if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT) {
 
 195         $form->addInput(array('type'=>'text','name'=>'cf_1','value'=>$cl_cf_1));
 
 196   } else if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
 
 197     $form->addInput(array('type'=>'combobox',
 
 199       'style'=>'width: 250px;',
 
 201       'data'=>$custom_fields->options,
 
 202       'empty' => array('' => $i18n->getKey('dropdown.select'))
 
 206 // Hidden control for record id.
 
 207 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$cl_id));
 
 208 if (in_array('iv', explode(',', $user->plugins)))
 
 209   $form->addInput(array('type'=>'checkbox','name'=>'billable','data'=>1,'value'=>$cl_billable));
 
 210 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_save click.
 
 211 $form->addInput(array('type'=>'submit','name'=>'btn_save','onclick'=>'browser_today.value=get_date()','value'=>$i18n->getKey('button.save')));
 
 212 $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->getKey('label.delete')));
 
 214 if ($request->getMethod() == 'POST') {
 
 216   // Validate user input.
 
 217   if (in_array('cl', explode(',', $user->plugins)) && in_array('cm', explode(',', $user->plugins)) && !$cl_client)
 
 218     $errors->add($i18n->getKey('error.client'));
 
 219   if ($custom_fields) {
 
 220     if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $errors->add($i18n->getKey('error.field'), $custom_fields->fields[0]['label']);
 
 222   if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
 
 223     if (!$cl_project) $errors->add($i18n->getKey('error.project'));     
 
 225   if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
 
 226     if (!$cl_task) $errors->add($i18n->getKey('error.task'));
 
 229     if ('0' == $cl_duration)
 
 230       $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.duration'));
 
 231     else if ($cl_start || $cl_finish) {
 
 232       if (!ttTimeHelper::isValidTime($cl_start))
 
 233         $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.start'));
 
 235         if (!ttTimeHelper::isValidTime($cl_finish))
 
 236           $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.finish'));
 
 237         if (!ttTimeHelper::isValidInterval($cl_start, $cl_finish))
 
 238           $errors->add($i18n->getKey('error.interval'), $i18n->getKey('label.finish'), $i18n->getKey('label.start'));
 
 241       if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
 
 242         $errors->add($i18n->getKey('error.empty'), $i18n->getKey('label.start'));
 
 243         $errors->add($i18n->getKey('error.empty'), $i18n->getKey('label.finish'));
 
 245       if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
 
 246         $errors->add($i18n->getKey('error.empty'), $i18n->getKey('label.duration'));
 
 249     if (!ttTimeHelper::isValidDuration($cl_duration))
 
 250       $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.duration'));
 
 252   if (!ttValidDate($cl_date)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.date'));
 
 253   if (!ttValidString($cl_note, true)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.note'));
 
 254   // Finished validating user input.
 
 256   // Determine lock date.
 
 257   $lock_interval = $user->lock_interval;
 
 259   if ($lock_interval > 0) {
 
 260     $lockdate = new DateAndTime();
 
 261     $lockdate->decDay($lock_interval);
 
 264   // This is a new date for the time record.
 
 265   $new_date = new DateAndTime($user->date_format, $cl_date);
 
 267   // Prohibit creating entries in future.
 
 268   if (defined('FUTURE_ENTRIES') && !isTrue(FUTURE_ENTRIES)) {
 
 269     $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
 
 270     if ($new_date->after($browser_today))
 
 271       $errors->add($i18n->getKey('error.future_date'));
 
 275   if ($request->getParameter('btn_save')) {
 
 277     // 1) Prohibit saving locked time entries in any form.
 
 278     // 2) Prohibit saving completed unlocked entries into locked interval.
 
 279     // 3) Prohibit saving uncompleted unlocked entries when another uncompleted entry exists.
 
 281     // Now, step by step.
 
 282     if ($errors->isEmpty()) {
 
 283       // 1) Prohibit saving locked time entries in any form.
 
 284       if($lockdate && $item_date->before($lockdate))
 
 285         $errors->add($i18n->getKey('error.period_locked'));        
 
 286       // 2) Prohibit saving completed unlocked entries into locked interval.
 
 287       if($errors->isEmpty() && $lockdate && $new_date->before($lockdate))
 
 288         $errors->add($i18n->getKey('error.period_locked'));        
 
 289       // 3) Prohibit saving uncompleted unlocked entries when another uncompleted entry exists.
 
 290       $uncompleted = ($cl_finish == '' && $cl_duration == '');
 
 292         $not_completed_rec = ttTimeHelper::getUncompleted($user->getActiveUser());
 
 293         if ($not_completed_rec && ($time_rec['id'] <> $not_completed_rec['id'])) {
 
 294           // We have another not completed record.
 
 295           $errors->add($i18n->getKey('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->getKey('error.goto_uncompleted')."</a>");
 
 300     // Prohibit creating an overlapping record.
 
 301     if ($errors->isEmpty()) {
 
 302       if (ttTimeHelper::overlaps($user->getActiveUser(), $new_date->toString(DB_DATEFORMAT), $cl_start, $cl_finish, $cl_id))
 
 303         $errors->add($i18n->getKey('error.overlap'));
 
 307     if ($errors->isEmpty()) {
 
 308       $res = ttTimeHelper::update(array(
 
 310           'date'=>$new_date->toString(DB_DATEFORMAT),
 
 311           'user_id'=>$user->getActiveUser(),
 
 312           'client'=>$cl_client,
 
 313           'project'=>$cl_project,
 
 316           'finish'=>$cl_finish,
 
 317           'duration'=>$cl_duration,
 
 319           'billable'=>$cl_billable));
 
 321       // If we have custom fields - update values.
 
 322       if ($res && $custom_fields) {
 
 323         if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
 
 324           $res = $custom_fields->update($cl_id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
 
 325         else if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
 
 326           $res = $custom_fields->update($cl_id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
 
 330         header('Location: time.php?date='.$new_date->toString(DB_DATEFORMAT));
 
 336   if ($request->getParameter('btn_delete')) {
 
 337     header("Location: time_delete.php?id=$cl_id");
 
 340 } // End of if ($request->getMethod() == 'POST')
 
 342 $smarty->assign('client_list', $client_list);
 
 343 $smarty->assign('project_list', $project_list);
 
 344 $smarty->assign('task_list', $task_list);
 
 345 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
 
 346 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
 
 347 $smarty->assign('title', $i18n->getKey('title.edit_time_record'));
 
 348 $smarty->assign('content_page_name', 'mobile/time_edit.tpl');
 
 349 $smarty->display('mobile/index.tpl');