#10983 new function for get date parameter and added parent date in dashboard

This commit is contained in:
Daniel Cebrian 2023-09-06 08:52:46 +02:00
parent f2b99cf38e
commit d8e0df1912
9 changed files with 194 additions and 0 deletions

View File

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

View File

@ -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.
*

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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,
]
);
?>
<script>
function handle_date_range(element){
if(element.checked) {
$(".row_date_range").show();
} else {
$(".row_date_range").hide();
}
}
var date_range = $("#date_range")[0];
handle_date_range(date_range);
</script>