wip special days alerts

This commit is contained in:
Daniel Barbero Martin 2021-11-03 17:30:54 +01:00
parent 53361484fd
commit fdca5e4a78
12 changed files with 653 additions and 222 deletions

View File

@ -51,6 +51,188 @@ if (is_ajax() === true) {
echo json_encode($command); echo json_encode($command);
} }
$get_template_alerts = (bool) get_parameter('get_template_alerts');
if ($get_template_alerts === true) {
$filter['special_day'] = 1;
$templates = alerts_get_alert_templates($filter);
$date = get_parameter('date', '');
$id_group = get_parameter('id_group', 0);
$same_day = get_parameter('same_day', '');
$output = '<h4>'.__('Same as %s', ucfirst($same_day));
$output .= ' &raquo; ';
$output .= __('Templates not being fired');
$output .= '</h4>';
$columns = [
'name',
'id_group',
'type',
'monday',
'tuesday',
'wednesday',
'thursday',
'friday',
'saturday',
'sunday',
];
$column_names = [
__('Name'),
__('Group'),
__('Type'),
__('Mon'),
__('Tue'),
__('Wed'),
__('Thu'),
__('Fri'),
__('Sat'),
__('Sun'),
];
try {
$output .= ui_print_datatable(
[
'id' => 'templates_alerts_special_days',
'return' => true,
'class' => 'info_table',
'style' => 'width: 100%',
'columns' => $columns,
'column_names' => $column_names,
'ajax_url' => 'godmode/alerts/alert_special_days',
'ajax_data' => [
'get_template_alerts_data' => 1,
'same_day' => $same_day,
],
'no_sortable_columns' => [-1],
'order' => [
'field' => 'name',
'direction' => 'asc',
],
'search_button_class' => 'sub filter float-right',
'form' => [
'inputs' => [
[
'label' => __('Type'),
'type' => 'select',
'name' => 'type',
'fields' => alerts_get_alert_templates_types(),
'selected' => 0,
'nothing' => 'None',
'nothing_value' => 0,
],
[
'label' => __('Search'),
'type' => 'text',
'class' => 'mw250px',
'id' => 'name',
'name' => 'name',
],
],
],
]
);
} catch (Exception $e) {
$output .= $e->getMessage();
}
echo $output;
return;
}
$get_template_alerts_data = (bool) get_parameter('get_template_alerts_data');
if ($get_template_alerts_data === true) {
$filters = get_parameter('filter', []);
if (empty($filters['type']) === false) {
$filter['type'] = $filters['type'];
}
if (empty($filters['name']) === false) {
$filter[] = "name LIKE '%".$filters['name']."%'";
}
$filter['special_day'] = 1;
$templates = alerts_get_alert_templates($filter);
$count = alerts_get_alert_templates($filter, ['COUNT(*) AS total']);
$same_day = get_parameter('same_day', '');
$data = [];
if (empty($templates) === false) {
foreach ($templates as $template) {
if ((bool) $template[$same_day] === false) {
$data[] = [
'name' => $template['name'],
'id_group' => ui_print_group_icon(
$template['id_group'],
true
),
'type' => $template['type'],
'monday' => (bool) $template['monday'] === true
? html_print_image(
'images/tick.png',
true,
['class' => 'invert_filter']
)
: '',
'tuesday' => (bool) $template['tuesday'] === true
? html_print_image(
'images/tick.png',
true,
['class' => 'invert_filter']
)
: '',
'wednesday' => (bool) $template['wednesday'] === true
? html_print_image(
'images/tick.png',
true,
['class' => 'invert_filter']
)
: '',
'thursday' => (bool) $template['thursday'] === true
? html_print_image(
'images/tick.png',
true,
['class' => 'invert_filter']
)
: '',
'friday' => (bool) $template['friday'] === true
? html_print_image(
'images/tick.png',
true,
['class' => 'invert_filter']
)
: '',
'saturday' => (bool) $template['saturday'] === true
? html_print_image(
'images/tick.png',
true,
['class' => 'invert_filter']
)
: '',
'sunday' => (bool) $template['sunday'] === true
? html_print_image(
'images/tick.png',
true,
['class' => 'invert_filter']
)
: '',
];
}
}
}
echo json_encode(
[
'data' => $data,
'recordsTotal' => $count[0]['total'],
'recordsFiltered' => count($data),
]
);
return $data;
}
return; return;
} }
@ -338,6 +520,8 @@ if ($delete_special_day === true) {
); );
} }
ui_require_javascript_file('pandora_alerts'); ui_require_javascript_file('pandora_alerts');
?> ?>
<script type="text/javascript"> <script type="text/javascript">

View File

