Development

/branches/1.3/lib/widget/sfWidgetFormTime.class.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/widget/sfWidgetFormTime.class.php

Revision 30762, 4.4 kB (checked in by fabien, 4 years ago)

[1.3, 1.4] fixed phpdoc (closes #8931)

  • 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  * sfWidgetFormTime represents a time widget.
13  *
14  * @package    symfony
15  * @subpackage widget
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
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    * Renders the widget.
55    *
56    * @param  string $name        The element name
57    * @param  string $value       The time displayed in this widget
58    * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
59    * @param  array  $errors      An array of errors for the field
60    *
61    * @return string An HTML tag string
62    *
63    * @see sfWidgetForm
64    */
65   public function render($name, $value = null, $attributes = array(), $errors = array())
66   {
67     // convert value to an array
68     $default = array('hour' => null, 'minute' => null, 'second' => null);
69     if (is_array($value))
70     {
71       $value = array_merge($default, $value);
72     }
73     else
74     {
75       $value = ctype_digit($value) ? (integer) $value : strtotime($value);
76       if (false === $value)
77       {
78         $value = $default;
79       }
80       else
81       {
82         // int cast required to get rid of leading zeros
83         $value = array('hour' => (int) date('H', $value), 'minute' => (int) date('i', $value), 'second' => (int) date('s', $value));
84       }
85     }
86
87     $time = array();
88     $emptyValues = $this->getOption('empty_values');
89
90     // hours
91     $widget = new sfWidgetFormSelect(array('choices' => $this->getOption('can_be_empty') ? array('' => $emptyValues['hour']) + $this->getOption('hours') : $this->getOption('hours'), 'id_format' => $this->getOption('id_format')), array_merge($this->attributes, $attributes));
92     $time['%hour%'] = $widget->render($name.'[hour]', $value['hour']);
93
94     // minutes
95     $widget = new sfWidgetFormSelect(array('choices' => $this->getOption('can_be_empty') ? array('' => $emptyValues['minute']) + $this->getOption('minutes') : $this->getOption('minutes'), 'id_format' => $this->getOption('id_format')), array_merge($this->attributes, $attributes));
96     $time['%minute%'] = $widget->render($name.'[minute]', $value['minute']);
97
98     if ($this->getOption('with_seconds'))
99     {
100       // seconds
101       $widget = new sfWidgetFormSelect(array('choices' => $this->getOption('can_be_empty') ? array('' => $emptyValues['second']) + $this->getOption('seconds') : $this->getOption('seconds'), 'id_format' => $this->getOption('id_format')), array_merge($this->attributes, $attributes));
102       $time['%second%'] = $widget->render($name.'[second]', $value['second']);
103     }
104
105     return strtr($this->getOption('with_seconds') ? $this->getOption('format') : $this->getOption('format_without_seconds'), $time);
106   }
107 }
108
Note: See TracBrowser for help on using the browser.