1 <?php
  2   3   4   5   6   7   8 
  9 
 10 defined('JPATH_PLATFORM') or die;
 11 
 12  13  14  15  16  17  18  19 
 20 class JFormFieldCalendar extends JFormField
 21 {
 22      23  24  25  26  27 
 28     protected $type = 'Calendar';
 29 
 30      31  32  33  34  35 
 36     protected $maxlength;
 37 
 38      39  40  41  42  43 
 44     protected $format;
 45 
 46      47  48  49  50  51 
 52     protected $filter;
 53 
 54      55  56  57  58  59 
 60     protected $minyear;
 61 
 62      63  64  65  66  67 
 68     protected $maxyear;
 69 
 70      71  72  73  74  75 
 76     protected $layout = 'joomla.form.field.calendar';
 77 
 78      79  80  81  82  83  84  85  86 
 87     public function __get($name)
 88     {
 89         switch ($name)
 90         {
 91             case 'maxlength':
 92             case 'format':
 93             case 'filter':
 94             case 'timeformat':
 95             case 'todaybutton':
 96             case 'singleheader':
 97             case 'weeknumbers':
 98             case 'showtime':
 99             case 'filltable':
100             case 'minyear':
101             case 'maxyear':
102                 return $this->$name;
103         }
104 
105         return parent::__get($name);
106     }
107 
108     109 110 111 112 113 114 115 116 117 
118     public function __set($name, $value)
119     {
120         switch ($name)
121         {
122             case 'maxlength':
123             case 'timeformat':
124                 $this->$name = (int) $value;
125                 break;
126             case 'todaybutton':
127             case 'singleheader':
128             case 'weeknumbers':
129             case 'showtime':
130             case 'filltable':
131             case 'format':
132             case 'filter':
133             case 'minyear':
134             case 'maxyear':
135                 $this->$name = (string) $value;
136                 break;
137 
138             default:
139                 parent::__set($name, $value);
140         }
141     }
142 
143     144 145 146 147 148 149 150 151 152 153 154 155 156 
157     public function setup(SimpleXMLElement $element, $value, $group = null)
158     {
159         $return = parent::setup($element, $value, $group);
160 
161         if ($return)
162         {
163             $this->maxlength    = (int) $this->element['maxlength'] ? (int) $this->element['maxlength'] : 45;
164             $this->format       = (string) $this->element['format'] ? (string) $this->element['format'] : '%Y-%m-%d';
165             $this->filter       = (string) $this->element['filter'] ? (string) $this->element['filter'] : 'USER_UTC';
166             $this->todaybutton  = (string) $this->element['todaybutton'] ? (string) $this->element['todaybutton'] : 'true';
167             $this->weeknumbers  = (string) $this->element['weeknumbers'] ? (string) $this->element['weeknumbers'] : 'true';
168             $this->showtime     = (string) $this->element['showtime'] ? (string) $this->element['showtime'] : 'false';
169             $this->filltable    = (string) $this->element['filltable'] ? (string) $this->element['filltable'] : 'true';
170             $this->timeformat   = (int) $this->element['timeformat'] ? (int) $this->element['timeformat'] : 24;
171             $this->singleheader = (string) $this->element['singleheader'] ? (string) $this->element['singleheader'] : 'false';
172             $this->minyear      = (string) $this->element['minyear'] ? (string) $this->element['minyear'] : null;
173             $this->maxyear      = (string) $this->element['maxyear'] ? (string) $this->element['maxyear'] : null;
174 
175             if ($this->maxyear < 0 || $this->minyear > 0)
176             {
177                 $this->todaybutton = 'false';
178             }
179         }
180 
181         return $return;
182     }
183 
184     185 186 187 188 189 190 
191     protected function getInput()
192     {
193         $config    = JFactory::getConfig();
194         $user      = JFactory::getUser();
195 
196         
197         $translateFormat = (string) $this->element['translateformat'];
198 
199         if ($translateFormat && $translateFormat != 'false')
200         {
201             $showTime = (string) $this->element['showtime'];
202 
203             if ($showTime && $showTime != 'false')
204             {
205                 $this->format = JText::_('DATE_FORMAT_CALENDAR_DATETIME');
206             }
207             else
208             {
209                 $this->format = JText::_('DATE_FORMAT_CALENDAR_DATE');
210             }
211         }
212 
213         
214         switch (strtoupper($this->filter))
215         {
216             case 'SERVER_UTC':
217                 
218                 if ($this->value && $this->value != JFactory::getDbo()->getNullDate())
219                 {
220                     
221                     $date = JFactory::getDate($this->value, 'UTC');
222                     $date->setTimezone(new DateTimeZone($config->get('offset')));
223 
224                     
225                     $this->value = $date->format('Y-m-d H:i:s', true, false);
226                 }
227                 break;
228             case 'USER_UTC':
229                 
230                 if ($this->value && $this->value != JFactory::getDbo()->getNullDate())
231                 {
232                     
233                     $date = JFactory::getDate($this->value, 'UTC');
234                     $date->setTimezone($user->getTimezone());
235 
236                     
237                     $this->value = $date->format('Y-m-d H:i:s', true, false);
238                 }
239                 break;
240         }
241 
242         
243         if ($this->value && $this->value != JFactory::getDbo()->getNullDate() && strtotime($this->value) !== false)
244         {
245             $tz = date_default_timezone_get();
246             date_default_timezone_set('UTC');
247             $this->value = strftime($this->format, strtotime($this->value));
248             date_default_timezone_set($tz);
249         }
250         else
251         {
252             $this->value = '';
253         }
254 
255         return $this->getRenderer($this->layout)->render($this->getLayoutData());
256     }
257 
258     259 260 261 262 263 264 
265     protected function getLayoutData()
266     {
267         $data      = parent::getLayoutData();
268         $tag       = JFactory::getLanguage()->getTag();
269         $calendar  = JFactory::getLanguage()->getCalendar();
270         $direction = strtolower(JFactory::getDocument()->getDirection());
271 
272         
273         $helperPath = 'system/fields/calendar-locales/date/gregorian/date-helper.min.js';
274 
275         if (!empty($calendar) && is_dir(JPATH_ROOT . '/media/system/js/fields/calendar-locales/date/' . strtolower($calendar)))
276         {
277             $helperPath = 'system/fields/calendar-locales/date/' . strtolower($calendar) . '/date-helper.min.js';
278         }
279 
280         
281         $localesPath = 'system/fields/calendar-locales/en.js';
282 
283         if (is_file(JPATH_ROOT . '/media/system/js/fields/calendar-locales/' . strtolower($tag) . '.js'))
284         {
285             $localesPath = 'system/fields/calendar-locales/' . strtolower($tag) . '.js';
286         }
287         elseif (is_file(JPATH_ROOT . '/media/system/js/fields/calendar-locales/' . strtolower(substr($tag, 0, -3)) . '.js'))
288         {
289             $localesPath = 'system/fields/calendar-locales/' . strtolower(substr($tag, 0, -3)) . '.js';
290         }
291 
292         $extraData = array(
293             'value'        => $this->value,
294             'maxLength'    => $this->maxlength,
295             'format'       => $this->format,
296             'filter'       => $this->filter,
297             'todaybutton'  => ($this->todaybutton === 'true') ? 1 : 0,
298             'weeknumbers'  => ($this->weeknumbers === 'true') ? 1 : 0,
299             'showtime'     => ($this->showtime === 'true') ? 1 : 0,
300             'filltable'    => ($this->filltable === 'true') ? 1 : 0,
301             'timeformat'   => $this->timeformat,
302             'singleheader' => ($this->singleheader === 'true') ? 1 : 0,
303             'helperPath'   => $helperPath,
304             'localesPath'  => $localesPath,
305             'minYear'      => $this->minyear,
306             'maxYear'      => $this->maxyear,
307             'direction'    => $direction,
308         );
309 
310         return array_merge($data, $extraData);
311     }
312 }
313