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   // get - returns a report identified by its id for user.
 
  57   static function get($id) {
 
  59     $mdb2 = getConnection();
 
  61     $user_id = $user->getUser();
 
  62     $group_id = $user->getGroup();
 
  63     $org_id = $user->org_id;
 
  65     $sql = "select * from tt_fav_reports".
 
  66       " where id = $id and user_id = $user_id and group_id = $group_id and org_id = $org_id and status = 1";
 
  67     $res = $mdb2->query($sql);
 
  68     if (!is_a($res, 'PEAR_Error')) {
 
  69       if ($val = $res->fetchRow()) {
 
  75   // getReport - returns a report identified by its id.
 
  76   // TODO: get rid of this function by encapsulating all cron related tasks in its own class.
 
  77   // Because cron works for all orgs and we want this class to always work in context of
 
  78   // a logged on user, for better security.
 
  79   static function getReport($id) {
 
  80     $mdb2 = getConnection();
 
  82     $sql = "select * from tt_fav_reports where id = $id and status = 1";
 
  83     $res = $mdb2->query($sql);
 
  84     if (!is_a($res, 'PEAR_Error')) {
 
  85       if ($val = $res->fetchRow()) {
 
  92   // getReportByName - returns a report identified by its name.
 
  93   static function getReportByName($user_id, $report_name) {
 
  94     $mdb2 = getConnection();
 
  96     $sql = "select * from tt_fav_reports where user_id = $user_id and status = 1 and name = ".$mdb2->quote($report_name);
 
  97     $res = $mdb2->query($sql);
 
  98     if (!is_a($res, 'PEAR_Error')) {
 
  99       if ($val = $res->fetchRow()) {
 
 106   // insertReport - stores reports settings in database.
 
 107   static function insertReport($fields) {
 
 109     $mdb2 = getConnection();
 
 111     $group_id = $user->getGroup();
 
 112     $org_id = $user->org_id;
 
 114     $sql = "insert into tt_fav_reports".
 
 115       " (name, user_id, group_id, org_id, client_id, cf_1_option_id, project_id, task_id,".
 
 116       " billable, invoice, paid_status, users, period, period_start, period_end,".
 
 117       " show_client, show_invoice, show_paid, show_ip,".
 
 118       " show_project, show_start, show_duration, show_cost,".
 
 119       " show_task, show_end, show_note, show_custom_field_1, show_work_units,".
 
 120       " group_by1, group_by2, group_by3, show_totals_only)".
 
 122       $mdb2->quote($fields['name']).", ".$fields['user_id'].", $group_id, $org_id, ".
 
 123       $mdb2->quote($fields['client']).", ".$mdb2->quote($fields['option']).", ".
 
 124       $mdb2->quote($fields['project']).", ".$mdb2->quote($fields['task']).", ".
 
 125       $mdb2->quote($fields['billable']).", ".$mdb2->quote($fields['invoice']).", ".
 
 126       $mdb2->quote($fields['paid_status']).", ".
 
 127       $mdb2->quote($fields['users']).", ".$mdb2->quote($fields['period']).", ".
 
 128       $mdb2->quote($fields['from']).", ".$mdb2->quote($fields['to']).", ".
 
 129       $fields['chclient'].", ".$fields['chinvoice'].", ".$fields['chpaid'].", ".$fields['chip'].", ".
 
 130       $fields['chproject'].", ".$fields['chstart'].", ".$fields['chduration'].", ".$fields['chcost'].", ".
 
 131       $fields['chtask'].", ".$fields['chfinish'].", ".$fields['chnote'].", ".$fields['chcf_1'].", ".$fields['chunits'].", ".
 
 132       $mdb2->quote($fields['group_by1']).", ".$mdb2->quote($fields['group_by2']).", ".
 
 133       $mdb2->quote($fields['group_by3']).", ".$fields['chtotalsonly'].")";
 
 134     $affected = $mdb2->exec($sql);
 
 135     if (is_a($affected, 'PEAR_Error'))
 
 138     $last_id = $mdb2->lastInsertID('tt_fav_reports', 'id');
 
 142   // updateReport - updates report options in the database.
 
 143   function updateReport($fields) {
 
 144     $mdb2 = getConnection();
 
 145     $sql = "update tt_fav_reports set ".
 
 146       "name = ".$mdb2->quote($fields['name']).", ".
 
 147       "client_id = ".$mdb2->quote($fields['client']).", ".
 
 148       "cf_1_option_id = ".$mdb2->quote($fields['option']).", ".
 
 149       "project_id = ".$mdb2->quote($fields['project']).", ".
 
 150       "task_id = ".$mdb2->quote($fields['task']).", ".
 
 151       "billable = ".$mdb2->quote($fields['billable']).", ".
 
 152       "invoice = ".$mdb2->quote($fields['invoice']).", ".
 
 153       "paid_status = ".$mdb2->quote($fields['paid_status']).", ".
 
 154       "users = ".$mdb2->quote($fields['users']).", ".
 
 155       "period = ".$mdb2->quote($fields['period']).", ".
 
 156       "period_start = ".$mdb2->quote($fields['from']).", ".
 
 157       "period_end = ".$mdb2->quote($fields['to']).", ".
 
 158       "show_client = ".$fields['chclient'].", ".
 
 159       "show_invoice = ".$fields['chinvoice'].", ".
 
 160       "show_paid = ".$fields['chpaid'].", ".
 
 161       "show_ip = ".$fields['chip'].", ".
 
 162       "show_project = ".$fields['chproject'].", ".
 
 163       "show_start = ".$fields['chstart'].", ".
 
 164       "show_duration = ".$fields['chduration'].", ".
 
 165       "show_cost = ".$fields['chcost'].", ".
 
 166       "show_task = ".$fields['chtask'].", ".
 
 167       "show_end = ".$fields['chfinish'].", ".
 
 168       "show_note = ".$fields['chnote'].", ".
 
 169       "show_custom_field_1 = ".$fields['chcf_1'].", ".
 
 170       "show_work_units = ".$fields['chunits'].", ".
 
 171       "group_by1 = ".$mdb2->quote($fields['group_by1']).", ".
 
 172       "group_by2 = ".$mdb2->quote($fields['group_by2']).", ".
 
 173       "group_by3 = ".$mdb2->quote($fields['group_by3']).", ".
 
 174       "show_totals_only = ".$fields['chtotalsonly'].
 
 175       " where id = ".$fields['id'];
 
 176     $affected = $mdb2->exec($sql);
 
 177     if (is_a($affected, 'PEAR_Error'))
 
 180     return $fields['id'];
 
 183   // saveReport - saves report options in the database.
 
 184   static function saveReport($user_id, $bean) {
 
 187     //  Set default value of 0 for not set checkboxes (in bean).
 
 188     //  Later in this function we use it to construct $fields array to update database.
 
 189     if (!$bean->getAttribute('chclient')) $bean->setAttribute('chclient', 0);
 
 190     if (!$bean->getAttribute('chinvoice')) $bean->setAttribute('chinvoice', 0);
 
 191     if (!$bean->getAttribute('chpaid')) $bean->setAttribute('chpaid', 0);
 
 192     if (!$bean->getAttribute('chip')) $bean->setAttribute('chip', 0);
 
 193     if (!$bean->getAttribute('chproject')) $bean->setAttribute('chproject', 0);
 
 194     if (!$bean->getAttribute('chstart')) $bean->setAttribute('chstart', 0);
 
 195     if (!$bean->getAttribute('chduration')) $bean->setAttribute('chduration', 0);
 
 196     if (!$bean->getAttribute('chcost')) $bean->setAttribute('chcost', 0);
 
 197     if (!$bean->getAttribute('chtask')) $bean->setAttribute('chtask', 0);
 
 198     if (!$bean->getAttribute('chfinish')) $bean->setAttribute('chfinish', 0);
 
 199     if (!$bean->getAttribute('chnote')) $bean->setAttribute('chnote', 0);
 
 200     if (!$bean->getAttribute('chcf_1')) $bean->setAttribute('chcf_1', 0);
 
 201     if (!$bean->getAttribute('chunits')) $bean->setAttribute('chunits', 0);
 
 202     if (!$bean->getAttribute('chtotalsonly')) $bean->setAttribute('chtotalsonly', 0);
 
 204     $users_in_bean = $bean->getAttribute('users');
 
 205     if ($users_in_bean && is_array($users_in_bean)) {
 
 206       $users = join(',', $users_in_bean);
 
 208     if ($bean->getAttribute('start_date')) {
 
 209       $dt = new DateAndTime($user->date_format, $bean->getAttribute('start_date'));
 
 210       $from = $dt->toString(DB_DATEFORMAT);
 
 212     if ($bean->getAttribute('end_date')) {
 
 213       $dt = new DateAndTime($user->date_format, $bean->getAttribute('end_date'));
 
 214       $to = $dt->toString(DB_DATEFORMAT);
 
 218       'name'=>$bean->getAttribute('new_fav_report'),
 
 219       'client'=>$bean->getAttribute('client'),
 
 220       'option'=>$bean->getAttribute('option'),
 
 221       'project'=>$bean->getAttribute('project'),
 
 222       'task'=>$bean->getAttribute('task'),
 
 223       'billable'=>$bean->getAttribute('include_records'),
 
 224       'invoice'=>$bean->getAttribute('invoice'),
 
 225       'paid_status'=>$bean->getAttribute('paid_status'),
 
 227       'period'=>$bean->getAttribute('period'),
 
 230       'chclient'=>$bean->getAttribute('chclient'),
 
 231       'chinvoice'=>$bean->getAttribute('chinvoice'),
 
 232       'chpaid'=>$bean->getAttribute('chpaid'),
 
 233       'chip'=>$bean->getAttribute('chip'),
 
 234       'chproject'=>$bean->getAttribute('chproject'),
 
 235       'chstart'=>$bean->getAttribute('chstart'),
 
 236       'chduration'=>$bean->getAttribute('chduration'),
 
 237       'chcost'=>$bean->getAttribute('chcost'),
 
 238       'chtask'=>$bean->getAttribute('chtask'),
 
 239       'chfinish'=>$bean->getAttribute('chfinish'),
 
 240       'chnote'=>$bean->getAttribute('chnote'),
 
 241       'chcf_1'=>$bean->getAttribute('chcf_1'),
 
 242       'chunits'=>$bean->getAttribute('chunits'),
 
 243       'group_by1'=>$bean->getAttribute('group_by1'),
 
 244       'group_by2'=>$bean->getAttribute('group_by2'),
 
 245       'group_by3'=>$bean->getAttribute('group_by3'),
 
 246       'chtotalsonly'=>$bean->getAttribute('chtotalsonly'));
 
 249     $report = ttFavReportHelper::getReportByName($user_id, $fields['name']);
 
 251       $fields['id'] = $report['id'];
 
 252       $id = ttFavReportHelper::updateReport($fields);
 
 254       $fields['user_id'] = $user_id;
 
 255       $id = ttFavReportHelper::insertReport($fields);
 
 261   // deleteReport - deletes a favorite report.
 
 262   static function deleteReport($id) {
 
 263     $mdb2 = getConnection();
 
 265     $sql = "delete from tt_fav_reports where id = $id";
 
 266     $affected = $mdb2->exec($sql);
 
 267     return (!is_a($affected, 'PEAR_Error'));
 
 270   // loadReport - loads report options from database into a bean.
 
 271   static function loadReport($user_id, &$bean) {
 
 274     $val = ttFavReportHelper::getReport($bean->getAttribute('favorite_report'));
 
 276       $bean->setAttribute('client', $val['client_id']);
 
 277       $bean->setAttribute('option', $val['cf_1_option_id']);
 
 278       $bean->setAttribute('project', $val['project_id']);
 
 279       $bean->setAttribute('task', $val['task_id']);
 
 280       $bean->setAttribute('include_records', $val['billable']);
 
 281       $bean->setAttribute('invoice', $val['invoice']);
 
 282       $bean->setAttribute('paid_status', $val['paid_status']);
 
 283       $bean->setAttribute('users', explode(',', $val['users']));
 
 284       $bean->setAttribute('period', $val['period']);
 
 285       if ($val['period_start']) {
 
 286         $dt = new DateAndTime(DB_DATEFORMAT, $val['period_start']);
 
 287         $bean->setAttribute('start_date', $dt->toString($user->date_format));
 
 289       if ($val['period_end']) {
 
 290         $dt = new DateAndTime(DB_DATEFORMAT, $val['period_end']);
 
 291         $bean->setAttribute('end_date', $dt->toString($user->date_format));
 
 293       $bean->setAttribute('chclient', $val['show_client']);
 
 294       $bean->setAttribute('chinvoice', $val['show_invoice']);
 
 295       $bean->setAttribute('chpaid', $val['show_paid']);
 
 296       $bean->setAttribute('chip', $val['show_ip']);
 
 297       $bean->setAttribute('chproject', $val['show_project']);
 
 298       $bean->setAttribute('chstart', $val['show_start']);
 
 299       $bean->setAttribute('chduration', $val['show_duration']);
 
 300       $bean->setAttribute('chcost', $val['show_cost']);
 
 301       $bean->setAttribute('chtask', $val['show_task']);
 
 302       $bean->setAttribute('chfinish', $val['show_end']);
 
 303       $bean->setAttribute('chnote', $val['show_note']);
 
 304       $bean->setAttribute('chcf_1', $val['show_custom_field_1']);
 
 305       $bean->setAttribute('chunits', $val['show_work_units']);
 
 306       $bean->setAttribute('group_by1', $val['group_by1']);
 
 307       $bean->setAttribute('group_by2', $val['group_by2']);
 
 308       $bean->setAttribute('group_by3', $val['group_by3']);
 
 309       $bean->setAttribute('chtotalsonly', $val['show_totals_only']);
 
 310       $bean->setAttribute('new_fav_report', $val['name']);
 
 312       $attrs = $bean->getAttributes();
 
 313       $attrs = array_merge($attrs, array(
 
 318         'include_records'=>'',
 
 337         'new_fav_report'=>''));
 
 338       $bean->setAttributes($attrs);
 
 342   // getReportOptions - returns an array of fav report options from database data.
 
 343   // Note: this function is a part of refactoring to simplify maintenance of report
 
 344   // generating functions, as we currently have 2 sets: normal reporting (from bean),
 
 345   // and fav report emailing (from db fields). Using options obtained from either db or bean
 
 346   // shall allow us to use only one set of functions.
 
 347   static function getReportOptions($id) {
 
 349     // Start with getting the fields from the database.
 
 350     $db_fields = ttFavReportHelper::getReport($id);
 
 351     if (!$db_fields) return false;
 
 353     // Prepare an array of report options.
 
 354     $options = $db_fields; // For now, use db field names as options.
 
 355     // Drop things we don't need in reports.
 
 356     unset($options['id']);
 
 357     unset($options['report_spec']); // Currently not used.
 
 358     unset($options['status']);
 
 360     // Note: special handling for NULL users field is done in cron.php
 
 362     // $options now is a subset of db fields from tt_fav_reports table.
 
 366   // adjustOptions takes and array or report options and adjusts them for current user
 
 367   // (and group) settings. This is needed in situations when a fav report is stored in db
 
 368   // long ago, but user or group attributes are now changed, so we have to adjust.
 
 369   static function adjustOptions($options) {
 
 372     // Check and optionally adjust users.
 
 373     // Special handling of the NULL $options['users'] field (this used to mean "all users").
 
 374     if (!$options['users']) {
 
 375       if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) {
 
 376         if ($user->can('view_reports') || $user->can('view_all_reports')) {
 
 377           $max_rank = $user->rank-1;
 
 378           if ($user->can('view_all_reports')) $max_rank = 512;
 
 379           if ($user->can('view_own_reports'))
 
 380             $user_options = array('max_rank'=>$max_rank,'include_self'=>true);
 
 382             $user_options = array('max_rank'=>$max_rank);
 
 383           $users = $user->getUsers($user_options); // Active and inactive users.
 
 384         } elseif ($user->isClient()) {
 
 385           $users = ttTeamHelper::getUsersForClient(); // Active and inactive users for clients.
 
 387         foreach ($users as $single_user) {
 
 388           $user_ids[] = $single_user['id'];
 
 390         $options['users'] = implode(',', $user_ids);
 
 393       $users_to_adjust = explode(',', $options['users']); // Users to adjust.
 
 394       if ($user->isClient()) {
 
 395         $users = ttTeamHelper::getUsersForClient(); // Active and inactive users for clients.
 
 396         foreach ($users as $single_user) {
 
 397           $user_ids[] = $single_user['id'];
 
 399         foreach ($users_to_adjust as $user_to_adjust) {
 
 400           if (in_array($user_to_adjust['id'], $user_ids)) {
 
 401             $adjusted_user_ids[] = $user_to_adjust['id'];
 
 404         $options['users'] = implode(',', $adjusted_user_ids);
 
 406       // TODO: add checking the existing user list for potentially changed access rights for user.