@ -165,10 +165,11 @@ class CalendarManager
]; ];
if ($tab !== 'list') { if ($tab !== 'list') {
$id_calendar = get_parameter('id_calendar', 0);
$buttons['special_days'] = [ $buttons['special_days'] = [
'active' => false, 'active' => false,
'text' => '<a href="'.ui_get_full_url( 'text' => '<a href="'.ui_get_full_url(
$this->url.'&tab=special_days' $this->url.'&tab=special_days&id_calendar='.$id_calendar
).'&pure='.(int) $config['pure'].'">'.html_print_image( ).'&pure='.(int) $config['pure'].'">'.html_print_image(
'images/templates.png', 'images/templates.png',
true, true,
@ -193,22 +194,21 @@ class CalendarManager
*/ */
public function run() public function run()
{ {
\ui_require_css_file('alert');
$op = get_parameter('op'); $op = get_parameter('op');
$tab = get_parameter('tab'); $tab = get_parameter('tab');
switch ($tab) { switch ($tab) {
case 'special_days': case 'special_days':
hd('tab special_day');
if ($op === 'edit') { if ($op === 'edit') {
hd('op = edit');
if ($this->showSpecialDaysEdition() !== true) { if ($this->showSpecialDaysEdition() !== true) {
return; return;
} }
} else if ($op === 'delete') { } else if ($op === 'delete') {
// $this->deleteVendor(); $this->deleteSpecialDay();
hd('delete special days'); } else if ($op === 'upload_ical') {
$this->iCalendarSpecialDay();
} }
echo 'WIP special list';
$this->showSpecialDays(); $this->showSpecialDays();
break; break;
@ -219,8 +219,7 @@ class CalendarManager
return; return;
} }
} else if ($op === 'delete') { } else if ($op === 'delete') {
// $this->deleteVendor(); $this->deleteCalendar();
hd('delete calendar');
} }
$this->showCalendarList(); $this->showCalendarList();
@ -230,6 +229,183 @@ class CalendarManager
} }
/**
* Delete calendar
*
* @return void
*/
public function deleteCalendar()
{
$id = (int) get_parameter('id');
try {
$calendar = new Calendar($id);
if ($id === 0) {
return;
}
} catch (\Exception $e) {
if ($id > 0) {
$this->message = \ui_print_error_message(
\__('Calendar not found: %s', $e->getMessage()),
'',
true
);
}
return;
}
// Remove.
$calendar->delete();
$this->message = \ui_print_success_message(
\__('Calendar successfully deleted'),
'',
true
);
}
/**
* Delete special day.
*
* @return void
*/
public function deleteSpecialDay()
{
$id = (int) get_parameter('id');
try {
$specialDay = new SpecialDay($id);
if ($id === 0) {
return;
}
} catch (\Exception $e) {
if ($id > 0) {
$this->message = \ui_print_error_message(
\__('Special day not found: %s', $e->getMessage()),
'',
true
);
}
return;
}
// Remove.
$specialDay->delete();
$this->message = \ui_print_success_message(
\__('Special day successfully deleted'),
'',
true
);
}
/**
* Icalendar.
*
* @return void
*/
public function iCalendarSpecialDay()
{
$day_code = (string) get_parameter('day_code');
$overwrite = (bool) get_parameter('overwrite', 0);
$values = [];
$values['id_group'] = (string) get_parameter('id_group');
$values['id_calendar'] = get_parameter('id_calendar');
$values['day_code'] = $day_code;
$error = $_FILES['ical_file']['error'];
$extension = substr($_FILES['ical_file']['name'], -3);
if ($error == 0 && strcasecmp($extension, 'ics') == 0) {
$skipped_dates = '';
$this_month = date('Ym');
$ical = new \ICal($_FILES['ical_file']['tmp_name']);
$events = $ical->events();
foreach ($events as $event) {
$event_date = substr($event['DTSTART'], 0, 8);
$event_month = substr($event['DTSTART'], 0, 6);
if ($event_month >= $this_month) {
$values['description'] = @$event['SUMMARY'];
$values['date'] = $event_date;
$date = date('Y-m-d', strtotime($event_date));
$date_check = '';
$filter['id_group'] = $values['id_group'];
$filter['date'] = $date;
$date_check = db_get_value_filter(
'date',
'talert_special_days',
$filter
);
if ($date_check === $date) {
if ($overwrite === true) {
$id_special_day = db_get_value_filter(
'id',
'talert_special_days',
$filter
);
try {
$specialDay = new SpecialDay($id_special_day);
$specialDay->date($values['date']);
$specialDay->id_group($values['id_group']);
$specialDay->day_code($values['day_code']);
$specialDay->description($values['description']);
$specialDay->id_calendar($values['id_calendar']);
if ($specialDay->save() === true) {
$result = true;
} else {
$result = false;
}
} catch (\Exception $e) {
$result = false;
}
} else {
if ($skipped_dates == '') {
$skipped_dates = __('Skipped dates: ');
}
$skipped_dates .= $date.' ';
}
} else {
try {
$specialDay = new SpecialDay();
$specialDay->date($values['date']);
$specialDay->id_group($values['id_group']);
$specialDay->day_code($values['day_code']);
$specialDay->description($values['description']);
$specialDay->id_calendar($values['id_calendar']);
if ($specialDay->save() === true) {
$result = true;
} else {
$result = false;
}
} catch (\Exception $e) {
$result = false;
}
}
}
}
} else {
$result = false;
}
if ($result === true) {
db_pandora_audit(
'Special days list',
'Upload iCalendar '.$_FILES['ical_file']['name']
);
}
$this->message = \ui_print_result_message(
$result,
\__('Success to upload iCalendar').'<br />'.$skipped_dates,
\__('Fail to upload iCalendar')
);
}
/** /**
* Show a list of models registered in this system. * Show a list of models registered in this system.
* *
@ -256,6 +432,7 @@ class CalendarManager
*/ */
public function showCalendarEdition() public function showCalendarEdition()
{ {
global $config;
$id = (int) get_parameter('id'); $id = (int) get_parameter('id');
$new = false; $new = false;
try { try {
@ -288,19 +465,31 @@ class CalendarManager
} }
try { try {
$calendar->name(get_parameter('name', null)); $name = get_parameter('name', null);
$change_name = true;
if ($new === false && $name === $calendar->name()) {
$change_name = false;
}
$calendar->name($name);
$calendar->id_group(get_parameter('id_group', null)); $calendar->id_group(get_parameter('id_group', null));
$calendar->description(get_parameter('description', null)); $calendar->description(get_parameter('description', null));
// Save template. if ($change_name === true && empty($calendar->search(['name' => $calendar->name()])) === false) {
if ($calendar->save() === true) {
$success = true;
} else {
global $config;
$reason = \__( $reason = \__(
'Failed saving calendar: ', 'Failed saving calendar: name exists',
$config['dbconnection']->error $config['dbconnection']->error
); );
} else {
// Save template.
if ($calendar->save() === true) {
$success = true;
} else {
$reason = \__(
'Failed saving calendar: ',
$config['dbconnection']->error
);
}
} }
} catch (\Exception $e) { } catch (\Exception $e) {
$this->message = \ui_print_error_message( $this->message = \ui_print_error_message(
@ -439,7 +628,7 @@ class CalendarManager
// Options. Especial days. // Options. Especial days.
$tmp->options .= '<a href="'; $tmp->options .= '<a href="';
$tmp->options .= ui_get_full_url( $tmp->options .= ui_get_full_url(
$this->url.'&op=special_days&tab=special_days&id='.$tmp->id $this->url.'&op=special_days&tab=special_days&id_calendar='.$tmp->id
); );
$tmp->options .= '">'; $tmp->options .= '">';
$tmp->options .= html_print_image( $tmp->options .= html_print_image(
@ -512,7 +701,8 @@ class CalendarManager
public function showSpecialDays() public function showSpecialDays()
{ {
global $config; global $config;
$id = (int) get_parameter('id'); $id_calendar = (int) get_parameter('id_calendar');
$display_range = (int) get_parameter('display_range', 0); $display_range = (int) get_parameter('display_range', 0);
try { try {
// Datatables offset, limit and order. // Datatables offset, limit and order.
@ -527,7 +717,7 @@ class CalendarManager
$filter = []; $filter = [];
$filter['date'] = $date; $filter['date'] = $date;
$filter['futureDate'] = $futureDate; $filter['futureDate'] = $futureDate;
$filter['id_calendar'] = $id; $filter['id_calendar'] = $id_calendar;
if (!is_user_admin($config['id_user'])) { if (!is_user_admin($config['id_user'])) {
$filter['id_group'] = array_keys( $filter['id_group'] = array_keys(
users_get_groups(false, 'LM') users_get_groups(false, 'LM')
@ -555,7 +745,7 @@ class CalendarManager
true true
); );
} catch (\Exception $e) { } catch (\Exception $e) {
if ($id > 0) { if ($id_calendar > 0) {
$this->message = \ui_print_error_message( $this->message = \ui_print_error_message(
\__('Special days not found: %s', $e->getMessage()), \__('Special days not found: %s', $e->getMessage()),
'', '',
@ -572,7 +762,7 @@ class CalendarManager
'tabs' => $this->getTabs('special_days'), 'tabs' => $this->getTabs('special_days'),
'message' => $this->message, 'message' => $this->message,
'specialDays' => $specialDays, 'specialDays' => $specialDays,
'id_calendar' => $id, 'id_calendar' => $id_calendar,
'display_range' => $display_range, 'display_range' => $display_range,
] ]
); );
@ -586,13 +776,15 @@ class CalendarManager
*/ */
public function showSpecialDaysEdition() public function showSpecialDaysEdition()
{ {
global $config;
$id = (int) get_parameter('id'); $id = (int) get_parameter('id');
hd($id);
$new = false; $new = false;
try { try {
$specialDay = new SpecialDay($id); $specialDay = new SpecialDay($id);
if ($id === 0) { if ($id === 0) {
$specialDay->date(get_parameter('date', null)); $specialDay->date(get_parameter('date', null));
$specialDay->id_calendar(get_parameter('id_calendar', null));
$new = true; $new = true;
} }
} catch (\Exception $e) { } catch (\Exception $e) {
@ -620,20 +812,50 @@ class CalendarManager
} }
try { try {
$specialDay->date(get_parameter('date', null)); $date = get_parameter('date', null);
$specialDay->id_group(get_parameter('id_group', null)); $id_group = get_parameter('id_group', null);
$specialDay->day_code(get_parameter('day_code', null)); $day_code = get_parameter('day_code', null);
$specialDay->description(get_parameter('description', null)); $id_calendar = get_parameter('id_calendar', null);
$description = get_parameter('description', null);
$change = true;
if ($new === false
&& ($date === $specialDay->date()
|| $id_group === $specialDay->id_group()
|| $day_code === $specialDay->day_code())
) {
$change = false;
}
// Save template. $specialDay->date($date);
if ($specialDay->save() === true) { $specialDay->id_group($id_group);
$success = true; $specialDay->day_code($day_code);
} else { $specialDay->description($description);
global $config; $specialDay->id_calendar($id_calendar);
$search = specialDay::specialDays(
[ '`talert_special_days`.*' ],
[
'date_match' => $specialDay->date(),
'id_group' => [$specialDay->id_group()],
'day_code' => $specialDay->day_code(),
]
);
if ($change === true && empty($search) === false) {
$reason = \__( $reason = \__(
'Failed saving special day: ', 'Failed saving calendar: name exists',
$config['dbconnection']->error $config['dbconnection']->error
); );
} else {
// Save template.
if ($specialDay->save() === true) {
$success = true;
} else {
$reason = \__(
'Failed saving special day: ',
$config['dbconnection']->error
);
}
} }
} catch (\Exception $e) { } catch (\Exception $e) {
$this->message = \ui_print_error_message( $this->message = \ui_print_error_message(
@ -661,7 +883,7 @@ class CalendarManager
'calendar/special_days_edit', 'calendar/special_days_edit',
[ [
'ajax_url' => $this->ajaxUrl, 'ajax_url' => $this->ajaxUrl,
'url' => $this->url.'&op=edit&tab=special_days', 'url' => $this->url.'&id_calendar='.$specialDay->id_calendar().'&op=edit&tab=special_days',
'tabs' => $this->getTabs('special_days'), 'tabs' => $this->getTabs('special_days'),
'specialDay' => $specialDay, 'specialDay' => $specialDay,
'message' => $this->message, 'message' => $this->message,
@ -686,9 +908,9 @@ class CalendarManager
$templates = alerts_get_alert_templates($filter); $templates = alerts_get_alert_templates($filter);
$date = get_parameter('date', ''); $date = get_parameter('date', '');
$id_group = get_parameter('id_group', 0); $id_group = get_parameter('id_group', 0);
$same_day = get_parameter('same_day', ''); $day_code = get_parameter('day_code', '');
$output = '<h4>'.__('Same as %s', ucfirst($same_day)); $output = '<h4>'.__('Same as %s', $day_code);
$output .= ' &raquo; '; $output .= ' &raquo; ';
$output .= __('Templates not being fired'); $output .= __('Templates not being fired');
$output .= '</h4>'; $output .= '</h4>';
@ -730,7 +952,7 @@ class CalendarManager
'ajax_url' => 'godmode/alerts/alert_special_days', 'ajax_url' => 'godmode/alerts/alert_special_days',
'ajax_data' => [ 'ajax_data' => [
'method' => 'dataAlertTemplates', 'method' => 'dataAlertTemplates',
'same_day' => $same_day, 'day_code' => $day_code,
], ],
'no_sortable_columns' => [-1], 'no_sortable_columns' => [-1],
'order' => [ 'order' => [

View File

@ -2380,118 +2380,6 @@ function get_alert_last_fire_timestamp_in_period($id_alert_module, $period, $dat
} }
/**
* Insert in talert_special_days a new special day.
*
* @param date of special day.
* @param same day of the week.
* @param mixed A single value or array of values to insert (can be a multiple a mount of rows).
*
* @return mixed False in case of error or invalid values passed. Affected rows otherwise.
*/
function alerts_create_alert_special_day($date, $same_day, $values=false)
{
if (empty($date)) {
return false;
}
if (empty($same_day)) {
return false;
}
if (! is_array($values)) {
$values = [];
}
global $config;
$date_db = '';
switch ($config['dbtype']) {
case 'mysql':
$date_db = 'date';
break;
case 'oracle':
$date_db = '"date"';
break;
}
$values[$date_db] = $date;
$values['same_day'] = $same_day;
return @db_process_sql_insert('talert_special_days', $values);
}
/**
* Update a special day in talert_special_days.
*
* @param int special day Id.
* @param mixed Array of values to update.
*
* @return mixed False in case of error or invalid values passed. Affected rows otherwise
*/
function alerts_update_alert_special_day($id_special_day, $values)
{
$id_special_day = safe_int($id_special_day, 1);
if (empty($id_special_day)) {
return false;
}
if (! is_array($values)) {
return false;
}
return (@db_process_sql_update(
'talert_special_days',
$values,
['id' => $id_special_day]
)) !== false;
}
/**
* Delete a special day in talert_special_days.
*
* @param int special day Id.
*
* @return mixed False in case of error or invalid values passed. Affected rows otherwise
*/
function alerts_delete_alert_special_day($id_special_day)
{
$id_special_day = safe_int($id_special_day, 1);
if (empty($id_special_day)) {
return false;
}
return (@db_process_sql_delete(
'talert_special_days',
['id' => $id_special_day]
)) !== false;
}
/**
* Get a special day in talert_special_days.
*
* @param int special day Id.
*
* @return mixed False in case of error or invalid values passed. All row of the selected command otherwise
*/
function alerts_get_alert_special_day($id_special_day)
{
$id_special_day = safe_int($id_special_day, 1);
if (empty($id_special_day)) {
return false;
}
return db_get_row('talert_special_days', 'id', $id_special_day);
}
/** /**
* Get number of alert fired that an action is executed. Only fot non default alerts * Get number of alert fired that an action is executed. Only fot non default alerts
* *

View File

@ -55,6 +55,7 @@ enterprise_include_once('include/functions_alerts.php');
// Clases. // Clases.
use PandoraFMS\Module; use PandoraFMS\Module;
use PandoraFMS\Enterprise\Cluster; use PandoraFMS\Enterprise\Cluster;
use PandoraFMS\SpecialDay;
/** /**
@ -14131,7 +14132,7 @@ function api_get_special_days($thrash1, $thrash2, $other, $thrash3)
* *
* @param $thrash1 Don't use. * @param $thrash1 Don't use.
* @param $thrash2 Don't use. * @param $thrash2 Don't use.
* @param array $other it's array, $other as param is <special_day>;<same_day>;<description>;<id_group>; in this order * @param array $other it's array, $other as param is <special_day>;<day_code>;<description>;<id_group>; in this order
* and separator char (after text ; ) and separator (pass in param othermode as othermode=url_encode_separator_<separator>) * and separator char (after text ; ) and separator (pass in param othermode as othermode=url_encode_separator_<separator>)
* @param $thrash3 Don't use * @param $thrash3 Don't use
* *
@ -14147,9 +14148,10 @@ function api_set_create_special_day($thrash1, $thrash2, $other, $thrash3)
} }
$special_day = $other['data'][0]; $special_day = $other['data'][0];
$same_day = $other['data'][1]; $day_code = $other['data'][1];
$description = $other['data'][2]; $description = $other['data'][2];
$idGroup = $other['data'][3]; $idGroup = $other['data'][3];
$id_calendar = $other['data'][4] || 1;
if (!check_acl($config['id_user'], $idGroup, 'LM', true)) { if (!check_acl($config['id_user'], $idGroup, 'LM', true)) {
returnError('forbidden', 'string'); returnError('forbidden', 'string');
@ -14186,17 +14188,21 @@ function api_set_create_special_day($thrash1, $thrash2, $other, $thrash3)
} }
} }
$values = [ try {
'description' => $other['data'][2], $sd = new SpecialDay();
'id_group' => $other['data'][3], $sd->date($special_day);
]; $sd->id_group($idGroup);
$sd->day_code($day_code);
$idSpecialDay = alerts_create_alert_special_day($special_day, $same_day, $values); $sd->description($description);
$sd->id_calendar($id_calendar);
if (is_error($idSpecialDay)) { $sd->save();
returnError('Special Day could not be created'); if ($sd->save() === true) {
} else { returnError('Special Day could not be created');
returnData('string', ['type' => 'string', 'data' => $idSpecialDay]); } else {
returnData('string', ['type' => 'string', 'data' => $sd->id()]);
}
} catch (Exception $e) {
returnData('string', ['type' => 'string', 'data' => $e]);
} }
} }
@ -14693,7 +14699,7 @@ function api_set_recreate_service_modules($id, $id_agent)
* *
* @param string $id Id of the special day to update. * @param string $id Id of the special day to update.
* @param $thrash2 Don't use. * @param $thrash2 Don't use.
* @param array $other it's array, $other as param is <special_day>;<same_day>;<description>;<id_group>; in this order * @param array $other it's array, $other as param is <special_day>;<day_code>;<description>;<id_group>; in this order
* and separator char (after text ; ) and separator (pass in param othermode as othermode=url_encode_separator_<separator>) * and separator char (after text ; ) and separator (pass in param othermode as othermode=url_encode_separator_<separator>)
* @param $thrash3 Don't use * @param $thrash3 Don't use
* *
@ -14709,9 +14715,10 @@ function api_set_update_special_day($id_special_day, $thrash2, $other, $thrash3)
} }
$special_day = $other['data'][0]; $special_day = $other['data'][0];
$same_day = $other['data'][1]; $day_code = $other['data'][1];
$description = $other['data'][2]; $description = $other['data'][2];
$idGroup = $other['data'][3]; $idGroup = $other['data'][3];
$id_calendar = $other['data'][4];
if (!check_acl($config['id_user'], $idGroup, 'LM', true)) { if (!check_acl($config['id_user'], $idGroup, 'LM', true)) {
returnError('forbidden', 'string'); returnError('forbidden', 'string');
@ -14742,24 +14749,22 @@ function api_set_update_special_day($id_special_day, $thrash2, $other, $thrash3)
return; return;
} }
$return = db_process_sql_update( try {
'talert_special_days', $sd = new SpecialDay();
[ $sd->date($special_day);
'date' => $special_day, $sd->id_group($idGroup);
'same_day' => $same_day, $sd->day_code($day_code);
'description' => $description, $sd->description($description);
'id_group' => $idGroup, $sd->id_calendar($id_calendar);
], $sd->save();
['id' => $id_special_day] if ($sd->save() === true) {
); returnError('Special Day could not be updated');
} else {
returnData( returnData('string', ['type' => 'string', 'data' => $sd->id()]);
'string', }
[ } catch (Exception $e) {
'type' => 'string', returnData('string', ['type' => 'string', 'data' => $e]);
'data' => (int) ((bool) $return), }
]
);
} }
@ -14805,13 +14810,20 @@ function api_set_delete_special_day($id_special_day, $thrash2, $thrash3, $thrash
return; return;
} }
$return = alerts_delete_alert_special_day($id_special_day); try {
$specialDay = new SpecialDay($id_special_day);
} catch (\Exception $e) {
if ($id > 0) {
returnError('The Special Day could not be deleted.');
}
if (is_error($return)) { return;
returnError('The Special Day could not be deleted.');
} else {
returnData('string', ['type' => 'string', 'data' => $return]);
} }
// Remove.
$specialDay->delete();
$return = 'success';
returnData('string', ['type' => 'string', 'data' => $return]);
} }

View File

@ -4030,15 +4030,19 @@ function html_print_input_file($name, $return=false, $options=false)
if ($options) { if ($options) {
if (isset($options['size'])) { if (isset($options['size'])) {
$output .= 'size="'.$options['size'].'"'; $output .= ' size="'.$options['size'].'"';
} }
if (isset($options['disabled'])) { if (isset($options['disabled'])) {
$output .= 'disabled="disabled"'; $output .= ' disabled="disabled"';
} }
if (isset($options['class'])) { if (isset($options['class'])) {
$output .= 'class="'.$options['class'].'"'; $output .= ' class="'.$options['class'].'"';
}
if (isset($options['required'])) {
$output .= ' required';
} }
} }

View File

@ -37,6 +37,26 @@ class Calendar extends Entity
{ {
/**
* Search Calendar.
*
* @param array $filter Filters.
*
* @return array Rows.
*/
public static function search(array $filter)
{
$table = '`talert_calendar`';
$rows = \db_get_all_rows_filter(
$table,
$filter,
['`talert_calendar`.*']
);
return $rows;
}
/** /**
* Builds a PandoraFMS\Calendar object from given id. * Builds a PandoraFMS\Calendar object from given id.
* *
@ -44,16 +64,24 @@ class Calendar extends Entity
*/ */
public function __construct(?int $id=null) public function __construct(?int $id=null)
{ {
$table = 'talert_calendar';
$filter = ['id' => $id];
$this->existsInDB = false;
if (is_numeric($id) === true if (is_numeric($id) === true
&& $id > 0 && $id > 0
) { ) {
parent::__construct( parent::__construct(
'talert_calendar', $table,
['id' => $id] $filter,
null,
false
); );
$this->existsInDB = true;
} else { } else {
// Create empty skel. // Create empty skel.
parent::__construct('talert_calendar'); parent::__construct($table, null);
} }
} }
@ -71,7 +99,7 @@ class Calendar extends Entity
$updates = $this->fields; $updates = $this->fields;
$rs = \db_process_sql_update( $rs = \db_process_sql_update(
'talert_calendar', $this->table,
$updates, $updates,
['id' => $this->fields['id']] ['id' => $this->fields['id']]
); );
@ -94,7 +122,7 @@ class Calendar extends Entity
} }
$rs = \db_process_sql_insert( $rs = \db_process_sql_insert(
'talert_calendar', $this->table,
$inserts $inserts
); );
@ -112,6 +140,23 @@ class Calendar extends Entity
} }
/**
* Remove this calendar.
*
* @return void
*/
public function delete()
{
if ($this->existsInDB === true) {
\db_process_delete_temp(
$this->table,
'id',
$this->fields['id']
);
}
}
/** /**
* Returns an array with all calendar filtered. * Returns an array with all calendar filtered.
* *

View File

@ -44,16 +44,24 @@ class SpecialDay extends Entity
*/ */
public function __construct(?int $id=null) public function __construct(?int $id=null)
{ {
$table = 'talert_special_days';
$filter = ['id' => $id];
$this->existsInDB = false;
if (is_numeric($id) === true if (is_numeric($id) === true
&& $id > 0 && $id > 0
) { ) {
parent::__construct( parent::__construct(
'talert_special_days', $table,
['id' => $id] $filter,
null,
false
); );
$this->existsInDB = true;
} else { } else {
// Create empty skel. // Create empty skel.
parent::__construct('talert_special_days'); parent::__construct($table, null);
} }
} }
@ -71,7 +79,7 @@ class SpecialDay extends Entity
$updates = $this->fields; $updates = $this->fields;
$rs = \db_process_sql_update( $rs = \db_process_sql_update(
'talert_special_days', $this->table,
$updates, $updates,
['id' => $this->fields['id']] ['id' => $this->fields['id']]
); );
@ -94,7 +102,7 @@ class SpecialDay extends Entity
} }
$rs = \db_process_sql_insert( $rs = \db_process_sql_insert(
'talert_special_days', $this->table,
$inserts $inserts
); );
@ -112,6 +120,23 @@ class SpecialDay extends Entity
} }
/**
* Remove this Special day.
*
* @return void
*/
public function delete()
{
if ($this->existsInDB === true) {
\db_process_delete_temp(
$this->table,
'id',
$this->fields['id']
);
}
}
/** /**
* Returns an array with all special days filtered. * Returns an array with all special days filtered.
* *
@ -122,6 +147,7 @@ class SpecialDay extends Entity
* @param integer $limit Limit (pagination). * @param integer $limit Limit (pagination).
* @param string $order Sort order. * @param string $order Sort order.
* @param string $sort_field Sort field. * @param string $sort_field Sort field.
* @param boolean $reduce Reduce result [Year][month][day].
* *
* @return array With all results. * @return array With all results.
* @throws \Exception On error. * @throws \Exception On error.
@ -185,6 +211,24 @@ class SpecialDay extends Entity
); );
} }
if (isset($filter['date_match']) === true
&& empty($filter['date_match']) === false
) {
$sql_filters[] = sprintf(
' AND `talert_special_days`.`date` = "%s"',
$filter['date_match']
);
}
if (isset($filter['day_code']) === true
&& empty($filter['day_code']) === false
) {
$sql_filters[] = sprintf(
' AND `talert_special_days`.`day_code` = %d',
$filter['day_code']
);
}
if (isset($order) === true) { if (isset($order) === true) {
$dir = 'asc'; $dir = 'asc';
if ($order === 'desc') { if ($order === 'desc') {
@ -234,6 +278,8 @@ class SpecialDay extends Entity
$pagination $pagination
); );
hd($sql);
if ($count === true) { if ($count === true) {
$sql = sprintf('SELECT count(*) as n FROM ( %s ) tt', $sql); $sql = sprintf('SELECT count(*) as n FROM ( %s ) tt', $sql);

View File

@ -292,3 +292,29 @@ div#rules.show {
#rules select.click-list-elements:focus { #rules select.click-list-elements:focus {
outline-color: transparent; outline-color: transparent;
} }
#icalendar-special-days {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content: space-between;
align-items: center;
background-color: #fff;
border: 1px solid #e1e1e1;
border-radius: 5px;
padding: 20px 50px;
}
#icalendar-special-days ul.wizard {
flex: 0;
}
#icalendar-special-days ul.wizard:first-child {
flex: 1 1 100%;
display: flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content: space-between;
align-items: center;
margin-right: 80px;
}

View File

@ -589,7 +589,6 @@ CREATE TABLE IF NOT EXISTS `talert_special_days` (
`id_calendar` int(10) unsigned NOT NULL DEFAULT 1, `id_calendar` int(10) unsigned NOT NULL DEFAULT 1,
`id_group` INT(10) NOT NULL DEFAULT 0, `id_group` INT(10) NOT NULL DEFAULT 0,
`date` date NOT NULL DEFAULT '1970-01-01', `date` date NOT NULL DEFAULT '1970-01-01',
`same_day` enum('monday','tuesday','wednesday','thursday','friday','saturday','sunday') NOT NULL DEFAULT 'sunday',
`day_code` tinyint(2) NOT NULL, `day_code` tinyint(2) NOT NULL,
`description` text, `description` text,
PRIMARY KEY (`id`), PRIMARY KEY (`id`),

View File

@ -70,6 +70,7 @@ $inputs[] = [
'returnAllGroup' => true, 'returnAllGroup' => true,
'name' => 'id_group', 'name' => 'id_group',
'selected' => $calendar->id_group(), 'selected' => $calendar->id_group(),
'required' => true,
], ],
]; ];

View File

@ -58,6 +58,7 @@ $inputs[] = [
'type' => 'file', 'type' => 'file',
'columns' => 25, 'columns' => 25,
'rows' => 10, 'rows' => 10,
'options' => ['required' => 1],
], ],
]; ];
@ -117,15 +118,15 @@ $inputs[] = [
// Print form. // Print form.
HTML::printForm( HTML::printForm(
[ [
'id' => 'icalendar-special-days', 'form' => [
'form' => [ 'action' => $url.'&op=upload_ical&id='.$id_calendar,
'action' => $url.'&op=edit&action=upload_ical&id='.$id_calendar,
'method' => 'POST', 'method' => 'POST',
'id' => 'icalendar-special-days',
], ],
'inputs' => $inputs, 'inputs' => $inputs,
'enctype' => 'multipart/form-data',
], ],
false, false
true
); );
@ -137,6 +138,8 @@ if (!is_user_admin($config['id_user'])) {
$filter['id_group'] = array_keys(users_get_groups(false, 'LM')); $filter['id_group'] = array_keys(users_get_groups(false, 'LM'));
} }
$url = $url.'&id_calendar='.$id_calendar;
// Show display range. // Show display range.
$html = "<table cellpadding='4' cellspacing='4' width='100%' margin-bottom: 10px;'><tr><td>".__('Display range: '); $html = "<table cellpadding='4' cellspacing='4' width='100%' margin-bottom: 10px;'><tr><td>".__('Display range: ');
if ($display_range) { if ($display_range) {
@ -398,7 +401,7 @@ for ($month = 1; $month <= 12; $month++) {
true, true,
['class' => 'invert_filter'] ['class' => 'invert_filter']
).'</a> &nbsp;'; ).'</a> &nbsp;';
$url_delete = $url.'&delete_special_day=1&id='.$special_day['id']; $url_delete = $url.'&op=delete&id='.$special_day['id'];
$script_delete = 'if (!confirm(\''.__('Are you sure?').'\')) return false;'; $script_delete = 'if (!confirm(\''.__('Are you sure?').'\')) return false;';
$cal_table->data[$cal_line][$week] .= '<a href="'.$url_delete.'"'; $cal_table->data[$cal_line][$week] .= '<a href="'.$url_delete.'"';
$cal_table->data[$cal_line][$week] .= ' onClick="'.$script_delete.'"'; $cal_table->data[$cal_line][$week] .= ' onClick="'.$script_delete.'"';
@ -456,7 +459,7 @@ ui_require_javascript_file('pandora_alerts');
?> ?>
<script type="text/javascript"> <script type="text/javascript">
$(document).ready (function () { $(document).ready (function () {
$("#srcbutton").click (function (e) { $("#submit-button").click (function (e) {
e.preventDefault(); e.preventDefault();
load_templates_alerts_special_days({ load_templates_alerts_special_days({
date: '', date: '',

View File

@ -60,21 +60,15 @@ $inputs[] = [
'required' => true, 'required' => true,
'value' => $specialDay->date(), 'value' => $specialDay->date(),
], ],
]; 'extra' => html_print_image(
'images/calendar_view_day.png',
// Date img. true,
$inputs[] = [ [
'arguments' => [
'type' => 'image',
'src' => 'images/calendar_view_day.png',
'value' => $specialDay->date(),
'options' => [
'alt' => 'calendar', 'alt' => 'calendar',
'onclick' => "scwShow(scwID('text-date'),this);", 'onclick' => "scwShow(scwID('text-date'),this);",
'class' => 'invert_filter', 'class' => 'invert_filter',
], ]
),
],
]; ];
if (users_can_manage_group_all('LM') === true) { if (users_can_manage_group_all('LM') === true) {
@ -128,6 +122,15 @@ $inputs[] = [
], ],
]; ];
// Calendar.
$inputs[] = [
'arguments' => [
'type' => 'hidden',
'name' => 'id_calendar',
'value' => $specialDay->id_calendar(),
],
];
// Submit. // Submit.
$inputs[] = [ $inputs[] = [
'arguments' => [ 'arguments' => [
@ -143,7 +146,7 @@ HTML::printForm(
[ [
'form' => [ 'form' => [
'id' => 'form-special-days', 'id' => 'form-special-days',
'action' => $url.'&tab=special_days&op=edit&action=save&id='.$specialDay->id(), 'action' => $url.'&action=save&id='.$specialDay->id(),
'method' => 'POST', 'method' => 'POST',
], ],
'inputs' => $inputs, 'inputs' => $inputs,
@ -156,8 +159,6 @@ echo '<div id="modal-alert-templates" class="invisible"></div>';
ui_require_javascript_file('calendar'); ui_require_javascript_file('calendar');
ui_require_javascript_file('pandora_alerts'); ui_require_javascript_file('pandora_alerts');
hd($ajax_url);
?> ?>
<script type="text/javascript"> <script type="text/javascript">
$(document).ready (function () { $(document).ready (function () {