Development

/branches/1.1/lib/widget/sfWidgetFormDate.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/widget/sfWidgetFormDate.class.php

Revision 16259, 3.8 kB (checked in by fabien, 6 years ago)

[1.1, 1.2, 1.3] added more information to sfWidgetFormDate PHPDoc API

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * sfWidgetFormDate represents a date widget.
13  *
14  * @package    symfony
15  * @subpackage widget
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
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    * @param  string $name        The element name
53    * @param  string $value       The date displayed in this widget
54    * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
55    * @param  array  $errors      An array of errors for the field
56    *
57    * @return string An HTML tag string
58    *
59    * @see sfWidgetForm
60    */
61   public function render($name, $value = null, $attributes = array(), $errors = array())
62   {
63     // convert value to an array
64     $default = array('year' => null, 'month' => null, 'day' => null);
65     if (is_array($value))
66     {
67       $value = array_merge($default, $value);
68     }
69     else
70     {
71       $value = (string) $value == (string) (integer) $value ? (integer) $value : strtotime($value);
72       if (false === $value)
73       {
74         $value = $default;
75       }
76       else
77       {
78         $value = array('year' => date('Y', $value), 'month' => date('n', $value), 'day' => date('j', $value));
79       }
80     }
81
82     $date = array();
83     $emptyValues = $this->getOption('empty_values');
84
85     // days
86     $widget = new sfWidgetFormSelect(array('choices' => $this->getOption('can_be_empty') ? array('' => $emptyValues['day']) + $this->getOption('days') : $this->getOption('days')), array_merge($this->attributes, $attributes));
87     $date['%day%'] = $widget->render($name.'[day]', $value['day']);
88
89     // months
90     $widget = new sfWidgetFormSelect(array('choices' => $this->getOption('can_be_empty') ? array('' => $emptyValues['month']) + $this->getOption('months') : $this->getOption('months')), array_merge($this->attributes, $attributes));
91     $date['%month%'] = $widget->render($name.'[month]', $value['month']);
92
93     // years
94     $widget = new sfWidgetFormSelect(array('choices' => $this->getOption('can_be_empty') ? array('' => $emptyValues['year']) + $this->getOption('years') : $this->getOption('years')), array_merge($this->attributes, $attributes));
95     $date['%year%'] = $widget->render($name.'[year]', $value['year']);
96
97     return strtr($this->getOption('format'), $date);
98   }
99 }
100
Note: See TracBrowser for help on using the browser.