/** * Checks to ensure appropriate authorization * * @return bool * @throws Exception */ private function authorizeOrFail() { $permissions = new Permissions('com_time'); // Make sure action can be performed if (!$permissions->can('api')) { App::abort(401, 'Unauthorized'); } return true; }
/** * Get time of each task * * @return void */ public static function getTimePerTask() { $permissions = new Permissions('com_time'); $hub_id = Request::getInt('hub_id', null); $task_id = Request::getInt('task_id', null); $start = Request::getCmd('start_date', Date::of(strtotime('today - 1 month'))->format('Y-m-d')); $end = Request::getCmd('end_date', Date::format('Y-m-d')); $tasks = Task::blank(); $records = Record::all(); $records = $records->select('SUM(time)', 'hours')->select($records->getQualifiedFieldName('id'))->select('task_id')->select($tasks->getQualifiedFieldName('name'))->join($tasks->getTableName(), 'task_id', $tasks->getQualifiedFieldName('id'))->where('date', '>=', Date::of($start . ' 00:00:00', Config::get('offset'))->toSql())->where('date', '<=', Date::of($end . ' 23:59:59', Config::get('offset'))->toSql())->order('hours', 'asc')->group('task_id'); if (isset($task_id) && $task_id > 0) { $records->whereEquals('task_id', $task_id); } else { if (isset($hub_id) && $hub_id > 0) { $records->whereRelatedHas('task', function ($task) use($hub_id) { $task->whereEquals('hub_id', $hub_id); }); } } $summary = array(); // Loop through and check permissions and grab raw object from rows foreach ($records->including('task') as $record) { if ($permissions->can('view.report', 'hubs', $record->task->hub_id)) { $summary[] = $record->toObject(); } } echo json_encode($summary); exit; }
/** * Download CSV * * @return void */ public static function download() { // Load language Lang::load('plg_time_csv', __DIR__); $hub_id = Request::getInt('hub_id', null); $start = Request::getCmd('start_date', Date::of(strtotime('today - 1 month'))->format('Y-m-d')); $end = Request::getCmd('end_date', Date::format('Y-m-d')); $records = Record::all()->where('date', '>=', $start)->where('date', '<=', Date::of(strtotime($end . ' + 1 day'))->format('Y-m-d'))->order('date', 'asc'); if (isset($hub_id) && $hub_id > 0) { // @FIXME: is there a better way to do this? $records->whereIn('task_id', Task::select('id')->whereEquals('hub_id', $hub_id)->rows()->fieldsByKey('id')); $hubname = Hub::oneOrFail($hub_id)->name_normalized; } $all = true; foreach (Request::query() as $key => $value) { if (strpos($key, 'fields-') !== false) { $all = false; } } $filename = 'time_report'; $filename .= isset($hubname) ? '_' . $hubname : ''; $filename .= '_' . Date::of($start)->format('Ymd'); $filename .= '-' . Date::of($end)->format('Ymd'); $filename .= '.csv'; // Set content type headers header("Content-type: application/csv"); header("Content-Disposition: attachment; filename={$filename}"); header("Pragma: no-cache"); header("Expires: 0"); $row = array(); if ($hub = Request::getInt('fields-hub', $all)) { $row[] = Lang::txt('PLG_TIME_CSV_HUB'); } if ($task = Request::getInt('fields-task', $all)) { $row[] = Lang::txt('PLG_TIME_CSV_TASK'); } if ($user = Request::getInt('fields-user', $all)) { $row[] = Lang::txt('PLG_TIME_CSV_USER'); } if ($date = Request::getInt('fields-date', $all)) { $row[] = Lang::txt('PLG_TIME_CSV_DATE'); } if ($time = Request::getInt('fields-time', $all)) { $row[] = Lang::txt('PLG_TIME_CSV_TIME'); } if ($description = Request::getInt('fields-description', $all)) { $row[] = Lang::txt('PLG_TIME_CSV_DESCRIPTION'); } echo implode(',', $row) . "\n"; $permissions = new Permissions('com_time'); foreach ($records->including('task.hub', 'user') as $record) { if ($permissions->can('view.report', 'hub', $record->task->hub_id)) { $output = fopen('php://output', 'w'); $row = array(); if ($hub) { $row[] = $record->task->hub->name; } if ($task) { $row[] = $record->task->name; } if ($user) { $row[] = $record->user->name; } if ($date) { $row[] = Date::of($record->date)->toLocal(); } if ($time) { $row[] = $record->time; } if ($description) { $row[] = $record->description; } fputcsv($output, $row); fclose($output); } } exit; }