/** * Lists all time records constrained by given constraints * * @return void **/ public function index() { $limit = $this->arguments->getOpt('limit', 20); foreach (Record::all()->limit($limit) as $record) { $line = '(' . $record->user->name . ') ' . $record->task->name . ': ' . $record->description; $this->output->addLine($line); } }
/** * Deletes a time record * * @apiMethod DELETE * @apiUri /api/time/records/{id} * @apiParameter { * "name": "id", * "description": "Record id", * "type": "integer", * "required": true, * "default": null * } * @return void */ public function deleteTask() { // Require authentication and authorization $this->requiresAuthentication(); $this->authorizeOrFail(); // Create object and store content $record = Record::oneOrFail(Request::getInt('id')); if (!$record->isMine()) { App::abort(401, 'Unauthorized'); } // Do the actual save if (!$record->destroy()) { App::abort(500, 'Record deletion failed'); } // Return message $this->send('Record successfully deleted', 200); }
/** * Get summary of time for each person on each day of the week * * @return void */ public static function getTimeForWeeklyBar() { $records = Record::all(); $records->select('user_id')->select('SUM(time)', 'time')->select('DATE_FORMAT(CONVERT_TZ(date, "+00:00", "' . Config::get('offset', '+00:00') . '"), "%Y-%m-%d")', 'day')->group('user_id')->group('day'); $users = [User::get('id')]; // Add extra users for proxies foreach (Proxy::whereEquals('proxy_id', User::get('id')) as $proxy) { $users[] = $proxy->user_id; } $records->whereIn('user_id', $users); // Get the day of the week $today = Date::of(Request::getVar('week', time())); $dateofToday = $today->format('Y-m-d'); $dayOfWeek = $today->format('N') - 1; $records->having('day', '>=', Date::of(strtotime("{$dateofToday} - {$dayOfWeek}days"))->toLocal('Y-m-d'))->having('day', '<', Date::of(strtotime("{$dateofToday} + " . (7 - $dayOfWeek) . 'days'))->toLocal('Y-m-d')); $rows = $records->including('user')->rows(); foreach ($rows as $row) { $row->set('user_name', $row->user->name); } echo json_encode($rows->toArray()); exit; }
/** * 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; }
/** * Saves a new time records, updating it if it alread exists * * @apiMethod POST * @apiUri /time/postRecord * @apiParameter { * "name": "id", * "description": "Record id", * "type": "integer", * "required": false, * "default": null * } * @apiParameter { * "name": "task_id", * "description": "Task ID of record", * "type": "integer", * "required": true, * "default": null * } * @apiParameter { * "name": "start", * "description": "Start date/time of record", * "type": "string", * "required": true, * "default": null * } * @apiParameter { * "name": "end", * "description": "End date/time of record", * "type": "string", * "required": true, * "default": null * } * @apiParameter { * "name": "description", * "description": "Record description", * "type": "string", * "required": false, * "default": null * } * @return void */ public function postRecordTask() { // Require authentication and authorization $this->requiresAuthentication(); $this->authorizeOrFail(); // Incoming posted data (grab individually for added security) $r = []; $r['task_id'] = Request::getInt('task_id'); $r['date'] = Date::of(Request::getVar('start'))->toSql(); $r['end'] = Date::of(Request::getVar('end'))->toSql(); $r['description'] = Request::getVar('description'); $r['time'] = (strtotime($r['end']) - strtotime($r['date'])) / 3600; $r['user_id'] = App::get('authn')['user_id']; // Create object and store content $record = Record::oneOrNew(Request::getInt('id')); $update = false; // See if we have an incoming id, indicating update if (!$record->isNew()) { // Make sure updater is the owner of the record if (!$record->isMine()) { App::abort(401, 'You are only allowed to update your own records'); } $update = true; } // Do the actual save if (!$record->set($r)->save()) { App::abort(500, 'Record creation failed'); } // Return response $response = $update ? 'Record successfully saved' : 'Record successfully created'; $status = $update ? 200 : 201; $this->send($response, $status); }
/** * Delete records * * @return void */ public function deleteTask() { $record = Record::oneOrFail(Request::getInt('id')); // Only allow creator of the record to edit or delete if (!$record->isMine()) { // Set the redirect App::redirect(Route::url($this->base), Lang::txt('COM_TIME_RECORDS_WARNING_CANT_DELETE_OTHER'), 'warning'); return; } // Delete record $record->destroy(); // Set the redirect App::redirect(Route::url($this->base . $this->start($record)), Lang::txt('COM_TIME_RECORDS_DELETE_SUCCESSFUL'), 'passed'); }