Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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: sfWidgetFormDateTime.class.php 30762 2010-08-25 12:33:33Z fabien $
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
   * Renders the widget.
51
   *
52
   * @param  string $name        The element name
53
   * @param  string $value       The date and time 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
  function render($name, $value = null, $attributes = array(), $errors = array())
62
  {
63
    $date = $this->getDateWidget($attributes)->render($name, $value);
64
 
65
    if (!$this->getOption('with_time'))
66
    {
67
      return $date;
68
    }
69
 
70
    return strtr($this->getOption('format'), array(
71
      '%date%' => $date,
72
      '%time%' => $this->getTimeWidget($attributes)->render($name, $value),
73
    ));
74
  }
75
 
76
  /**
77
   * Returns the date widget.
78
   *
79
   * @param  array $attributes  An array of attributes
80
   *
81
   * @return sfWidgetForm A Widget representing the date
82
   */
83
  protected function getDateWidget($attributes = array())
84
  {
85
    return new sfWidgetFormDate($this->getOptionsFor('date'), $this->getAttributesFor('date', $attributes));
86
  }
87
 
88
  /**
89
   * Returns the time widget.
90
   *
91
   * @param  array $attributes  An array of attributes
92
   *
93
   * @return sfWidgetForm A Widget representing the time
94
   */
95
  protected function getTimeWidget($attributes = array())
96
  {
97
    return new sfWidgetFormTime($this->getOptionsFor('time'), $this->getAttributesFor('time', $attributes));
98
  }
99
 
100
  /**
101
   * Returns an array of options for the given type.
102
   *
103
   * @param  string $type  The type (date or time)
104
   *
105
   * @return array  An array of options
106
   *
107
   * @throws InvalidArgumentException when option date|time type is not array
108
   */
109
  protected function getOptionsFor($type)
110
  {
111
    $options = $this->getOption($type);
112
    if (!is_array($options))
113
    {
114
      throw new InvalidArgumentException(sprintf('You must pass an array for the %s option.', $type));
115
    }
116
 
117
    // add id_format if it's not there already
118
    $options += array('id_format' => $this->getOption('id_format'));
119
 
120
    return $options;
121
  }
122
 
123
  /**
124
   * Returns an array of HTML attributes for the given type.
125
   *
126
   * @param  string $type        The type (date or time)
127
   * @param  array  $attributes  An array of attributes
128
   *
129
   * @return array  An array of HTML attributes
130
   */
131
  protected function getAttributesFor($type, $attributes)
132
  {
133
    $defaults = isset($this->attributes[$type]) ? $this->attributes[$type] : array();
134
 
135
    return isset($attributes[$type]) ? array_merge($defaults, $attributes[$type]) : $defaults;
136
  }
137
}