Files
librenms-librenms/includes/html/forms/schedule-maintenance.inc.php
Tony Murray 020c5fd7e1 Remove legacy code and fix missing device graphs (#11950)
* removing $graphs global

* remove unused things

* fix some additional graphs

* Fix graphs persisting too soon

* correct name for poller module performance graph

* only one type of graph is used here
2020-07-23 09:57:22 -05:00

223 lines
8.2 KiB
PHP

<?php
use App\Models\UserPref;
use Illuminate\Support\Str;
/*
* 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.
*/
if (!Auth::user()->hasGlobalAdmin()) {
header('Content-type: text/plain');
die('ERROR: You need to be admin');
}
$sub_type = $_POST['sub_type'];
if ($sub_type == 'new-maintenance') {
// Defaults
$status = 'error';
$update = 0;
$message = '';
$schedule_id = mres($_POST['schedule_id']);
if ($schedule_id > 0) {
$update = 1;
}
$title = mres($_POST['title']);
$notes = mres($_POST['notes']);
$recurring = $_POST['recurring'] ? 1 : 0;
$start_recurring_dt = mres($_POST['start_recurring_dt']);
$end_recurring_dt = mres($_POST['end_recurring_dt']);
$start_recurring_hr = mres($_POST['start_recurring_hr']);
$end_recurring_hr = mres($_POST['end_recurring_hr']);
$recurring_day = mres($_POST['recurring_day']);
$start = mres($_POST['start']);
[$duration_hour, $duration_min] = explode(':', mres($_POST['duration']));
$end = mres($_POST['end']);
$maps = mres($_POST['maps']);
if (isset($duration_hour) && isset($duration_min)) {
$end = date('Y-m-d H:i:00', strtotime('+'.intval($duration_hour).' hour '.intval($duration_min).' minute', strtotime($start)));
}
if (empty($title)) {
$message = 'Missing title<br />';
}
if (!in_array($recurring, array(0,1))) {
$message .= 'Missing recurring choice<br />';
}
// check values if recurring is set to yes
$recurring_day = null;
if ($recurring == 1) {
if (empty($start_recurring_dt)) {
$message .= 'Missing start recurring date<br />';
} else {
// check if date is correct
[$ysrd, $msrd, $dsrd] = explode('-', $start_recurring_dt);
if (!checkdate($msrd, $dsrd, $ysrd)) {
$message .= 'Please check start recurring date<br />';
}
}
// end recurring dt not mandatory.. but if set, check if correct
if (!empty($end_recurring_dt) && $end_recurring_dt != '0000-00-00' && $end_recurring_dt != '') {
[$yerd, $merd, $derd] = explode('-', $end_recurring_dt);
if (!checkdate($merd, $derd, $yerd)) {
$message .= 'Please check end recurring date<br />';
}
} else {
$end_recurring_dt = '9000-09-09';
}
if (empty($start_recurring_hr)) {
$message .= 'Missing start recurring hour<br />';
}
if (empty($end_recurring_hr)) {
$message .= 'Missing end recurring hour<br />';
}
if (isset($_POST['recurring_day']) && is_array($_POST['recurring_day']) && !empty($_POST['recurring_day'])) {
$recurring_day = $_POST['recurring_day'];
}
// recurring = 1 => empty no reccurency values to be sure.
$start = '0000-00-00 00:00:00';
$end = '0000-00-00 00:00:00';
} else {
if (empty($start)) {
$message .= 'Missing start date<br />';
}
if (empty($end)) {
$message .= 'Missing end date<br />';
}
// recurring = 0 => empty no reccurency values to be sure.
$start_recurring_dt = '1970-01-02';
$end_recurring_dt = '1970-01-02';
$start_recurring_hr = '00:00:00';
$end_recurring_hr = '00:00:00';
}
if (!is_array($_POST['maps'])) {
$message .= 'Not mapped to any groups or devices<br />';
}
if (empty($message)) {
$alert_schedule = \App\Models\AlertSchedule::findOrNew($schedule_id);
$alert_schedule->title = $title;
$alert_schedule->notes = $notes;
$alert_schedule->recurring = $recurring;
$alert_schedule->start = $start;
$alert_schedule->end = $end;
if ($recurring) {
$alert_schedule->start_recurring_dt = $start_recurring_dt;
$alert_schedule->start_recurring_hr = $start_recurring_hr;
$alert_schedule->end_recurring_dt = $end_recurring_dt;
$alert_schedule->end_recurring_hr = $end_recurring_hr;
$alert_schedule->recurring_day = $recurring_day;
}
$alert_schedule->save();
if ($alert_schedule->schedule_id > 0) {
$items = array();
$fail = 0;
if ($update == 1) {
dbDelete('alert_schedulables', '`schedule_id`=?', [$alert_schedule->schedule_id]);
}
foreach ($_POST['maps'] as $target) {
$type = 'device';
if (Str::startsWith($target, 'l')) {
$type = 'location';
$target = substr($target, 1);
} elseif (Str::startsWith($target, 'g')) {
$type = 'device_group';
$target = substr($target, 1);
}
$item = dbInsert(['schedule_id' => $alert_schedule->schedule_id, 'alert_schedulable_type' => $type, 'alert_schedulable_id' => $target], 'alert_schedulables');
if ($notes && $type = 'device' && UserPref::getPref(Auth::user(), 'add_schedule_note_to_device')) {
$device_notes = dbFetchCell('SELECT `notes` FROM `devices` WHERE `device_id` = ?;', [$target]);
$device_notes.= ((empty($device_notes)) ? '' : PHP_EOL) . date("Y-m-d H:i") . ' Alerts delayed: ' . $notes;
dbUpdate(['notes' => $device_notes], 'devices', '`device_id` = ?', [$target]);
}
if ($item > 0) {
array_push($items, $item);
} else {
$fail = 1;
}
}
if ($fail == 1 && $update == 0) {
foreach ($items as $item) {
dbDelete('alert_schedulables', '`item_id`=?', array($item));
}
dbDelete('alert_schedule', '`schedule_id`=?', [$alert_schedule->schedule_id]);
$message = 'Issue scheduling maintenance';
} else {
$status = 'ok';
$message = 'Scheduling maintenance ok';
}
} else {
$message = 'Issue scheduling maintenance';
}//end if
}//end if
$response = array(
'status' => $status,
'message' => $message,
);
} elseif ($sub_type == 'parse-maintenance') {
$alert_schedule = \App\Models\AlertSchedule::findOrFail($_POST['schedule_id']);
$items = [];
foreach (dbFetchRows('SELECT `alert_schedulable_type`, `alert_schedulable_id` FROM `alert_schedulables` WHERE `schedule_id`=?', [$alert_schedule->schedule_id]) as $target) {
$id = $target['alert_schedulable_id'];
if ($target['alert_schedulable_type'] == 'location') {
$text = dbFetchCell('SELECT location FROM locations WHERE id = ?', [$id]);
$id = 'l' . $id;
} elseif ($target['alert_schedulable_type'] == 'device_group') {
$text = dbFetchCell('SELECT name FROM device_groups WHERE id = ?', [$id]);
$id = 'g' . $id;
} else {
$text = dbFetchCell('SELECT hostname FROM devices WHERE device_id = ?', [$id]);
}
$items[] = [
'id' => $id,
'text' => $text,
];
}
$response = $alert_schedule->toArray();
$response['recurring_day'] = $alert_schedule->getOriginal('recurring_day');
$response['targets'] = $items;
} elseif ($sub_type == 'del-maintenance') {
$schedule_id = mres($_POST['del_schedule_id']);
dbDelete('alert_schedule', '`schedule_id`=?', array($schedule_id));
dbDelete('alert_schedulables', '`schedule_id`=?', array($schedule_id));
$status = 'ok';
$message = 'Maintenance schedule has been removed';
$response = array(
'status' => $status,
'message' => $message,
);
}//end if
header('Content-type: application/json');
echo _json_encode($response);