From 86839e757d3103ee1999100bd8ca253835615bf7 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Wed, 23 Oct 2013 17:31:29 +0200 Subject: [PATCH] Add interval selectbox and group legend refs #4190 --- library/Icinga/Timeline/TimeLine.php | 130 ++++++++++++++++++++++++++- 1 file changed, 127 insertions(+), 3 deletions(-) diff --git a/library/Icinga/Timeline/TimeLine.php b/library/Icinga/Timeline/TimeLine.php index d44083767..07386f9e7 100644 --- a/library/Icinga/Timeline/TimeLine.php +++ b/library/Icinga/Timeline/TimeLine.php @@ -29,7 +29,9 @@ namespace Icinga\Timeline; use \DateInterval; +use \Zend_View_Interface; use \Icinga\Web\Form; +use \Icinga\Web\Form\Element\Note; /** * Represents a set of events in a specific time range @@ -112,7 +114,7 @@ class TimeLine extends Form */ public function getInterval() { - switch ($this->getRequest()->getPost('interval', '4h')) + switch ($this->getRequest()->getPost('timelineInterval', '4h')) { case '4h': return new DateInterval('PT4H'); @@ -138,10 +140,132 @@ class TimeLine extends Form } /** - * Build the timeline + * Render the form and timeline to HTML + * + * @param Zend_View_Interface $view + * @return string + */ + public function render(Zend_View_Interface $view = null) + { + $this->buildForm(); + $this->postCreate(); + 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 + */ + private function postCreate() + { + $timeline = new Note( + 'timeline', + array( + 'value' => '
' . $this->buildTimeline() . '
' + ) + ); + $this->addElement($timeline); // Form::addElement adjusts the element's decorators + $timeline->clearDecorators(); + $timeline->addDecorator('ViewHelper'); + + $legend = new Note( + 'legend', + array( + 'value' => '
' . $this->buildLegend() . '
' + ) + ); + $this->addElement($legend); + $legend->clearDecorators(); + $legend->addDecorator('ViewHelper'); + } + + /** + * Build the legend + */ + private function buildLegend() + { + // TODO: Put this in some sort of dedicated stylesheet + $circleStyle = 'width:100%;height:90px;border-radius:50%;box-shadow:4px 4px 8px grey;border:2px solid;'; + $labelStyle = 'font-size:12px;margin-top:10px;text-align:center;'; + $titleStyle = 'margin-left:25px;'; + + $elements = array(); + foreach ($this->getGroups() as $groupName => $groupInfo) { + $groupColor = $groupInfo['color'] ? $groupInfo['color'] : + ('#' . str_pad(dechex(rand(256,16777215)), 6, '0', STR_PAD_LEFT)); // TODO: This should be kind of cached! + $elements[] = '' . + '
' . + '

' . $groupName . '

'; + } + + $legend = '' . + '

' . t('Shown event groups') . '

' . + '
' . + '
' . + implode( + '', + array_map( + function ($e) { return '
' . $e . '
'; }, + $elements + ) + ) . + '
' . + '
'; + + return $legend; + } + + /** + * Build the timeline + */ + private function buildTimeline() + { + return ''; + } + + /** + * Return contextless attributes of all available distinct group types + * + * Returns an associative array where each key refers to the name + * and the value to the attributes of a specific group type. + * + * @return array + */ + private function getGroups() + { + $groups = array(); + foreach ($this->displayData as $group) { + if (!array_key_exists($group->getName(), $groups)) { + $groups[$group->getName()] = array( + 'color' => $group->getColor(), + 'weight' => $group->getWeight() + ); + } + } + + return $groups; } }