[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Joomla! Content Management System 5 * 6 * @copyright (C) 2009 Open Source Matters, Inc. <https://www.joomla.org> 7 * @license GNU General Public License version 2 or later; see LICENSE.txt 8 */ 9 10 namespace Joomla\CMS\Form\Field; 11 12 use DateTime; 13 use Joomla\CMS\Factory; 14 use Joomla\CMS\Form\FormField; 15 use Joomla\CMS\Language\Text; 16 use Joomla\Registry\Registry; 17 18 // phpcs:disable PSR1.Files.SideEffects 19 \defined('JPATH_PLATFORM') or die; 20 // phpcs:enable PSR1.Files.SideEffects 21 22 /** 23 * Form Field class for the Joomla Platform. 24 * 25 * Provides a pop up date picker linked to a button. 26 * Optionally may be filtered to use user's or server's time zone. 27 * 28 * @since 1.7.0 29 */ 30 class CalendarField extends FormField 31 { 32 /** 33 * The form field type. 34 * 35 * @var string 36 * @since 1.7.0 37 */ 38 protected $type = 'Calendar'; 39 40 /** 41 * The allowable maxlength of calendar field. 42 * 43 * @var integer 44 * @since 3.2 45 */ 46 protected $maxlength; 47 48 /** 49 * The format of date and time. 50 * 51 * @var string 52 * @since 3.2 53 */ 54 protected $format; 55 56 /** 57 * The format will be used to filter submitted date and time. 58 * 59 * @var string 60 * @since 4.0.1 61 */ 62 protected $filterFormat; 63 64 /** 65 * The filter. 66 * 67 * @var integer 68 * @since 3.2 69 */ 70 protected $filter; 71 72 /** 73 * The minimum year number to subtract/add from the current year 74 * 75 * @var integer 76 * @since 3.7.0 77 */ 78 protected $minyear; 79 80 /** 81 * The maximum year number to subtract/add from the current year 82 * 83 * @var integer 84 * @since 3.7.0 85 */ 86 protected $maxyear; 87 88 /** 89 * Name of the layout being used to render the field 90 * 91 * @var string 92 * @since 3.7.0 93 */ 94 protected $layout = 'joomla.form.field.calendar'; 95 96 /** 97 * The parent class of the field 98 * 99 * @var string 100 * @since 4.0.0 101 */ 102 protected $parentclass; 103 104 /** 105 * Method to get certain otherwise inaccessible properties from the form field object. 106 * 107 * @param string $name The property name for which to get the value. 108 * 109 * @return mixed The property value or null. 110 * 111 * @since 3.2 112 */ 113 public function __get($name) 114 { 115 switch ($name) { 116 case 'maxlength': 117 case 'format': 118 case 'filterFormat': 119 case 'filter': 120 case 'timeformat': 121 case 'todaybutton': 122 case 'singleheader': 123 case 'weeknumbers': 124 case 'showtime': 125 case 'filltable': 126 case 'minyear': 127 case 'maxyear': 128 return $this->$name; 129 } 130 131 return parent::__get($name); 132 } 133 134 /** 135 * Method to set certain otherwise inaccessible properties of the form field object. 136 * 137 * @param string $name The property name for which to set the value. 138 * @param mixed $value The value of the property. 139 * 140 * @return void 141 * 142 * @since 3.2 143 */ 144 public function __set($name, $value) 145 { 146 switch ($name) { 147 case 'maxlength': 148 case 'timeformat': 149 $this->$name = (int) $value; 150 break; 151 case 'todaybutton': 152 case 'singleheader': 153 case 'weeknumbers': 154 case 'showtime': 155 case 'filltable': 156 case 'format': 157 case 'filterFormat': 158 case 'filter': 159 case 'minyear': 160 case 'maxyear': 161 $this->$name = (string) $value; 162 break; 163 164 default: 165 parent::__set($name, $value); 166 } 167 } 168 169 /** 170 * Method to attach a Form object to the field. 171 * 172 * @param \SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object. 173 * @param mixed $value The form field value to validate. 174 * @param string $group The field name group control value. This acts as an array container for the field. 175 * For example if the field has name="foo" and the group value is set to "bar" then the 176 * full field name would end up being "bar[foo]". 177 * 178 * @return boolean True on success. 179 * 180 * @see FormField::setup() 181 * @since 3.2 182 */ 183 public function setup(\SimpleXMLElement $element, $value, $group = null) 184 { 185 $return = parent::setup($element, $value, $group); 186 187 if ($return) { 188 $this->maxlength = (int) $this->element['maxlength'] ? (int) $this->element['maxlength'] : 45; 189 $this->format = (string) $this->element['format'] ? (string) $this->element['format'] : '%Y-%m-%d'; 190 $this->filter = (string) $this->element['filter'] ? (string) $this->element['filter'] : 'USER_UTC'; 191 $this->todaybutton = (string) $this->element['todaybutton'] ? (string) $this->element['todaybutton'] : 'true'; 192 $this->weeknumbers = (string) $this->element['weeknumbers'] ? (string) $this->element['weeknumbers'] : 'true'; 193 $this->showtime = (string) $this->element['showtime'] ? (string) $this->element['showtime'] : 'false'; 194 $this->filltable = (string) $this->element['filltable'] ? (string) $this->element['filltable'] : 'true'; 195 $this->timeformat = (int) $this->element['timeformat'] ? (int) $this->element['timeformat'] : 24; 196 $this->singleheader = (string) $this->element['singleheader'] ? (string) $this->element['singleheader'] : 'false'; 197 $this->minyear = \strlen((string) $this->element['minyear']) ? (string) $this->element['minyear'] : null; 198 $this->maxyear = \strlen((string) $this->element['maxyear']) ? (string) $this->element['maxyear'] : null; 199 200 if ($this->maxyear < 0 || $this->minyear > 0) { 201 $this->todaybutton = 'false'; 202 } 203 204 $translateFormat = (string) $this->element['translateformat']; 205 206 if ($translateFormat && $translateFormat !== 'false') { 207 $showTime = (string) $this->element['showtime']; 208 209 $lang = Factory::getLanguage(); 210 $debug = $lang->setDebug(false); 211 212 if ($showTime && $showTime !== 'false') { 213 $this->format = Text::_('DATE_FORMAT_CALENDAR_DATETIME'); 214 $this->filterFormat = Text::_('DATE_FORMAT_FILTER_DATETIME'); 215 } else { 216 $this->format = Text::_('DATE_FORMAT_CALENDAR_DATE'); 217 $this->filterFormat = Text::_('DATE_FORMAT_FILTER_DATE'); 218 } 219 220 $lang->setDebug($debug); 221 } 222 } 223 224 return $return; 225 } 226 227 /** 228 * Method to get the field input markup. 229 * 230 * @return string The field input markup. 231 * 232 * @since 1.7.0 233 */ 234 protected function getInput() 235 { 236 $user = Factory::getApplication()->getIdentity(); 237 238 // If a known filter is given use it. 239 switch (strtoupper($this->filter)) { 240 case 'SERVER_UTC': 241 // Convert a date to UTC based on the server timezone. 242 if ($this->value && $this->value != $this->getDatabase()->getNullDate()) { 243 // Get a date object based on the correct timezone. 244 $date = Factory::getDate($this->value, 'UTC'); 245 $date->setTimezone(new \DateTimeZone(Factory::getApplication()->get('offset'))); 246 247 // Transform the date string. 248 $this->value = $date->format('Y-m-d H:i:s', true, false); 249 } 250 break; 251 case 'USER_UTC': 252 // Convert a date to UTC based on the user timezone. 253 if ($this->value && $this->value != $this->getDatabase()->getNullDate()) { 254 // Get a date object based on the correct timezone. 255 $date = Factory::getDate($this->value, 'UTC'); 256 $date->setTimezone($user->getTimezone()); 257 258 // Transform the date string. 259 $this->value = $date->format('Y-m-d H:i:s', true, false); 260 } 261 break; 262 } 263 264 // Format value when not nulldate ('0000-00-00 00:00:00'), otherwise blank it as it would result in 1970-01-01. 265 if ($this->value && $this->value != $this->getDatabase()->getNullDate() && strtotime($this->value) !== false) { 266 $tz = date_default_timezone_get(); 267 date_default_timezone_set('UTC'); 268 269 if ($this->filterFormat) { 270 $date = \DateTimeImmutable::createFromFormat('U', strtotime($this->value)); 271 $this->value = $date->format($this->filterFormat); 272 } else { 273 $this->value = strftime($this->format, strtotime($this->value)); 274 } 275 276 date_default_timezone_set($tz); 277 } else { 278 $this->value = ''; 279 } 280 281 return $this->getRenderer($this->layout)->render($this->getLayoutData()); 282 } 283 284 /** 285 * Method to get the data to be passed to the layout for rendering. 286 * 287 * @return array 288 * 289 * @since 3.7.0 290 */ 291 protected function getLayoutData() 292 { 293 $data = parent::getLayoutData(); 294 $lang = Factory::getApplication()->getLanguage(); 295 $calendar = $lang->getCalendar(); 296 $direction = strtolower(Factory::getDocument()->getDirection()); 297 298 // Get the appropriate file for the current language date helper 299 $helperPath = 'system/fields/calendar-locales/date/gregorian/date-helper.min.js'; 300 301 if ($calendar && is_dir(JPATH_ROOT . '/media/system/js/fields/calendar-locales/date/' . strtolower($calendar))) { 302 $helperPath = 'system/fields/calendar-locales/date/' . strtolower($calendar) . '/date-helper.min.js'; 303 } 304 305 $extraData = array( 306 'value' => $this->value, 307 'maxLength' => $this->maxlength, 308 'format' => $this->format, 309 'filter' => $this->filter, 310 'todaybutton' => ($this->todaybutton === 'true') ? 1 : 0, 311 'weeknumbers' => ($this->weeknumbers === 'true') ? 1 : 0, 312 'showtime' => ($this->showtime === 'true') ? 1 : 0, 313 'filltable' => ($this->filltable === 'true') ? 1 : 0, 314 'timeformat' => $this->timeformat, 315 'singleheader' => ($this->singleheader === 'true') ? 1 : 0, 316 'helperPath' => $helperPath, 317 'minYear' => $this->minyear, 318 'maxYear' => $this->maxyear, 319 'direction' => $direction, 320 'calendar' => $calendar, 321 'firstday' => $lang->getFirstDay(), 322 'weekend' => explode(',', $lang->getWeekEnd()), 323 ); 324 325 return array_merge($data, $extraData); 326 } 327 328 /** 329 * Method to filter a field value. 330 * 331 * @param mixed $value The optional value to use as the default for the field. 332 * @param string $group The optional dot-separated form group path on which to find the field. 333 * @param Registry $input An optional Registry object with the entire data set to filter 334 * against the entire form. 335 * 336 * @return mixed The filtered value. 337 * 338 * @since 4.0.0 339 */ 340 public function filter($value, $group = null, Registry $input = null) 341 { 342 // Make sure there is a valid SimpleXMLElement. 343 if (!($this->element instanceof \SimpleXMLElement)) { 344 throw new \UnexpectedValueException(sprintf('%s::filter `element` is not an instance of SimpleXMLElement', \get_class($this))); 345 } 346 347 if ((int) $value <= 0) { 348 return ''; 349 } 350 351 if ($this->filterFormat) { 352 $value = DateTime::createFromFormat($this->filterFormat, $value)->format('Y-m-d H:i:s'); 353 } 354 355 $app = Factory::getApplication(); 356 357 // Get the field filter type. 358 $filter = (string) $this->element['filter']; 359 360 $return = $value; 361 362 switch (strtoupper($filter)) { 363 // Convert a date to UTC based on the server timezone offset. 364 case 'SERVER_UTC': 365 // Return an SQL formatted datetime string in UTC. 366 $return = Factory::getDate($value, $app->get('offset'))->toSql(); 367 break; 368 369 // Convert a date to UTC based on the user timezone offset. 370 case 'USER_UTC': 371 // Get the user timezone setting defaulting to the server timezone setting. 372 $offset = $app->getIdentity()->getParam('timezone', $app->get('offset')); 373 374 // Return an SQL formatted datetime string in UTC. 375 $return = Factory::getDate($value, $offset)->toSql(); 376 break; 377 } 378 379 return $return; 380 } 381 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |