Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
calendar.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Form
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * Form Field class for the Joomla Platform.
14  *
15  * Provides a pop up date picker linked to a button.
16  * Optionally may be filtered to use user's or server's time zone.
17  *
18  * @package Joomla.Platform
19  * @subpackage Form
20  * @since 11.1
21  */
23 {
24  /**
25  * The form field type.
26  *
27  * @var string
28  * @since 11.1
29  */
30  protected $type = 'Calendar';
31 
32  /**
33  * The allowable maxlength of calendar field.
34  *
35  * @var integer
36  * @since 3.2
37  */
38  protected $maxlength;
39 
40  /**
41  * The format of date and time.
42  *
43  * @var integer
44  * @since 3.2
45  */
46  protected $format;
47 
48  /**
49  * The filter.
50  *
51  * @var integer
52  * @since 3.2
53  */
54  protected $filter;
55 
56  /**
57  * Method to get certain otherwise inaccessible properties from the form field object.
58  *
59  * @param string $name The property name for which to the the value.
60  *
61  * @return mixed The property value or null.
62  *
63  * @since 3.2
64  */
65  public function __get($name)
66  {
67  switch ($name)
68  {
69  case 'maxlength':
70  case 'format':
71  case 'filter':
72  return $this->$name;
73  }
74 
75  return parent::__get($name);
76  }
77 
78  /**
79  * Method to set certain otherwise inaccessible properties of the form field object.
80  *
81  * @param string $name The property name for which to the the value.
82  * @param mixed $value The value of the property.
83  *
84  * @return void
85  *
86  * @since 3.2
87  */
88  public function __set($name, $value)
89  {
90  switch ($name)
91  {
92  case 'maxlength':
93  $value = (int) $value;
94 
95  case 'format':
96  case 'filter':
97  $this->$name = (string) $value;
98  break;
99 
100  default:
101  parent::__set($name, $value);
102  }
103  }
104 
105  /**
106  * Method to attach a JForm object to the field.
107  *
108  * @param SimpleXMLElement $element The SimpleXMLElement object representing the <field /> tag for the form field object.
109  * @param mixed $value The form field value to validate.
110  * @param string $group The field name group control value. This acts as as an array container for the field.
111  * For example if the field has name="foo" and the group value is set to "bar" then the
112  * full field name would end up being "bar[foo]".
113  *
114  * @return boolean True on success.
115  *
116  * @see JFormField::setup()
117  * @since 3.2
118  */
119  public function setup(SimpleXMLElement $element, $value, $group = null)
120  {
121  $return = parent::setup($element, $value, $group);
122 
123  if ($return)
124  {
125  $this->maxlength = (int) $this->element['maxlength'] ? (int) $this->element['maxlength'] : 45;
126  $this->format = (string) $this->element['format'] ? (string) $this->element['format'] : '%Y-%m-%d';
127  $this->filter = (string) $this->element['filter'] ? (string) $this->element['filter'] : 'USER_UTC';
128  }
129 
130  return $return;
131  }
132 
133  /**
134  * Method to get the field input markup.
135  *
136  * @return string The field input markup.
137  *
138  * @since 11.1
139  */
140  protected function getInput()
141  {
142  // Translate placeholder text
143  $hint = $this->translateHint ? JText::_($this->hint) : $this->hint;
144 
145  // Initialize some field attributes.
146  $format = $this->format;
147 
148  // Build the attributes array.
149  $attributes = array();
150 
151  empty($this->size) ? null : $attributes['size'] = $this->size;
152  empty($this->maxlength) ? null : $attributes['maxlength'] = $this->maxlength;
153  empty($this->class) ? null : $attributes['class'] = $this->class;
154  !$this->readonly ? null : $attributes['readonly'] = '';
155  !$this->disabled ? null : $attributes['disabled'] = '';
156  empty($this->onchange) ? null : $attributes['onchange'] = $this->onchange;
157  empty($hint) ? null : $attributes['placeholder'] = $hint;
158  $this->autocomplete ? null : $attributes['autocomplete'] = 'off';
159  !$this->autofocus ? null : $attributes['autofocus'] = '';
160 
161  if ($this->required)
162  {
163  $attributes['required'] = '';
164  $attributes['aria-required'] = 'true';
165  }
166 
167  // Handle the special case for "now".
168  if (strtoupper($this->value) == 'NOW')
169  {
170  $this->value = strftime($format);
171  }
172 
173  // Get some system objects.
174  $config = JFactory::getConfig();
175  $user = JFactory::getUser();
176 
177  // If a known filter is given use it.
178  switch (strtoupper($this->filter))
179  {
180  case 'SERVER_UTC':
181  // Convert a date to UTC based on the server timezone.
182  if ((int) $this->value)
183  {
184  // Get a date object based on the correct timezone.
185  $date = JFactory::getDate($this->value, 'UTC');
186  $date->setTimezone(new DateTimeZone($config->get('offset')));
187 
188  // Transform the date string.
189  $this->value = $date->format('Y-m-d H:i:s', true, false);
190  }
191 
192  break;
193 
194  case 'USER_UTC':
195  // Convert a date to UTC based on the user timezone.
196  if ((int) $this->value)
197  {
198  // Get a date object based on the correct timezone.
199  $date = JFactory::getDate($this->value, 'UTC');
200 
201  $date->setTimezone(new DateTimeZone($user->getParam('timezone', $config->get('offset'))));
202 
203  // Transform the date string.
204  $this->value = $date->format('Y-m-d H:i:s', true, false);
205  }
206 
207  break;
208  }
209 
210  // Including fallback code for HTML5 non supported browsers.
211  JHtml::_('jquery.framework');
212  JHtml::_('script', 'system/html5fallback.js', false, true);
213 
214  return JHtml::_('calendar', $this->value, $this->name, $this->id, $format, $attributes);
215  }
216 }