diff --git a/app/Http/Controllers/PaginatedAjaxController.php b/app/Http/Controllers/PaginatedAjaxController.php index aca30ae9ff..534a68edd0 100644 --- a/app/Http/Controllers/PaginatedAjaxController.php +++ b/app/Http/Controllers/PaginatedAjaxController.php @@ -33,6 +33,12 @@ use Illuminate\Support\Collection; abstract class PaginatedAjaxController extends Controller { + /** + * Default sort, column => direction + * @var array + */ + protected $default_sort = []; + /** * Base rules for this controller. * @@ -88,6 +94,18 @@ abstract class PaginatedAjaxController extends Controller return []; } + /** + * Defines sortable fields. The incoming sort field should be the key, the sql column or DB::raw() should be the value + * + * @param \Illuminate\Http\Request $request + * @return array + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + protected function sortFields($request) + { + return []; + } + /** * Format an item for display. Default is pass-through * @@ -148,9 +166,14 @@ abstract class PaginatedAjaxController extends Controller */ protected function sort($request, $query) { - $sort = $request->get('sort', []); + $columns = $this->sortFields($request); + + $sort = $request->get('sort', $this->default_sort); + foreach ($sort as $column => $direction) { - $query->orderBy($column, $direction); + if (isset($columns[$column])) { + $query->orderBy($columns[$column], $direction == 'desc' ? 'desc' : 'asc'); + } } return $query; diff --git a/app/Http/Controllers/Table/AlertScheduleController.php b/app/Http/Controllers/Table/AlertScheduleController.php new file mode 100644 index 0000000000..385c4a2b10 --- /dev/null +++ b/app/Http/Controllers/Table/AlertScheduleController.php @@ -0,0 +1,101 @@ +. + * + * @package LibreNMS + * @link http://librenms.org + * @copyright 2020 Tony Murray + * @author Tony Murray + */ + +namespace App\Http\Controllers\Table; + +use App\Models\AlertSchedule; +use Carbon\Carbon; +use DB; + +class AlertScheduleController extends TableController +{ + protected $default_sort = ['title' => 'asc', 'start' => 'asc']; + + protected function baseQuery($request) + { + return AlertSchedule::query(); + } + + protected function searchFields($request) + { + return['title', 'start', 'end']; + } + + protected function sortFields($request) + { + return [ + 'start_recurring_dt' => DB::raw('DATE(`start`)'), + 'start_recurring_ht' => DB::raw('TIME(`start`)'), + 'end_recurring_dt' => DB::raw('DATE(`end`)'), + 'end_recurring_ht' => DB::raw('TIME(`end`)'), + 'title' => 'title', + 'recurring' => 'recurring', + 'start' => 'start', + 'end' => 'end', + 'status' => DB::raw("end < '" . Carbon::now('UTC') ."'"), // only partition lapsed + ]; + } + + /** + * @param AlertSchedule $schedule + * @return array + */ + public function formatItem($schedule) + { + return [ + 'title' => $schedule->title, + 'notes' => $schedule->notes, + 'id' => $schedule->schedule_id, + 'start' => $schedule->recurring ? '' : $schedule->start->toDateTimeString('minutes'), + 'end' => $schedule->recurring ? '' : $schedule->end->toDateTimeString('minutes'), + 'start_recurring_dt' => $schedule->recurring ? $schedule->start_recurring_dt : '', + 'start_recurring_hr' => $schedule->recurring ? $schedule->start_recurring_hr : '', + 'end_recurring_dt' => $schedule->recurring ? $schedule->end_recurring_dt : '', + 'end_recurring_hr' => $schedule->recurring ? $schedule->end_recurring_hr : '', + 'recurring' => $schedule->recurring ? __('Yes') : __('No'), + 'recurring_day' => $schedule->recurring ? implode(',', $schedule->recurring_day) : '', + 'status' => $schedule->status, + ]; + } + +// /** +// * @param Request $request +// * @param Builder $query +// * @return Builder +// */ +// protected function sort($request, $query) +// { +// $columns = $this->sortFields($request); +// $sort = $request->get('sort', $this->default_sort); +// +// foreach ($sort as $column => $direction) { +// if (isset($columns[$column])) { +// $query->orderBy($columns[$column], $direction == 'desc' ? 'desc' : 'asc'); +// } +// } +// +// return $query; +// } +} \ No newline at end of file diff --git a/app/Http/Controllers/Table/TableController.php b/app/Http/Controllers/Table/TableController.php index 7ee2d6738d..5acffbfb70 100644 --- a/app/Http/Controllers/Table/TableController.php +++ b/app/Http/Controllers/Table/TableController.php @@ -32,7 +32,17 @@ use Illuminate\Http\Request; abstract class TableController extends PaginatedAjaxController { - protected $default_sort = []; + protected $model; + + protected function sortFields($request) + { + if (isset($this->model)) { + $fields = \Schema::getColumnListing((new $this->model)->getTable()); + return array_combine($fields, $fields); + } + + return []; + } final protected function baseRules() { diff --git a/includes/html/pages/alert-schedule.inc.php b/includes/html/pages/alert-schedule.inc.php index b18ae3dd6c..bc4b3cd84c 100644 --- a/includes/html/pages/alert-schedule.inc.php +++ b/includes/html/pages/alert-schedule.inc.php @@ -77,12 +77,7 @@ var grid = $("#alert-schedule").bootgrid({ "

" }, rowCount: [50, 100, 250, -1], - post: function () { - return { - id: "alert-schedule", - }; - }, - url: "ajax_table.php" + url: "ajax/table/alert-schedule" }).on("loaded.rs.jquery.bootgrid", function() { /* Executes after data is loaded and rendered */ grid.find(".command-edit").on("click", function(e) { diff --git a/includes/html/table/alert-schedule.inc.php b/includes/html/table/alert-schedule.inc.php deleted file mode 100644 index 96d27557ec..0000000000 --- a/includes/html/table/alert-schedule.inc.php +++ /dev/null @@ -1,88 +0,0 @@ - - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. Please see LICENSE.txt at the top level of - * the source code distribution for details. - */ - -use Carbon\Carbon; - -if (!Auth::user()->hasGlobalRead()) { - return [ - 'current' => 0, - 'rowCount' => 0, - 'rows' => [], - 'total' => 0, - ]; -} - -$query = \App\Models\AlertSchedule::query() - ->when($searchPhrase, function ($query, $searchPhrase) { - $query->where(function ($query) use ($searchPhrase) { - $query->where('title', 'like', "%$searchPhrase%") - ->orWhere('start', 'like', "%$searchPhrase%") - ->orWhere('end', 'like', "%$searchPhrase%"); - }); - }); - -$total = $query->count(); - -if ($rowCount != -1) { - $query->offset(($current * $rowCount) - $rowCount) - ->limit($rowCount); -} - -if (isset($sort) && !empty($sort)) { - list($sort_column, $sort_order) = explode(' ', trim($sort)); - $columns = [ - 'start_recurring_dt' => DB::raw('DATE(`start`)'), - 'start_recurring_ht' => DB::raw('TIME(`start`)'), - 'end_recurring_dt' => DB::raw('DATE(`end`)'), - 'end_recurring_ht' => DB::raw('TIME(`end`)'), - 'title' => 'title', - 'recurring' => 'recurring', - 'start' => 'start', - 'end' => 'end', - 'status' => DB::raw("end < '" . Carbon::now('UTC') ."'"), // only partition lapsed - ]; - if (array_key_exists($sort_column, $columns)) { - $query->orderBy($columns[$sort_column], $sort_order == 'asc' ? 'asc' : 'desc')->orderBy('title'); - } -} else { - $query->orderBy('start')->orderBy('title'); -} - -$now = Carbon::now(); - -//header('Caontent-Type: text/html'); - -$schedules = $query->get()->map(function ($schedule) use ($now) { - /** @var \App\Models\AlertSchedule $schedule */ - $data = $schedule->only(['title', 'notes']); - $data['id'] = $schedule->schedule_id; - $data['start'] = $schedule->recurring ? '' : $schedule->start->toDateTimeString('minutes'); - $data['end'] = $schedule->recurring ? '' : $schedule->end->toDateTimeString('minutes'); - $data['start_recurring_dt'] = $schedule->recurring ? $schedule->start_recurring_dt : ''; - $data['start_recurring_hr'] = $schedule->recurring ? $schedule->start_recurring_hr : ''; - $data['end_recurring_dt'] = $schedule->recurring ? $schedule->end_recurring_dt : ''; - $data['end_recurring_hr'] = $schedule->recurring ? $schedule->end_recurring_hr : ''; - $data['recurring'] = $schedule->recurring ? __('Yes') : __('No'); - $data['recurring_day'] = $schedule->recurring ? implode(',', $schedule->recurring_day) : ''; - $data['status'] = $schedule->status; - - return $data; -}); - -echo json_encode([ - 'current' => $current, - 'rowCount' => $rowCount, - 'rows' => $schedules, - 'total' => $total, -]); diff --git a/routes/web.php b/routes/web.php index 3c55a75eec..3f265db1a3 100644 --- a/routes/web.php +++ b/routes/web.php @@ -109,6 +109,7 @@ Route::group(['middleware' => ['auth', '2fa'], 'guard' => 'auth'], function () { // jquery bootgrid data controllers Route::group(['prefix' => 'table', 'namespace' => 'Table'], function () { + Route::post('alert-schedule', 'AlertScheduleController'); Route::post('customers', 'CustomersController'); Route::post('device', 'DeviceController'); Route::post('eventlog', 'EventlogController');