Table display working (and using Eloquent)

This commit is contained in:
Tony Murray
2020-04-10 17:04:33 -05:00
parent a68be76015
commit bf54634e4b

View File

@@ -12,122 +12,88 @@
* the source code distribution for details.
*/
$where = 1;
use Carbon\Carbon;
$sql = " FROM `alert_schedule` AS S WHERE $where";
if (!Auth::user()->hasGlobalRead()) {
$param[] = Auth::id();
return [
'current' => 0,
'rowCount' => 0,
'rows' => [],
'total' => 0,
];
}
$query = \App\Models\AlertSchedule::query();
if (isset($searchPhrase) && !empty($searchPhrase)) {
$sql .= " AND (`S`.`title` LIKE '%$searchPhrase%' OR `S`.`start` LIKE '%$searchPhrase%' OR `S`.`end` LIKE '%$searchPhrase%')";
$query->where(function ($query) use ($searchPhrase) {
$query->where('title', 'like', "%$searchPhrase%")
->orWhere('start', 'like', "%$searchPhrase%")
->orWhere('end', 'like', "%$searchPhrase%");
});
}
$count_sql = "SELECT COUNT(`schedule_id`) $sql";
$total = dbFetchCell($count_sql, $param);
if (empty($total)) {
$total = 0;
$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));
if ($sort_column == 'status') {
$sort_by_status = true;
$sort = "`S`.`start` $sort_order";
$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 {
$sort = '`S`.`start` DESC ';
$query->orderBy('start')->orderBy('title');
}
$sql .= " ORDER BY $sort";
$now = Carbon::now(config('app.timezone'));
$days = [
'from' => ['1', '2', '3', '4', '5', '6', '7'],
'to' => ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su']
];
if (isset($current)) {
$limit_low = (($current * $rowCount) - ($rowCount));
$limit_high = $rowCount;
}
$schedules = $query->get()->map(function ($schedule) use ($now, $days) {
$start = Carbon::parse($schedule->start, 'UTC')->tz(config('app.timezone'));
$end = Carbon::parse($schedule->end, 'UTC')->tz(config('app.timezone'));
if ($rowCount != -1) {
$sql .= " LIMIT $limit_low,$limit_high";
}
$sql = "SELECT `S`.`schedule_id`, `S`.`recurring`, DATE_FORMAT(NOW(), '" . \LibreNMS\Config::get('dateformat.mysql.compact') . "') AS now, DATE_FORMAT(`S`.`start`, '" . \LibreNMS\Config::get('dateformat.mysql.compact') . "') AS `start`, DATE_FORMAT(`S`.`end`, '" . \LibreNMS\Config::get('dateformat.mysql.compact') . "') AS `end`, DATE_FORMAT(`S`.`start_recurring_dt`, '" . \LibreNMS\Config::get('dateformat.mysql.date') . "') AS `start_recurring_dt`, DATE_FORMAT(`S`.`end_recurring_dt`, '" . \LibreNMS\Config::get('dateformat.mysql.date') . "') AS `end_recurring_dt`, `S`.`start_recurring_hr`, `S`.`end_recurring_hr`, `S`.`recurring_day`, `S`.`title` $sql";
foreach (dbFetchRows($sql, $param) as $schedule) {
$status = 0;
if ($schedule['recurring'] == 0) {
$start = strtotime($schedule['start']);
$end = strtotime($schedule['end']);
$now = strtotime($schedule['now']);
if ($end < $now) {
$status = 1;
}
if ($now >= $start && $now < $end) {
$status = 2;
}
} else {
$start = strtotime($schedule['start_recurring_dt']);
$end = $schedule['end_recurring_dt'] != '' && $schedule['end_recurring_dt'] != '0000-00-00' ? strtotime($schedule['end_recurring_dt'].' '. $schedule['end_recurring_hr']) : 0;
$now = strtotime($schedule['now']);
if ($end < $now && $end != 0) {
$status =1;
}
if ($start <= $now && ($now <= $end || $end == 0)) {
$status = 2;
}
}
$table_rd = '';
if ($schedule['recurring_day'] != '') {
$array_days = array(
0 => 'Su',
1 => 'Mo',
2 => 'Tu',
3 => 'We',
4 => 'Th',
5 => 'Fr',
6 => 'Sa'
);
$array_rd = explode(',', $schedule['recurring_day']);
foreach ($array_rd as $key_rd => $val_rd) {
if (array_key_exists($val_rd, $array_days)) {
$table_rd .= $table_rd != '' ? ','. $array_days[$val_rd] : $array_days[$val_rd];
}
}
$status = $end < $now ? 1 : 0; // set or lapsed
// check if current
if ($now->between($start, $end) && (!$schedule->recurring || $now->between($start->toTimeString(), $end->toTimeString()))) {
$status = 2;
}
return [
'title' => $schedule->title,
'recurring' => $schedule->recurring ? 'yes' : 'no',
'start' => $schedule->recurring ? '' : $start->toDateTimeString('minute'),
'end' => $schedule->recurring ? '' : $end->toDateTimeString('minute'),
'start_recurring_dt' => $schedule->recurring == 0 ? '' : $start->toDateString(),
'end_recurring_dt' => $schedule->recurring == 0 || $end->year == 9000 ? '' : $end->toDateString(),
'start_recurring_hr' => $schedule->recurring == 0 ? '' : $start->toTimeString('minute'),
'end_recurring_hr' => $schedule->recurring == 0 ? '' : $end->toTimeString('minute'),
'recurring_day' => $schedule->recurring == 0 ? '' : str_replace($days['from'], $days['to'], $schedule->recurring_day),
'id' => $schedule->schedule_id,
'status' => $status,
];
});
$response[] = array(
'title' => $schedule['title'],
'recurring' => $schedule['recurring'] == 1 ? 'yes' : 'no',
'start' => $schedule['recurring'] == 1 ? '' : $schedule['start'],
'end' => $schedule['recurring'] == 1 ? '' : $schedule['end'],
'start_recurring_dt' => $schedule['recurring'] == 0 || $schedule['start_recurring_dt'] == '0000-00-00' ? '' : $schedule['start_recurring_dt'],
'end_recurring_dt' => $schedule['recurring'] == 0 || $schedule['end_recurring_dt'] == '0000-00-00' ? '' : $schedule['end_recurring_dt'],
'start_recurring_hr' => $schedule['recurring'] == 0 ? '' : substr($schedule['start_recurring_hr'], 0, 5),
'end_recurring_hr' => $schedule['recurring'] == 0 ? '' : substr($schedule['end_recurring_hr'], 0, 5),
'recurring_day' => $schedule['recurring'] == 0 ? '' : $table_rd,
'id' => $schedule['schedule_id'],
'status' => $status,
);
}
if (isset($sort_by_status) && $sort_by_status) {
if ($sort_order == 'asc') {
usort($response, function ($a, $b) {
return $a['status'] - $b['status'];
});
} else {
usort($response, function ($a, $b) {
return $b['status'] - $a['status'];
});
}
}
$output = array(
echo json_encode([
'current' => $current,
'rowCount' => $rowCount,
'rows' => $response,
'rows' => $schedules,
'total' => $total,
);
echo _json_encode($output);
]);