1<?php 2 3namespace Icinga\Module\Director\Forms; 4 5use Icinga\Module\Director\Objects\IcingaObject; 6use Icinga\Module\Director\Objects\IcingaTimePeriod; 7use Icinga\Module\Director\Objects\IcingaTimePeriodRange; 8use Icinga\Module\Director\Web\Form\DirectorObjectForm; 9 10class IcingaTimePeriodRangeForm extends DirectorObjectForm 11{ 12 /** 13 * @var IcingaTimePeriod 14 */ 15 private $period; 16 17 public function setup() 18 { 19 $this->addHidden('timeperiod_id', $this->period->get('id')); 20 $this->addElement('text', 'range_key', array( 21 'label' => $this->translate('Day(s)'), 22 'description' => $this->translate( 23 'Might be, monday, tuesday, 2016-01-28 - have a look at the documentation for more examples' 24 ), 25 )); 26 27 $this->addElement('text', 'range_value', array( 28 'label' => $this->translate('Timerperiods'), 29 'description' => $this->translate( 30 'One or more time periods, e.g. 00:00-24:00 or 00:00-09:00,17:00-24:00' 31 ), 32 )); 33 34 $this->setButtons(); 35 } 36 37 public function setTimePeriod(IcingaTimePeriod $period) 38 { 39 $this->period = $period; 40 $this->setDb($period->getConnection()); 41 return $this; 42 } 43 44 /** 45 * @param IcingaTimePeriodRange $object 46 */ 47 protected function deleteObject($object) 48 { 49 $key = $object->get('range_key'); 50 $period = $this->period; 51 $period->ranges()->remove($key); 52 $period->store(); 53 $msg = sprintf( 54 'Time period range "%s" has been removed from %s', 55 $key, 56 $period->getObjectName() 57 ); 58 59 $url = $this->getSuccessUrl()->without( 60 ['range', 'range_type'] 61 ); 62 63 $this->setSuccessUrl($url); 64 $this->redirectOnSuccess($msg); 65 } 66 67 public function onSuccess() 68 { 69 $object = $this->object(); 70 if ($object->hasBeenModified()) { 71 $this->period->ranges()->setRange( 72 $this->getValue('range_key'), 73 $this->getValue('range_value') 74 ); 75 } 76 77 if ($this->period->hasBeenModified()) { 78 if (! $object->hasBeenLoadedFromDb()) { 79 $this->setHttpResponseCode(201); 80 } 81 82 $msg = sprintf( 83 $object->hasBeenLoadedFromDb() 84 ? $this->translate('The %s has successfully been stored') 85 : $this->translate('A new %s has successfully been created'), 86 $this->translate($this->getObjectShortClassName()) 87 ); 88 89 $this->period->store($this->db); 90 } else { 91 if ($this->isApiRequest()) { 92 $this->setHttpResponseCode(304); 93 } 94 $msg = $this->translate('No action taken, object has not been modified'); 95 } 96 if ($object instanceof IcingaObject) { 97 $this->setSuccessUrl( 98 'director/' . strtolower($this->getObjectShortClassName()), 99 $object->getUrlParams() 100 ); 101 } 102 103 $this->redirectOnSuccess($msg); 104 } 105} 106