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 import('ttGroupHelper');
31 // Class ttFavReportHelper is used to help with favorite report related tasks.
32 class ttFavReportHelper {
34 // getReports - returns an array of favorite reports for user.
35 static function getReports() {
37 $mdb2 = getConnection();
39 $user_id = $user->getUser();
40 $group_id = $user->getGroup();
41 $org_id = $user->org_id;
44 $sql = "select * from tt_fav_reports".
45 " where user_id = $user_id and group_id = $group_id and org_id = $org_id and status = 1";
46 $res = $mdb2->query($sql);
47 if (!is_a($res, 'PEAR_Error')) {
48 while ($val = $res->fetchRow()) {
51 return mu_sort($result, 'name');
56 // get - returns a report identified by its id for user.
57 static function get($id) {
59 $mdb2 = getConnection();
61 $user_id = $user->getUser();
62 $group_id = $user->getGroup();
63 $org_id = $user->org_id;
65 $sql = "select * from tt_fav_reports".
66 " where id = $id and user_id = $user_id and group_id = $group_id and org_id = $org_id and status = 1";
67 $res = $mdb2->query($sql);
68 if (!is_a($res, 'PEAR_Error')) {
69 if ($val = $res->fetchRow()) {
75 // getReport - returns a report identified by its id.
76 // TODO: get rid of this function by encapsulating all cron related tasks in its own class.
77 // Because cron works for all orgs and we want this class to always work in context of
78 // a logged on user, for better security.
79 static function getReport($id) {
80 $mdb2 = getConnection();
82 $sql = "select * from tt_fav_reports where id = $id and status = 1";
83 $res = $mdb2->query($sql);
84 if (!is_a($res, 'PEAR_Error')) {
85 if ($val = $res->fetchRow()) {
92 // getReportByName - returns a report identified by its name.
93 static function getReportByName($report_name) {
95 $mdb2 = getConnection();
97 $user_id = $user->getUser();
98 $group_id = $user->getGroup();
99 $org_id = $user->org_id;
101 $sql = "select id from tt_fav_reports".
102 " where user_id = $user_id and group_id = $group_id and org_id = $org_id and status = 1 and name = ".$mdb2->quote($report_name);
103 $res = $mdb2->query($sql);
104 if (!is_a($res, 'PEAR_Error')) {
105 if ($val = $res->fetchRow()) {
112 // insertReport - stores reports settings in database.
113 static function insertReport($fields) {
115 $mdb2 = getConnection();
117 $user_id = $user->getUser();
118 $group_id = $user->getGroup();
119 $org_id = $user->org_id;
121 $sql = "insert into tt_fav_reports".
122 " (name, user_id, group_id, org_id, client_id, cf_1_option_id, project_id, task_id,".
123 " billable, approved, invoice, timesheet, paid_status, users, period, period_start,".
124 " period_end, show_client, show_invoice, show_paid, show_ip,".
125 " show_project, show_timesheet, show_start, show_duration, show_cost,".
126 " show_task, show_end, show_note, show_approved, show_custom_field_1, show_work_units,".
127 " group_by1, group_by2, group_by3, show_totals_only)".
129 $mdb2->quote($fields['name']).", $user_id, $group_id, $org_id, ".
130 $mdb2->quote($fields['client']).", ".$mdb2->quote($fields['option']).", ".
131 $mdb2->quote($fields['project']).", ".$mdb2->quote($fields['task']).", ".
132 $mdb2->quote($fields['billable']).", ".$mdb2->quote($fields['approved']).", ".
133 $mdb2->quote($fields['invoice']).", ".$mdb2->quote($fields['timesheet']).", ".
134 $mdb2->quote($fields['paid_status']).", ".
135 $mdb2->quote($fields['users']).", ".$mdb2->quote($fields['period']).", ".
136 $mdb2->quote($fields['from']).", ".$mdb2->quote($fields['to']).", ".
137 $fields['chclient'].", ".$fields['chinvoice'].", ".$fields['chpaid'].", ".$fields['chip'].", ".
138 $fields['chproject'].", ".$fields['chtimesheet'].", ".$fields['chstart'].", ".$fields['chduration'].", ".$fields['chcost'].", ".
139 $fields['chtask'].", ".$fields['chfinish'].", ".$fields['chnote'].", ".$fields['chapproved'].", ".$fields['chcf_1'].", ".$fields['chunits'].", ".
140 $mdb2->quote($fields['group_by1']).", ".$mdb2->quote($fields['group_by2']).", ".
141 $mdb2->quote($fields['group_by3']).", ".$fields['chtotalsonly'].")";
142 $affected = $mdb2->exec($sql);
143 if (is_a($affected, 'PEAR_Error'))
146 $last_id = $mdb2->lastInsertID('tt_fav_reports', 'id');
150 // updateReport - updates report options in the database.
151 static function updateReport($fields) {
153 $mdb2 = getConnection();
155 $user_id = $user->getUser();
156 $group_id = $user->getGroup();
157 $org_id = $user->org_id;
159 $sql = "update tt_fav_reports set ".
160 "name = ".$mdb2->quote($fields['name']).", ".
161 "client_id = ".$mdb2->quote($fields['client']).", ".
162 "cf_1_option_id = ".$mdb2->quote($fields['option']).", ".
163 "project_id = ".$mdb2->quote($fields['project']).", ".
164 "task_id = ".$mdb2->quote($fields['task']).", ".
165 "billable = ".$mdb2->quote($fields['billable']).", ".
166 "approved = ".$mdb2->quote($fields['approved']).", ".
167 "invoice = ".$mdb2->quote($fields['invoice']).", ".
168 "timesheet = ".$mdb2->quote($fields['timesheet']).", ".
169 "paid_status = ".$mdb2->quote($fields['paid_status']).", ".
170 "users = ".$mdb2->quote($fields['users']).", ".
171 "period = ".$mdb2->quote($fields['period']).", ".
172 "period_start = ".$mdb2->quote($fields['from']).", ".
173 "period_end = ".$mdb2->quote($fields['to']).", ".
174 "show_client = ".$fields['chclient'].", ".
175 "show_invoice = ".$fields['chinvoice'].", ".
176 "show_paid = ".$fields['chpaid'].", ".
177 "show_ip = ".$fields['chip'].", ".
178 "show_project = ".$fields['chproject'].", ".
179 "show_timesheet = ".$fields['chtimesheet'].", ".
180 "show_start = ".$fields['chstart'].", ".
181 "show_duration = ".$fields['chduration'].", ".
182 "show_cost = ".$fields['chcost'].", ".
183 "show_task = ".$fields['chtask'].", ".
184 "show_end = ".$fields['chfinish'].", ".
185 "show_note = ".$fields['chnote'].", ".
186 "show_approved = ".$fields['chapproved'].", ".
187 "show_custom_field_1 = ".$fields['chcf_1'].", ".
188 "show_work_units = ".$fields['chunits'].", ".
189 "group_by1 = ".$mdb2->quote($fields['group_by1']).", ".
190 "group_by2 = ".$mdb2->quote($fields['group_by2']).", ".
191 "group_by3 = ".$mdb2->quote($fields['group_by3']).", ".
192 "show_totals_only = ".$fields['chtotalsonly'].
193 " where id = ".$fields['id']." and user_id = $user_id and group_id = $group_id and org_id = $org_id";
194 $affected = $mdb2->exec($sql);
195 if (is_a($affected, 'PEAR_Error'))
198 return $fields['id'];
201 // saveReport - saves report options in the database.
202 static function saveReport($bean) {
205 // Set default value of 0 for not set checkboxes (in bean).
206 // Later in this function we use it to construct $fields array to update database.
207 if (!$bean->getAttribute('chclient')) $bean->setAttribute('chclient', 0);
208 if (!$bean->getAttribute('chstart')) $bean->setAttribute('chstart', 0);
209 if (!$bean->getAttribute('chfinish')) $bean->setAttribute('chfinish', 0);
210 if (!$bean->getAttribute('chduration')) $bean->setAttribute('chduration', 0);
212 if (!$bean->getAttribute('chproject')) $bean->setAttribute('chproject', 0);
213 if (!$bean->getAttribute('chtask')) $bean->setAttribute('chtask', 0);
214 if (!$bean->getAttribute('chnote')) $bean->setAttribute('chnote', 0);
215 if (!$bean->getAttribute('chcost')) $bean->setAttribute('chcost', 0);
217 if (!$bean->getAttribute('chtimesheet')) $bean->setAttribute('chtimesheet', 0);
218 if (!$bean->getAttribute('chip')) $bean->setAttribute('chip', 0);
219 if (!$bean->getAttribute('chapproved')) $bean->setAttribute('chapproved', 0);
220 if (!$bean->getAttribute('chpaid')) $bean->setAttribute('chpaid', 0);
222 if (!$bean->getAttribute('chcf_1')) $bean->setAttribute('chcf_1', 0);
223 if (!$bean->getAttribute('chunits')) $bean->setAttribute('chunits', 0);
224 if (!$bean->getAttribute('chinvoice')) $bean->setAttribute('chinvoice', 0);
226 if (!$bean->getAttribute('chtotalsonly')) $bean->setAttribute('chtotalsonly', 0);
228 $users_in_bean = $bean->getAttribute('users');
229 if ($users_in_bean && is_array($users_in_bean)) {
230 $users = join(',', $users_in_bean);
232 if ($bean->getAttribute('start_date')) {
233 $dt = new DateAndTime($user->getDateFormat(), $bean->getAttribute('start_date'));
234 $from = $dt->toString(DB_DATEFORMAT);
236 if ($bean->getAttribute('end_date')) {
237 $dt = new DateAndTime($user->getDateFormat(), $bean->getAttribute('end_date'));
238 $to = $dt->toString(DB_DATEFORMAT);
242 'name'=>$bean->getAttribute('new_fav_report'),
243 'client'=>$bean->getAttribute('client'),
244 'option'=>$bean->getAttribute('option'),
245 'project'=>$bean->getAttribute('project'),
246 'task'=>$bean->getAttribute('task'),
247 'billable'=>$bean->getAttribute('include_records'),
248 'approved'=>$bean->getAttribute('approved'),
249 'paid_status'=>$bean->getAttribute('paid_status'),
250 'invoice'=>$bean->getAttribute('invoice'),
251 'timesheet'=>$bean->getAttribute('timesheet'),
253 'period'=>$bean->getAttribute('period'),
256 'chclient'=>$bean->getAttribute('chclient'),
257 'chstart'=>$bean->getAttribute('chstart'),
258 'chfinish'=>$bean->getAttribute('chfinish'),
259 'chduration'=>$bean->getAttribute('chduration'),
260 'chproject'=>$bean->getAttribute('chproject'),
261 'chtask'=>$bean->getAttribute('chtask'),
262 'chnote'=>$bean->getAttribute('chnote'),
263 'chcost'=>$bean->getAttribute('chcost'),
264 'chtimesheet'=>$bean->getAttribute('chtimesheet'),
265 'chip'=>$bean->getAttribute('chip'),
266 'chapproved'=>$bean->getAttribute('chapproved'),
267 'chpaid'=>$bean->getAttribute('chpaid'),
268 'chcf_1'=>$bean->getAttribute('chcf_1'),
269 'chunits'=>$bean->getAttribute('chunits'),
270 'chinvoice'=>$bean->getAttribute('chinvoice'),
271 'group_by1'=>$bean->getAttribute('group_by1'),
272 'group_by2'=>$bean->getAttribute('group_by2'),
273 'group_by3'=>$bean->getAttribute('group_by3'),
274 'chtotalsonly'=>$bean->getAttribute('chtotalsonly'));
277 $report = ttFavReportHelper::getReportByName($fields['name']);
279 $fields['id'] = $report['id'];
280 $id = ttFavReportHelper::updateReport($fields);
282 $id = ttFavReportHelper::insertReport($fields);
288 // deleteReport - deletes a favorite report.
289 static function deleteReport($id) {
291 $mdb2 = getConnection();
293 $user_id = $user->getUser();
294 $group_id = $user->getGroup();
295 $org_id = $user->org_id;
297 $sql = "delete from tt_cron".
298 " where report_id = $id and group_id = $group_id and org_id = $org_id";
299 $affected = $mdb2->exec($sql);
300 if (is_a($affected, 'PEAR_Error'))
303 $sql = "delete from tt_fav_reports".
304 " where id = $id and user_id = $user_id and group_id = $group_id and org_id = $org_id";
305 $affected = $mdb2->exec($sql);
306 return (!is_a($affected, 'PEAR_Error'));
309 // loadReport - loads report options from database into a bean.
310 static function loadReport(&$bean) {
312 $user_id = $user->getUser();
314 $val = ttFavReportHelper::get($bean->getAttribute('favorite_report'));
316 $bean->setAttribute('client', $val['client_id']);
317 $bean->setAttribute('option', $val['cf_1_option_id']);
318 $bean->setAttribute('project', $val['project_id']);
319 $bean->setAttribute('task', $val['task_id']);
320 $bean->setAttribute('include_records', $val['billable']);
321 $bean->setAttribute('approved', $val['approved']);
322 $bean->setAttribute('invoice', $val['invoice']);
323 $bean->setAttribute('paid_status', $val['paid_status']);
324 $bean->setAttribute('timesheet', $val['timesheet']);
325 $bean->setAttribute('users', explode(',', $val['users']));
326 $bean->setAttribute('period', $val['period']);
327 if ($val['period_start']) {
328 $dt = new DateAndTime(DB_DATEFORMAT, $val['period_start']);
329 $bean->setAttribute('start_date', $dt->toString($user->getDateFormat()));
331 if ($val['period_end']) {
332 $dt = new DateAndTime(DB_DATEFORMAT, $val['period_end']);
333 $bean->setAttribute('end_date', $dt->toString($user->getDateFormat()));
335 $bean->setAttribute('chclient', $val['show_client']);
336 $bean->setAttribute('chinvoice', $val['show_invoice']);
337 $bean->setAttribute('chpaid', $val['show_paid']);
338 $bean->setAttribute('chip', $val['show_ip']);
339 $bean->setAttribute('chproject', $val['show_project']);
340 $bean->setAttribute('chtimesheet', $val['show_timesheet']);
341 $bean->setAttribute('chstart', $val['show_start']);
342 $bean->setAttribute('chduration', $val['show_duration']);
343 $bean->setAttribute('chcost', $val['show_cost']);
344 $bean->setAttribute('chtask', $val['show_task']);
345 $bean->setAttribute('chfinish', $val['show_end']);
346 $bean->setAttribute('chnote', $val['show_note']);
347 $bean->setAttribute('chapproved', $val['show_approved']);
348 $bean->setAttribute('chcf_1', $val['show_custom_field_1']);
349 $bean->setAttribute('chunits', $val['show_work_units']);
350 $bean->setAttribute('group_by1', $val['group_by1']);
351 $bean->setAttribute('group_by2', $val['group_by2']);
352 $bean->setAttribute('group_by3', $val['group_by3']);
353 $bean->setAttribute('chtotalsonly', $val['show_totals_only']);
354 $bean->setAttribute('new_fav_report', $val['name']);
356 $attrs = $bean->getAttributes();
357 $attrs = array_merge($attrs, array(
362 'include_records'=>'',
388 'new_fav_report'=>''));
389 $bean->setAttributes($attrs);
393 // getReportOptions - returns an array of fav report options from database data.
394 // Note: this function is a part of refactoring to simplify maintenance of report
395 // generating functions, as we currently have 2 sets: normal reporting (from bean),
396 // and fav report emailing (from db fields). Using options obtained from either db or bean
397 // shall allow us to use only one set of functions.
398 static function getReportOptions($id) {
400 // Start with getting the fields from the database.
401 $db_fields = ttFavReportHelper::getReport($id);
402 if (!$db_fields) return false;
404 // Prepare an array of report options.
405 $options = $db_fields; // For now, use db field names as options.
406 // Drop things we don't need in reports.
407 unset($options['id']);
408 unset($options['report_spec']); // Currently not used.
409 unset($options['status']);
411 // Note: special handling for NULL users field is done in cron.php
413 // $options now is a subset of db fields from tt_fav_reports table.
417 // adjustOptions takes an array or report options and adjusts them for current user
418 // (and group) settings. This is needed in situations when a fav report is stored in db
419 // long ago, but user or group attributes are now changed, so we have to adjust.
420 static function adjustOptions($options) {
423 // Check and optionally adjust users.
424 // Special handling of the NULL $options['users'] field (this used to mean "all users").
425 if (!$options['users']) {
426 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) {
427 if ($user->can('view_reports') || $user->can('view_all_reports')) {
428 $max_rank = $user->rank-1;
429 if ($user->can('view_all_reports')) $max_rank = 512;
430 if ($user->can('view_own_reports'))
431 $user_options = array('max_rank'=>$max_rank,'include_self'=>true);
433 $user_options = array('max_rank'=>$max_rank);
434 $users = $user->getUsers($user_options); // Active and inactive users.
435 } elseif ($user->isClient()) {
436 $users = ttGroupHelper::getUsersForClient(); // Active and inactive users for clients.
438 foreach ($users as $single_user) {
439 $user_ids[] = $single_user['id'];
441 $options['users'] = implode(',', $user_ids);
444 $users_to_adjust = explode(',', $options['users']); // Users to adjust.
445 if ($user->isClient()) {
446 $users = ttGroupHelper::getUsersForClient(); // Active and inactive users for clients.
447 foreach ($users as $single_user) {
448 $user_ids[] = $single_user['id'];
450 foreach ($users_to_adjust as $user_to_adjust) {
451 if (in_array($user_to_adjust, $user_ids)) {
452 $adjusted_user_ids[] = $user_to_adjust;
455 $options['users'] = implode(',', $adjusted_user_ids);
457 // TODO: add checking the existing user list for potentially changed access rights for user.
460 if ($user->isPluginEnabled('ap') && $user->isClient() && !$user->can('view_client_unapproved'))
461 $options['approved'] = 1; // Restrict clients to approved records only.