<?php
namespace Engine\Form\Input; class TimeInterval extends \Engine\Form\Input\Text { protected $class = "timeInterval"; protected $timeStart = NULL; protected $timeEnd = NULL; public function __construct($label=null, $name=null, $value=[]) { parent::__construct($label, null, null); if(!$value)$value = []; $this->timeStart = \Engine\Form\Input\Time::create(null, "{$name}[start]", null); $this->timeEnd = \Engine\Form\Input\Time::create(null, "{$name}[end]", null); if(isset($value['start']) || isset($value['end'])){ $this->timeStart->setValue($value['start']); $this->timeEnd->setValue($value['end']); } else{ $this->timeStart->setValue(reset($value)); $this->timeEnd->setValue(next($value)); } $this->setName($name); } public function getStart() { return $this->timeStart->getValue(); } public function setStart($start) { $this->timeStart->setValue($start); } public function getEnd() { return $this->timeEnd->getValue(); } public function setEnd($end) { $this->timeEnd->setValue($end); } public function setValue($interval){ if(isset($interval['start']) || isset($interval['end'])){ $this->timeStart->setValue($interval['start']); $this->timeEnd->setValue($interval['end']); } elseif($interval){ $this->timeStart->setValue(reset($interval)); $this->timeEnd->setValue(next($interval)); } return $this; } protected function getTemplateParams($templateClass = null, $parentElement = null){ $params = parent::getTemplateParams($templateClass, $parentElement ); $this->timeStart->setWidth($params['style']['width'] / 2 - 10); $this->timeEnd->setWidth($params['style']['width'] / 2 - 9); if($this->disabled){ $this->timeStart->setDisabled($this->disabled); $this->timeEnd->setDisabled($this->disabled); } if($this->getInvisible()){ $this->timeStart->setInvisible($this->getInvisible()); $this->timeEnd->setInvisible($this->getInvisible()); } $params['timeStart'] = $this->timeStart; $params['timeEnd'] = $this->timeEnd; return $params; } public function addEvent($event, $jsCallback){ switch($event){ case 'onChange': $this->timeStart->addEvent($event, $jsCallback); $this->timeEnd->addEvent($event, $jsCallback); break; } return parent::addEvent($event, $jsCallback); } public function setName($name){ $this->timeStart->setName("{$name}[start]"); $this->timeEnd->setName("{$name}[end]"); $this->name = $name; return $this; } public function setId($ids){ if (!is_array($ids) or count($ids)!=2) { throw new \Exception('`Time interval` field id can only be set as an array.'); } if(isset($ids['start']) || isset($ids['end'])){ $this->timeStart->setId($ids['start']); $this->timeEnd->setId($ids['end']); } elseif($ids){ $this->timeStart->setId(reset($ids)); $this->timeEnd->setId(next($ids)); } return $this; } }