| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class sfWidgetFormTime extends sfWidgetForm |
|---|
| 20 |
{ |
|---|
| 21 |
|
|---|
| 22 |
* Constructor. |
|---|
| 23 |
* |
|---|
| 24 |
* Available options: |
|---|
| 25 |
* |
|---|
| 26 |
* * format: The time format string (%hour%:%minute%:%second%) |
|---|
| 27 |
* * format_without_seconds: The time format string without seconds (%hour%:%minute%) |
|---|
| 28 |
* * with_seconds: Whether to include a select for seconds (false by default) |
|---|
| 29 |
* * hours: An array of hours for the hour select tag (optional) |
|---|
| 30 |
* * minutes: An array of minutes for the minute select tag (optional) |
|---|
| 31 |
* * seconds: An array of seconds for the second select tag (optional) |
|---|
| 32 |
* * can_be_empty: Whether the widget accept an empty value (true by default) |
|---|
| 33 |
* * empty_values: An array of values to use for the empty value (empty string for hours, minutes, and seconds by default) |
|---|
| 34 |
* |
|---|
| 35 |
* @param array $options An array of options |
|---|
| 36 |
* @param array $attributes An array of default HTML attributes |
|---|
| 37 |
* |
|---|
| 38 |
* @see sfWidgetForm |
|---|
| 39 |
*/ |
|---|
| 40 |
protected function configure($options = array(), $attributes = array()) |
|---|
| 41 |
{ |
|---|
| 42 |
$this->addOption('format', '%hour%:%minute%:%second%'); |
|---|
| 43 |
$this->addOption('format_without_seconds', '%hour%:%minute%'); |
|---|
| 44 |
$this->addOption('with_seconds', false); |
|---|
| 45 |
$this->addOption('hours', parent::generateTwoCharsRange(0, 23)); |
|---|
| 46 |
$this->addOption('minutes', parent::generateTwoCharsRange(0, 59)); |
|---|
| 47 |
$this->addOption('seconds', parent::generateTwoCharsRange(0, 59)); |
|---|
| 48 |
|
|---|
| 49 |
$this->addOption('can_be_empty', true); |
|---|
| 50 |
$this->addOption('empty_values', array('hour' => '', 'minute' => '', 'second' => '')); |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
* @param string $name The element name |
|---|
| 55 |
* @param string $value The time displayed in this widget |
|---|
| 56 |
* @param array $attributes An array of HTML attributes to be merged with the default HTML attributes |
|---|
| 57 |
* @param array $errors An array of errors for the field |
|---|
| 58 |
* |
|---|
| 59 |
* @return string An HTML tag string |
|---|
| 60 |
* |
|---|
| 61 |
* @see sfWidgetForm |
|---|
| 62 |
*/ |
|---|
| 63 |
public function render($name, $value = null, $attributes = array(), $errors = array()) |
|---|
| 64 |
{ |
|---|
| 65 |
|
|---|
| 66 |
$default = array('hour' => null, 'minute' => null, 'second' => null); |
|---|
| 67 |
if (is_array($value)) |
|---|
| 68 |
{ |
|---|
| 69 |
$value = array_merge($default, $value); |
|---|
| 70 |
} |
|---|
| 71 |
else |
|---|
| 72 |
{ |
|---|
| 73 |
$value = ctype_digit($value) ? (integer) $value : strtotime($value); |
|---|
| 74 |
if (false === $value) |
|---|
| 75 |
{ |
|---|
| 76 |
$value = $default; |
|---|
| 77 |
} |
|---|
| 78 |
else |
|---|
| 79 |
{ |
|---|
| 80 |
|
|---|
| 81 |
$value = array('hour' => (int) date('H', $value), 'minute' => (int) date('i', $value), 'second' => (int) date('s', $value)); |
|---|
| 82 |
} |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
$time = array(); |
|---|
| 86 |
$emptyValues = $this->getOption('empty_values'); |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
$widget = new sfWidgetFormSelect(array('choices' => $this->getOption('can_be_empty') ? array('' => $emptyValues['hour']) + $this->getOption('hours') : $this->getOption('hours')), array_merge($this->attributes, $attributes)); |
|---|
| 90 |
$time['%hour%'] = $widget->render($name.'[hour]', $value['hour']); |
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
$widget = new sfWidgetFormSelect(array('choices' => $this->getOption('can_be_empty') ? array('' => $emptyValues['minute']) + $this->getOption('minutes') : $this->getOption('minutes')), array_merge($this->attributes, $attributes)); |
|---|
| 94 |
$time['%minute%'] = $widget->render($name.'[minute]', $value['minute']); |
|---|
| 95 |
|
|---|
| 96 |
if ($this->getOption('with_seconds')) |
|---|
| 97 |
{ |
|---|
| 98 |
|
|---|
| 99 |
$widget = new sfWidgetFormSelect(array('choices' => $this->getOption('can_be_empty') ? array('' => $emptyValues['second']) + $this->getOption('seconds') : $this->getOption('seconds')), array_merge($this->attributes, $attributes)); |
|---|
| 100 |
$time['%second%'] = $widget->render($name.'[second]', $value['second']); |
|---|
| 101 |
} |
|---|
| 102 |
|
|---|
| 103 |
return strtr($this->getOption('with_seconds') ? $this->getOption('format') : $this->getOption('format_without_seconds'), $time); |
|---|
| 104 |
} |
|---|
| 105 |
} |
|---|
| 106 |
|
|---|