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($user_id) {
36 $mdb2 = getConnection();
39 $sql = "select * from tt_fav_reports where user_id = $user_id and status = 1";
40 $res = $mdb2->query($sql);
41 if (!is_a($res, 'PEAR_Error')) {
42 while ($val = $res->fetchRow()) {
45 return mu_sort($result, 'name');
50 // getReport - returns a report identified by its id.
51 static function getReport($id) {
52 $mdb2 = getConnection();
54 $sql = "select * from tt_fav_reports where id = $id and status = 1";
55 $res = $mdb2->query($sql);
56 if (!is_a($res, 'PEAR_Error')) {
57 if ($val = $res->fetchRow()) {
64 // getReportByName - returns a report identified by its name.
65 static function getReportByName($user_id, $report_name) {
66 $mdb2 = getConnection();
68 $sql = "select * from tt_fav_reports where user_id = $user_id and status = 1 and name = ".$mdb2->quote($report_name);
69 $res = $mdb2->query($sql);
70 if (!is_a($res, 'PEAR_Error')) {
71 if ($val = $res->fetchRow()) {
78 // insertReport - stores reports settings in database.
79 static function insertReport($fields) {
80 $mdb2 = getConnection();
82 $sql = "insert into tt_fav_reports (name, user_id, client_id, cf_1_option_id, project_id, task_id,
83 billable, invoice, paid_status, users, period, period_start, period_end,
84 show_client, show_invoice, show_paid, show_ip,
85 show_project, show_start, show_duration, show_cost,
86 show_task, show_end, show_note, show_custom_field_1, show_work_units,
87 group_by1, group_by2, group_by3, show_totals_only)
89 $mdb2->quote($fields['name']).", ".$fields['user_id'].", ".
90 $mdb2->quote($fields['client']).", ".$mdb2->quote($fields['option']).", ".
91 $mdb2->quote($fields['project']).", ".$mdb2->quote($fields['task']).", ".
92 $mdb2->quote($fields['billable']).", ".$mdb2->quote($fields['invoice']).", ".
93 $mdb2->quote($fields['paid_status']).", ".
94 $mdb2->quote($fields['users']).", ".$mdb2->quote($fields['period']).", ".
95 $mdb2->quote($fields['from']).", ".$mdb2->quote($fields['to']).", ".
96 $fields['chclient'].", ".$fields['chinvoice'].", ".$fields['chpaid'].", ".$fields['chip'].", ".
97 $fields['chproject'].", ".$fields['chstart'].", ".$fields['chduration'].", ".$fields['chcost'].", ".
98 $fields['chtask'].", ".$fields['chfinish'].", ".$fields['chnote'].", ".$fields['chcf_1'].", ".$fields['chunits'].", ".
99 $mdb2->quote($fields['group_by1']).", ".$mdb2->quote($fields['group_by2']).", ".
100 $mdb2->quote($fields['group_by3']).", ".$fields['chtotalsonly'].")";
101 $affected = $mdb2->exec($sql);
102 if (is_a($affected, 'PEAR_Error'))
105 $sql = "select last_insert_id() as last_id";
106 $res = $mdb2->query($sql);
107 if (is_a($res, 'PEAR_Error'))
110 $val = $res->fetchRow();
111 return $val['last_id'];
114 // updateReport - updates report options in the database.
115 function updateReport($fields) {
116 $mdb2 = getConnection();
117 $sql = "update tt_fav_reports set ".
118 "name = ".$mdb2->quote($fields['name']).", ".
119 "client_id = ".$mdb2->quote($fields['client']).", ".
120 "cf_1_option_id = ".$mdb2->quote($fields['option']).", ".
121 "project_id = ".$mdb2->quote($fields['project']).", ".
122 "task_id = ".$mdb2->quote($fields['task']).", ".
123 "billable = ".$mdb2->quote($fields['billable']).", ".
124 "invoice = ".$mdb2->quote($fields['invoice']).", ".
125 "paid_status = ".$mdb2->quote($fields['paid_status']).", ".
126 "users = ".$mdb2->quote($fields['users']).", ".
127 "period = ".$mdb2->quote($fields['period']).", ".
128 "period_start = ".$mdb2->quote($fields['from']).", ".
129 "period_end = ".$mdb2->quote($fields['to']).", ".
130 "show_client = ".$fields['chclient'].", ".
131 "show_invoice = ".$fields['chinvoice'].", ".
132 "show_paid = ".$fields['chpaid'].", ".
133 "show_ip = ".$fields['chip'].", ".
134 "show_project = ".$fields['chproject'].", ".
135 "show_start = ".$fields['chstart'].", ".
136 "show_duration = ".$fields['chduration'].", ".
137 "show_cost = ".$fields['chcost'].", ".
138 "show_task = ".$fields['chtask'].", ".
139 "show_end = ".$fields['chfinish'].", ".
140 "show_note = ".$fields['chnote'].", ".
141 "show_custom_field_1 = ".$fields['chcf_1'].", ".
142 "show_work_units = ".$fields['chunits'].", ".
143 "group_by1 = ".$mdb2->quote($fields['group_by1']).", ".
144 "group_by2 = ".$mdb2->quote($fields['group_by2']).", ".
145 "group_by3 = ".$mdb2->quote($fields['group_by3']).", ".
146 "show_totals_only = ".$fields['chtotalsonly'].
147 " where id = ".$fields['id'];
148 $affected = $mdb2->exec($sql);
149 if (is_a($affected, 'PEAR_Error'))
152 return $fields['id'];
155 // saveReport - saves report options in the database.
156 static function saveReport($user_id, $bean) {
159 // Set default value of 0 for not set checkboxes (in bean).
160 // Later in this function we use it to construct $fields array to update database.
161 if (!$bean->getAttribute('chclient')) $bean->setAttribute('chclient', 0);
162 if (!$bean->getAttribute('chinvoice')) $bean->setAttribute('chinvoice', 0);
163 if (!$bean->getAttribute('chpaid')) $bean->setAttribute('chpaid', 0);
164 if (!$bean->getAttribute('chip')) $bean->setAttribute('chip', 0);
165 if (!$bean->getAttribute('chproject')) $bean->setAttribute('chproject', 0);
166 if (!$bean->getAttribute('chstart')) $bean->setAttribute('chstart', 0);
167 if (!$bean->getAttribute('chduration')) $bean->setAttribute('chduration', 0);
168 if (!$bean->getAttribute('chcost')) $bean->setAttribute('chcost', 0);
169 if (!$bean->getAttribute('chtask')) $bean->setAttribute('chtask', 0);
170 if (!$bean->getAttribute('chfinish')) $bean->setAttribute('chfinish', 0);
171 if (!$bean->getAttribute('chnote')) $bean->setAttribute('chnote', 0);
172 if (!$bean->getAttribute('chcf_1')) $bean->setAttribute('chcf_1', 0);
173 if (!$bean->getAttribute('chunits')) $bean->setAttribute('chunits', 0);
174 if (!$bean->getAttribute('chtotalsonly')) $bean->setAttribute('chtotalsonly', 0);
176 $users_in_bean = $bean->getAttribute('users');
177 if ($users_in_bean && is_array($users_in_bean)) {
178 $users = join(',', $users_in_bean);
180 if ($bean->getAttribute('start_date')) {
181 $dt = new DateAndTime($user->date_format, $bean->getAttribute('start_date'));
182 $from = $dt->toString(DB_DATEFORMAT);
184 if ($bean->getAttribute('end_date')) {
185 $dt = new DateAndTime($user->date_format, $bean->getAttribute('end_date'));
186 $to = $dt->toString(DB_DATEFORMAT);
190 'name'=>$bean->getAttribute('new_fav_report'),
191 'client'=>$bean->getAttribute('client'),
192 'option'=>$bean->getAttribute('option'),
193 'project'=>$bean->getAttribute('project'),
194 'task'=>$bean->getAttribute('task'),
195 'billable'=>$bean->getAttribute('include_records'),
196 'invoice'=>$bean->getAttribute('invoice'),
197 'paid_status'=>$bean->getAttribute('paid_status'),
199 'period'=>$bean->getAttribute('period'),
202 'chclient'=>$bean->getAttribute('chclient'),
203 'chinvoice'=>$bean->getAttribute('chinvoice'),
204 'chpaid'=>$bean->getAttribute('chpaid'),
205 'chip'=>$bean->getAttribute('chip'),
206 'chproject'=>$bean->getAttribute('chproject'),
207 'chstart'=>$bean->getAttribute('chstart'),
208 'chduration'=>$bean->getAttribute('chduration'),
209 'chcost'=>$bean->getAttribute('chcost'),
210 'chtask'=>$bean->getAttribute('chtask'),
211 'chfinish'=>$bean->getAttribute('chfinish'),
212 'chnote'=>$bean->getAttribute('chnote'),
213 'chcf_1'=>$bean->getAttribute('chcf_1'),
214 'chunits'=>$bean->getAttribute('chunits'),
215 'group_by1'=>$bean->getAttribute('group_by1'),
216 'group_by2'=>$bean->getAttribute('group_by2'),
217 'group_by3'=>$bean->getAttribute('group_by3'),
218 'chtotalsonly'=>$bean->getAttribute('chtotalsonly'));
221 $report = ttFavReportHelper::getReportByName($user_id, $fields['name']);
223 $fields['id'] = $report['id'];
224 $id = ttFavReportHelper::updateReport($fields);
226 $fields['user_id'] = $user_id;
227 $id = ttFavReportHelper::insertReport($fields);
233 // deleteReport - deletes a favorite report.
234 static function deleteReport($id) {
235 $mdb2 = getConnection();
237 $sql = "delete from tt_fav_reports where id = $id";
238 $affected = $mdb2->exec($sql);
239 return (!is_a($affected, 'PEAR_Error'));
242 // loadReport - loads report options from database into a bean.
243 static function loadReport($user_id, &$bean) {
246 $val = ttFavReportHelper::getReport($bean->getAttribute('favorite_report'));
248 $bean->setAttribute('client', $val['client_id']);
249 $bean->setAttribute('option', $val['cf_1_option_id']);
250 $bean->setAttribute('project', $val['project_id']);
251 $bean->setAttribute('task', $val['task_id']);
252 $bean->setAttribute('include_records', $val['billable']);
253 $bean->setAttribute('invoice', $val['invoice']);
254 $bean->setAttribute('paid_status', $val['paid_status']);
255 $bean->setAttribute('users', explode(',', $val['users']));
256 $bean->setAttribute('period', $val['period']);
257 if ($val['period_start']) {
258 $dt = new DateAndTime(DB_DATEFORMAT, $val['period_start']);
259 $bean->setAttribute('start_date', $dt->toString($user->date_format));
261 if ($val['period_end']) {
262 $dt = new DateAndTime(DB_DATEFORMAT, $val['period_end']);
263 $bean->setAttribute('end_date', $dt->toString($user->date_format));
265 $bean->setAttribute('chclient', $val['show_client']);
266 $bean->setAttribute('chinvoice', $val['show_invoice']);
267 $bean->setAttribute('chpaid', $val['show_paid']);
268 $bean->setAttribute('chip', $val['show_ip']);
269 $bean->setAttribute('chproject', $val['show_project']);
270 $bean->setAttribute('chstart', $val['show_start']);
271 $bean->setAttribute('chduration', $val['show_duration']);
272 $bean->setAttribute('chcost', $val['show_cost']);
273 $bean->setAttribute('chtask', $val['show_task']);
274 $bean->setAttribute('chfinish', $val['show_end']);
275 $bean->setAttribute('chnote', $val['show_note']);
276 $bean->setAttribute('chcf_1', $val['show_custom_field_1']);
277 $bean->setAttribute('chunits', $val['show_work_units']);
278 $bean->setAttribute('group_by1', $val['group_by1']);
279 $bean->setAttribute('group_by2', $val['group_by2']);
280 $bean->setAttribute('group_by3', $val['group_by3']);
281 $bean->setAttribute('chtotalsonly', $val['show_totals_only']);
282 $bean->setAttribute('new_fav_report', $val['name']);
284 $attrs = $bean->getAttributes();
285 $attrs = array_merge($attrs, array(
290 'include_records'=>'',
309 'new_fav_report'=>''));
310 $bean->setAttributes($attrs);
314 // getReportOptions - returns an array of fav report options from database data.
315 // Note: this function is a part of refactoring to simplify maintenance of report
316 // generating functions, as we currently have 2 sets: normal reporting (from bean),
317 // and fav report emailing (from db fields). Using options obtained from either db or bean
318 // shall allow us to use only one set of functions.
319 static function getReportOptions($id) {
321 // Start with getting the fields from the database.
322 $db_fields = ttFavReportHelper::getReport($id);
323 if (!$db_fields) return false;
325 // Prepare an array of report options.
326 $options = $db_fields; // For now, use db field names as options.
327 // Drop things we don't need in reports.
328 unset($options['id']);
329 unset($options['report_spec']); // Currently not used.
330 unset($options['status']);
332 // Note: special handling for NULL users field is done in cron.php
334 // $options now is a subset of db fields from tt_fav_reports table.
338 // adjustOptions takes and array or report options and adjusts them for current user
339 // (and group) settings. This is needed in situations when a fav report is stored in db
340 // long ago, but user or group attributes are now changed, so we have to adjust.
341 static function adjustOptions($options) {
344 // Check and optionally adjust users.
345 // Special handling of the NULL $options['users'] field (this used to mean "all users").
346 if (!$options['users']) {
347 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) {
348 if ($user->can('view_reports') || $user->can('view_all_reports')) {
349 $max_rank = $user->rank-1;
350 if ($user->can('view_all_reports')) $max_rank = 512;
351 if ($user->can('view_own_reports'))
352 $user_options = array('max_rank'=>$max_rank,'include_self'=>true);
354 $user_options = array('max_rank'=>$max_rank);
355 $users = $user->getUsers($user_options); // Active and inactive users.
356 } elseif ($user->isClient()) {
357 $users = ttTeamHelper::getUsersForClient(); // Active and inactive users for clients.
359 foreach ($users as $single_user) {
360 $user_ids[] = $single_user['id'];
362 $options['users'] = implode(',', $user_ids);
365 $users_to_adjust = explode(',', $options['users']); // Users to adjust.
366 if ($user->isClient()) {
367 $users = ttTeamHelper::getUsersForClient(); // Active and inactive users for clients.
368 foreach ($users as $single_user) {
369 $user_ids[] = $single_user['id'];
371 foreach ($users_to_adjust as $user_to_adjust) {
372 if (in_array($user_to_adjust['id'], $user_ids)) {
373 $adjusted_user_ids[] = $user_to_adjust['id'];
376 $options['users'] = implode(',', $adjusted_user_ids);
378 // TODO: add checking the existing user list for potentially changed access rights for user.