Refactoring. Renamed team_id fields to become group_id.
[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 checks.
38 if (!ttAccessAllowed('track_own_time')) {
39   header('Location: access_denied.php');
40   exit();
41 }
42 // End of access checks.
43
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);
49 if(!$cl_date)
50   $cl_date = $selected_date->toString(DB_DATEFORMAT);
51 $_SESSION['date'] = $cl_date;
52
53 // Determine previous and next dates for simple navigation.
54 $prev_date = date('Y-m-d', strtotime('-1 day', strtotime($cl_date)));
55 $next_date = date('Y-m-d', strtotime('+1 day', strtotime($cl_date)));
56
57 // Use custom fields plugin if it is enabled.
58 if ($user->isPluginEnabled('cf')) {
59   require_once('../plugins/CustomFields.class.php');
60   $custom_fields = new CustomFields($user->group_id);
61   $smarty->assign('custom_fields', $custom_fields);
62 }
63
64 // Initialize variables.
65 $cl_start = trim($request->getParameter('start'));
66 $cl_finish = trim($request->getParameter('finish'));
67 $cl_duration = trim($request->getParameter('duration'));
68 $cl_note = trim($request->getParameter('note'));
69 // Custom field.
70 $cl_cf_1 = trim($request->getParameter('cf_1', ($request->isPost() ? null : @$_SESSION['cf_1'])));
71 $_SESSION['cf_1'] = $cl_cf_1;
72 $cl_billable = 1;
73 if ($user->isPluginEnabled('iv')) {
74   if ($request->isPost()) {
75     $cl_billable = $request->getParameter('billable');
76     $_SESSION['billable'] = (int) $cl_billable;
77   } else 
78     if (isset($_SESSION['billable']))
79       $cl_billable = $_SESSION['billable'];
80 }
81 $cl_client = $request->getParameter('client', ($request->isPost() ? null : @$_SESSION['client']));
82 $_SESSION['client'] = $cl_client;
83 $cl_project = $request->getParameter('project', ($request->isPost() ? null : @$_SESSION['project']));
84 $_SESSION['project'] = $cl_project;
85 $cl_task = $request->getParameter('task', ($request->isPost() ? null : @$_SESSION['task']));
86 $_SESSION['task'] = $cl_task;
87
88 // Elements of timeRecordForm.
89 $form = new Form('timeRecordForm');
90
91 // Dropdown for clients in MODE_TIME. Use all active clients.
92 if (MODE_TIME == $user->tracking_mode && $user->isPluginEnabled('cl')) {
93     $active_clients = ttTeamHelper::getActiveClients($user->group_id, true);
94     $form->addInput(array('type'=>'combobox',
95       'onchange'=>'fillProjectDropdown(this.value);',
96       'name'=>'client',
97       'style'=>'width: 250px;',
98       'value'=>$cl_client,
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.
103 }
104
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);',
110     'name'=>'project',
111     'style'=>'width: 250px;',
112     'value'=>$cl_project,
113     'data'=>$project_list,
114     'datakeys'=>array('id','name'),
115     'empty'=>array(''=>$i18n->get('dropdown.select'))));
116
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'];
123
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       if (is_array($projects_assigned_to_client) && is_array($projects_assigned_to_user))
129         $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
130       if ($intersection) {
131         $client['projects'] = implode(',', $intersection);
132         $client_list[] = $client;
133       }
134     }
135     $form->addInput(array('type'=>'combobox',
136       'onchange'=>'fillProjectDropdown(this.value);',
137       'name'=>'client',
138       'style'=>'width: 250px;',
139       'value'=>$cl_client,
140       'data'=>$client_list,
141       'datakeys'=>array('id', 'name'),
142       'empty'=>array(''=>$i18n->get('dropdown.select'))));
143   }
144 }
145
146 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
147   $task_list = ttTeamHelper::getActiveTasks($user->group_id);
148   $form->addInput(array('type'=>'combobox',
149     'name'=>'task',
150     'style'=>'width: 250px;',
151     'value'=>$cl_task,
152     'data'=>$task_list,
153     'datakeys'=>array('id','name'),
154     'empty'=>array(''=>$i18n->get('dropdown.select'))));
155 }
156 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
157   $form->addInput(array('type'=>'text','name'=>'start','value'=>$cl_start,'onchange'=>"formDisable('start');"));
158   $form->addInput(array('type'=>'text','name'=>'finish','value'=>$cl_finish,'onchange'=>"formDisable('finish');"));
159   if ($user->punch_mode && !$user->canOverridePunchMode()) {
160     // Make the start and finish fields read-only.
161     $form->getElement('start')->setEnabled(false);
162     $form->getElement('finish')->setEnabled(false);
163   }
164 }
165 if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
166   $form->addInput(array('type'=>'text','name'=>'duration','value'=>$cl_duration,'onchange'=>"formDisable('duration');"));
167 $form->addInput(array('type'=>'textarea','name'=>'note','style'=>'width: 250px; height: 60px;','value'=>$cl_note));
168 if ($user->isPluginEnabled('iv'))
169   $form->addInput(array('type'=>'checkbox','name'=>'billable','value'=>$cl_billable));
170 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
171 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->get('button.submit')));
172
173 // If we have custom fields - add controls for them.
174 if ($custom_fields && $custom_fields->fields[0]) {
175   // Only one custom field is supported at this time.
176   if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT) {
177     $form->addInput(array('type'=>'text','name'=>'cf_1','value'=>$cl_cf_1));
178   } elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
179     $form->addInput(array('type'=>'combobox','name'=>'cf_1',
180       'style'=>'width: 250px;',
181       'value'=>$cl_cf_1,
182       'data'=>$custom_fields->options,
183       'empty'=>array(''=>$i18n->get('dropdown.select'))));
184   }
185 }
186
187 // Submit.
188 if ($request->isPost()) {
189   if ($request->getParameter('btn_submit')) {
190
191     // Validate user input.
192     if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
193       $err->add($i18n->get('error.client'));
194     if ($custom_fields) {
195       if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $err->add($i18n->get('error.field'), $custom_fields->fields[0]['label']);
196     }
197     if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
198       if (!$cl_project) $err->add($i18n->get('error.project'));
199     }
200     if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode && $user->task_required) {
201       if (!$cl_task) $err->add($i18n->get('error.task'));
202     }
203     if (strlen($cl_duration) == 0) {
204       if ($cl_start || $cl_finish) {
205         if (!ttTimeHelper::isValidTime($cl_start))
206           $err->add($i18n->get('error.field'), $i18n->get('label.start'));
207         if ($cl_finish) {
208           if (!ttTimeHelper::isValidTime($cl_finish))
209             $err->add($i18n->get('error.field'), $i18n->get('label.finish'));
210           if (!ttTimeHelper::isValidInterval($cl_start, $cl_finish))
211             $err->add($i18n->get('error.interval'), $i18n->get('label.finish'), $i18n->get('label.start'));
212         }
213       } else {
214         if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
215           $err->add($i18n->get('error.empty'), $i18n->get('label.start'));
216           $err->add($i18n->get('error.empty'), $i18n->get('label.finish'));
217         }
218         if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
219           $err->add($i18n->get('error.empty'), $i18n->get('label.duration'));
220       }
221     } else {
222       if (false === ttTimeHelper::postedDurationToMinutes($cl_duration))
223         $err->add($i18n->get('error.field'), $i18n->get('label.duration'));
224     }
225     if (!ttValidString($cl_note, true)) $err->add($i18n->get('error.field'), $i18n->get('label.note'));
226     // Finished validating user input.
227
228     // Prohibit creating entries in future.
229     if (!$user->future_entries) {
230       $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
231       if ($selected_date->after($browser_today))
232         $err->add($i18n->get('error.future_date'));
233     }
234
235     // Prohibit creating entries in locked range.
236     if ($user->isDateLocked($selected_date))
237       $err->add($i18n->get('error.range_locked'));
238
239     // Prohibit creating another uncompleted record.
240     if ($err->no()) {
241       if (($not_completed_rec = ttTimeHelper::getUncompleted($user->getActiveUser())) && (($cl_finish == '') && ($cl_duration == '')))
242         $err->add($i18n->get('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->get('error.goto_uncompleted')."</a>");
243     }
244
245     // Prohibit creating an overlapping record.
246     if ($err->no()) {
247       if (ttTimeHelper::overlaps($user->getActiveUser(), $cl_date, $cl_start, $cl_finish))
248         $err->add($i18n->get('error.overlap'));
249     }
250
251     if ($err->no()) {
252       $id = ttTimeHelper::insert(array(
253         'date' => $cl_date,
254         'user_id' => $user->getActiveUser(),
255         'client' => $cl_client,
256         'project' => $cl_project,
257         'task' => $cl_task,
258         'start' => $cl_start,
259         'finish' => $cl_finish,
260         'duration' => $cl_duration,
261         'note' => $cl_note,
262         'billable' => $cl_billable));
263
264       // Insert a custom field if we have it.
265       $result = true;
266       if ($id && $custom_fields && $cl_cf_1) {
267         if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
268           $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
269         elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
270           $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
271       }
272
273       if ($id && $result) {
274         header('Location: time.php');
275         exit();
276       }
277       $err->add($i18n->get('error.db'));
278     }
279   }
280 } // isPost
281
282 $smarty->assign('next_date', $next_date);
283 $smarty->assign('prev_date', $prev_date);
284 $smarty->assign('time_records', ttTimeHelper::getRecords($user->getActiveUser(), $cl_date));
285 $smarty->assign('day_total', ttTimeHelper::getTimeForDay($user->getActiveUser(), $cl_date));
286 $smarty->assign('client_list', $client_list);
287 $smarty->assign('project_list', $project_list);
288 $smarty->assign('task_list', $task_list);
289 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
290 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
291 $smarty->assign('timestring', $selected_date->toString($user->date_format));
292 $smarty->assign('title', $i18n->get('title.time'));
293 $smarty->assign('content_page_name', 'mobile/time.tpl');
294 $smarty->display('mobile/index.tpl');