Created
June 19, 2010 17:31
-
-
Save JanTvrdik/445083 to your computer and use it in GitHub Desktop.
DatePicker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Form control for selecting date | |
* | |
* Internally holds date as DateTime instance which is also returned by getValue method | |
* | |
* @author Jan Tvrdík | |
*/ | |
class DatePicker extends Nette\Forms\FormControl | |
{ | |
/** @var string class name of the input */ | |
public $className = 'datepicker'; | |
/** @var string standard date format */ | |
public $dateFormat = 'j. n. Y'; | |
/** @var bool always generate value attribute in standard format */ | |
public $alwaysReformat = FALSE; | |
/** @var DateTime|NULL internal date reprezentation */ | |
protected $value; | |
/** @var string unfiltered, by user entered value */ | |
protected $rawValue; | |
/** | |
* Sets class name for input element. | |
* | |
* @author Jan Tvrdík | |
* @param string class name of the input element | |
* @return self | |
*/ | |
public function setClassName($className) | |
{ | |
$this->className = $className; | |
return $this; | |
} | |
/** | |
* Sets standard date format. | |
* | |
* @author Jan Tvrdík | |
* @param string standard date format | |
* @return self | |
*/ | |
public function setDateFormat($dateFormat) | |
{ | |
$this->dateFormat = $dateFormat; | |
return $this; | |
} | |
/** | |
* Forces DatePicker to always generate the value attribute in standard format. | |
* | |
* @author Jan Tvrdík | |
* @param bool always generate value attribute in standard format | |
* @return self | |
*/ | |
public function alwaysReformat($alwaysReformat = TRUE) | |
{ | |
$this->alwaysReformat = $alwaysReformat; | |
return $this; | |
} | |
/** | |
* Generates control's HTML element. | |
* | |
* @author Jan Tvrdík | |
* @return Html | |
*/ | |
public function getControl() | |
{ | |
$control = parent::getControl(); | |
$control->value = ($this->alwaysReformat && $this->value ? $this->value->format($this->dateFormat) : $this->rawValue); | |
$control->class[] = $this->className; | |
return $control; | |
} | |
/** | |
* Set DatePicker value | |
* | |
* @author Jan Tvrdík | |
* @param DateTime|int|string | |
* @return self | |
*/ | |
public function setValue($value) | |
{ | |
if ($value instanceof \DateTime) { | |
} elseif (is_int($value)) { // timestamp | |
} elseif (empty($value)) { | |
$rawValue = $value; | |
$value = NULL; | |
} elseif (is_string($value)) { | |
$rawValue = $value; | |
if (preg_match('#^(?P<dd>\d{1,2})[. -] *(?P<mm>\d{1,2})([. -] *(?P<yyyy>\d{4})?)?$#', $value, $matches)) { | |
$dd = $matches['dd']; | |
$mm = $matches['mm']; | |
$yyyy = isset($matches['yyyy']) ? $matches['yyyy'] : date('Y'); | |
if (checkdate($mm, $dd, $yyyy)) { | |
$value = "$yyyy-$mm-$dd"; | |
} else { | |
$value = NULL; | |
} | |
} | |
} else { | |
throw new \InvalidArgumentException(); | |
} | |
if ($value !== NULL) { | |
// DateTime constructor throws Exception when invalid input given | |
try { | |
$value = Nette\Tools::createDateTime($value); // clone DateTime when given | |
} catch (\Exception $e) { | |
$value = NULL; | |
} | |
} | |
if (!isset($rawValue) && isset($value)) { | |
$rawValue = $value->format($this->dateFormat); | |
} | |
$this->value = $value; | |
$this->rawValue = $rawValue; | |
return $this; | |
} | |
/** | |
* Returns unfiltered value. | |
* | |
* @author Jan Tvrdík | |
* @return string | |
*/ | |
public function getRawValue() | |
{ | |
return $this->rawValue; | |
} | |
/** | |
* Does user enter anything? (the value doesn't have to be valid) | |
* | |
* @author Jan Tvrdík | |
* @param self | |
* @return bool | |
*/ | |
public static function validateFilled(Nette\Forms\IFormControl $control) | |
{ | |
$rawValue = $control->getRawValue(); | |
return !empty($rawValue); | |
} | |
/** | |
* Is entered value valid? (empty value is also valid!) | |
* | |
* @author Jan Tvrdík | |
* @param self | |
* @return bool | |
*/ | |
public static function validateValid(Nette\Forms\IFormControl $control) | |
{ | |
$rawValue = $control->getRawValue(); | |
return (empty($rawValue) || $control->getValue() instanceof \DateTime); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment