Refactoring, finished replacing with ttUser::isPluginEnabled().
[timetracker.git] / mobile / time.php
1 <?php
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.
10 // |
11 // | There are only two ways to violate the license:
12 // |
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).
16 // |
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).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 require_once('../initialize.php');
30 import('form.Form');
31 import('ttUserHelper');
32 import('ttTeamHelper');
33 import('ttClientHelper');
34 import('ttTimeHelper');
35 import('DateAndTime');
36
37 // Access check.
38 if (!ttAccessCheck(right_data_entry)) {
39   header('Location: access_denied.php');
40   exit();
41 }
42
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);
48 if(!$cl_date)
49   $cl_date = $selected_date->toString(DB_DATEFORMAT);
50 $_SESSION['date'] = $cl_date;
51
52 // Determine previous and next dates for simple navigation.
53 $prev_date = date('Y-m-d', strtotime('-1 day', strtotime($cl_date)));
54 $next_date = date('Y-m-d', strtotime('+1 day', strtotime($cl_date)));
55
56 // Use custom fields plugin if it is enabled.
57 if ($user->isPluginEnabled('cf')) {
58   require_once('../plugins/CustomFields.class.php');
59   $custom_fields = new CustomFields($user->team_id);
60   $smarty->assign('custom_fields', $custom_fields);
61 }
62
63 // Initialize variables.
64 $cl_start = trim($request->getParameter('start'));
65 $cl_finish = trim($request->getParameter('finish'));
66 $cl_duration = trim($request->getParameter('duration'));
67 $cl_note = trim($request->getParameter('note'));
68 // Custom field.
69 $cl_cf_1 = trim($request->getParameter('cf_1', ($request->getMethod()=='POST'? null : @$_SESSION['cf_1'])));
70 $_SESSION['cf_1'] = $cl_cf_1;
71 $cl_billable = 1;
72 if ($user->isPluginEnabled('iv')) {
73   if ($request->isPost()) {
74     $cl_billable = $request->getParameter('billable');
75     $_SESSION['billable'] = (int) $cl_billable;
76   } else 
77     if (isset($_SESSION['billable']))
78       $cl_billable = $_SESSION['billable'];
79 }
80 $cl_client = $request->getParameter('client', ($request->getMethod()=='POST'? null : @$_SESSION['client']));
81 $_SESSION['client'] = $cl_client;
82 $cl_project = $request->getParameter('project', ($request->getMethod()=='POST'? null : @$_SESSION['project']));
83 $_SESSION['project'] = $cl_project;
84 $cl_task = $request->getParameter('task', ($request->getMethod()=='POST'? null : @$_SESSION['task']));
85 $_SESSION['task'] = $cl_task;
86
87 // Elements of timeRecordForm.
88 $form = new Form('timeRecordForm');
89
90 // Dropdown for clients in MODE_TIME. Use all active clients.
91 if (MODE_TIME == $user->tracking_mode && $user->isPluginEnabled('cl')) {
92     $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
93     $form->addInput(array('type'=>'combobox',
94       'onchange'=>'fillProjectDropdown(this.value);',
95       'name'=>'client',
96       'style'=>'width: 250px;',
97       'value'=>$cl_client,
98       'data'=>$active_clients,
99       'datakeys'=>array('id', 'name'),
100       'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
101   // Note: in other modes the client list is filtered to relevant clients only. See below.
102 }
103
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);',
109     'name'=>'project',
110     'style'=>'width: 250px;',
111     'value'=>$cl_project,
112     'data'=>$project_list,
113     'datakeys'=>array('id','name'),
114     'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
115
116   // Dropdown for clients if the clients plugin is enabled.
117   if ($user->isPluginEnabled('cl')) {
118     $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
119     // We need an array of assigned project ids to do some trimming. 
120     foreach($project_list as $project)
121       $projects_assigned_to_user[] = $project['id'];
122
123     // Build a client list out of active clients. Use only clients that are relevant to user.
124     // Also trim their associated project list to only assigned projects (to user).
125     foreach($active_clients as $client) {
126       $projects_assigned_to_client = explode(',', $client['projects']);
127       if (is_array($projects_assigned_to_client) && is_array($projects_assigned_to_user))
128         $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
129       if ($intersection) {
130         $client['projects'] = implode(',', $intersection);
131         $client_list[] = $client;
132       }
133     }
134     $form->addInput(array('type'=>'combobox',
135       'onchange'=>'fillProjectDropdown(this.value);',
136       'name'=>'client',
137       'style'=>'width: 250px;',
138       'value'=>$cl_client,
139       'data'=>$client_list,
140       'datakeys'=>array('id', 'name'),
141       'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
142   }
143 }
144
145 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
146   $task_list = ttTeamHelper::getActiveTasks($user->team_id);
147   $form->addInput(array('type'=>'combobox',
148     'name'=>'task',
149     'style'=>'width: 250px;',
150     'value'=>$cl_task,
151     'data'=>$task_list,
152     'datakeys'=>array('id','name'),
153     'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
154 }
155 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
156   $form->addInput(array('type'=>'text','name'=>'start','value'=>$cl_start,'onchange'=>"formDisable('start');"));
157   $form->addInput(array('type'=>'text','name'=>'finish','value'=>$cl_finish,'onchange'=>"formDisable('finish');"));
158 }
159 if (!$user->canManageTeam() && defined('READONLY_START_FINISH') && isTrue(READONLY_START_FINISH)) {
160   // Make the start and finish fields read-only.
161   $form->getElement('start')->setEnable(false);
162   $form->getElement('finish')->setEnable(false);
163 }
164 if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
165   $form->addInput(array('type'=>'text','name'=>'duration','value'=>$cl_duration,'onchange'=>"formDisable('duration');"));
166 $form->addInput(array('type'=>'textarea','name'=>'note','style'=>'width: 250px; height: 60px;','value'=>$cl_note));
167 if ($user->isPluginEnabled('iv'))
168   $form->addInput(array('type'=>'checkbox','name'=>'billable','data'=>1,'value'=>$cl_billable));
169 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
170 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->getKey('button.submit')));
171
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   } elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
178     $form->addInput(array('type'=>'combobox','name'=>'cf_1',
179       'style'=>'width: 250px;',
180       'value'=>$cl_cf_1,
181       'data'=>$custom_fields->options,
182       'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
183   }
184 }
185
186 // Determine lock date. Time entries earlier than lock date cannot be created or modified. 
187 $lock_interval = $user->lock_interval;
188 $lockdate = 0;
189 if ($lock_interval > 0) {
190   $lockdate = new DateAndTime();
191   $lockdate->decDay($lock_interval);
192 }
193
194 // Submit.
195 if ($request->isPost()) {
196   if ($request->getParameter('btn_submit')) {
197
198     // Validate user input.
199     if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
200       $err->add($i18n->getKey('error.client'));
201     if ($custom_fields) {
202       if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $err->add($i18n->getKey('error.field'), $custom_fields->fields[0]['label']);
203     }
204     if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
205       if (!$cl_project) $err->add($i18n->getKey('error.project'));
206     }
207     if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
208       if (!$cl_task) $err->add($i18n->getKey('error.task'));
209     }
210     if (!$cl_duration) {
211       if ('0' == $cl_duration)
212         $err->add($i18n->getKey('error.field'), $i18n->getKey('label.duration'));
213       elseif ($cl_start || $cl_finish) {
214         if (!ttTimeHelper::isValidTime($cl_start))
215           $err->add($i18n->getKey('error.field'), $i18n->getKey('label.start'));
216         if ($cl_finish) {
217           if (!ttTimeHelper::isValidTime($cl_finish))
218             $err->add($i18n->getKey('error.field'), $i18n->getKey('label.finish'));
219           if (!ttTimeHelper::isValidInterval($cl_start, $cl_finish))
220             $err->add($i18n->getKey('error.interval'), $i18n->getKey('label.finish'), $i18n->getKey('label.start'));
221         }
222       } else {
223         if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
224           $err->add($i18n->getKey('error.empty'), $i18n->getKey('label.start'));
225           $err->add($i18n->getKey('error.empty'), $i18n->getKey('label.finish'));
226         }
227         if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
228           $err->add($i18n->getKey('error.empty'), $i18n->getKey('label.duration'));
229       }
230     } else {
231       if (!ttTimeHelper::isValidDuration($cl_duration))
232         $err->add($i18n->getKey('error.field'), $i18n->getKey('label.duration'));
233     }
234     if (!ttValidString($cl_note, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.note'));
235     // Finished validating user input.
236
237     // Prohibit creating entries in future.
238     if (defined('FUTURE_ENTRIES') && !isTrue(FUTURE_ENTRIES)) {
239       $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
240       if ($selected_date->after($browser_today))
241         $err->add($i18n->getKey('error.future_date'));
242     }
243
244     // Prohibit creating time entries in locked interval.
245     if($lockdate && $selected_date->before($lockdate))
246       $err->add($i18n->getKey('error.period_locked'));
247
248     // Prohibit creating another uncompleted record.
249     if ($err->no()) {
250       if (($not_completed_rec = ttTimeHelper::getUncompleted($user->getActiveUser())) && (($cl_finish == '') && ($cl_duration == '')))
251         $err->add($i18n->getKey('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->getKey('error.goto_uncompleted')."</a>");
252     }
253
254     // Prohibit creating an overlapping record.
255     if ($err->no()) {
256       if (ttTimeHelper::overlaps($user->getActiveUser(), $cl_date, $cl_start, $cl_finish))
257         $err->add($i18n->getKey('error.overlap'));
258     }
259
260     if ($err->no()) {
261       $id = ttTimeHelper::insert(array(
262         'date' => $cl_date,
263         'user_id' => $user->getActiveUser(),
264         'client' => $cl_client,
265         'project' => $cl_project,
266         'task' => $cl_task,
267         'start' => $cl_start,
268         'finish' => $cl_finish,
269         'duration' => $cl_duration,
270         'note' => $cl_note,
271         'billable' => $cl_billable));
272
273       // Insert a custom field if we have it.
274       $result = true;
275       if ($id && $custom_fields && $cl_cf_1) {
276         if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
277           $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
278         elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
279           $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
280       }
281
282       if ($id && $result) {
283         header('Location: time.php');
284         exit();
285       }
286       $err->add($i18n->getKey('error.db'));
287     }
288   }
289 } // isPost
290
291 $smarty->assign('next_date', $next_date);
292 $smarty->assign('prev_date', $prev_date);
293 $smarty->assign('time_records', ttTimeHelper::getRecords($user->getActiveUser(), $cl_date));
294 $smarty->assign('day_total', ttTimeHelper::getTimeForDay($user->getActiveUser(), $cl_date));
295 $smarty->assign('client_list', $client_list);
296 $smarty->assign('project_list', $project_list);
297 $smarty->assign('task_list', $task_list);
298 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
299 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
300 $smarty->assign('timestring', $selected_date->toString($user->date_format));
301 $smarty->assign('title', $i18n->getKey('title.time'));
302 $smarty->assign('content_page_name', 'mobile/time.tpl');
303 $smarty->display('mobile/index.tpl');