| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class sfWidgetFormDateTime extends sfWidgetForm |
|---|
| 20 |
{ |
|---|
| 21 |
|
|---|
| 22 |
* Configures the current widget. |
|---|
| 23 |
* |
|---|
| 24 |
* The attributes are passed to both the date and the time widget. |
|---|
| 25 |
* |
|---|
| 26 |
* If you want to pass HTML attributes to one of the two widget, pass an |
|---|
| 27 |
* attributes option to the date or time option (see below). |
|---|
| 28 |
* |
|---|
| 29 |
* Available options: |
|---|
| 30 |
* |
|---|
| 31 |
* * date: Options for the date widget (see sfWidgetFormDate) |
|---|
| 32 |
* * time: Options for the time widget (see sfWidgetFormTime) |
|---|
| 33 |
* * with_time: Whether to include time (true by default) |
|---|
| 34 |
* * format: The format string for the date and the time widget (default to %date% %time%) |
|---|
| 35 |
* |
|---|
| 36 |
* @param array $options An array of options |
|---|
| 37 |
* @param array $attributes An array of default HTML attributes |
|---|
| 38 |
* |
|---|
| 39 |
* @see sfWidgetForm |
|---|
| 40 |
*/ |
|---|
| 41 |
protected function configure($options = array(), $attributes = array()) |
|---|
| 42 |
{ |
|---|
| 43 |
$this->addOption('date', array()); |
|---|
| 44 |
$this->addOption('time', array()); |
|---|
| 45 |
$this->addOption('with_time', true); |
|---|
| 46 |
$this->addOption('format', '%date% %time%'); |
|---|
| 47 |
} |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
* @param string $name The element name |
|---|
| 51 |
* @param string $value The date and time displayed in this widget |
|---|
| 52 |
* @param array $attributes An array of HTML attributes to be merged with the default HTML attributes |
|---|
| 53 |
* @param array $errors An array of errors for the field |
|---|
| 54 |
* |
|---|
| 55 |
* @return string An HTML tag string |
|---|
| 56 |
* |
|---|
| 57 |
* @see sfWidgetForm |
|---|
| 58 |
*/ |
|---|
| 59 |
function render($name, $value = null, $attributes = array(), $errors = array()) |
|---|
| 60 |
{ |
|---|
| 61 |
$date = $this->getDateWidget($attributes)->render($name, $value); |
|---|
| 62 |
|
|---|
| 63 |
if (!$this->getOption('with_time')) |
|---|
| 64 |
{ |
|---|
| 65 |
return $date; |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
return strtr($this->getOption('format'), array( |
|---|
| 69 |
'%date%' => $date, |
|---|
| 70 |
'%time%' => $this->getTimeWidget($attributes)->render($name, $value), |
|---|
| 71 |
)); |
|---|
| 72 |
} |
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
* Returns the date widget. |
|---|
| 76 |
* |
|---|
| 77 |
* @param array $attributes An array of attributes |
|---|
| 78 |
* |
|---|
| 79 |
* @return sfWidgetForm A Widget representing the date |
|---|
| 80 |
*/ |
|---|
| 81 |
protected function getDateWidget($attributes = array()) |
|---|
| 82 |
{ |
|---|
| 83 |
return new sfWidgetFormDate($this->getOptionsFor('date'), $this->getAttributesFor('date', $attributes)); |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
* Returns the time widget. |
|---|
| 88 |
* |
|---|
| 89 |
* @param array $attributes An array of attributes |
|---|
| 90 |
* |
|---|
| 91 |
* @return sfWidgetForm A Widget representing the time |
|---|
| 92 |
*/ |
|---|
| 93 |
protected function getTimeWidget($attributes = array()) |
|---|
| 94 |
{ |
|---|
| 95 |
return new sfWidgetFormTime($this->getOptionsFor('time'), $this->getAttributesFor('time', $attributes)); |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
* Returns an array of options for the given type. |
|---|
| 100 |
* |
|---|
| 101 |
* @param string $type The type (date or time) |
|---|
| 102 |
* |
|---|
| 103 |
* @return array An array of options |
|---|
| 104 |
* |
|---|
| 105 |
* @throws InvalidArgumentException when option date|time type is not array |
|---|
| 106 |
*/ |
|---|
| 107 |
protected function getOptionsFor($type) |
|---|
| 108 |
{ |
|---|
| 109 |
$options = $this->getOption($type); |
|---|
| 110 |
if (!is_array($options)) |
|---|
| 111 |
{ |
|---|
| 112 |
throw new InvalidArgumentException(sprintf('You must pass an array for the %s option.', $type)); |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
return $options; |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
* Returns an array of HTML attributes for the given type. |
|---|
| 120 |
* |
|---|
| 121 |
* @param string $type The type (date or time) |
|---|
| 122 |
* @param array $attributes An array of attributes |
|---|
| 123 |
* |
|---|
| 124 |
* @return array An array of HTML attributes |
|---|
| 125 |
*/ |
|---|
| 126 |
protected function getAttributesFor($type, $attributes) |
|---|
| 127 |
{ |
|---|
| 128 |
$defaults = isset($this->attributes[$type]) ? $this->attributes[$type] : array(); |
|---|
| 129 |
|
|---|
| 130 |
return isset($attributes[$type]) ? array_merge($defaults, $attributes[$type]) : $defaults; |
|---|
| 131 |
} |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|