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";
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";
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 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, users, period, period_start, period_end,
84 show_client, show_invoice,
85 show_project, show_start, show_duration, show_cost,
86 show_task, show_end, show_note, show_custom_field_1,
87 group_by, 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['users']).", ".$mdb2->quote($fields['period']).", ".
94 $mdb2->quote($fields['from']).", ".$mdb2->quote($fields['to']).", ".
95 $fields['chclient'].", ".$fields['chinvoice'].", ".
96 $fields['chproject'].", ".$fields['chstart'].", ".$fields['chduration'].", ".$fields['chcost'].", ".
97 $fields['chtask'].", ".$fields['chfinish'].", ".$fields['chnote'].", ".$fields['chcf_1'].", ".
98 $mdb2->quote($fields['group_by']).", ".$fields['chtotalsonly'].")";
99 $affected = $mdb2->exec($sql);
100 if (is_a($affected, 'PEAR_Error'))
103 $sql = "select last_insert_id() as last_id";
104 $res = $mdb2->query($sql);
105 if (is_a($res, 'PEAR_Error'))
108 $val = $res->fetchRow();
109 return $val['last_id'];
112 // updateReport - updates report options in the database.
113 function updateReport($fields) {
114 $mdb2 = getConnection();
115 $sql = "update tt_fav_reports set ".
116 "name = ".$mdb2->quote($fields['name']).", ".
117 "client_id = ".$mdb2->quote($fields['client']).", ".
118 "cf_1_option_id = ".$mdb2->quote($fields['option']).", ".
119 "project_id = ".$mdb2->quote($fields['project']).", ".
120 "task_id = ".$mdb2->quote($fields['task']).", ".
121 "billable = ".$mdb2->quote($fields['billable']).", ".
122 "invoice = ".$mdb2->quote($fields['invoice']).", ".
123 "users = ".$mdb2->quote($fields['users']).", ".
124 "period = ".$mdb2->quote($fields['period']).", ".
125 "period_start = ".$mdb2->quote($fields['from']).", ".
126 "period_end = ".$mdb2->quote($fields['to']).", ".
127 "show_client = ".$fields['chclient'].", ".
128 "show_invoice = ".$fields['chinvoice'].", ".
129 "show_project = ".$fields['chproject'].", ".
130 "show_start = ".$fields['chstart'].", ".
131 "show_duration = ".$fields['chduration'].", ".
132 "show_cost = ".$fields['chcost'].", ".
133 "show_task = ".$fields['chtask'].", ".
134 "show_end = ".$fields['chfinish'].", ".
135 "show_note = ".$fields['chnote'].", ".
136 "show_custom_field_1 = ".$fields['chcf_1'].", ".
137 "group_by = ".$mdb2->quote($fields['group_by']).", ".
138 "show_totals_only = ".$fields['chtotalsonly'].
139 " where id = ".$fields['id'];
140 $affected = $mdb2->exec($sql);
141 if (is_a($affected, 'PEAR_Error'))
144 return $fields['id'];
147 // saveReport - saves report options in the database.
148 static function saveReport($user_id, $bean) {
151 // Set default value of 0 for not set checkboxes (in bean).
152 // Later in this function we use it to construct $fields array to update database.
153 if (!$bean->getAttribute('chclient')) $bean->setAttribute('chclient', 0);
154 if (!$bean->getAttribute('chinvoice')) $bean->setAttribute('chinvoice', 0);
155 if (!$bean->getAttribute('chproject')) $bean->setAttribute('chproject', 0);
156 if (!$bean->getAttribute('chstart')) $bean->setAttribute('chstart', 0);
157 if (!$bean->getAttribute('chduration')) $bean->setAttribute('chduration', 0);
158 if (!$bean->getAttribute('chcost')) $bean->setAttribute('chcost', 0);
159 if (!$bean->getAttribute('chtask')) $bean->setAttribute('chtask', 0);
160 if (!$bean->getAttribute('chfinish')) $bean->setAttribute('chfinish', 0);
161 if (!$bean->getAttribute('chnote')) $bean->setAttribute('chnote', 0);
162 if (!$bean->getAttribute('chcf_1')) $bean->setAttribute('chcf_1', 0);
163 if (!$bean->getAttribute('chtotalsonly')) $bean->setAttribute('chtotalsonly', 0);
165 if ($bean->getAttribute('users') && is_array($bean->getAttribute('users'))) {
166 $users_in_bean = $bean->getAttribute('users');
168 // If all users are selected - use a null value (which means "all users").
169 $all_users_selected = true;
170 if ($user->canManageTeam()) {
171 $all = ttTeamHelper::getActiveUsers();
172 foreach ($all as $one) {
173 if (!in_array($one['id'], $users_in_bean)) {
174 $all_users_selected = false;
179 if ($all_users_selected)
182 $users = join(',', $users_in_bean);
184 if ($bean->getAttribute('start_date')) {
185 $dt = new DateAndTime($user->date_format, $bean->getAttribute('start_date'));
186 $from = $dt->toString(DB_DATEFORMAT);
188 if ($bean->getAttribute('end_date')) {
189 $dt = new DateAndTime($user->date_format, $bean->getAttribute('end_date'));
190 $to = $dt->toString(DB_DATEFORMAT);
194 'name'=>$bean->getAttribute('new_fav_report'),
195 'client'=>$bean->getAttribute('client'),
196 'option'=>$bean->getAttribute('option'),
197 'project'=>$bean->getAttribute('project'),
198 'task'=>$bean->getAttribute('task'),
199 'billable'=>$bean->getAttribute('include_records'),
200 'invoice'=>$bean->getAttribute('invoice'),
202 'period'=>$bean->getAttribute('period'),
205 'chclient'=>$bean->getAttribute('chclient'),
206 'chinvoice'=>$bean->getAttribute('chinvoice'),
207 'chproject'=>$bean->getAttribute('chproject'),
208 'chstart'=>$bean->getAttribute('chstart'),
209 'chduration'=>$bean->getAttribute('chduration'),
210 'chcost'=>$bean->getAttribute('chcost'),
211 'chtask'=>$bean->getAttribute('chtask'),
212 'chfinish'=>$bean->getAttribute('chfinish'),
213 'chnote'=>$bean->getAttribute('chnote'),
214 'chcf_1'=>$bean->getAttribute('chcf_1'),
215 'group_by'=>$bean->getAttribute('group_by'),
216 'chtotalsonly'=>$bean->getAttribute('chtotalsonly'));
219 $report = ttFavReportHelper::getReportByName($user_id, $fields['name']);
221 $fields['id'] = $report['id'];
222 $id = ttFavReportHelper::updateReport($fields);
225 $fields['user_id'] = $user_id;
226 $id = ttFavReportHelper::insertReport($fields);
232 // deleteReport - deletes a favorite report.
233 static function deleteReport($id) {
234 $mdb2 = getConnection();
236 $sql = "delete from tt_fav_reports where id = $id";
237 $affected = $mdb2->exec($sql);
238 return (!is_a($affected, 'PEAR_Error'));
241 // loadReport - loads report options from database into a bean.
242 static function loadReport($user_id, &$bean) {
245 $val = ttFavReportHelper::getReport($bean->getAttribute('favorite_report'));
247 $bean->setAttribute('client', $val['client_id']);
248 $bean->setAttribute('option', $val['cf_1_option_id']);
249 $bean->setAttribute('project', $val['project_id']);
250 $bean->setAttribute('task', $val['task_id']);
251 $bean->setAttribute('include_records', $val['billable']);
252 $bean->setAttribute('invoice', $val['invoice']);
254 $bean->setAttribute('users', explode(',', $val['users']));
256 // Null users value means "all users". Add them to the bean.
257 if ($user->canManageTeam()) {
258 $all = ttTeamHelper::getActiveUsers();
259 foreach ($all as $one) {
260 $all_user_ids[] = $one['id'];
262 $bean->setAttribute('users', $all_user_ids);
265 $bean->setAttribute('period', $val['period']);
266 if ($val['period_start']) {
267 $dt = new DateAndTime(DB_DATEFORMAT, $val['period_start']);
268 $bean->setAttribute('start_date', $dt->toString($user->date_format));
270 if ($val['period_end']) {
271 $dt = new DateAndTime(DB_DATEFORMAT, $val['period_end']);
272 $bean->setAttribute('end_date', $dt->toString($user->date_format));
274 $bean->setAttribute('chclient', $val['show_client']);
275 $bean->setAttribute('chinvoice', $val['show_invoice']);
276 $bean->setAttribute('chproject', $val['show_project']);
277 $bean->setAttribute('chstart', $val['show_start']);
278 $bean->setAttribute('chduration', $val['show_duration']);
279 $bean->setAttribute('chcost', $val['show_cost']);
280 $bean->setAttribute('chtask', $val['show_task']);
281 $bean->setAttribute('chfinish', $val['show_end']);
282 $bean->setAttribute('chnote', $val['show_note']);
283 $bean->setAttribute('chcf_1', $val['show_custom_field_1']);
284 $bean->setAttribute('group_by', $val['group_by']);
285 $bean->setAttribute('chtotalsonly', $val['show_totals_only']);
286 $bean->setAttribute('new_fav_report', $val['name']);
288 $attrs = $bean->getAttributes();
289 $attrs = array_merge($attrs, array(
294 'include_records'=>'',
312 $bean->setAttributes($attrs);