Development

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

You must first sign up to be able to contribute.

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

Revision 15962, 3.8 kB (checked in by hartym, 6 years ago)

[1.1][1.2][1.3] code conventions: useless comment removed, trailing spaces

  • 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  * sfWidgetFormDateTime represents a datetime widget.
13  *
14  * @package    symfony
15  * @subpackage widget
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
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   protected function getOptionsFor($type)
106   {
107     $options = $this->getOption($type);
108     if (!is_array($options))
109     {
110       throw new InvalidArgumentException(sprintf('You must pass an array for the %s option.', $type));
111     }
112
113     return $options;
114   }
115
116   /**
117    * Returns an array of HTML attributes for the given type.
118    *
119    * @param  string $type        The type (date or time)
120    * @param  array  $attributes  An array of attributes
121    *
122    * @return array  An array of HTML attributes
123    */
124   protected function getAttributesFor($type, $attributes)
125   {
126     $defaults = isset($this->attributes[$type]) ? $this->attributes[$type] : array();
127
128     return isset($attributes[$type]) ? array_merge($defaults, $attributes[$type]) : $defaults;
129   }
130 }
131
Note: See TracBrowser for help on using the browser.