| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class sfWidgetFormDate extends sfWidgetForm |
|---|
| 20 |
{ |
|---|
| 21 |
|
|---|
| 22 |
* Configures the current widget. |
|---|
| 23 |
* |
|---|
| 24 |
* Available options: |
|---|
| 25 |
* |
|---|
| 26 |
* * format: The date format string (%month%/%day%/%year% by default) |
|---|
| 27 |
* * years: An array of years for the year select tag (optional) |
|---|
| 28 |
* Be careful that the keys must be the years, and the values what will be displayed to the user |
|---|
| 29 |
* * months: An array of months for the month select tag (optional) |
|---|
| 30 |
* * days: An array of days for the day select tag (optional) |
|---|
| 31 |
* * can_be_empty: Whether the widget accept an empty value (true by default) |
|---|
| 32 |
* * empty_values: An array of values to use for the empty value (empty string for year, month, and day by default) |
|---|
| 33 |
* |
|---|
| 34 |
* @param array $options An array of options |
|---|
| 35 |
* @param array $attributes An array of default HTML attributes |
|---|
| 36 |
* |
|---|
| 37 |
* @see sfWidgetForm |
|---|
| 38 |
*/ |
|---|
| 39 |
protected function configure($options = array(), $attributes = array()) |
|---|
| 40 |
{ |
|---|
| 41 |
$this->addOption('format', '%month%/%day%/%year%'); |
|---|
| 42 |
$this->addOption('days', parent::generateTwoCharsRange(1, 31)); |
|---|
| 43 |
$this->addOption('months', parent::generateTwoCharsRange(1, 12)); |
|---|
| 44 |
$years = range(date('Y') - 5, date('Y') + 5); |
|---|
| 45 |
$this->addOption('years', array_combine($years, $years)); |
|---|
| 46 |
|
|---|
| 47 |
$this->addOption('can_be_empty', true); |
|---|
| 48 |
$this->addOption('empty_values', array('year' => '', 'month' => '', 'day' => '')); |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
* Renders the widget. |
|---|
| 53 |
* |
|---|
| 54 |
* @param string $name The element name |
|---|
| 55 |
* @param string $value The date 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('year' => null, 'month' => null, 'day' => null); |
|---|
| 67 |
if (is_array($value)) |
|---|
| 68 |
{ |
|---|
| 69 |
$value = array_merge($default, $value); |
|---|
| 70 |
} |
|---|
| 71 |
else |
|---|
| 72 |
{ |
|---|
| 73 |
$value = (string) $value == (string) (integer) $value ? (integer) $value : strtotime($value); |
|---|
| 74 |
if (false === $value) |
|---|
| 75 |
{ |
|---|
| 76 |
$value = $default; |
|---|
| 77 |
} |
|---|
| 78 |
else |
|---|
| 79 |
{ |
|---|
| 80 |
$value = array('year' => date('Y', $value), 'month' => date('n', $value), 'day' => date('j', $value)); |
|---|
| 81 |
} |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
$date = array(); |
|---|
| 85 |
$emptyValues = $this->getOption('empty_values'); |
|---|
| 86 |
|
|---|
| 87 |
$date['%day%'] = $this->renderDayWidget($name.'[day]', $value['day'], array('choices' => $this->getOption('can_be_empty') ? array('' => $emptyValues['day']) + $this->getOption('days') : $this->getOption('days'), 'id_format' => $this->getOption('id_format')), array_merge($this->attributes, $attributes)); |
|---|
| 88 |
$date['%month%'] = $this->renderMonthWidget($name.'[month]', $value['month'], array('choices' => $this->getOption('can_be_empty') ? array('' => $emptyValues['month']) + $this->getOption('months') : $this->getOption('months'), 'id_format' => $this->getOption('id_format')), array_merge($this->attributes, $attributes)); |
|---|
| 89 |
$date['%year%'] = $this->renderYearWidget($name.'[year]', $value['year'], array('choices' => $this->getOption('can_be_empty') ? array('' => $emptyValues['year']) + $this->getOption('years') : $this->getOption('years'), 'id_format' => $this->getOption('id_format')), array_merge($this->attributes, $attributes)); |
|---|
| 90 |
|
|---|
| 91 |
return strtr($this->getOption('format'), $date); |
|---|
| 92 |
} |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
* @param string $name |
|---|
| 96 |
* @param string $value |
|---|
| 97 |
* @param array $options |
|---|
| 98 |
* @param array $attributes |
|---|
| 99 |
* @return string rendered widget |
|---|
| 100 |
*/ |
|---|
| 101 |
protected function renderDayWidget($name, $value, $options, $attributes) |
|---|
| 102 |
{ |
|---|
| 103 |
$widget = new sfWidgetFormSelect($options, $attributes); |
|---|
| 104 |
return $widget->render($name, $value); |
|---|
| 105 |
} |
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
* @param string $name |
|---|
| 109 |
* @param string $value |
|---|
| 110 |
* @param array $options |
|---|
| 111 |
* @param array $attributes |
|---|
| 112 |
* @return string rendered widget |
|---|
| 113 |
*/ |
|---|
| 114 |
protected function renderMonthWidget($name, $value, $options, $attributes) |
|---|
| 115 |
{ |
|---|
| 116 |
$widget = new sfWidgetFormSelect($options, $attributes); |
|---|
| 117 |
return $widget->render($name, $value); |
|---|
| 118 |
} |
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
* @param string $name |
|---|
| 122 |
* @param string $value |
|---|
| 123 |
* @param array $options |
|---|
| 124 |
* @param array $attributes |
|---|
| 125 |
* @return string rendered widget |
|---|
| 126 |
*/ |
|---|
| 127 |
protected function renderYearWidget($name, $value, $options, $attributes) |
|---|
| 128 |
{ |
|---|
| 129 |
$widget = new sfWidgetFormSelect($options, $attributes); |
|---|
| 130 |
return $widget->render($name, $value); |
|---|
| 131 |
} |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|