Skip to content

Commit

Permalink
DateTime picker
Browse files Browse the repository at this point in the history
  • Loading branch information
dgrammatiko committed Nov 19, 2016
1 parent 010742e commit d3982d9
Show file tree
Hide file tree
Showing 3 changed files with 1,643 additions and 53 deletions.
213 changes: 213 additions & 0 deletions layouts/joomla/form/field/calendar.php
@@ -0,0 +1,213 @@
<?php
/**
* @package Joomla.Site
* @subpackage Layout
*
* @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('JPATH_BASE') or die;

use Joomla\Utilities\ArrayHelper;

extract($displayData);

// Get some system objects.
$config = JFactory::getConfig();
$user = JFactory::getUser();
$document = JFactory::getDocument();
$tag = JFactory::getLanguage()->getTag();

/**
* Layout variables
* -----------------
* @var string $autocomplete Autocomplete attribute for the field.
* @var boolean $autofocus Is autofocus enabled?
* @var string $class Classes for the input.
* @var string $description Description of the field.
* @var boolean $disabled Is this field disabled?
* @var string $group Group the field belongs to. <fields> section in form XML.
* @var boolean $hidden Is this field hidden in the form?
* @var string $hint Placeholder for the field.
* @var string $id DOM id of the field.
* @var string $label Label of the field.
* @var string $labelclass Classes to apply to the label.
* @var boolean $multiple Does this field support multiple values?
* @var string $name Name of the input field.
* @var string $onchange Onchange attribute for the field.
* @var string $onclick Onclick attribute for the field.
* @var string $pattern Pattern (Reg Ex) of value of the form field.
* @var boolean $readonly Is this field read only?
* @var boolean $repeat Allows extensions to duplicate elements.
* @var boolean $required Is this field required?
* @var integer $size Size attribute of the input.
* @var boolean $spellchec Spellcheck state for the form field.
* @var string $validate Validation rules to apply.
* @var string $value Value attribute of the field.
* @var array $checkedOptions Options that will be set as checked.
* @var boolean $hasValue Has this field a value assigned?
* @var array $options Options available for this field.
* @var array $inputType Options available for this field.
* @var array $spellcheck Options available for this field.
*/

$inputvalue = '';

// Build the attributes array.
$attributes = array();

empty($size) ? null : $attributes['size'] = $size;
empty($maxlength) ? null : $attributes['maxlength'] = ' maxlength="' . $maxLength . '"';
empty($class) ? null : $attributes['class'] = $class;
!$readonly ? null : $attributes['readonly'] = 'readonly';
!$disabled ? null : $attributes['disabled'] = 'disabled';
empty($onchange) ? null : $attributes['onchange'] = $onchange;
empty($hint) ? null : $attributes['placeholder'] = $hint;
$autocomplete ? null : $attributes['autocomplete'] = 'off';
!$autofocus ? null : $attributes['autofocus'] = '';

/**
* These variables control the date/time picker
*/
$todaybutton = !empty($todaybutton) ? (bool) $todaybutton : true;
$weeknumbers = !empty($weeknumbers) ? (bool) $weeknumbers : false;
$showtime = !empty($showtime) ? (bool) $showtime : false;
$filltable = !empty($filltable) ? (bool) $filltable : true;
//$multiple = !empty($multiple) ? (bool) $multiple : false; // Needed??????
$timeformat = !empty($timeformat) ? (int) $timeformat : 24;
$minyear = !empty($minyear) ? (int) $minyear : 1970;
$maxyear = !empty($maxyear) ? (int) $maxyear : 2050;
$singleheader = !empty($singleheader) ?(bool) $singleheader : false;

if ($required)
{
$attributes['required'] = '';
$attributes['aria-required'] = 'true';
}

// Handle the special case for "now".
if (strtoupper($value) == 'NOW')
{
$value = JFactory::getDate()->format('Y-m-d H:i:s');
}

$readonly = isset($attributes['readonly']) && $attributes['readonly'] == 'readonly';
$disabled = isset($attributes['disabled']) && $attributes['disabled'] == 'disabled';

if (is_array($attributes))
{
$attributes = ArrayHelper::toString($attributes);
}

// Format value when not nulldate ('0000-00-00 00:00:00'), otherwise blank it as it would result in 1970-01-01.
if ($value && $value != JFactory::getDbo()->getNullDate() && strtotime($value) !== false)
{
$tz = date_default_timezone_get();
date_default_timezone_set('UTC');
$inputvalue = strftime($format, strtotime($value));
date_default_timezone_set($tz);
}

// If a known filter is given use it.
switch (strtoupper($filter))
{
case 'SERVER_UTC':
// Convert a date to UTC based on the server timezone.
if ($value && $value != JFactory::getDbo()->getNullDate())
{
// Get a date object based on the correct timezone.
$date = JFactory::getDate($value, 'UTC');
$date->setTimezone(new DateTimeZone($config->get('offset')));

// Transform the date string.
$value = $date->format('Y-m-d H:i:s', true, false);
}

break;

case 'USER_UTC':
// Convert a date to UTC based on the user timezone.
if ($value && $value != JFactory::getDbo()->getNullDate())
{
// Get a date object based on the correct timezone.
$date = JFactory::getDate($value, 'UTC');

$date->setTimezone(new DateTimeZone($user->getParam('timezone', $config->get('offset'))));

// Transform the date string.
$value = $date->format('Y-m-d H:i:s', true, false);
}

break;
}

