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,
 
  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['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'].", ".
 
  97       $fields['chproject'].", ".$fields['chstart'].", ".$fields['chduration'].", ".$fields['chcost'].", ".
 
  98       $fields['chtask'].", ".$fields['chfinish'].", ".$fields['chnote'].", ".$fields['chcf_1'].", ".
 
  99       $mdb2->quote($fields['group_by']).", ".$fields['chtotalsonly'].")";
 
 100     $affected = $mdb2->exec($sql);
 
 101     if (is_a($affected, 'PEAR_Error'))
 
 104     $sql = "select last_insert_id() as last_id";
 
 105     $res = $mdb2->query($sql);
 
 106     if (is_a($res, 'PEAR_Error'))
 
 109     $val = $res->fetchRow();
 
 110     return $val['last_id'];
 
 113   // updateReport - updates report options in the database.
 
 114   function updateReport($fields) {
 
 115     $mdb2 = getConnection();
 
 116     $sql = "update tt_fav_reports set ".
 
 117       "name = ".$mdb2->quote($fields['name']).", ".
 
 118       "client_id = ".$mdb2->quote($fields['client']).", ".
 
 119       "cf_1_option_id = ".$mdb2->quote($fields['option']).", ".
 
 120       "project_id = ".$mdb2->quote($fields['project']).", ".
 
 121       "task_id = ".$mdb2->quote($fields['task']).", ".
 
 122       "billable = ".$mdb2->quote($fields['billable']).", ".
 
 123       "invoice = ".$mdb2->quote($fields['invoice']).", ".
 
 124       "paid_status = ".$mdb2->quote($fields['paid_status']).", ".
 
 125       "users = ".$mdb2->quote($fields['users']).", ".
 
 126       "period = ".$mdb2->quote($fields['period']).", ".
 
 127       "period_start = ".$mdb2->quote($fields['from']).", ".
 
 128       "period_end = ".$mdb2->quote($fields['to']).", ".
 
 129       "show_client = ".$fields['chclient'].", ".
 
 130       "show_invoice = ".$fields['chinvoice'].", ".
 
 131       "show_paid = ".$fields['chpaid'].", ".
 
 132       "show_project = ".$fields['chproject'].", ".
 
 133       "show_start = ".$fields['chstart'].", ".
 
 134       "show_duration = ".$fields['chduration'].", ".
 
 135       "show_cost = ".$fields['chcost'].", ".
 
 136       "show_task = ".$fields['chtask'].", ".
 
 137       "show_end = ".$fields['chfinish'].", ".
 
 138       "show_note = ".$fields['chnote'].", ".
 
 139       "show_custom_field_1 = ".$fields['chcf_1'].", ".
 
 140       "group_by = ".$mdb2->quote($fields['group_by']).", ".
 
 141       "show_totals_only = ".$fields['chtotalsonly'].
 
 142       " where id = ".$fields['id'];
 
 143     $affected = $mdb2->exec($sql);
 
 144     if (is_a($affected, 'PEAR_Error'))
 
 147     return $fields['id'];
 
 150   // saveReport - saves report options in the database.
 
 151   static function saveReport($user_id, $bean) {
 
 154     //  Set default value of 0 for not set checkboxes (in bean).
 
 155     //  Later in this function we use it to construct $fields array to update database.
 
 156     if (!$bean->getAttribute('chclient')) $bean->setAttribute('chclient', 0);
 
 157     if (!$bean->getAttribute('chinvoice')) $bean->setAttribute('chinvoice', 0);
 
 158     if (!$bean->getAttribute('chpaid')) $bean->setAttribute('chpaid', 0);
 
 159     if (!$bean->getAttribute('chproject')) $bean->setAttribute('chproject', 0);
 
 160     if (!$bean->getAttribute('chstart')) $bean->setAttribute('chstart', 0);
 
 161     if (!$bean->getAttribute('chduration')) $bean->setAttribute('chduration', 0);
 
 162     if (!$bean->getAttribute('chcost')) $bean->setAttribute('chcost', 0);
 
 163     if (!$bean->getAttribute('chtask')) $bean->setAttribute('chtask', 0);
 
 164     if (!$bean->getAttribute('chfinish')) $bean->setAttribute('chfinish', 0);
 
 165     if (!$bean->getAttribute('chnote')) $bean->setAttribute('chnote', 0);
 
 166     if (!$bean->getAttribute('chcf_1')) $bean->setAttribute('chcf_1', 0);
 
 167     if (!$bean->getAttribute('chtotalsonly')) $bean->setAttribute('chtotalsonly', 0);
 
 169     if ($bean->getAttribute('users') && is_array($bean->getAttribute('users'))) {
 
 170       $users_in_bean = $bean->getAttribute('users');
 
 172       // If all users are selected - use a null value (which means "all users").
 
 173       $all_users_selected = true;
 
 174       if ($user->canManageTeam()) {
 
 175         $all = ttTeamHelper::getActiveUsers();
 
 176         foreach ($all as $one) {
 
 177           if (!in_array($one['id'], $users_in_bean)) {
 
 178             $all_users_selected = false;
 
 183       if ($all_users_selected)
 
 186         $users = join(',', $users_in_bean);
 
 188     if ($bean->getAttribute('start_date')) {
 
 189       $dt = new DateAndTime($user->date_format, $bean->getAttribute('start_date'));
 
 190       $from = $dt->toString(DB_DATEFORMAT);
 
 192     if ($bean->getAttribute('end_date')) {
 
 193       $dt = new DateAndTime($user->date_format, $bean->getAttribute('end_date'));
 
 194       $to = $dt->toString(DB_DATEFORMAT);
 
 198       'name'=>$bean->getAttribute('new_fav_report'),
 
 199       'client'=>$bean->getAttribute('client'),
 
 200       'option'=>$bean->getAttribute('option'),
 
 201       'project'=>$bean->getAttribute('project'),
 
 202       'task'=>$bean->getAttribute('task'),
 
 203       'billable'=>$bean->getAttribute('include_records'),
 
 204       'invoice'=>$bean->getAttribute('invoice'),
 
 205       'paid_status'=>$bean->getAttribute('paid_status'),
 
 207       'period'=>$bean->getAttribute('period'),
 
 210       'chclient'=>$bean->getAttribute('chclient'),
 
 211       'chinvoice'=>$bean->getAttribute('chinvoice'),
 
 212       'chpaid'=>$bean->getAttribute('chpaid'),
 
 213       'chproject'=>$bean->getAttribute('chproject'),
 
 214       'chstart'=>$bean->getAttribute('chstart'),
 
 215       'chduration'=>$bean->getAttribute('chduration'),
 
 216       'chcost'=>$bean->getAttribute('chcost'),
 
 217       'chtask'=>$bean->getAttribute('chtask'),
 
 218       'chfinish'=>$bean->getAttribute('chfinish'),
 
 219       'chnote'=>$bean->getAttribute('chnote'),
 
 220       'chcf_1'=>$bean->getAttribute('chcf_1'),
 
 221       'group_by'=>$bean->getAttribute('group_by'),
 
 222       'chtotalsonly'=>$bean->getAttribute('chtotalsonly'));
 
 225     $report = ttFavReportHelper::getReportByName($user_id, $fields['name']);
 
 227       $fields['id'] = $report['id'];
 
 228       $id = ttFavReportHelper::updateReport($fields);
 
 230       $fields['user_id'] = $user_id;
 
 231       $id = ttFavReportHelper::insertReport($fields);
 
 237   // deleteReport - deletes a favorite report.
 
 238   static function deleteReport($id) {
 
 239     $mdb2 = getConnection();
 
 241     $sql = "delete from tt_fav_reports where id = $id";
 
 242     $affected = $mdb2->exec($sql);
 
 243     return (!is_a($affected, 'PEAR_Error'));
 
 246   // loadReport - loads report options from database into a bean.
 
 247   static function loadReport($user_id, &$bean) {
 
 250     $val = ttFavReportHelper::getReport($bean->getAttribute('favorite_report'));
 
 252       $bean->setAttribute('client', $val['client_id']);
 
 253       $bean->setAttribute('option', $val['cf_1_option_id']);
 
 254       $bean->setAttribute('project', $val['project_id']);
 
 255       $bean->setAttribute('task', $val['task_id']);
 
 256       $bean->setAttribute('include_records', $val['billable']);
 
 257       $bean->setAttribute('invoice', $val['invoice']);
 
 258       $bean->setAttribute('paid_status', $val['paid_status']);
 
 260         $bean->setAttribute('users', explode(',', $val['users']));
 
 262         // Null users value means "all users". Add them to the bean.
 
 263         if ($user->canManageTeam()) {
 
 264           $all = ttTeamHelper::getActiveUsers();
 
 265           foreach ($all as $one) {
 
 266             $all_user_ids[] = $one['id'];
 
 268           $bean->setAttribute('users', $all_user_ids);
 
 271       $bean->setAttribute('period', $val['period']);
 
 272       if ($val['period_start']) {
 
 273         $dt = new DateAndTime(DB_DATEFORMAT, $val['period_start']);
 
 274         $bean->setAttribute('start_date', $dt->toString($user->date_format));
 
 276       if ($val['period_end']) {
 
 277         $dt = new DateAndTime(DB_DATEFORMAT, $val['period_end']);
 
 278         $bean->setAttribute('end_date', $dt->toString($user->date_format));
 
 280       $bean->setAttribute('chclient', $val['show_client']);
 
 281       $bean->setAttribute('chinvoice', $val['show_invoice']);
 
 282       $bean->setAttribute('chpaid', $val['show_paid']);
 
 283       $bean->setAttribute('chproject', $val['show_project']);
 
 284       $bean->setAttribute('chstart', $val['show_start']);
 
 285       $bean->setAttribute('chduration', $val['show_duration']);
 
 286       $bean->setAttribute('chcost', $val['show_cost']);
 
 287       $bean->setAttribute('chtask', $val['show_task']);
 
 288       $bean->setAttribute('chfinish', $val['show_end']);
 
 289       $bean->setAttribute('chnote', $val['show_note']);
 
 290       $bean->setAttribute('chcf_1', $val['show_custom_field_1']);
 
 291       $bean->setAttribute('group_by', $val['group_by']);
 
 292       $bean->setAttribute('chtotalsonly', $val['show_totals_only']);
 
 293       $bean->setAttribute('new_fav_report', $val['name']);
 
 295       $attrs = $bean->getAttributes();
 
 296       $attrs = array_merge($attrs, array(
 
 301         'include_records'=>'',
 
 317         'new_fav_report'=>''));
 
 318       $bean->setAttributes($attrs);