From d8e0df19122f2b12f84029e09beab960066ab40f Mon Sep 17 00:00:00 2001 From: Daniel Cebrian Date: Wed, 6 Sep 2023 08:52:46 +0200 Subject: [PATCH] #10983 new function for get date parameter and added parent date in dashboard --- pandora_console/extras/mr/66.sql | 5 ++ pandora_console/include/functions.php | 64 +++++++++++++++++++ pandora_console/include/functions_html.php | 14 ++++ .../include/lib/Dashboard/Manager.php | 11 ++++ .../include/lib/Dashboard/Widget.php | 51 +++++++++++++++ .../lib/Dashboard/Widgets/BlockHistogram.php | 3 + .../lib/Dashboard/Widgets/single_graph.php | 4 ++ pandora_console/pandoradb.sql | 3 + .../views/dashboard/formDashboard.php | 39 +++++++++++ 9 files changed, 194 insertions(+) diff --git a/pandora_console/extras/mr/66.sql b/pandora_console/extras/mr/66.sql index 8161c4c2b6..a30bcb4565 100644 --- a/pandora_console/extras/mr/66.sql +++ b/pandora_console/extras/mr/66.sql @@ -44,4 +44,9 @@ ADD COLUMN `time_init` VARCHAR(45) NULL AFTER `date_init`, ADD COLUMN `date_end` VARCHAR(45) NULL AFTER `time_init`, ADD COLUMN `time_end` VARCHAR(45) NULL AFTER `date_end`; +ALTER TABLE `tdashboard` +ADD COLUMN `date_range` TINYINT NOT NULL DEFAULT 0 AFTER `cells_slideshow`, +ADD COLUMN `date_from` INT NOT NULL DEFAULT 0 AFTER `date_range`, +ADD COLUMN `date_to` INT NOT NULL DEFAULT 0 AFTER `date_from`; + COMMIT; diff --git a/pandora_console/include/functions.php b/pandora_console/include/functions.php index 0895325ef5..0a166a28d2 100644 --- a/pandora_console/include/functions.php +++ b/pandora_console/include/functions.php @@ -990,6 +990,70 @@ function get_parameter($name, $default='') } +function get_parameter_date($name, $default='', $date_format='Y/m/d') +{ + $date_end = get_parameter('date_end', 0); + $time_end = get_parameter('time_end'); + $datetime_end = strtotime($date_end.' '.$time_end); + + $custom_date = get_parameter('custom_date', 0); + $range = get_parameter('range', SECONDS_1DAY); + $date_text = get_parameter('range_text', SECONDS_1DAY); + $date_init_less = (strtotime(date('Y/m/d')) - SECONDS_1DAY); + $date_init = get_parameter('date_init', date(DATE_FORMAT, $date_init_less)); + $time_init = get_parameter('time_init', date(TIME_FORMAT, $date_init_less)); + $datetime_init = strtotime($date_init.' '.$time_init); + if ($custom_date === '1') { + if ($datetime_init >= $datetime_end) { + $datetime_init = $date_init_less; + } + + $date_init = date('Y/m/d H:i:s', $datetime_init); + $date_end = date('Y/m/d H:i:s', $datetime_end); + $period = ($datetime_end - $datetime_init); + } else if ($custom_date === '2') { + $date_units = get_parameter('range_units'); + $date_end = date('Y/m/d H:i:s'); + $date_init = date('Y/m/d H:i:s', (strtotime($date_end) - ((int) $date_text * (int) $date_units))); + $period = (strtotime($date_end) - strtotime($date_init)); + } else if (in_array($range, ['this_week', 'this_month', 'past_week', 'past_month'])) { + if ($range === 'this_week') { + $monday = date('Y/m/d', strtotime('last monday')); + + $sunday = date('Y/m/d', strtotime($monday.' +6 days')); + $period = (strtotime($sunday) - strtotime($monday)); + $date_init = $monday; + $date_end = $sunday; + } else if ($range === 'this_month') { + $date_end = date('Y/m/d', strtotime('last day of this month')); + $first_of_month = date('Y/m/d', strtotime('first day of this month')); + $date_init = $first_of_month; + $period = (strtotime($date_end) - strtotime($first_of_month)); + } else if ($range === 'past_month') { + $date_end = date('Y/m/d', strtotime('last day of previous month')); + $first_of_month = date('Y/m/d', strtotime('first day of previous month')); + $date_init = $first_of_month; + $period = (strtotime($date_end) - strtotime($first_of_month)); + } else if ($range === 'past_week') { + $date_end = date('Y/m/d', strtotime('sunday', strtotime('last week'))); + $first_of_week = date('Y/m/d', strtotime('monday', strtotime('last week'))); + $date_init = $first_of_week; + $period = (strtotime($date_end) - strtotime($first_of_week)); + } + } else { + $date_end = date('Y/m/d H:i:s'); + $date_init = date('Y/m/d H:i:s', (strtotime($date_end) - $range)); + $period = (strtotime($date_end) - strtotime($date_init)); + } + + return [ + 'date_init' => date($date_format, strtotime($date_init)), + 'date_end' => date($date_format, strtotime($date_end)), + 'period' => $period, + ]; +} + + /** * Get a parameter from a get request. * diff --git a/pandora_console/include/functions_html.php b/pandora_console/include/functions_html.php index 3d8eb231b9..1f33023bf9 100644 --- a/pandora_console/include/functions_html.php +++ b/pandora_console/include/functions_html.php @@ -6280,6 +6280,20 @@ function html_print_input($data, $wrapper='div', $input_only=false) ); break; + case 'date_range': + $output .= html_print_select_date_range( + $data['name'], + true, + (isset($data['selected']) === true) ? $data['selected'] : SECONDS_1DAY, + (isset($data['date_init']) === true) ? $data['date_init'] : '', + (isset($data['time_init']) === true) ? $data['time_init'] : '', + (isset($data['date_end']) === true) ? $data['date_end'] : '', + (isset($data['time_end']) === true) ? $data['time_end'] : '', + (isset($data['date_text']) === true) ? $data['date_text'] : SECONDS_1DAY, + (isset($data['class']) === true) ? $data['class'] : 'w100p', + ); + break; + default: // Ignore. break; diff --git a/pandora_console/include/lib/Dashboard/Manager.php b/pandora_console/include/lib/Dashboard/Manager.php index 40b6d2d1a0..10ab96986c 100644 --- a/pandora_console/include/lib/Dashboard/Manager.php +++ b/pandora_console/include/lib/Dashboard/Manager.php @@ -458,6 +458,12 @@ class Manager implements PublicLogin $this->publicLink ); + if ((bool) $this->dashboardFields['date_range'] === true) { + $dateFrom = $this->dashboardFields['date_from']; + $dateTo = $this->dashboardFields['date_to']; + $instance->setDateRange($dateFrom, $dateTo); + } + return $instance; } @@ -1015,6 +1021,8 @@ class Manager implements PublicLogin $id_group = \get_parameter('id_group'); $slideshow = \get_parameter_switch('slideshow'); $favourite = \get_parameter_switch('favourite'); + $dateRange = \get_parameter_switch('date_range'); + $dateData = \get_parameter_date('range', '', 'U'); $id_user = (empty($private) === false) ? $config['id_user'] : ''; @@ -1024,6 +1032,9 @@ class Manager implements PublicLogin 'id_group' => $id_group, 'cells_slideshow' => $slideshow, 'active' => $favourite, + 'date_range' => $dateRange, + 'date_from' => $dateData['date_init'], + 'date_to' => $dateData['date_end'], ]; if ($this->dashboardId === 0) { diff --git a/pandora_console/include/lib/Dashboard/Widget.php b/pandora_console/include/lib/Dashboard/Widget.php index b9364fcd31..acd9f4c94f 100644 --- a/pandora_console/include/lib/Dashboard/Widget.php +++ b/pandora_console/include/lib/Dashboard/Widget.php @@ -51,6 +51,20 @@ class Widget */ private $showSelectNodeMeta; + /** + * Date from init for filter widget. + * + * @var integer + */ + private $dateFrom; + + /** + * Date from end for filter widget. + * + * @var integer + */ + private $dateTo; + /** * Contructor widget. @@ -804,4 +818,41 @@ class Widget } + /** + * Set the date range of parent configuration. + * + * @param integer $dateFrom Date from init for filter widget. + * @param integer $dateTo Date from end for filter widget. + * + * @return void + */ + public function setDateRange(int $dateFrom, int $dateTo) + { + $this->dateFrom = $dateFrom; + $this->dateTo = $dateTo; + } + + + public function getDateFrom() + { + return $this->dateFrom; + } + + + public function getDateTo() + { + return $this->dateTo; + } + + + public function getPeriod():mixed + { + if (empty($this->dateFrom) === false && empty($this->dateTo) === false) { + return ($this->dateTo - $this->dateFrom); + } else { + return null; + } + } + + } diff --git a/pandora_console/include/lib/Dashboard/Widgets/BlockHistogram.php b/pandora_console/include/lib/Dashboard/Widgets/BlockHistogram.php index a3ff215c76..1484823518 100644 --- a/pandora_console/include/lib/Dashboard/Widgets/BlockHistogram.php +++ b/pandora_console/include/lib/Dashboard/Widgets/BlockHistogram.php @@ -520,6 +520,9 @@ class BlockHistogram extends Widget global $config; $size = parent::getSize(); + if (empty(parent::getPeriod()) === false) { + $this->values['period'] = parent::getPeriod(); + } // Desactive scroll bars only this item. $id_agent = $data['agent_id']; diff --git a/pandora_console/include/lib/Dashboard/Widgets/single_graph.php b/pandora_console/include/lib/Dashboard/Widgets/single_graph.php index 72ad55309b..013faf1e67 100644 --- a/pandora_console/include/lib/Dashboard/Widgets/single_graph.php +++ b/pandora_console/include/lib/Dashboard/Widgets/single_graph.php @@ -399,6 +399,10 @@ class SingleGraphWidget extends Widget $module_name = \modules_get_agentmodule_name($this->values['moduleId']); $units_name = \modules_get_unit($this->values['moduleId']); + if (empty(parent::getPeriod()) === false) { + $this->values['period'] = parent::getPeriod(); + } + $trickHight = 0; if ($this->values['showLegend'] === 1) { // Needed for legend. diff --git a/pandora_console/pandoradb.sql b/pandora_console/pandoradb.sql index b50a5ba149..a92eb3b2c5 100644 --- a/pandora_console/pandoradb.sql +++ b/pandora_console/pandoradb.sql @@ -2665,6 +2665,9 @@ CREATE TABLE IF NOT EXISTS `tdashboard` ( `active` TINYINT NOT NULL DEFAULT 0, `cells` INT UNSIGNED DEFAULT 0, `cells_slideshow` TINYINT NOT NULL DEFAULT 0, + `date_range` TINYINT NOT NULL DEFAULT 0, + `date_from` INT NOT NULL DEFAULT 0, + `date_to` INT NOT NULL DEFAULT 0, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=UTF8MB4; diff --git a/pandora_console/views/dashboard/formDashboard.php b/pandora_console/views/dashboard/formDashboard.php index c36df6cf06..fafacd583d 100644 --- a/pandora_console/views/dashboard/formDashboard.php +++ b/pandora_console/views/dashboard/formDashboard.php @@ -102,6 +102,31 @@ $inputs = [ ], ], ], + [ + 'label' => __('Date range'), + 'arguments' => [ + 'name' => 'date_range', + 'id' => 'date_range', + 'type' => 'switch', + 'value' => $arrayDashboard['date_range'], + 'onchange' => 'handle_date_range(this)', + ], + ], + [ + 'label' => __('Select range'), + 'style' => 'display: none;', + 'class' => 'row_date_range', + 'arguments' => [ + 'name' => 'range', + 'id' => 'range', + 'selected' => ($arrayDashboard['date_to'] - $arrayDashboard['date_from']), + 'type' => 'date_range', + 'date_init' => $arrayDashboard['date_from'], + 'time_init' => $arrayDashboard['date_from'], + 'date_end' => $arrayDashboard['date_to'], + 'time_end' => $arrayDashboard['date_to'], + ], + ], [ 'block_id' => 'private', 'direct' => 1, @@ -135,3 +160,17 @@ HTML::printForm( 'inputs' => $inputs, ] ); + +?> + + \ No newline at end of file