forked from anuko/timetracker
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathttFavReportHelper.class.php
More file actions
713 lines (649 loc) · 31.9 KB
/
ttFavReportHelper.class.php
File metadata and controls
713 lines (649 loc) · 31.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
<?php
/* Copyright (c) Anuko International Ltd. https://www.anuko.com
License: See license.txt */
import('ttGroupHelper');
import('ttDate');
// Class ttFavReportHelper is used to help with favorite report related tasks.
class ttFavReportHelper {
// getReports - returns an array of favorite reports for user.
static function getReports() {
global $user;
$mdb2 = getConnection();
$user_id = $user->getUser();
$group_id = $user->getGroup();
$org_id = $user->org_id;
$result = array();
$sql = "select * from tt_fav_reports".
" where user_id = $user_id and group_id = $group_id and org_id = $org_id and status = 1";
$res = $mdb2->query($sql);
if (!is_a($res, 'PEAR_Error')) {
while ($val = $res->fetchRow()) {
$result[] = $val;
}
return mu_sort($result, 'name');
}
return false;
}
// get - returns a report identified by its id for user.
static function get($id) {
global $user;
$mdb2 = getConnection();
$user_id = $user->getUser();
$group_id = $user->getGroup();
$org_id = $user->org_id;
$sql = "select * from tt_fav_reports".
" where id = $id and user_id = $user_id and group_id = $group_id and org_id = $org_id and status = 1";
$res = $mdb2->query($sql);
if (!is_a($res, 'PEAR_Error')) {
if ($val = $res->fetchRow()) {
return $val;
}
}
return false;
}
// getReport - returns a report identified by its id.
// TODO: get rid of this function by encapsulating all cron related tasks in its own class.
// Because cron works for all orgs and we want this class to always work in context of
// a logged on user, for better security.
static function getReport($id) {
$mdb2 = getConnection();
$sql = "select * from tt_fav_reports where id = $id and status = 1";
$res = $mdb2->query($sql);
if (!is_a($res, 'PEAR_Error')) {
if ($val = $res->fetchRow()) {
return $val;
}
}
return false;
}
// getReportByName - returns a report identified by its name.
static function getReportByName($report_name) {
global $user;
$mdb2 = getConnection();
$user_id = $user->getUser();
$group_id = $user->getGroup();
$org_id = $user->org_id;
$sql = "select id from tt_fav_reports".
" where user_id = $user_id and group_id = $group_id and org_id = $org_id and status = 1 and name = ".$mdb2->quote($report_name);
$res = $mdb2->query($sql);
if (!is_a($res, 'PEAR_Error')) {
if ($val = $res->fetchRow()) {
return $val;
}
}
return false;
}
// insertReport - stores reports settings in database.
static function insertReport($fields) {
global $user;
$mdb2 = getConnection();
$user_id = $user->getUser();
$group_id = $user->getGroup();
$org_id = $user->org_id;
$sql = "insert into tt_fav_reports".
" (name, user_id, group_id, org_id, report_spec, client_id, task_id,".
" billable, approved, invoice, timesheet, paid_status, note_containing, users, period, period_start,".
" period_end, show_client, show_invoice, show_paid, show_ip,".
" show_project, show_timesheet, show_start, show_duration, show_cost,".
" show_task, show_end, show_note, show_approved, show_work_units,".
" group_by1, group_by2, group_by3, show_totals_only)".
" values(".
$mdb2->quote($fields['name']).", $user_id, $group_id, $org_id, ".
$mdb2->quote($fields['report_spec']).", ".
$mdb2->quote($fields['client']).", ".
$mdb2->quote($fields['task']).", ".
$mdb2->quote($fields['billable']).", ".$mdb2->quote($fields['approved']).", ".
$mdb2->quote($fields['invoice']).", ".$mdb2->quote($fields['timesheet']).", ".
$mdb2->quote($fields['paid_status']).", ".$mdb2->quote($fields['note_containing']).",".
$mdb2->quote($fields['users']).", ".$mdb2->quote($fields['period']).", ".
$mdb2->quote($fields['from']).", ".$mdb2->quote($fields['to']).", ".
$fields['chclient'].", ".$fields['chinvoice'].", ".$fields['chpaid'].", ".$fields['chip'].", ".
$fields['chproject'].", ".$fields['chtimesheet'].", ".$fields['chstart'].", ".$fields['chduration'].", ".$fields['chcost'].", ".
$fields['chtask'].", ".$fields['chfinish'].", ".$fields['chnote'].", ".$fields['chapproved'].", ".$fields['chunits'].", ".
$mdb2->quote($fields['group_by1']).", ".$mdb2->quote($fields['group_by2']).", ".
$mdb2->quote($fields['group_by3']).", ".$fields['chtotalsonly'].")";
$affected = $mdb2->exec($sql);
if (is_a($affected, 'PEAR_Error'))
return false;
$last_id = $mdb2->lastInsertID('tt_fav_reports', 'id');
return $last_id;
}
// updateReport - updates report options in the database.
static function updateReport($fields) {
global $user;
$mdb2 = getConnection();
$user_id = $user->getUser();
$group_id = $user->getGroup();
$org_id = $user->org_id;
$sql = "update tt_fav_reports set ".
"name = ".$mdb2->quote($fields['name']).", ".
"report_spec = ".$mdb2->quote($fields['report_spec']).", ".
"client_id = ".$mdb2->quote($fields['client']).", ".
//"project_id = ".$mdb2->quote($fields['project']).", ".
"task_id = ".$mdb2->quote($fields['task']).", ".
"billable = ".$mdb2->quote($fields['billable']).", ".
"approved = ".$mdb2->quote($fields['approved']).", ".
"invoice = ".$mdb2->quote($fields['invoice']).", ".
"timesheet = ".$mdb2->quote($fields['timesheet']).", ".
"paid_status = ".$mdb2->quote($fields['paid_status']).", ".
"note_containing = ".$mdb2->quote($fields['note_containing']).", ".
"users = ".$mdb2->quote($fields['users']).", ".
"period = ".$mdb2->quote($fields['period']).", ".
"period_start = ".$mdb2->quote($fields['from']).", ".
"period_end = ".$mdb2->quote($fields['to']).", ".
"show_client = ".$fields['chclient'].", ".
"show_invoice = ".$fields['chinvoice'].", ".
"show_paid = ".$fields['chpaid'].", ".
"show_ip = ".$fields['chip'].", ".
"show_project = ".$fields['chproject'].", ".
"show_timesheet = ".$fields['chtimesheet'].", ".
"show_start = ".$fields['chstart'].", ".
"show_duration = ".$fields['chduration'].", ".
"show_cost = ".$fields['chcost'].", ".
"show_task = ".$fields['chtask'].", ".
"show_end = ".$fields['chfinish'].", ".
"show_note = ".$fields['chnote'].", ".
"show_approved = ".$fields['chapproved'].", ".
"show_work_units = ".$fields['chunits'].", ".
"group_by1 = ".$mdb2->quote($fields['group_by1']).", ".
"group_by2 = ".$mdb2->quote($fields['group_by2']).", ".
"group_by3 = ".$mdb2->quote($fields['group_by3']).", ".
"show_totals_only = ".$fields['chtotalsonly'].
" where id = ".$fields['id']." and user_id = $user_id and group_id = $group_id and org_id = $org_id";
$affected = $mdb2->exec($sql);
if (is_a($affected, 'PEAR_Error'))
return false;
return $fields['id'];
}
// saveReport - saves report options in the database.
static function saveReport($bean) {
global $user;
// Set default value of 0 for not set checkboxes (in bean).
// Later in this function we use it to construct $fields array to update database.
if (!$bean->getAttribute('chclient')) $bean->setAttribute('chclient', 0);
if (!$bean->getAttribute('chstart')) $bean->setAttribute('chstart', 0);
if (!$bean->getAttribute('chfinish')) $bean->setAttribute('chfinish', 0);
if (!$bean->getAttribute('chduration')) $bean->setAttribute('chduration', 0);
if (!$bean->getAttribute('chproject')) $bean->setAttribute('chproject', 0);
if (!$bean->getAttribute('chtask')) $bean->setAttribute('chtask', 0);
if (!$bean->getAttribute('chnote')) $bean->setAttribute('chnote', 0);
if (!$bean->getAttribute('chcost')) $bean->setAttribute('chcost', 0);
if (!$bean->getAttribute('chtimesheet')) $bean->setAttribute('chtimesheet', 0);
if (!$bean->getAttribute('chip')) $bean->setAttribute('chip', 0);
if (!$bean->getAttribute('chapproved')) $bean->setAttribute('chapproved', 0);
if (!$bean->getAttribute('chpaid')) $bean->setAttribute('chpaid', 0);
if (!$bean->getAttribute('chunits')) $bean->setAttribute('chunits', 0);
if (!$bean->getAttribute('chinvoice')) $bean->setAttribute('chinvoice', 0);
if (!$bean->getAttribute('chtotalsonly')) $bean->setAttribute('chtotalsonly', 0);
$active_users_in_bean = $bean->getAttribute('users_active');
if ($active_users_in_bean && is_array($active_users_in_bean)) {
$users = join(',', $active_users_in_bean);
}
$inactive_users_in_bean = $bean->getAttribute('users_inactive');
if ($inactive_users_in_bean && is_array($inactive_users_in_bean)) {
if ($users) $users .= ',';
$users .= join(',', $inactive_users_in_bean);
}
if ($bean->getAttribute('start_date')) {
$dt = new ttDate($bean->getAttribute('start_date'), $user->getDateFormat());
$from = $dt->toString();
}
if ($bean->getAttribute('end_date')) {
$dt = new ttDate($bean->getAttribute('end_date'), $user->getDateFormat());
$to = $dt->toString();
}
$fields = array(
'name'=>$bean->getAttribute('new_fav_report'),
'report_spec'=>ttFavReportHelper::makeReportSpec($bean),
'client'=>$bean->getAttribute('client'),
'option'=>$bean->getAttribute('option'),
//'project'=>$bean->getAttribute('project'),
'task'=>$bean->getAttribute('task'),
'billable'=>$bean->getAttribute('include_records'),
'approved'=>$bean->getAttribute('approved'),
'paid_status'=>$bean->getAttribute('paid_status'),
'invoice'=>$bean->getAttribute('invoice'),
'timesheet'=>$bean->getAttribute('timesheet'),
'note_containing'=>$bean->getAttribute('note_containing'),
'users'=>$users,
'period'=>$bean->getAttribute('period'),
'from'=>$from,
'to'=>$to,
'chclient'=>$bean->getAttribute('chclient'),
'chstart'=>$bean->getAttribute('chstart'),
'chfinish'=>$bean->getAttribute('chfinish'),
'chduration'=>$bean->getAttribute('chduration'),
'chproject'=>$bean->getAttribute('chproject'),
'chtask'=>$bean->getAttribute('chtask'),
'chnote'=>$bean->getAttribute('chnote'),
'chcost'=>$bean->getAttribute('chcost'),
'chtimesheet'=>$bean->getAttribute('chtimesheet'),
'chip'=>$bean->getAttribute('chip'),
'chapproved'=>$bean->getAttribute('chapproved'),
'chpaid'=>$bean->getAttribute('chpaid'),
'chunits'=>$bean->getAttribute('chunits'),
'chinvoice'=>$bean->getAttribute('chinvoice'),
'group_by1'=>$bean->getAttribute('group_by1'),
'group_by2'=>$bean->getAttribute('group_by2'),
'group_by3'=>$bean->getAttribute('group_by3'),
'chtotalsonly'=>$bean->getAttribute('chtotalsonly'));
$id = false;
$report = ttFavReportHelper::getReportByName($fields['name']);
if ($report) {
$fields['id'] = $report['id'];
$id = ttFavReportHelper::updateReport($fields);
} else {
$id = ttFavReportHelper::insertReport($fields);
}
return $id;
}
// deleteReport - deletes a favorite report.
static function deleteReport($id) {
global $user;
$mdb2 = getConnection();
$user_id = $user->getUser();
$group_id = $user->getGroup();
$org_id = $user->org_id;
$sql = "delete from tt_cron".
" where report_id = $id and group_id = $group_id and org_id = $org_id";
$affected = $mdb2->exec($sql);
if (is_a($affected, 'PEAR_Error'))
return false;
$sql = "delete from tt_fav_reports".
" where id = $id and user_id = $user_id and group_id = $group_id and org_id = $org_id";
$affected = $mdb2->exec($sql);
return (!is_a($affected, 'PEAR_Error'));
}
// loadReport - loads report options from database into a bean.
static function loadReport(&$bean) {
global $user;
// Custom fields.
if ($user->isPluginEnabled('cf')) {
global $custom_fields;
if (!$custom_fields) $custom_fields = new CustomFields();
}
$user_id = $user->getUser();
$val = ttFavReportHelper::get($bean->getAttribute('favorite_report'));
if ($val) {
// Custom field settings.
if ($val['report_spec']) {
$report_spec = $val['report_spec'];
// Time custom field settings.
if (isset($custom_fields) && $custom_fields->timeFields) {
foreach ($custom_fields->timeFields as $timeField) {
$field_name = 'time_field_'.$timeField['id'];
$checkbox_field_name = 'show_'.$field_name;
$field_value = ttFavReportHelper::getFieldSettingFromReportSpec($field_name, $report_spec);
$bean->setAttribute($field_name, $field_value);
$checkbox_value = ttFavReportHelper::getFieldSettingFromReportSpec($checkbox_field_name, $report_spec);
$bean->setAttribute($checkbox_field_name, $checkbox_value);
}
}
// User custom field settings.
if (isset($custom_fields) && $custom_fields->userFields) {
foreach ($custom_fields->userFields as $userField) {
$field_name = 'user_field_'.$userField['id'];
$checkbox_field_name = 'show_'.$field_name;
$field_value = ttFavReportHelper::getFieldSettingFromReportSpec($field_name, $report_spec);
$bean->setAttribute($field_name, $field_value);
$checkbox_value = ttFavReportHelper::getFieldSettingFromReportSpec($checkbox_field_name, $report_spec);
$bean->setAttribute($checkbox_field_name, $checkbox_value);
}
}
// Project custom field settings.
if (isset($custom_fields) && $custom_fields->projectFields) {
foreach ($custom_fields->projectFields as $projectField) {
$field_name = 'project_field_'.$projectField['id'];
$checkbox_field_name = 'show_'.$field_name;
$field_value = ttFavReportHelper::getFieldSettingFromReportSpec($field_name, $report_spec);
$bean->setAttribute($field_name, $field_value);
$checkbox_value = ttFavReportHelper::getFieldSettingFromReportSpec($checkbox_field_name, $report_spec);
$bean->setAttribute($checkbox_field_name, $checkbox_value);
}
}
// Project ids.
$project_ids = ttFavReportHelper::getFieldSettingFromReportSpec('project_ids', $report_spec);
if (isset($project_ids)) {
$projects = explode('|', $project_ids);
$bean->setAttribute('project', $projects);
}
}
$bean->setAttribute('client', $val['client_id']);
// $bean->setAttribute('project', $val['project_id']);
$bean->setAttribute('task', $val['task_id']);
$bean->setAttribute('include_records', $val['billable']);
$bean->setAttribute('approved', $val['approved']);
$bean->setAttribute('invoice', $val['invoice']);
$bean->setAttribute('paid_status', $val['paid_status']);
$bean->setAttribute('timesheet', $val['timesheet']);
$bean->setAttribute('note_containing', $val['note_containing']);
$bean->setAttribute('users_active', explode(',', $val['users']));
$bean->setAttribute('users_inactive', explode(',', $val['users']));
$bean->setAttribute('period', $val['period']);
if ($val['period_start']) {
$dt = new ttDate($val['period_start']);
$bean->setAttribute('start_date', $dt->toString($user->getDateFormat()));
}
if ($val['period_end']) {
$dt = new ttDate($val['period_end']);
$bean->setAttribute('end_date', $dt->toString($user->getDateFormat()));
}
$bean->setAttribute('chclient', $val['show_client']);
$bean->setAttribute('chinvoice', $val['show_invoice']);
$bean->setAttribute('chpaid', $val['show_paid']);
$bean->setAttribute('chip', $val['show_ip']);
$bean->setAttribute('chproject', $val['show_project']);
$bean->setAttribute('chtimesheet', $val['show_timesheet']);
$bean->setAttribute('chstart', $val['show_start']);
$bean->setAttribute('chduration', $val['show_duration']);
$bean->setAttribute('chcost', $val['show_cost']);
$bean->setAttribute('chtask', $val['show_task']);
$bean->setAttribute('chfinish', $val['show_end']);
$bean->setAttribute('chnote', $val['show_note']);
$bean->setAttribute('chapproved', $val['show_approved']);
$bean->setAttribute('chunits', $val['show_work_units']);
$bean->setAttribute('group_by1', $val['group_by1']);
$bean->setAttribute('group_by2', $val['group_by2']);
$bean->setAttribute('group_by3', $val['group_by3']);
$bean->setAttribute('chtotalsonly', $val['show_totals_only']);
$bean->setAttribute('new_fav_report', $val['name']);
} else {
$attrs = $bean->getAttributes();
$attrs = array_merge($attrs, array(
'client' => null,
'project'=> null,
'task' => null,
'include_records' => null,
'approved' => null,
'paid_status' => null,
'invoice' => null,
'timesheet' => null,
'users' => $user_id,
'period' => null,
'chclient' => '1',
'chstart' => '1',
'chfinish' => '1',
'chduration' => '1',
'chproject' => '1',
'chtask' => '1',
'chnote' => '1',
'chcost' => null,
'chtimesheet' => null,
'chip' => null,
'chapproved' => null,
'chpaid' => null,
'chunits' => null,
'chinvoice' => null,
'chfiles' => '1',
'group_by1' => null,
'group_by2' => null,
'group_by3' => null,
'chtotalsonly' => null,
'new_fav_report' => null));
// Time custom fields.
if (isset($custom_fields) && $custom_fields->timeFields) {
foreach ($custom_fields->timeFields as $timeField) {
$field_name = 'time_field_'.$timeField['id'];
$checkbox_field_name = 'show_'.$field_name;
$custom_field_attrs[$field_name] = null;
$custom_field_attrs[$checkbox_field_name] = null;
}
}
// User custom fields.
if (isset($custom_fields) && $custom_fields->userFields) {
foreach ($custom_fields->userFields as $userField) {
$field_name = 'user_field_'.$userField['id'];
$checkbox_field_name = 'show_'.$field_name;
$custom_field_attrs[$field_name] = null;
$custom_field_attrs[$checkbox_field_name] = null;
}
}
if (isset($custom_field_attrs))
$attrs = array_merge($attrs, $custom_field_attrs);
$bean->setAttributes($attrs);
}
}
// getReportOptions - returns an array of fav report options from database data.
// Note: this function is a part of refactoring to simplify maintenance of report
// generating functions, as we currently have 2 sets: normal reporting (from bean),
// and fav report emailing (from db fields). Using options obtained from either db or bean
// shall allow us to use only one set of functions.
static function getReportOptions($id) {
// Start with getting the fields from the database.
$db_fields = ttFavReportHelper::getReport($id);
if (!$db_fields) return false;
// Prepare an array of report options.
$options = $db_fields; // For now, use db field names as options.
// Drop things we don't need in reports.
unset($options['id']);
unset($options['status']);
// Cast to int some values similar to ttReportHelper::getReportOptions
// where we do it for extra protection against SQL injections.
// This step is redundant, though, as data validation is supposed to work.
// However, in case we missed something, casting to int reduces our risks.
$options['client_id'] = isset($options['client_id']) ? (int)$options['client_id'] : 0;
// Obtain project ids from report_spec.
$project_ids = ttFavReportHelper::getFieldSettingFromReportSpec('project_ids', $options['report_spec']);
if (isset($project_ids)) {
$projects = explode('|', $project_ids);
$options['project_ids'] = join(',', $projects);
}
$options['task_id'] = isset($options['task_id']) ? (int)$options['task_id'] : 0;
$options['billable'] = isset($options['billable']) ? (int)$options['billable'] : 0;
$options['invoice'] = isset($options['invoice']) ? (int)$options['invoice'] : 0;
$options['paid_status'] = isset($options['paid_status']) ? (int)$options['paid_status'] : 0;
$options['approved'] = isset($options['approved']) ? (int)$options['approved'] : 0;
$options['timesheet'] = isset($options['timesheet']) ? (int)$options['timesheet'] : 0;
$options['period'] = isset($options['period']) ? (int)$options['period'] : 0;
$options['show_client'] = isset($options['show_client']) ? (int)$options['show_client'] : 0;
$options['show_invoice'] = isset($options['show_invoice']) ? (int)$options['show_invoice'] : 0;
$options['show_approved'] = isset($options['show_approved']) ? (int)$options['show_approved'] : 0;
$options['show_paid'] = isset($options['show_paid']) ? (int)$options['show_paid'] : 0;
$options['show_ip'] = isset($options['show_ip']) ? (int)$options['show_ip'] : 0;
$options['show_project'] = isset($options['show_project']) ? (int)$options['show_project'] : 0;
$options['show_start'] = isset($options['show_start']) ? (int)$options['show_start'] : 0;
$options['show_duration'] = isset($options['show_duration']) ? (int)$options['show_duration'] : 0;
$options['show_cost'] = isset($options['show_cost']) ? (int)$options['show_cost'] : 0;
$options['show_task'] = isset($options['show_task']) ? (int)$options['show_task'] : 0;
$options['show_end'] = isset($options['show_end']) ? (int)$options['show_end'] : 0;
$options['show_note'] = isset($options['show_note']) ? (int)$options['show_note'] : 0;
$options['show_work_units'] = isset($options['show_work_units']) ? (int)$options['show_work_units'] : 0;
$options['show_timesheet'] = isset($options['show_timesheet']) ? (int)$options['show_timesheet'] : 0;
$options['show_files'] = isset($options['show_files']) ? (int)$options['show_files'] : 0;
$options['show_totals_only'] = isset($options['show_totals_only']) ? (int)$options['show_totals_only'] : 0;
// Note: We can't cast anything to int for custom fields because global $user object is not recycled
// yet for user id. See cron.php. Because of this we do it in adjustOptions instead.
// Note: special handling for NULL users field is done in cron.php
// $options now is a subset of db fields from tt_fav_reports table.
return $options;
}
// adjustOptions takes an array or report options and adjusts them for current user
// (and group) settings. This is needed in situations when a fav report is stored in db
// long ago, but user or group attributes are now changed, so we have to adjust.
static function adjustOptions($options) {
global $user;
// Check and optionally adjust users.
// Special handling of the NULL $options['users'] field (this used to mean "all users").
if (!$options['users']) {
if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) {
if ($user->can('view_reports') || $user->can('view_all_reports')) {
$max_rank = $user->rank-1;
if ($user->can('view_all_reports')) $max_rank = 512;
if ($user->can('view_own_reports'))
$user_options = array('max_rank'=>$max_rank,'include_self'=>true);
else
$user_options = array('max_rank'=>$max_rank);
$users = $user->getUsers($user_options); // Active and inactive users.
} elseif ($user->isClient()) {
$users = ttGroupHelper::getUsersForClient(); // Active and inactive users for clients.
}
foreach ($users as $single_user) {
$user_ids[] = $single_user['id'];
}
$options['users'] = implode(',', $user_ids);
}
} else {
$users_to_adjust = explode(',', $options['users']); // Users to adjust.
// Adjust user list for a client.
if ($user->isClient()) {
$users = ttGroupHelper::getUsersForClient(); // Active and inactive users for clients.
foreach ($users as $single_user) {
$user_ids[] = $single_user['id'];
}
foreach ($users_to_adjust as $user_to_adjust) {
if (in_array($user_to_adjust, $user_ids)) {
$adjusted_user_ids[] = $user_to_adjust;
}
}
$options['users'] = implode(',', $adjusted_user_ids);
}
// Reset user list for a role that no longer has view_reports or view_all_reports rights.
if (!($user->can('view_reports') || $user->can('view_all_reports'))) {
$options['users'] = null;
// Also remove grouping by user if we can't do it.
if (isset($options['group_by1']) && $options['group_by1'] == 'user') unset($options['group_by1']);
if (isset($options['group_by2']) && $options['group_by2'] == 'user') unset($options['group_by2']);
if (isset($options['group_by3']) && $options['group_by3'] == 'user') unset($options['group_by3']);
}
// TODO: improve checking the existing user list for potentially changed access rights for user.
}
if ($user->isPluginEnabled('ap') && $user->isClient() && !$user->can('view_client_unapproved'))
$options['approved'] = 1; // Restrict clients to approved records only.
// Prepare custom field options.
if ($user->isPluginEnabled('cf') && $options['report_spec']) {
$custom_fields = new CustomFields();
$report_spec = $options['report_spec'];
// Time fields.
if ($custom_fields && $custom_fields->timeFields) {
foreach ($custom_fields->timeFields as $timeField) {
$field_name = 'time_field_'.$timeField['id'];
$checkbox_field_name = 'show_'.$field_name;
$field_value = ttFavReportHelper::getFieldSettingFromReportSpec($field_name, $report_spec);
if ($timeField['type'] == CustomFields::TYPE_DROPDOWN) {
// Cast to int for a dropdown for extra security.
$options[$field_name] = (int)$field_value;
} elseif ($timeField['type'] == CustomFields::TYPE_TEXT) {
// No need to cast to int for a text field.
$options[$field_name] = $field_value;
}
$checkbox_value = ttFavReportHelper::getFieldSettingFromReportSpec($checkbox_field_name, $report_spec);
$options[$checkbox_field_name] = (int)$checkbox_value;
}
}
// User fields.
if ($custom_fields && $custom_fields->userFields) {
foreach ($custom_fields->userFields as $userField) {
$field_name = 'user_field_'.$userField['id'];
$checkbox_field_name = 'show_'.$field_name;
$field_value = ttFavReportHelper::getFieldSettingFromReportSpec($field_name, $report_spec);
if ($userField['type'] == CustomFields::TYPE_DROPDOWN) {
// Cast to int for a dropdown for extra security.
$options[$field_name] = (int)$field_value;
} elseif ($userField['type'] == CustomFields::TYPE_TEXT) {
// No need to cast to int for a text field.
$options[$field_name] = $field_value;
}
$checkbox_value = ttFavReportHelper::getFieldSettingFromReportSpec($checkbox_field_name, $report_spec);
$options[$checkbox_field_name] = (int)$checkbox_value;
}
}
// Project fields.
if ($custom_fields && $custom_fields->projectFields) {
foreach ($custom_fields->projectFields as $projectField) {
$field_name = 'project_field_'.$projectField['id'];
$checkbox_field_name = 'show_'.$field_name;
$field_value = ttFavReportHelper::getFieldSettingFromReportSpec($field_name, $report_spec);
if ($projectField['type'] == CustomFields::TYPE_DROPDOWN) {
// Cast to int for a dropdown for extra security.
$options[$field_name] = (int)$field_value;
} elseif ($projectField['type'] == CustomFields::TYPE_TEXT) {
// No need to cast to int for a text field.
$options[$field_name] = $field_value;
}
$checkbox_value = ttFavReportHelper::getFieldSettingFromReportSpec($checkbox_field_name, $report_spec);
$options[$checkbox_field_name] = (int)$checkbox_value;
}
}
}
// Adjust period_start and period_end to user date format.
if ($options['period_start'])
$options['period_start'] = ttDateToUserFormat($options['period_start']);
if ($options['period_end'])
$options['period_end'] = ttDateToUserFormat($options['period_end']);
return $options;
}
// makeReportSpec - prepares a value for report_spec field.
//
// Currently, only custom field settings go there.
// Format:
// time_field_25:117,show_time_field_25:1,time_field_28:qwerty,show_time_field_28:0
static function makeReportSpec($bean) {
global $user;
if ($user->isPluginEnabled('cf')) {
global $custom_fields;
if (!$custom_fields) $custom_fields = new CustomFields();
}
// Add time custom field settings.
if (isset($custom_fields) && $custom_fields->timeFields) {
foreach ($custom_fields->timeFields as $timeField) {
$field_name = 'time_field_'.$timeField['id'];
$field_value = is_null($bean->getAttribute($field_name)) ? '' : str_replace(',',',',$bean->getAttribute($field_name));
$checkbox_field_name = 'show_'.$field_name;
$checkbox_field_value = (int) $bean->getAttribute($checkbox_field_name);
if ($field_value) $reportSpecArray[] = $field_name.':'.$field_value;
if ($checkbox_field_value) $reportSpecArray[] = $checkbox_field_name.':1';
}
}
// Add user custom field settings.
if (isset($custom_fields) && $custom_fields->userFields) {
foreach ($custom_fields->userFields as $userField) {
$field_name = 'user_field_'.$userField['id'];
$field_value = is_null($bean->getAttribute($field_name)) ? '' : str_replace(',',',',$bean->getAttribute($field_name));
$checkbox_field_name = 'show_'.$field_name;
$checkbox_field_value = (int) $bean->getAttribute($checkbox_field_name);
if ($field_value) $reportSpecArray[] = $field_name.':'.$field_value;
if ($checkbox_field_value) $reportSpecArray[] = $checkbox_field_name.':1';
}
}
// Add project custom field settings.
if (isset($custom_fields) && $custom_fields->projectFields) {
foreach ($custom_fields->projectFields as $projectField) {
$field_name = 'project_field_'.$projectField['id'];
$field_value = is_null($bean->getAttribute($field_name)) ? '' : str_replace(',',',',$bean->getAttribute($field_name));
$checkbox_field_name = 'show_'.$field_name;
$checkbox_field_value = (int) $bean->getAttribute($checkbox_field_name);
if ($field_value) $reportSpecArray[] = $field_name.':'.$field_value;
if ($checkbox_field_value) $reportSpecArray[] = $checkbox_field_name.':1';
}
}
// Add projects ids. Format: project_ids:25&756&567&1023. Use the & sign to join multiple project ids.
$selected_projects_in_bean = $bean->getAttribute('project');
if (!is_array($selected_projects_in_bean)) {
$selectedProjects = $bean->getAttribute('project[]');
}
if (is_array($selected_projects_in_bean) && !empty($selected_projects_in_bean[0])) {
$selectedProjectsSpec = join('|', $selected_projects_in_bean);
}
if (isset($selectedProjectsSpec)) $reportSpecArray[] = 'project_ids:'.$selectedProjectsSpec;
$reportSpec = null;
if (isset($reportSpecArray))
$reportSpec = implode(',', $reportSpecArray);
return $reportSpec;
}
// getFieldSettingFromReportSpec - obtains custom field setting from report_spec string.
// See makeReportSpec above.
//
// $fieldKey is something like "time_field_26", "show_time_field_26", or
// "user_field_765", "show_user_field_765".
static function getFieldSettingFromReportSpec($fieldKey, $report_spec) {
$reportSpecArray = explode(',', $report_spec);
foreach ($reportSpecArray as $fieldSetting) {
if (ttStartsWith($fieldSetting, $fieldKey.':')) {
$value = substr($fieldSetting, strlen($fieldKey)+1);
$value = str_replace(',',',',$value); // Restore commas.
return $value;
}
}
return null; // Not found.
}
}