JHtml::_('script', $tag . '/calendar-vanilla.js', false, true, false, false, true);
JHtml::_('stylesheet', 'system/calendar-vanilla.css', array(), true);

// To keep the code simple here, run strings through JText::_() using array_map()
$callback = array('JText','_');
$weekdays_full = array_map(
$callback, array(
'SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY'
)
);
$weekdays_short = array_map(
$callback,
array(
'SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN'
)
);
$months_long = array_map(
$callback, array(
'JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE',
'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER'
)
);
$months_short = array_map(
$callback, array(
'JANUARY_SHORT', 'FEBRUARY_SHORT', 'MARCH_SHORT', 'APRIL_SHORT', 'MAY_SHORT', 'JUNE_SHORT',
'JULY_SHORT', 'AUGUST_SHORT', 'SEPTEMBER_SHORT', 'OCTOBER_SHORT', 'NOVEMBER_SHORT', 'DECEMBER_SHORT'
)
);
?>
<?php if ($readonly || $disabled) : ?>
<input type="text" title="<?php echo ($inputvalue ? JHtml::_('date', $value, null, null) : ''); ?>" name="<?php
echo $name; ?>" id="<?php echo $id; ?>" value="<?php
echo htmlspecialchars($inputvalue, ENT_COMPAT, 'UTF-8'); ?>"<?php echo $attributes; ?> />
<?php else : ?>
<div class="field-calendar">
<div class="input-append">
<input type="text" title="<?php echo ($inputvalue ? JHtml::_('date', $value, null, null) : ''); ?>" name="<?php
echo $name; ?>" id="<?php echo $id; ?>" value="<?php
echo htmlspecialchars($inputvalue, ENT_COMPAT, 'UTF-8'); ?>"<?php echo $attributes; ?>
placeholder="<?php echo $description; ?>"/>
<button type="button" class="btn btn-secondary"
id="<?php echo $id; ?>_btn"
data-inputfield="<?php echo $id; ?>"
data-ifformat="<?php echo $format; ?>"
data-button="<?php echo $id; ?>_btn"
data-firstday="<?php echo JFactory::getLanguage()->getFirstDay(); ?>"
data-weekend="<?php echo JFactory::getLanguage()->getWeekEnd(); ?>"
data-today_btn="<?php echo ($todaybutton === true) ? 1 : 0; ?>"
data-week_numbers="<?php echo ($weeknumbers === true) ? 1 : 0; ?>"
data-shows_time="<?php echo ($showtime === true) ? 1 : 0; ?>"
data-show_others="<?php echo ($filltable === true) ? 1 : 0; ?>"
data-time_24="<?php echo $timeformat; ?>"
data-min_year="<?php echo $minyear; ?>"
data-max_year="<?php echo $maxyear; ?>"
data-only_months_nav="<?php echo ($singleheader === true) ? 1 : 0; ?>"
data-today_trans="<?php echo JText::_('JLIB_HTML_BEHAVIOR_TODAY'); ?>"
data-weekdays_full="<?php echo implode("_", $weekdays_full); ?>"
data-weekdays_short="<?php echo implode("_", $weekdays_short); ?>"
data-months_long="<?php echo implode("_", $months_long); ?>"
data-months_short="<?php echo implode("_", $months_short); ?>"
data-day_first="<?php echo JText::_('JLIB_HTML_BEHAVIOR_DISPLAY_S_FIRST'); ?>"
data-wk="<?php echo JText::_('JLIB_HTML_BEHAVIOR_WK'); ?>"
data-time="<?php echo JText::_('JLIB_HTML_BEHAVIOR_TIME'); ?>"
data-time_am="<?php echo JText::_('JLIB_HTML_BEHAVIOR_TIME_AM'); ?>"
data-time_pm="<?php echo JText::_('JLIB_HTML_BEHAVIOR_TIME_PM'); ?>"
><span class="icon-calendar"></span></button>
</div>
</div>
<?php endif; ?>
126 changes: 73 additions & 53 deletions libraries/joomla/form/fields/calendar.php
Expand Up @@ -51,6 +51,14 @@ class JFormFieldCalendar extends JFormField implements JFormDomfieldinterface
*/
protected $filter;

/**
* Name of the layout being used to render the field
*
* @var string
* @since 3.7
*/
protected $layout = 'joomla.form.field.calendar';

