123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- /*
- * This file is part of the symfony package.
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- /**
- * sfWidgetFormDate represents a date widget.
- *
- * @package symfony
- * @subpackage widget
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- * @version SVN: $Id: sfWidgetFormDate.class.php 16259 2009-03-12 11:42:00Z fabien $
- */
- class sfWidgetFormDate extends sfWidgetForm
- {
- /**
- * Configures the current widget.
- *
- * Available options:
- *
- * * format: The date format string (%month%/%day%/%year% by default)
- * * years: An array of years for the year select tag (optional)
- * Be careful that the keys must be the years, and the values what will be displayed to the user
- * * months: An array of months for the month select tag (optional)
- * * days: An array of days for the day select tag (optional)
- * * can_be_empty: Whether the widget accept an empty value (true by default)
- * * empty_values: An array of values to use for the empty value (empty string for year, month, and day by default)
- *
- * @param array $options An array of options
- * @param array $attributes An array of default HTML attributes
- *
- * @see sfWidgetForm
- */
- protected function configure($options = array(), $attributes = array())
- {
- $this->addOption('format', '%month%/%day%/%year%');
- $this->addOption('days', parent::generateTwoCharsRange(1, 31));
- $this->addOption('months', parent::generateTwoCharsRange(1, 12));
- $years = range(date('Y') - 5, date('Y') + 5);
- $this->addOption('years', array_combine($years, $years));
- $this->addOption('can_be_empty', true);
- $this->addOption('empty_values', array('year' => '', 'month' => '', 'day' => ''));
- }
- /**
- * @param string $name The element name
- * @param string $value The date displayed in this widget
- * @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
- * @param array $errors An array of errors for the field
- *
- * @return string An HTML tag string
- *
- * @see sfWidgetForm
- */
- public function render($name, $value = null, $attributes = array(), $errors = array())
- {
- // convert value to an array
- $default = array('year' => null, 'month' => null, 'day' => null);
- if (is_array($value))
- {
- $value = array_merge($default, $value);
- }
- else
- {
- $value = (string) $value == (string) (integer) $value ? (integer) $value : strtotime($value);
- if (false === $value)
- {
- $value = $default;
- }
- else
- {
- $value = array('year' => date('Y', $value), 'month' => date('n', $value), 'day' => date('j', $value));
- }
- }
- $date = array();
- $emptyValues = $this->getOption('empty_values');
- // days
- $widget = new sfWidgetFormSelect(array('choices' => $this->getOption('can_be_empty') ? array('' => $emptyValues['day']) + $this->getOption('days') : $this->getOption('days')), array_merge($this->attributes, $attributes));
- $date['%day%'] = $widget->render($name.'[day]', $value['day']);
- // months
- $widget = new sfWidgetFormSelect(array('choices' => $this->getOption('can_be_empty') ? array('' => $emptyValues['month']) + $this->getOption('months') : $this->getOption('months')), array_merge($this->attributes, $attributes));
- $date['%month%'] = $widget->render($name.'[month]', $value['month']);
- // years
- $widget = new sfWidgetFormSelect(array('choices' => $this->getOption('can_be_empty') ? array('' => $emptyValues['year']) + $this->getOption('years') : $this->getOption('years')), array_merge($this->attributes, $attributes));
- $date['%year%'] = $widget->render($name.'[year]', $value['year']);
- return strtr($this->getOption('format'), $date);
- }
- }
|