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('ttTeamHelper');
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 // getReport - returns a report identified by its id.
57 static function getReport($id) {
58 $mdb2 = getConnection();
60 $sql = "select * from tt_fav_reports where id = $id and status = 1";
61 $res = $mdb2->query($sql);
62 if (!is_a($res, 'PEAR_Error')) {
63 if ($val = $res->fetchRow()) {
70 // getReportByName - returns a report identified by its name.
71 static function getReportByName($user_id, $report_name) {
72 $mdb2 = getConnection();
74 $sql = "select * from tt_fav_reports where user_id = $user_id and status = 1 and name = ".$mdb2->quote($report_name);
75 $res = $mdb2->query($sql);
76 if (!is_a($res, 'PEAR_Error')) {
77 if ($val = $res->fetchRow()) {
84 // insertReport - stores reports settings in database.
85 static function insertReport($fields) {
87 $mdb2 = getConnection();
89 $group_id = $user->getGroup();
90 $org_id = $user->org_id;
92 $sql = "insert into tt_fav_reports".
93 " (name, user_id, group_id, org_id, client_id, cf_1_option_id, project_id, task_id,".
94 " billable, invoice, paid_status, users, period, period_start, period_end,".
95 " show_client, show_invoice, show_paid, show_ip,".
96 " show_project, show_start, show_duration, show_cost,".
97 " show_task, show_end, show_note, show_custom_field_1, show_work_units,".
98 " group_by1, group_by2, group_by3, show_totals_only)".
100 $mdb2->quote($fields['name']).", ".$fields['user_id'].", $group_id, $org_id, ".
101 $mdb2->quote($fields['client']).", ".$mdb2->quote($fields['option']).", ".
102 $mdb2->quote($fields['project']).", ".$mdb2->quote($fields['task']).", ".
103 $mdb2->quote($fields['billable']).", ".$mdb2->quote($fields['invoice']).", ".
104 $mdb2->quote($fields['paid_status']).", ".
105 $mdb2->quote($fields['users']).", ".$mdb2->quote($fields['period']).", ".
106 $mdb2->quote($fields['from']).", ".$mdb2->quote($fields['to']).", ".
107 $fields['chclient'].", ".$fields['chinvoice'].", ".$fields['chpaid'].", ".$fields['chip'].", ".
108 $fields['chproject'].", ".$fields['chstart'].", ".$fields['chduration'].", ".$fields['chcost'].", ".
109 $fields['chtask'].", ".$fields['chfinish'].", ".$fields['chnote'].", ".$fields['chcf_1'].", ".$fields['chunits'].", ".
110 $mdb2->quote($fields['group_by1']).", ".$mdb2->quote($fields['group_by2']).", ".
111 $mdb2->quote($fields['group_by3']).", ".$fields['chtotalsonly'].")";
112 $affected = $mdb2->exec($sql);
113 if (is_a($affected, 'PEAR_Error'))
116 $last_id = $mdb2->lastInsertID('tt_fav_reports', 'id');
120 // updateReport - updates report options in the database.
121 function updateReport($fields) {
122 $mdb2 = getConnection();
123 $sql = "update tt_fav_reports set ".
124 "name = ".$mdb2->quote($fields['name']).", ".
125 "client_id = ".$mdb2->quote($fields['client']).", ".
126 "cf_1_option_id = ".$mdb2->quote($fields['option']).", ".
127 "project_id = ".$mdb2->quote($fields['project']).", ".
128 "task_id = ".$mdb2->quote($fields['task']).", ".
129 "billable = ".$mdb2->quote($fields['billable']).", ".
130 "invoice = ".$mdb2->quote($fields['invoice']).", ".
131 "paid_status = ".$mdb2->quote($fields['paid_status']).", ".
132 "users = ".$mdb2->quote($fields['users']).", ".
133 "period = ".$mdb2->quote($fields['period']).", ".
134 "period_start = ".$mdb2->quote($fields['from']).", ".
135 "period_end = ".$mdb2->quote($fields['to']).", ".
136 "show_client = ".$fields['chclient'].", ".
137 "show_invoice = ".$fields['chinvoice'].", ".
138 "show_paid = ".$fields['chpaid'].", ".
139 "show_ip = ".$fields['chip'].", ".
140 "show_project = ".$fields['chproject'].", ".
141 "show_start = ".$fields['chstart'].", ".
142 "show_duration = ".$fields['chduration'].", ".
143 "show_cost = ".$fields['chcost'].", ".
144 "show_task = ".$fields['chtask'].", ".
145 "show_end = ".$fields['chfinish'].", ".
146 "show_note = ".$fields['chnote'].", ".
147 "show_custom_field_1 = ".$fields['chcf_1'].", ".
148 "show_work_units = ".$fields['chunits'].", ".
149 "group_by1 = ".$mdb2->quote($fields['group_by1']).", ".
150 "group_by2 = ".$mdb2->quote($fields['group_by2']).", ".
151 "group_by3 = ".$mdb2->quote($fields['group_by3']).", ".
152 "show_totals_only = ".$fields['chtotalsonly'].
153 " where id = ".$fields['id'];
154 $affected = $mdb2->exec($sql);
155 if (is_a($affected, 'PEAR_Error'))
158 return $fields['id'];
161 // saveReport - saves report options in the database.
162 static function saveReport($user_id, $bean) {
165 // Set default value of 0 for not set checkboxes (in bean).
166 // Later in this function we use it to construct $fields array to update database.
167 if (!$bean->getAttribute('chclient')) $bean->setAttribute('chclient', 0);
168 if (!$bean->getAttribute('chinvoice')) $bean->setAttribute('chinvoice', 0);
169 if (!$bean->getAttribute('chpaid')) $bean->setAttribute('chpaid', 0);
170 if (!$bean->getAttribute('chip')) $bean->setAttribute('chip', 0);
171 if (!$bean->getAttribute('chproject')) $bean->setAttribute('chproject', 0);
172 if (!$bean->getAttribute('chstart')) $bean->setAttribute('chstart', 0);
173 if (!$bean->getAttribute('chduration')) $bean->setAttribute('chduration', 0);
174 if (!$bean->getAttribute('chcost')) $bean->setAttribute('chcost', 0);
175 if (!$bean->getAttribute('chtask')) $bean->setAttribute('chtask', 0);
176 if (!$bean->getAttribute('chfinish')) $bean->setAttribute('chfinish', 0);
177 if (!$bean->getAttribute('chnote')) $bean->setAttribute('chnote', 0);
178 if (!$bean->getAttribute('chcf_1')) $bean->setAttribute('chcf_1', 0);
179 if (!$bean->getAttribute('chunits')) $bean->setAttribute('chunits', 0);
180 if (!$bean->getAttribute('chtotalsonly')) $bean->setAttribute('chtotalsonly', 0);
182 $users_in_bean = $bean->getAttribute('users');
183 if ($users_in_bean && is_array($users_in_bean)) {
184 $users = join(',', $users_in_bean);
186 if ($bean->getAttribute('start_date')) {
187 $dt = new DateAndTime($user->date_format, $bean->getAttribute('start_date'));
188 $from = $dt->toString(DB_DATEFORMAT);
190 if ($bean->getAttribute('end_date')) {
191 $dt = new DateAndTime($user->date_format, $bean->getAttribute('end_date'));
192 $to = $dt->toString(DB_DATEFORMAT);
196 'name'=>$bean->getAttribute('new_fav_report'),
197 'client'=>$bean->getAttribute('client'),
198 'option'=>$bean->getAttribute('option'),
199 'project'=>$bean->getAttribute('project'),
200 'task'=>$bean->getAttribute('task'),
201 'billable'=>$bean->getAttribute('include_records'),
202 'invoice'=>$bean->getAttribute('invoice'),
203 'paid_status'=>$bean->getAttribute('paid_status'),
205 'period'=>$bean->getAttribute('period'),
208 'chclient'=>$bean->getAttribute('chclient'),
209 'chinvoice'=>$bean->getAttribute('chinvoice'),
210 'chpaid'=>$bean->getAttribute('chpaid'),
211 'chip'=>$bean->getAttribute('chip'),
212 'chproject'=>$bean->getAttribute('chproject'),
213 'chstart'=>$bean->getAttribute('chstart'),
214 'chduration'=>$bean->getAttribute('chduration'),
215 'chcost'=>$bean->getAttribute('chcost'),
216 'chtask'=>$bean->getAttribute('chtask'),
217 'chfinish'=>$bean->getAttribute('chfinish'),
218 'chnote'=>$bean->getAttribute('chnote'),
219 'chcf_1'=>$bean->getAttribute('chcf_1'),
220 'chunits'=>$bean->getAttribute('chunits'),
221 'group_by1'=>$bean->getAttribute('group_by1'),
222 'group_by2'=>$bean->getAttribute('group_by2'),
223 'group_by3'=>$bean->getAttribute('group_by3'),
224 'chtotalsonly'=>$bean->getAttribute('chtotalsonly'));
227 $report = ttFavReportHelper::getReportByName($user_id, $fields['name']);
229 $fields['id'] = $report['id'];
230 $id = ttFavReportHelper::updateReport($fields);
232 $fields['user_id'] = $user_id;
233 $id = ttFavReportHelper::insertReport($fields);
239 // deleteReport - deletes a favorite report.
240 static function deleteReport($id) {
241 $mdb2 = getConnection();
243 $sql = "delete from tt_fav_reports where id = $id";
244 $affected = $mdb2->exec($sql);
245 return (!is_a($affected, 'PEAR_Error'));
248 // loadReport - loads report options from database into a bean.
249 static function loadReport($user_id, &$bean) {
252 $val = ttFavReportHelper::getReport($bean->getAttribute('favorite_report'));
254 $bean->setAttribute('client', $val['client_id']);
255 $bean->setAttribute('option', $val['cf_1_option_id']);
256 $bean->setAttribute('project', $val['project_id']);
257 $bean->setAttribute('task', $val['task_id']);
258 $bean->setAttribute('include_records', $val['billable']);
259 $bean->setAttribute('invoice', $val['invoice']);
260 $bean->setAttribute('paid_status', $val['paid_status']);
261 $bean->setAttribute('users', explode(',', $val['users']));
262 $bean->setAttribute('period', $val['period']);
263 if ($val['period_start']) {
264 $dt = new DateAndTime(DB_DATEFORMAT, $val['period_start']);
265 $bean->setAttribute('start_date', $dt->toString($user->date_format));
267 if ($val['period_end']) {
268 $dt = new DateAndTime(DB_DATEFORMAT, $val['period_end']);
269 $bean->setAttribute('end_date', $dt->toString($user->date_format));
271 $bean->setAttribute('chclient', $val['show_client']);
272 $bean->setAttribute('chinvoice', $val['show_invoice']);
273 $bean->setAttribute('chpaid', $val['show_paid']);
274 $bean->setAttribute('chip', $val['show_ip']);
275 $bean->setAttribute('chproject', $val['show_project']);
276 $bean->setAttribute('chstart', $val['show_start']);
277 $bean->setAttribute('chduration', $val['show_duration']);
278 $bean->setAttribute('chcost', $val['show_cost']);
279 $bean->setAttribute('chtask', $val['show_task']);
280 $bean->setAttribute('chfinish', $val['show_end']);
281 $bean->setAttribute('chnote', $val['show_note']);
282 $bean->setAttribute('chcf_1', $val['show_custom_field_1']);
283 $bean->setAttribute('chunits', $val['show_work_units']);
284 $bean->setAttribute('group_by1', $val['group_by1']);
285 $bean->setAttribute('group_by2', $val['group_by2']);
286 $bean->setAttribute('group_by3', $val['group_by3']);
287 $bean->setAttribute('chtotalsonly', $val['show_totals_only']);
288 $bean->setAttribute('new_fav_report', $val['name']);
290 $attrs = $bean->getAttributes();
291 $attrs = array_merge($attrs, array(
296 'include_records'=>'',
315 'new_fav_report'=>''));
316 $bean->setAttributes($attrs);
320 // getReportOptions - returns an array of fav report options from database data.
321 // Note: this function is a part of refactoring to simplify maintenance of report
322 // generating functions, as we currently have 2 sets: normal reporting (from bean),
323 // and fav report emailing (from db fields). Using options obtained from either db or bean
324 // shall allow us to use only one set of functions.
325 static function getReportOptions($id) {
327 // Start with getting the fields from the database.
328 $db_fields = ttFavReportHelper::getReport($id);
329 if (!$db_fields) return false;
331 // Prepare an array of report options.
332 $options = $db_fields; // For now, use db field names as options.
333 // Drop things we don't need in reports.
334 unset($options['id']);
335 unset($options['report_spec']); // Currently not used.
336 unset($options['status']);
338 // Note: special handling for NULL users field is done in cron.php
340 // $options now is a subset of db fields from tt_fav_reports table.
344 // adjustOptions takes and array or report options and adjusts them for current user
345 // (and group) settings. This is needed in situations when a fav report is stored in db
346 // long ago, but user or group attributes are now changed, so we have to adjust.
347 static function adjustOptions($options) {
350 // Check and optionally adjust users.
351 // Special handling of the NULL $options['users'] field (this used to mean "all users").
352 if (!$options['users']) {
353 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) {
354 if ($user->can('view_reports') || $user->can('view_all_reports')) {
355 $max_rank = $user->rank-1;
356 if ($user->can('view_all_reports')) $max_rank = 512;
357 if ($user->can('view_own_reports'))
358 $user_options = array('max_rank'=>$max_rank,'include_self'=>true);
360 $user_options = array('max_rank'=>$max_rank);
361 $users = $user->getUsers($user_options); // Active and inactive users.
362 } elseif ($user->isClient()) {
363 $users = ttTeamHelper::getUsersForClient(); // Active and inactive users for clients.
365 foreach ($users as $single_user) {
366 $user_ids[] = $single_user['id'];
368 $options['users'] = implode(',', $user_ids);
371 $users_to_adjust = explode(',', $options['users']); // Users to adjust.
372 if ($user->isClient()) {
373 $users = ttTeamHelper::getUsersForClient(); // Active and inactive users for clients.
374 foreach ($users as $single_user) {
375 $user_ids[] = $single_user['id'];
377 foreach ($users_to_adjust as $user_to_adjust) {
378 if (in_array($user_to_adjust['id'], $user_ids)) {
379 $adjusted_user_ids[] = $user_to_adjust['id'];
382 $options['users'] = implode(',', $adjusted_user_ids);
384 // TODO: add checking the existing user list for potentially changed access rights for user.