Convert alert-schedule to new style

Improved sorting customization
This commit is contained in:
Tony Murray
2020-04-11 18:03:19 -05:00
parent 89bfecc926
commit d4ce98a5d9
6 changed files with 139 additions and 97 deletions

View File

@@ -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;

View File

@@ -0,0 +1,101 @@
<?php
/**
* AlertScheduleController.php
*
* -Description-
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2020 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
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;
// }
}

View File

@@ -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()
{

View File

@@ -77,12 +77,7 @@ var grid = $("#alert-schedule").bootgrid({
"<div class=\"col-sm-4 actionBar\"><p class=\"{{css.search}}\"></p><p class=\"{{css.actions}}\"></p></div></div></div>"
},
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) {

View File

@@ -1,88 +0,0 @@
<?php
/*
* LibreNMS
*
* Copyright (c) 2014 Neil Lathwood <https://github.com/laf/ http://www.lathwood.co.uk/fa>
*
* 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,
]);

View File

@@ -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');