/** * Default view function * * @return void */ public function displayTask() { $filters = Filters::getFilters("{$this->_option}.{$this->_controller}"); $records = Record::all(); // Take filters and apply them to the tasks if ($filters['search']) { foreach ($filters['search'] as $term) { $records->where('description', 'LIKE', "%{$term}%", 'and', 1); $records->orWhereRelatedHas('task', function ($task) use($term) { $task->where('name', 'LIKE', "%{$term}%"); }, 1); } } if ($filters['q']) { foreach ($filters['q'] as $q) { if ($q['o'] == '=' && $q['column'] == 'date') { $q['o'] = 'LIKE'; $q['value'] .= '%'; } if ($q['o'] == '!=' && $q['column'] == 'date') { $q['o'] = 'NOT LIKE'; $q['value'] .= '%'; } $records->where($q['column'], $q['o'], $q['value']); } } // Display $this->view->filters = $filters; $this->view->records = $records->paginated()->ordered()->including('task', 'user', 'task.hub'); $this->view->display(); }
/** * 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); } }
/** * 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; }
/** * Lists all applicable time records * * @apiMethod GET * @apiUri /time/indexRecords * @apiParameter { * "name": "tid", * "description": "Task id by which to limit records", * "type": "integer", * "required": false, * "default": null * } * @apiParameter { * "name": "startdate", * "description": "Beginning date threshold", * "type": "string", * "required": false, * "default": null * } * @apiParameter { * "name": "enddate", * "description": "Ending date threshold", * "type": "string", * "required": false, * "default": null * } * @apiParameter { * "name": "limit", * "description": "Maximim number of records to return", * "type": "integer", * "required": false, * "default": 1000 * } * @apiParameter { * "name": "start", * "description": "Record index to start at (for pagination)", * "type": "integer", * "required": false, * "default": 0 * } * @apiParameter { * "name": "orderby", * "description": "Field by which to order results", * "type": "string", * "required": false, * "default": "id" * } * @apiParameter { * "name": "orderdir", * "description": "Direction by which to order results", * "type": "string", * "required": false, * "default": "asc" * } * @return void */ public function indexRecordsTask() { // Require authentication and authorization $this->requiresAuthentication(); $this->authorizeOrFail(); $record = Record::all(); if ($task_id = Request::getInt('tid', false)) { $record->whereEquals('task_id', $task_id); } if ($start_date = Request::getVar('startdate', false)) { $record->where('date', '>=', $start_date); } if ($end_date = Request::getVar('enddate', false)) { $record->where('date', '<=', $end_date); } if ($limit = Request::getInt('limit', 1000)) { $record->limit($limit); } if ($start = Request::getInt('start', 0)) { $record->start($start); } if (($orderby = Request::getCmd('orderby', 'id')) && ($orderdir = Request::getCmd('orderdir', 'asc'))) { $record->order($orderby, $orderdir); } // Create object with records property $response = new stdClass(); $response->records = $record->rows()->toObject(); // Return object $this->send($response); }