summaryrefslogtreecommitdiffstats
path: root/application/forms
diff options
context:
space:
mode:
Diffstat (limited to 'application/forms')
-rw-r--r--application/forms/Config/AdvancedForm.php95
-rw-r--r--application/forms/Config/BackendForm.php59
-rw-r--r--application/forms/TimeRangePicker/CommonForm.php196
-rw-r--r--application/forms/TimeRangePicker/CustomForm.php246
4 files changed, 596 insertions, 0 deletions
diff --git a/application/forms/Config/AdvancedForm.php b/application/forms/Config/AdvancedForm.php
new file mode 100644
index 0000000..1fc196b
--- /dev/null
+++ b/application/forms/Config/AdvancedForm.php
@@ -0,0 +1,95 @@
+<?php
+
+namespace Icinga\Module\Graphite\Forms\Config;
+
+use Icinga\Forms\ConfigForm;
+use Icinga\Module\Graphite\Web\Form\Validator\MacroTemplateValidator;
+use Zend_Validate_Regex;
+
+class AdvancedForm extends ConfigForm
+{
+ public function init()
+ {
+ $this->setName('form_config_graphite_advanced');
+ $this->setSubmitLabel($this->translate('Save Changes'));
+ }
+
+ public function createElements(array $formData)
+ {
+ $this->addElements([
+ [
+ 'number',
+ 'ui_default_time_range',
+ [
+ 'label' => $this->translate('Default time range'),
+ 'description' => $this->translate('The default time range for graphs'),
+ 'min' => 0,
+ 'value' => 1
+ ]
+ ],
+ [
+ 'select',
+ 'ui_default_time_range_unit',
+ [
+ 'label' => $this->translate('Default time range unit'),
+ 'description' => $this->translate('The above range\'s unit'),
+ 'multiOptions' => [
+ 'minutes' => $this->translate('Minutes'),
+ 'hours' => $this->translate('Hours'),
+ 'days' => $this->translate('Days'),
+ 'weeks' => $this->translate('Weeks'),
+ 'months' => $this->translate('Months'),
+ 'years' => $this->translate('Years')
+ ],
+ 'value' => 'hours'
+ ]
+ ],
+ [
+ 'checkbox',
+ 'ui_disable_no_graphs_found',
+ [
+ 'label' => $this->translate('Disable "no graphs found"'),
+ 'description' => $this->translate(
+ 'If no graphs were found for a monitored object, just display nothing at all'
+ ),
+ ]
+ ],
+ [
+ 'text',
+ 'icinga_graphite_writer_host_name_template',
+ [
+ 'label' => $this->translate('Host name template'),
+ 'description' => $this->translate(
+ 'The value of your Icinga 2 GraphiteWriter\'s'
+ . ' attribute host_name_template (if specified)'
+ ),
+ 'validators' => [new MacroTemplateValidator()]
+ ]
+ ],
+ [
+ 'text',
+ 'icinga_graphite_writer_service_name_template',
+ [
+ 'label' => $this->translate('Service name template'),
+ 'description' => $this->translate(
+ 'The value of your Icinga 2 GraphiteWriter\'s'
+ . ' attribute service_name_template (if specified)'
+ ),
+ 'validators' => [new MacroTemplateValidator()]
+ ]
+ ],
+ [
+ 'text',
+ 'icinga_customvar_obscured_check_command',
+ [
+ 'label' => $this->translate('Obscured check command custom variable'),
+ 'description' => $this->translate(
+ 'The Icinga custom variable with the "actual" check command obscured'
+ . ' by e.g. check_by_ssh (defaults to check_command)'
+ ),
+ 'validators' => [new Zend_Validate_Regex('/\A\w*\z/')]
+ ]
+ ]
+ ]);
+ }
+}
diff --git a/application/forms/Config/BackendForm.php b/application/forms/Config/BackendForm.php
new file mode 100644
index 0000000..90e0af2
--- /dev/null
+++ b/application/forms/Config/BackendForm.php
@@ -0,0 +1,59 @@
+<?php
+
+namespace Icinga\Module\Graphite\Forms\Config;
+
+use Icinga\Forms\ConfigForm;
+use Icinga\Module\Graphite\Web\Form\Validator\HttpUserValidator;
+
+class BackendForm extends ConfigForm
+{
+ public function init()
+ {
+ $this->setName('form_config_graphite_backend');
+ $this->setSubmitLabel($this->translate('Save Changes'));
+ }
+
+ public function createElements(array $formData)
+ {
+ $this->addElements([
+ [
+ 'text',
+ 'graphite_url',
+ [
+ 'required' => true,
+ 'label' => $this->translate('Graphite Web URL'),
+ 'description' => $this->translate('URL to your Graphite Web'),
+ 'validators' => ['UrlValidator']
+ ]
+ ],
+ [
+ 'text',
+ 'graphite_user',
+ [
+ 'label' => $this->translate('Graphite Web user'),
+ 'description' => $this->translate(
+ 'A user with access to your Graphite Web via HTTP basic authentication'
+ ),
+ 'validators' => [new HttpUserValidator()]
+ ]
+ ],
+ [
+ 'password',
+ 'graphite_password',
+ [
+ 'renderPassword' => true,
+ 'label' => $this->translate('Graphite Web password'),
+ 'description' => $this->translate('The above user\'s password')
+ ]
+ ],
+ [
+ 'checkbox',
+ 'graphite_insecure',
+ [
+ 'label' => $this->translate('Connect insecurely'),
+ 'description' => $this->translate('Check this to not verify the remote\'s TLS certificate')
+ ]
+ ]
+ ]);
+ }
+}
diff --git a/application/forms/TimeRangePicker/CommonForm.php b/application/forms/TimeRangePicker/CommonForm.php
new file mode 100644
index 0000000..21e2096
--- /dev/null
+++ b/application/forms/TimeRangePicker/CommonForm.php
@@ -0,0 +1,196 @@
+<?php
+
+namespace Icinga\Module\Graphite\Forms\TimeRangePicker;
+
+use Icinga\Module\Graphite\Util\TimeRangePickerTools;
+use Icinga\Web\Form;
+use Zend_Form_Element_Select;
+
+class CommonForm extends Form
+{
+ /**
+ * The selectable units with themselves in seconds
+ *
+ * One month equals 30 days and one year equals 365.25 days. This should cover enough cases.
+ *
+ * @var int[]
+ */
+ protected $rangeFactors = [
+ 'minutes' => 60,
+ 'hours' => 3600,
+ 'days' => 86400,
+ 'weeks' => 604800,
+ 'months' => 2592000,
+ 'years' => 31557600
+ ];
+
+ /**
+ * The elements' default values
+ *
+ * @var string[]|null
+ */
+ protected $defaultFormData;
+
+ public function init()
+ {
+ $this->setName('form_timerangepickercommon_graphite');
+ $this->setAttrib('data-base-target', '_self');
+ $this->setAttrib('class', 'icinga-form icinga-controls inline');
+ }
+
+ public function createElements(array $formData)
+ {
+ $this->addElements([
+ $this->createSelect(
+ 'minutes',
+ $this->translate('Minutes'),
+ $this->translate('Show the last … minutes'),
+ [5, 10, 15, 30, 45],
+ $this->translate('%d minute'),
+ $this->translate('%d minutes')
+ ),
+ $this->createSelect(
+ 'hours',
+ $this->translate('Hours'),
+ $this->translate('Show the last … hours'),
+ [1, 2, 3, 6, 12, 18],
+ $this->translate('%d hour'),
+ $this->translate('%d hours')
+ ),
+ $this->createSelect(
+ 'days',
+ $this->translate('Days'),
+ $this->translate('Show the last … days'),
+ range(1, 6),
+ $this->translate('%d day'),
+ $this->translate('%d days')
+ ),
+ $this->createSelect(
+ 'weeks',
+ $this->translate('Weeks'),
+ $this->translate('Show the last … weeks'),
+ range(1, 4),
+ $this->translate('%d week'),
+ $this->translate('%d weeks')
+ ),
+ $this->createSelect(
+ 'months',
+ $this->translate('Months'),
+ $this->translate('Show the last … months'),
+ [1, 2, 3, 6, 9],
+ $this->translate('%d month'),
+ $this->translate('%d months')
+ ),
+ $this->createSelect(
+ 'years',
+ $this->translate('Years'),
+ $this->translate('Show the last … years'),
+ range(1, 3),
+ $this->translate('%d year'),
+ $this->translate('%d years')
+ )
+ ]);
+
+ $this->urlToForm();
+
+ $this->defaultFormData = $this->getValues();
+ }
+
+ public function onSuccess()
+ {
+ $this->formToUrl();
+ $this->getRedirectUrl()->remove(array_values(TimeRangePickerTools::getAbsoluteRangeParameters()));
+ }
+
+ /**
+ * Create a common range picker for a specific time unit
+ *
+ * @param string $name
+ * @param string $label
+ * @param string $description
+ * @param int[] $options
+ * @param string $singular
+ * @param string $plural
+ *
+ * @return Zend_Form_Element_Select
+ */
+ protected function createSelect($name, $label, $description, array $options, $singular, $plural)
+ {
+ $multiOptions = ['' => $label];
+ foreach ($options as $option) {
+ $multiOptions[$option] = sprintf($option === 1 ? $singular : $plural, $option);
+ }
+
+ $element = $this->createElement('select', $name, [
+ 'label' => $label,
+ 'description' => $description,
+ 'multiOptions' => $multiOptions,
+ 'title' => $description,
+ 'autosubmit' => true
+ ]);
+
+ $decorators = $element->getDecorators();
+ $element->setDecorators([
+ 'Zend_Form_Decorator_ViewHelper' => $decorators['Zend_Form_Decorator_ViewHelper']
+ ]);
+
+ return $element;
+ }
+
+ /**
+ * Set this form's elements' default values based on the redirect URL's parameters
+ */
+ protected function urlToForm()
+ {
+ $params = $this->getRedirectUrl()->getParams();
+ $seconds = TimeRangePickerTools::getRelativeSeconds($params);
+
+ if (
+ $seconds === null
+ && count(array_intersect_key(
+ $params->toArray(false),
+ array_keys(TimeRangePickerTools::getAllRangeParameters())
+ )) === 0
+ ) {
+ $seconds = TimeRangePickerTools::getDefaultRelativeTimeRange();
+ }
+
+ if ($seconds !== null) {
+ if ($seconds !== false) {
+ foreach ($this->rangeFactors as $unit => $factor) {
+ /** @var Zend_Form_Element_Select $element */
+ $element = $this->getElement($unit);
+
+ $options = $element->getMultiOptions();
+ unset($options['']);
+
+ foreach ($options as $option => $_) {
+ if ($seconds === $option * $factor) {
+ $element->setValue((string) $option);
+ return;
+ }
+ }
+ }
+ }
+
+ $params->remove(TimeRangePickerTools::getRelativeRangeParameter());
+ }
+ }
+
+ /**
+ * Change the redirect URL's parameters based on this form's elements' values
+ */
+ protected function formToUrl()
+ {
+ $formData = $this->getValues();
+ foreach ($this->rangeFactors as $unit => $factor) {
+ if ($formData[$unit] !== '' && $formData[$unit] !== $this->defaultFormData[$unit]) {
+ $this->getRedirectUrl()->setParam(
+ TimeRangePickerTools::getRelativeRangeParameter(),
+ (string) ((int) $formData[$unit] * $factor)
+ );
+ return;
+ }
+ }
+ }
+}
diff --git a/application/forms/TimeRangePicker/CustomForm.php b/application/forms/TimeRangePicker/CustomForm.php
new file mode 100644
index 0000000..89b5833
--- /dev/null
+++ b/application/forms/TimeRangePicker/CustomForm.php
@@ -0,0 +1,246 @@
+<?php
+
+namespace Icinga\Module\Graphite\Forms\TimeRangePicker;
+
+use DateInterval;
+use DateTime;
+use DateTimeZone;
+use Icinga\Module\Graphite\Util\TimeRangePickerTools;
+use Icinga\Module\Graphite\Web\Form\Decorator\Proxy;
+use Icinga\Web\Form;
+
+class CustomForm extends Form
+{
+ /**
+ * @var string
+ */
+ protected $dateTimeFormat = 'Y-m-d\TH:i';
+
+ /**
+ * @var string
+ */
+ protected $timestamp = '/^(?:0|-?[1-9]\d*)$/';
+
+ /**
+ * Right now
+ *
+ * @var DateTime
+ */
+ protected $now;
+
+ public function init()
+ {
+ $this->setName('form_timerangepickercustom_graphite');
+ $this->setAttrib('data-base-target', '_self');
+ }
+
+ public function createElements(array $formData)
+ {
+ $this->addElements([
+ [
+ 'date',
+ 'start_date',
+ [
+ 'placeholder' => 'YYYY-MM-DD',
+ 'label' => $this->translate('Start'),
+ 'description' => $this->translate('Start of the date/time range')
+ ]
+ ],
+ [
+ 'time',
+ 'start_time',
+ [
+ 'placeholder' => 'HH:MM',
+ 'label' => $this->translate('Start'),
+ 'description' => $this->translate('Start of the date/time range')
+ ]
+ ],
+ [
+ 'date',
+ 'end_date',
+ [
+ 'placeholder' => 'YYYY-MM-DD',
+ 'label' => $this->translate('End'),
+ 'description' => $this->translate('End of the date/time range')
+ ]
+ ],
+ [
+ 'time',
+ 'end_time',
+ [
+ 'placeholder' => 'HH:MM',
+ 'label' => $this->translate('End'),
+ 'description' => $this->translate('End of the date/time range')
+ ]
+ ]
+ ]);
+
+ $this->groupDateTime('start');
+ $this->groupDateTime('end');
+
+ $this->setSubmitLabel($this->translate('Update'));
+
+ $this->urlToForm('start', $this->getRelativeTimestamp());
+ $this->urlToForm('end');
+ }
+
+ public function addSubmitButton()
+ {
+ $result = parent::addSubmitButton();
+
+ $this->getElement('btn_submit')->class = 'flyover-toggle';
+
+ return $result;
+ }
+
+ public function onSuccess()
+ {
+ $start = $this->formToUrl('start', '00:00');
+ $end = $this->formToUrl('end', '23:59', 'PT59S');
+ if ($start > $end) {
+ $absoluteRangeParameters = TimeRangePickerTools::getAbsoluteRangeParameters();
+ $this->getRedirectUrl()->getParams()
+ ->set($absoluteRangeParameters['start'], $end)
+ ->set($absoluteRangeParameters['end'], $start);
+ }
+
+ $this->getRedirectUrl()->remove(TimeRangePickerTools::getRelativeRangeParameter());
+ }
+
+ /**
+ * Add display group for a date and a time input belonging together
+ *
+ * @param string $part Either 'start' or 'end'
+ */
+ protected function groupDateTime($part)
+ {
+ $this->addDisplayGroup(["{$part}_date", "{$part}_time"], $part);
+ $group = $this->getDisplayGroup($part);
+
+ foreach ($group->getElements() as $element) {
+ /** @var \Zend_Form_Element $element */
+
+ $elementDecorators = $element->getDecorators();
+ $element->setDecorators([
+ 'Zend_Form_Decorator_ViewHelper' => $elementDecorators['Zend_Form_Decorator_ViewHelper']
+ ]);
+ }
+
+ $decorators = [];
+ foreach ($elementDecorators as $key => $decorator) {
+ if ($key === 'Zend_Form_Decorator_ViewHelper') {
+ $decorators['Zend_Form_Decorator_FormElements'] =
+ $group->getDecorators()['Zend_Form_Decorator_FormElements'];
+ } else {
+ $decorators[$key] = (new Proxy())->setActualDecorator($decorator->setElement($element));
+ }
+ }
+
+ $group->setDecorators($decorators);
+ }
+
+ /**
+ * Set this form's elements' default values based on the redirect URL's parameters
+ *
+ * @param string $part Either 'start' or 'end'
+ * @param int $defaultTimestamp Fallback
+ */
+ protected function urlToForm($part, $defaultTimestamp = null)
+ {
+ $params = $this->getRedirectUrl()->getParams();
+ $absoluteRangeParameters = TimeRangePickerTools::getAbsoluteRangeParameters();
+ $timestamp = $params->get($absoluteRangeParameters[$part], $defaultTimestamp);
+
+ if ($timestamp !== null) {
+ if (preg_match($this->timestamp, $timestamp)) {
+ list($date, $time) = explode(
+ 'T',
+ DateTime::createFromFormat('U', $timestamp)
+ ->setTimezone(new DateTimeZone(date_default_timezone_get()))
+ ->format($this->dateTimeFormat)
+ );
+
+ $this->getElement("{$part}_date")->setValue($date);
+ $this->getElement("{$part}_time")->setValue($time);
+ } else {
+ $params->remove($absoluteRangeParameters[$part]);
+ }
+ }
+ }
+
+ /**
+ * Get the relative range start (if any) set by {@link CommonForm}
+ *
+ * @return int|null
+ */
+ protected function getRelativeTimestamp()
+ {
+ $seconds = TimeRangePickerTools::getRelativeSeconds($this->getRedirectUrl()->getParams());
+ return is_int($seconds) ? $this->getNow()->getTimestamp() - $seconds : null;
+ }
+
+ /**
+ * Change the redirect URL's parameters based on this form's elements' values
+ *
+ * @param string $part Either 'start' or 'end'
+ * @param string $defaultTime Default if no time given
+ * @param string $addInterval Add this interval to the result
+ *
+ * @return int|null The updated timestamp (if any)
+ */
+ protected function formToUrl($part, $defaultTime, $addInterval = null)
+ {
+ $date = $this->getValue("{$part}_date");
+ $time = $this->getValue("{$part}_time");
+ $params = $this->getRedirectUrl()->getParams();
+ $absoluteRangeParameters = TimeRangePickerTools::getAbsoluteRangeParameters();
+
+ if ($date === '' && $time === '') {
+ $params->remove($absoluteRangeParameters[$part]);
+ } else {
+ $dateTime = DateTime::createFromFormat(
+ $this->dateTimeFormat,
+ ($date === '' ? $this->getNow()->format('Y-m-d') : $date)
+ . 'T' . ($time === '' ? $defaultTime : $time)
+ );
+
+ if ($dateTime === false) {
+ $params->remove($absoluteRangeParameters[$part]);
+ } else {
+ if ($addInterval !== null) {
+ $dateTime->add(new DateInterval($addInterval));
+ }
+
+ $params->set($absoluteRangeParameters[$part], $dateTime->format('U'));
+ return $dateTime->getTimestamp();
+ }
+ }
+ }
+
+ /**
+ * Get {@link now}
+ *
+ * @return DateTime
+ */
+ public function getNow()
+ {
+ if ($this->now === null) {
+ $this->now = new DateTime();
+ }
+
+ return $this->now;
+ }
+
+ /**
+ * Set {@link now}
+ *
+ * @param DateTime $now
+ *
+ * @return $this
+ */
+ public function setNow($now)
+ {
+ $this->now = $now;
+ return $this;
+ }
+}