From 5e945e31940673eb0e4c8fe95fc16b5a0ab0ae48 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 20 Mar 2014 15:39:04 +0100 Subject: [PATCH] Make interval selection being a widget refs #4190 --- .../controllers/TimelineController.php | 49 ++++++++- .../views/scripts/timeline/show.phtml | 9 +- .../library/Monitoring/Timeline/TimeLine.php | 64 ----------- .../Web/Widget/TimelineIntervalBox.php | 103 ++++++++++++++++++ 4 files changed, 157 insertions(+), 68 deletions(-) create mode 100644 modules/monitoring/library/Monitoring/Web/Widget/TimelineIntervalBox.php diff --git a/modules/monitoring/application/controllers/TimelineController.php b/modules/monitoring/application/controllers/TimelineController.php index 70dce5354..8052a8d24 100644 --- a/modules/monitoring/application/controllers/TimelineController.php +++ b/modules/monitoring/application/controllers/TimelineController.php @@ -36,12 +36,14 @@ use Icinga\Web\Controller\ActionController; use Icinga\Module\Monitoring\Timeline\TimeLine; use Icinga\Module\Monitoring\Timeline\TimeEntry; use Icinga\Module\Monitoring\Timeline\TimeRange; +use Icinga\Module\Monitoring\Web\Widget\TimelineIntervalBox; use Icinga\Module\Monitoring\DataView\EventHistory as EventHistoryView; class Monitoring_TimelineController extends ActionController { public function showAction() { + $this->setupIntervalBox(); $timeline = new TimeLine(); $timeline->setName('Timeline'); $timeline->setRequest($this->_request); @@ -49,7 +51,7 @@ class Monitoring_TimelineController extends ActionController $timeline->buildForm(); // Necessary in order to populate request parameters $timeline->populate($this->_request->getParams()); $timeline->setAttrib('data-icinga-component', 'monitoring/timelineComponent'); - list($displayRange, $forecastRange) = $this->buildTimeRanges($timeline->getInterval()); + list($displayRange, $forecastRange) = $this->buildTimeRanges($this->getTimelineInterval()); $timeline->setTimeRange($displayRange); $timeline->setDisplayData($this->loadData($displayRange)); $timeline->setForecastData($this->loadData($forecastRange)); @@ -58,11 +60,11 @@ class Monitoring_TimelineController extends ActionController public function extendAction() { + $this->setupIntervalBox(); $timeline = new TimeLine(); - $timeline->showLineOnly(); $timeline->setRequest($this->_request); $timeline->setConfiguration(Config::app()); - list($displayRange, $forecastRange) = $this->buildTimeRanges($timeline->getInterval()); + list($displayRange, $forecastRange) = $this->buildTimeRanges($this->getTimelineInterval()); $timeline->setTimeRange($displayRange); $timeline->setDisplayData($this->loadData($displayRange)); $timeline->setForecastData($this->loadData($forecastRange)); @@ -72,6 +74,47 @@ class Monitoring_TimelineController extends ActionController $this->_helper->layout()->disableLayout(); } + /** + * Create a select box the user can choose the timeline interval from + */ + private function setupIntervalBox() + { + $box = new TimelineIntervalBox( + 'intervalBox', + array( + '4h' => t('4 Hours'), + '1d' => t('One day'), + '1w' => t('One week'), + '1m' => t('One month'), + '1y' => t('One year') + ) + ); + $box->applyRequest($this->getRequest()); + $this->view->intervalBox = $box; + } + + /** + * Return the chosen interval + * + * @return DateInterval The chosen interval + */ + private function getTimelineInterval() + { + switch ($this->view->intervalBox->getInterval()) + { + case '1d': + return new DateInterval('P1D'); + case '1w': + return new DateInterval('P1W'); + case '1m': + return new DateInterval('P1M'); + case '1y': + return new DateInterval('P1Y'); + default: + return new DateInterval('PT4H'); + } + } + /** * Return a new display- and forecast time range * diff --git a/modules/monitoring/application/views/scripts/timeline/show.phtml b/modules/monitoring/application/views/scripts/timeline/show.phtml index 9ec18756c..4bab9e26d 100644 --- a/modules/monitoring/application/views/scripts/timeline/show.phtml +++ b/modules/monitoring/application/views/scripts/timeline/show.phtml @@ -1 +1,8 @@ -timeline ?> \ No newline at end of file +compact): ?> +
+
+ intervalBox; ?> +
+
+ +timeline; ?> diff --git a/modules/monitoring/library/Monitoring/Timeline/TimeLine.php b/modules/monitoring/library/Monitoring/Timeline/TimeLine.php index a759656ba..56ace53fb 100644 --- a/modules/monitoring/library/Monitoring/Timeline/TimeLine.php +++ b/modules/monitoring/library/Monitoring/Timeline/TimeLine.php @@ -62,13 +62,6 @@ class TimeLine extends Form */ private $forecastData; - /** - * Whether only the timeline is shown - * - * @var bool - */ - private $hideOuterElements = false; - /** * The maximum diameter each circle can have * @@ -120,14 +113,6 @@ class TimeLine extends Form $this->forecastData = $entries; } - /** - * Define that only the timeline itself should be rendered - */ - public function showLineOnly() - { - $this->hideOuterElements = true; - } - /** * Set the maximum diameter each circle can have * @@ -145,31 +130,6 @@ class TimeLine extends Form } } - /** - * Return the chosen interval - * - * @return DateInterval The chosen interval - * @throws Exception If an invalid interval is given in the current request - */ - public function getInterval() - { - switch ($this->getRequest()->getParam('timelineInterval', '4h')) - { - case '4h': - return new DateInterval('PT4H'); - case '1d': - return new DateInterval('P1D'); - case '1w': - return new DateInterval('P1W'); - case '1m': - return new DateInterval('P1M'); - case '1y': - return new DateInterval('P1Y'); - default: - throw new Exception('Invalid interval given in request'); - } - } - /** * Disable the CSRF token */ @@ -191,30 +151,6 @@ class TimeLine extends Form return parent::render($view); } - /** - * Add form elements - */ - public function create() - { - if (!$this->hideOuterElements) { - $this->addElement( - 'select', - 'timelineInterval', - array( - 'multiOptions' => array( - '4h' => t('4 Hours'), - '1d' => t('One day'), - '1w' => t('One week'), - '1m' => t('One month'), - '1y' => t('One year') - ) - ) - ); - $this->enableAutoSubmit(array('timelineInterval')); - $this->setIgnoreChangeDiscarding(false); - } - } - /** * Add timeline elements */ diff --git a/modules/monitoring/library/Monitoring/Web/Widget/TimelineIntervalBox.php b/modules/monitoring/library/Monitoring/Web/Widget/TimelineIntervalBox.php new file mode 100644 index 000000000..354c05658 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Web/Widget/TimelineIntervalBox.php @@ -0,0 +1,103 @@ +name = $name; + $this->values = $values; + } + + /** + * Apply the parameters from the given request on this widget + * + * @param Request $request The request to use for populating the form + */ + public function applyRequest(Request $request) + { + $this->request = $request; + } + + /** + * Return the chosen interval value or null + * + * @param Request $request The request to fetch the value from + * + * @return string|null + */ + public function getInterval(Request $request = null) + { + if ($request === null && $this->request) { + $request = $this->request; + } + + if ($request) { + return $request->getParam('timelineInterval'); + } + } + + /** + * Renders this widget and returns the HTML as a string + * + * @return string + */ + public function render() + { + $form = new Form(); + $form->setAttrib('class', 'inline'); + $form->setMethod('GET'); + $form->setTokenDisabled(); + $form->setName($this->name); + $form->addElement( + 'select', + 'timelineInterval', + array( + 'label' => 'Timeline Interval', + 'multiOptions' => $this->values, + 'class' => 'autosubmit' + ) + ); + + if ($this->request) { + $form->setAction($this->request->getRequestUri()); + $form->populate($this->request->getParams()); + } + + return $form; + } +}