1 <?php
2 /**
3 * @package FrameworkOnFramework
4 * @subpackage form
5 * @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
6 * @license GNU General Public License version 2 or later; see LICENSE.txt
7 */
8 // Protect from unauthorized access
9 defined('FOF_INCLUDED') or die;
10
11 JFormHelper::loadFieldClass('calendar');
12
13 /**
14 * Form Field class for the FOF framework
15 * Supports a calendar / date field.
16 *
17 * @package FrameworkOnFramework
18 * @since 2.0
19 */
20 class FOFFormFieldCalendar extends JFormFieldCalendar implements FOFFormField
21 {
22 protected $static;
23
24 protected $repeatable;
25
26 /** @var FOFTable The item being rendered in a repeatable form field */
27 public $item;
28
29 /** @var int A monotonically increasing number, denoting the row number in a repeatable view */
30 public $rowid;
31
32 /**
33 * Method to get certain otherwise inaccessible properties from the form field object.
34 *
35 * @param string $name The property name for which to the the value.
36 *
37 * @return mixed The property value or null.
38 *
39 * @since 2.0
40 */
41 public function __get($name)
42 {
43 switch ($name)
44 {
45 // ATTENTION: Redirected getInput() to getStatic()
46 case 'input':
47 case 'static':
48 if (empty($this->static))
49 {
50 $this->static = $this->getStatic();
51 }
52
53 return $this->static;
54 break;
55
56 case 'repeatable':
57 if (empty($this->repeatable))
58 {
59 $this->repeatable = $this->getRepeatable();
60 }
61
62 return $this->repeatable;
63 break;
64
65 default:
66 return parent::__get($name);
67 }
68 }
69
70 /**
71 * Get the rendering of this field type for static display, e.g. in a single
72 * item view (typically a "read" task).
73 *
74 * @since 2.0
75 *
76 * @return string The field HTML
77 */
78 public function getStatic()
79 {
80 return $this->getCalendar('static');
81 }
82
83 /**
84 * Get the rendering of this field type for a repeatable (grid) display,
85 * e.g. in a view listing many item (typically a "browse" task)
86 *
87 * @since 2.0
88 *
89 * @return string The field HTML
90 */
91 public function getRepeatable()
92 {
93 return $this->getCalendar('repeatable');
94 }
95
96 /**
97 * Method to get the calendar input markup.
98 *
99 * @param string $display The display to render ('static' or 'repeatable')
100 *
101 * @return string The field input markup.
102 *
103 * @since 2.1.rc4
104 */
105 protected function getCalendar($display)
106 {
107 // Initialize some field attributes.
108 $format = $this->element['format'] ? (string) $this->element['format'] : '%Y-%m-%d';
109 $class = $this->element['class'] ? (string) $this->element['class'] : '';
110 $default = $this->element['default'] ? (string) $this->element['default'] : '';
111
112 // PHP date doesn't use percentages (%) for the format, but the calendar Javascript
113 // DOES use it (@see: calendar-uncompressed.js). Therefore we have to convert it.
114 $formatJS = $format;
115 $formatPHP = str_replace(array('%', 'H:M:S', 'B'), array('', 'H:i:s', 'F'), $formatJS);
116
117 // Check for empty date values
118 if (empty($this->value) || $this->value == FOFPlatform::getInstance()->getDbo()->getNullDate() || $this->value == '0000-00-00')
119 {
120 $this->value = $default;
121 }
122
123 // Get some system objects.
124 $config = FOFPlatform::getInstance()->getConfig();
125 $user = JFactory::getUser();
126
127 // Format date if exists
128 if (!empty($this->value))
129 {
130 $date = FOFPlatform::getInstance()->getDate($this->value, 'UTC');
131
132 // If a known filter is given use it.
133 switch (strtoupper((string) $this->element['filter']))
134 {
135 case 'SERVER_UTC':
136 // Convert a date to UTC based on the server timezone.
137 if ((int) $this->value)
138 {
139 // Get a date object based on the correct timezone.
140 $date->setTimezone(new DateTimeZone($config->get('offset')));
141 }
142 break;
143
144 case 'USER_UTC':
145 // Convert a date to UTC based on the user timezone.
146 if ((int) $this->value)
147 {
148 // Get a date object based on the correct timezone.
149 $date->setTimezone($user->getTimezone());
150 }
151 break;
152
153 default:
154 break;
155 }
156
157 // Transform the date string.
158 $this->value = $date->format($formatPHP, true, false);
159 }
160
161 if ($display == 'static')
162 {
163 // Build the attributes array.
164 $attributes = array();
165
166 if ($this->element['size'])
167 {
168 $attributes['size'] = (int) $this->element['size'];
169 }
170
171 if ($this->element['maxlength'])
172 {
173 $attributes['maxlength'] = (int) $this->element['maxlength'];
174 }
175
176 if ($this->element['class'])
177 {
178 $attributes['class'] = (string) $this->element['class'];
179 }
180
181 if ((string) $this->element['readonly'] == 'true')
182 {
183 $attributes['readonly'] = 'readonly';
184 }
185
186 if ((string) $this->element['disabled'] == 'true')
187 {
188 $attributes['disabled'] = 'disabled';
189 }
190
191 if ($this->element['onchange'])
192 {
193 $attributes['onchange'] = (string) $this->element['onchange'];
194 }
195
196 if ($this->required)
197 {
198 $attributes['required'] = 'required';
199 $attributes['aria-required'] = 'true';
200 }
201
202 return JHtml::_('calendar', $this->value, $this->name, $this->id, $formatJS, $attributes);
203 }
204 else
205 {
206 return '<span class="' . $this->id . ' ' . $class . '">' .
207 htmlspecialchars($this->value, ENT_COMPAT, 'UTF-8') .
208 '</span>';
209 }
210 }
211 }
212