From 22f23eb8f2f79853e18a4e4e5ded5c4db66cc5f8 Mon Sep 17 00:00:00 2001 From: Nik Okuntseff Date: Sun, 29 Jul 2018 23:30:38 +0000 Subject: [PATCH] Merged getWhere and getFavWhere into one function. --- WEB-INF/lib/ttFavReportHelper.class.php | 36 ++----- WEB-INF/lib/ttReportHelper.class.php | 138 +++--------------------- WEB-INF/templates/footer.tpl | 2 +- cron.php | 23 ++++ 4 files changed, 45 insertions(+), 154 deletions(-) diff --git a/WEB-INF/lib/ttFavReportHelper.class.php b/WEB-INF/lib/ttFavReportHelper.class.php index 88a1338d..0a21f7c9 100644 --- a/WEB-INF/lib/ttFavReportHelper.class.php +++ b/WEB-INF/lib/ttFavReportHelper.class.php @@ -170,24 +170,9 @@ class ttFavReportHelper { if (!$bean->getAttribute('chunits')) $bean->setAttribute('chunits', 0); if (!$bean->getAttribute('chtotalsonly')) $bean->setAttribute('chtotalsonly', 0); - if ($bean->getAttribute('users') && is_array($bean->getAttribute('users'))) { - $users_in_bean = $bean->getAttribute('users'); - - // If all users are selected - use a null value (which means "all users"). - $all_users_selected = true; - if ($user->can('view_reports')) { - $all = ttTeamHelper::getActiveUsers(); - foreach ($all as $one) { - if (!in_array($one['id'], $users_in_bean)) { - $all_users_selected = false; - break; - } - } - } - if ($all_users_selected) - $users = null; - else - $users = join(',', $users_in_bean); + $users_in_bean = $bean->getAttribute('users'); + if ($users_in_bean && is_array($users_in_bean)) { + $users = join(',', $users_in_bean); } if ($bean->getAttribute('start_date')) { $dt = new DateAndTime($user->date_format, $bean->getAttribute('start_date')); @@ -262,18 +247,7 @@ class ttFavReportHelper { $bean->setAttribute('include_records', $val['billable']); $bean->setAttribute('invoice', $val['invoice']); $bean->setAttribute('paid_status', $val['paid_status']); - if ($val['users']) - $bean->setAttribute('users', explode(',', $val['users'])); - else { - // Null users value means "all users". Add them to the bean. - if ($user->can('view_reports')) { - $all = ttTeamHelper::getActiveUsers(); - foreach ($all as $one) { - $all_user_ids[] = $one['id']; - } - $bean->setAttribute('users', $all_user_ids); - } - } + $bean->setAttribute('users', explode(',', $val['users'])); $bean->setAttribute('period', $val['period']); if ($val['period_start']) { $dt = new DateAndTime(DB_DATEFORMAT, $val['period_start']); @@ -346,6 +320,8 @@ class ttFavReportHelper { unset($options['report_spec']); // Currently not used. unset($options['status']); + // Note: special handling for NULL users field is done in cron.php + // $options now is a subset of db fields from tt_fav_reports table. return $options; } diff --git a/WEB-INF/lib/ttReportHelper.class.php b/WEB-INF/lib/ttReportHelper.class.php index d46e783f..d76b027a 100644 --- a/WEB-INF/lib/ttReportHelper.class.php +++ b/WEB-INF/lib/ttReportHelper.class.php @@ -37,8 +37,7 @@ require_once(dirname(__FILE__).'/../../plugins/CustomFields.class.php'); class ttReportHelper { // getWhere prepares a WHERE clause for a report query. - // Note: $options is a future replacement of $bean, which is work in progress. - static function getWhere($bean, $options) { + static function getWhere($options) { global $user; // Prepare dropdown parts. @@ -57,11 +56,6 @@ class ttReportHelper { if ($options['paid_status']=='1') $dropdown_parts .= ' and l.paid = 1'; if ($options['paid_status']=='2') $dropdown_parts .= ' and l.paid = 0'; - // Note: "Prepare sql query part for user list" is different in getFavWhere because of - // special meaning of NULL value (all "active" users). - // - // If we are merging into one function, one needs to take care of this, perhaps, with redesign. - // Prepare sql query part for user list. $userlist = $options['users'] ? $options['users'] : '-1'; if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) @@ -84,99 +78,8 @@ class ttReportHelper { return $where; } - // getFavWhere prepares a WHERE clause for a favorite report query. - static function getFavWhere($options) { - global $user; - - // Prepare dropdown parts. - $dropdown_parts = ''; - if ($options['client_id']) - $dropdown_parts .= ' and l.client_id = '.$options['client_id']; - elseif ($user->isClient() && $user->client_id) - $dropdown_parts .= ' and l.client_id = '.$user->client_id; - if ($options['cf_1_option_id']) $dropdown_parts .= ' and l.id in(select log_id from tt_custom_field_log where status = 1 and option_id = '.$options['cf_1_option_id'].')'; - if ($options['project_id']) $dropdown_parts .= ' and l.project_id = '.$options['project_id']; - if ($options['task_id']) $dropdown_parts .= ' and l.task_id = '.$options['task_id']; - if ($options['billable']=='1') $dropdown_parts .= ' and l.billable = 1'; - if ($options['billable']=='2') $dropdown_parts .= ' and l.billable = 0'; - if ($options['invoice']=='1') $dropdown_parts .= ' and l.invoice_id is not NULL'; - if ($options['invoice']=='2') $dropdown_parts .= ' and l.invoice_id is NULL'; - if ($options['paid_status']=='1') $dropdown_parts .= ' and l.paid = 1'; - if ($options['paid_status']=='2') $dropdown_parts .= ' and l.paid = 0'; - - // Prepare user list part. - $userlist = -1; - if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) { - if ($options['users']) - $userlist = $options['users']; - else { - $group_users = ttTeamHelper::getUsers(); // active and inactive users - foreach ($group_users as $single_user) - $users[] = $single_user['id']; - $userlist = join(',', $users); - } - } - // Prepare sql query part for user list. - if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) - $user_list_part = " and l.user_id in ($userlist)"; - else - $user_list_part = " and l.user_id = ".$user->id; - $user_list_part .= " and l.group_id = ".$user->getActiveGroup(); - - // Prepare sql query part for where. - if ($options['period']) - $period = new Period($options['period'], new DateAndTime($user->date_format)); - else { - $period = new Period(); - $period->setPeriod( - new DateAndTime($user->date_format, $options['period_start']), - new DateAndTime($user->date_format, $options['period_end'])); - } - $where = " where l.status = 1 and l.date >= '".$period->getStartDate(DB_DATEFORMAT)."' and l.date <= '".$period->getEndDate(DB_DATEFORMAT)."'". - " $user_list_part $dropdown_parts"; - return $where; - } - // getExpenseWhere prepares WHERE clause for expenses query in a report. - static function getExpenseWhere($bean, $options) { - global $user; - - // Prepare dropdown parts. - $dropdown_parts = ''; - if ($options['client_id']) - $dropdown_parts .= ' and l.client_id = '.$options['client_id']; - elseif ($user->isClient() && $user->client_id) - $dropdown_parts .= ' and ei.client_id = '.$user->client_id; - if ($options['project_id']) $dropdown_parts .= ' and ei.project_id = '.$options['project_id']; - if ($options['invoice']=='1') $dropdown_parts .= ' and ei.invoice_id is not NULL'; - if ($options['invoice']=='2') $dropdown_parts .= ' and ei.invoice_id is NULL'; - if ($options['paid_status']=='1') $dropdown_parts .= ' and ei.paid = 1'; - if ($options['paid_status']=='2') $dropdown_parts .= ' and ei.paid = 0'; - - // Prepare sql query part for user list. - $userlist = $options['users'] ? $options['users'] : '-1'; - if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) - $user_list_part = " and ei.user_id in ($userlist)"; - else - $user_list_part = " and ei.user_id = ".$user->id; - $user_list_part .= " and ei.group_id = ".$user->getActiveGroup(); - - // Prepare sql query part for where. - if ($options['period']) - $period = new Period($options['period'], new DateAndTime($user->date_format)); - else { - $period = new Period(); - $period->setPeriod( - new DateAndTime($user->date_format, $options['period_start']), - new DateAndTime($user->date_format, $options['period_end'])); - } - $where = " where ei.status = 1 and ei.date >= '".$period->getStartDate(DB_DATEFORMAT)."' and ei.date <= '".$period->getEndDate(DB_DATEFORMAT)."'". - " $user_list_part $dropdown_parts"; - return $where; - } - - // getFavExpenseWhere prepares a WHERE clause for expenses query in a favorite report. - static function getFavExpenseWhere($options) { + static function getExpenseWhere($options) { global $user; // Prepare dropdown parts. @@ -191,19 +94,8 @@ class ttReportHelper { if ($options['paid_status']=='1') $dropdown_parts .= ' and ei.paid = 1'; if ($options['paid_status']=='2') $dropdown_parts .= ' and ei.paid = 0'; - // Prepare user list part. - $userlist = -1; - if (($user->can('view_reports') || $user->isClient())) { - if ($options['users']) - $userlist = $options['users']; - else { - $active_users = ttTeamHelper::getActiveUsers(); - foreach ($active_users as $single_user) - $users[] = $single_user['id']; - $userlist = join(',', $users); - } - } // Prepare sql query part for user list. + $userlist = $options['users'] ? $options['users'] : '-1'; if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) $user_list_part = " and ei.user_id in ($userlist)"; else @@ -333,7 +225,7 @@ class ttReportHelper { if ($includeCost && MODE_TIME != $user->tracking_mode) $left_joins .= " left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id)"; - $where = ttReportHelper::getWhere($bean, $options); + $where = ttReportHelper::getWhere($options); // Construct sql query for tt_log items. $sql = "select ".join(', ', $fields)." from tt_log l $left_joins $where"; @@ -401,7 +293,7 @@ class ttReportHelper { if (($canViewReports || $isClient) && $bean->getAttribute('chinvoice')) $left_joins .= " left join tt_invoices i on (i.id = ei.invoice_id and i.status = 1)"; - $where = ttReportHelper::getExpenseWhere($bean, $options); + $where = ttReportHelper::getExpenseWhere($options); // Construct sql query for expense items. $sql_for_expense_items = "select ".join(', ', $fields)." from tt_expense_items ei $left_joins $where"; @@ -608,7 +500,7 @@ class ttReportHelper { if ($includeCost && MODE_TIME != $user->tracking_mode) $left_joins .= " left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id)"; - $where = ttReportHelper::getFavWhere($options); + $where = ttReportHelper::getWhere($options); // Construct sql query for tt_log items. $sql = "select ".join(', ', $fields)." from tt_log l $left_joins $where"; @@ -674,7 +566,7 @@ class ttReportHelper { if (($canViewReports || $isClient) && $options['show_invoice']) $left_joins .= " left join tt_invoices i on (i.id = ei.invoice_id and i.status = 1)"; - $where = ttReportHelper::getFavExpenseWhere($options); + $where = ttReportHelper::getExpenseWhere($options); // Construct sql query for expense items. $sql_for_expense_items = "select ".join(', ', $fields)." from tt_expense_items ei $left_joins $where"; @@ -784,7 +676,7 @@ class ttReportHelper { break; } - $where = ttReportHelper::getWhere($bean, $options); + $where = ttReportHelper::getWhere($options); if ($bean->getAttribute('chcost')) { if (MODE_TIME == $user->tracking_mode) { if ($group_by_option != 'user') @@ -851,7 +743,7 @@ class ttReportHelper { break; } - $where = ttReportHelper::getExpenseWhere($bean, $options); + $where = ttReportHelper::getExpenseWhere($options); $sql_for_expenses = "select $group_field as group_field, null as time"; if ($bean->getAttribute('chunits')) $sql_for_expenses .= ", null as units"; $sql_for_expenses .= ", sum(ei.cost) as cost, sum(ei.cost) as expenses from tt_expense_items ei $group_join $where"; @@ -935,7 +827,7 @@ class ttReportHelper { break; } - $where = ttReportHelper::getFavWhere($options); + $where = ttReportHelper::getWhere($options); if ($options['show_cost']) { if (MODE_TIME == $user->tracking_mode) { if ($group_by_option != 'user') @@ -1003,7 +895,7 @@ class ttReportHelper { break; } - $where = ttReportHelper::getFavExpenseWhere($options); + $where = ttReportHelper::getExpenseWhere($options); $sql_for_expenses = "select $group_field as group_field, null as time"; if ($options['show_work_units']) $sql_for_expenses .= ", null as units"; $sql_for_expenses .= ", sum(ei.cost) as cost, sum(ei.cost) as expenses from tt_expense_items ei $group_join $where"; @@ -1049,7 +941,7 @@ class ttReportHelper { $mdb2 = getConnection(); - $where = ttReportHelper::getWhere($bean, $options); + $where = ttReportHelper::getWhere($options); // Prepare parts. $time_part = "sum(time_to_sec(l.duration)) as time"; @@ -1076,7 +968,7 @@ class ttReportHelper { // If we have expenses, query becomes a bit more complex. if ($bean->getAttribute('chcost') && $user->isPluginEnabled('ex')) { - $where = ttReportHelper::getExpenseWhere($bean, $options); + $where = ttReportHelper::getExpenseWhere($options); $sql_for_expenses = "select null as time"; if ($bean->getAttribute('chunits')) $sql_for_expenses .= ", null as units"; $sql_for_expenses .= ", sum(cost) as cost, sum(cost) as expenses from tt_expense_items ei $where"; @@ -1131,7 +1023,7 @@ class ttReportHelper { $mdb2 = getConnection(); - $where = ttReportHelper::getFavWhere($options); + $where = ttReportHelper::getWhere($options); // Prepare parts. $time_part = "sum(time_to_sec(l.duration)) as time"; @@ -1158,7 +1050,7 @@ class ttReportHelper { // If we have expenses, query becomes a bit more complex. if ($options['show_cost'] && $user->isPluginEnabled('ex')) { - $where = ttReportHelper::getFavExpenseWhere($options); + $where = ttReportHelper::getExpenseWhere($options); $sql_for_expenses = "select null as time"; if ($options['show_work_units']) $sql_for_expenses .= ", null as units"; $sql_for_expenses .= ", sum(cost) as cost, sum(cost) as expenses from tt_expense_items ei $where"; diff --git a/WEB-INF/templates/footer.tpl b/WEB-INF/templates/footer.tpl index c8f66d4f..fc683d0c 100644 --- a/WEB-INF/templates/footer.tpl +++ b/WEB-INF/templates/footer.tpl @@ -12,7 +12,7 @@
-
 Anuko Time Tracker 1.17.97.4302 | Copyright © Anuko | +  Anuko Time Tracker 1.17.97.4303 | Copyright © Anuko | {$i18n.footer.credits} | {$i18n.footer.license} | {$i18n.footer.improve} diff --git a/cron.php b/cron.php index 94a20dda..473f27ef 100644 --- a/cron.php +++ b/cron.php @@ -66,6 +66,29 @@ while ($val = $res->fetchRow()) { $user = new ttUser(null, $options['user_id']); if (!$user->id) continue; // Skip not found user. + // Special handling of the NULL $options['users'] field (this used to mean "all users"). + if (!$options['users']) { + if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) { + if ($user->can('view_reports') || $user->can('view_all_reports')) { + $max_rank = $user->rank-1; + if ($user->can('view_all_reports')) $max_rank = 512; + if ($user->can('view_own_reports')) + $user_options = array('max_rank'=>$max_rank,'include_self'=>true); + else + $user_options = array('max_rank'=>$max_rank); + $users = $user->getUsers($user_options); // Active and inactive users. + } elseif ($user->isClient()) { + $users = ttTeamHelper::getUsersForClient(); // Active and inactive users for clients. + } + foreach ($users as $single_user) { + $user_ids[] = $single_user['id']; + } + $options['users'] = implode(',', $user_ids); + } + } else { + // TODO: add checking the existing user list for potentially changed access rights for user. + } + // Skip users with disabled Notifications plugin. if (!$user->isPluginEnabled('no')) continue; -- 2.20.1