/**
* Method to get certain otherwise inaccessible properties from the form field object.
*
Expand All @@ -67,6 +75,15 @@ public function __get($name)
case 'maxlength':
case 'format':
case 'filter':
case 'timeformat':
case 'minyear':
case 'maxyear':
case 'todaybutton':
case 'singleheader':
case 'weeknumbers':
case 'showtime':
case 'filltable':
//case 'multiple':
return $this->$name;
}

Expand All @@ -88,7 +105,19 @@ public function __set($name, $value)
switch ($name)
{
case 'maxlength':
$value = (int) $value;
case 'timeformat':
case 'minyear':
case 'maxyear':
$this->$name = (int) $value;
break;
case 'todaybutton':
case 'singleheader':
case 'weeknumbers':
case 'showtime':
case 'filltable':
//case 'multiple':
$this->$name = (bool) $value;
break;
case 'format':
case 'filter':
$this->$name = (string) $value;
Expand Down Expand Up @@ -119,9 +148,18 @@ public function setup(SimpleXMLElement $element, $value, $group = null)

if ($return)
{
$this->maxlength = (int) $this->element['maxlength'] ? (int) $this->element['maxlength'] : 45;
$this->format = (string) $this->element['format'] ? (string) $this->element['format'] : '%Y-%m-%d';
$this->filter = (string) $this->element['filter'] ? (string) $this->element['filter'] : 'USER_UTC';
$this->maxlength = (int) $this->element['maxlength'] ? (int) $this->element['maxlength'] : 45;
$this->format = (string) $this->element['format'] ? (string) $this->element['format'] : '%Y-%m-%d';
$this->filter = (string) $this->element['filter'] ? (string) $this->element['filter'] : 'USER_UTC';
$this->todaybutton = (bool) $this->element['todaybutton'] ? (bool) $this->element['todaybutton'] : true;
$this->weeknumbers = (bool) $this->element['weeknumbers'] ? (bool) $this->element['weeknumbers'] : false;
$this->showtime = (bool) $this->element['showtime'] ? (bool) $this->element['showtime'] : false;
$this->filltable = (bool) $this->element['filltable'] ? (bool) $this->element['filltable'] : true;
//$this->multiple = (bool) $this->element['multiple'] ? (bool) $this->element['multiple'] : false;
$this->timeformat = (int) $this->element['timeformat'] ? (int) $this->element['timeformat'] : 24;
$this->minyear = (int) $this->element['minyear'] ? (int) $this->element['minyear'] : 1970;
$this->maxyear = (int) $this->element['maxyear'] ? (int) $this->element['maxyear'] : 2050;
$this->singleheader = (bool) $this->element['singleheader'] ? (bool) $this->element['singleheader'] : false;
}

return $return;
Expand Down Expand Up @@ -175,57 +213,39 @@ protected function getInput()

if ($this->required)
{
$attributes['required'] = '';
$attributes['aria-required'] = 'true';
throw new UnexpectedValueException(sprintf('%s has no layout assigned.', $this->name));
}

// Handle the special case for "now".
if (strtoupper($this->value) == 'NOW')
{
$this->value = JFactory::getDate()->format('Y-m-d H:i:s');
}

// Get some system objects.
$config = JFactory::getConfig();
$user = JFactory::getUser();

// If a known filter is given use it.
switch (strtoupper($this->filter))
{
case 'SERVER_UTC':
// Convert a date to UTC based on the server timezone.
if ($this->value && $this->value != JFactory::getDbo()->getNullDate())
{
// Get a date object based on the correct timezone.
$date = JFactory::getDate($this->value, 'UTC');
$date->setTimezone(new DateTimeZone($config->get('offset')));

// Transform the date string.
$this->value = $date->format('Y-m-d H:i:s', true, false);
}

break;

case 'USER_UTC':
// Convert a date to UTC based on the user timezone.
if ($this->value && $this->value != JFactory::getDbo()->getNullDate())
{
// Get a date object based on the correct timezone.
$date = JFactory::getDate($this->value, 'UTC');

$date->setTimezone(new DateTimeZone($user->getParam('timezone', $config->get('offset'))));

// Transform the date string.
$this->value = $date->format('Y-m-d H:i:s', true, false);
}

break;
}

// Including fallback code for HTML5 non supported browsers.
JHtml::_('jquery.framework');
JHtml::_('script', 'system/html5fallback.js', array('version' => 'auto', 'relative' => true));
return $this->getRenderer($this->layout)->render($this->getLayoutData());
}

return JHtml::_('calendar', $this->value, $this->name, $this->id, $format, $attributes);
/**
* Method to get the data to be passed to the layout for rendering.
*
* @return array
*
* @since 3.5
*/
protected function getLayoutData()
{
$data = parent::getLayoutData();

$extraData = array(
'maxLength' => $this->maxlength,
'format' => $this->format,
'filter' => $this->filter,
'todaybutton' => $this->todaybutton,
'weeknumbers' => $this->weeknumbers,
'showtime' => $this->showtime,
'filltable' => $this->filltable,
//'multiple' => $this->multiple,
'timeformat' => $this->timeformat,
'minyear' => $this->minyear,
'maxyear' => $this->maxyear,
'weekenddays' => $this->weekenddays,
'singleheader' => $this->singleheader,
);

return array_merge($data, $extraData);
}
}

0 comments on commit d3982d9

Please sign in to comment.