diff --git a/libraries/legacy/model/admin.php b/libraries/legacy/model/admin.php index 73d229ac9552c..a5aa338f5c37b 100644 --- a/libraries/legacy/model/admin.php +++ b/libraries/legacy/model/admin.php @@ -1156,6 +1156,13 @@ public function save($data) return false; } + // When the data contains params, then we need to add them to the item for the custom fields + // plugins which do add new params fieldsets + if (isset($data['params']) && !isset($table->params) && !isset($table->_params)) + { + $table->_params = $data['params']; + } + // Prepare the row for saving $this->prepareTable($table); diff --git a/plugins/fields/calendar/calendar.php b/plugins/fields/calendar/calendar.php index ff963799ddbe8..f5c1e6d8ecbdc 100644 --- a/plugins/fields/calendar/calendar.php +++ b/plugins/fields/calendar/calendar.php @@ -18,4 +18,104 @@ */ class PlgFieldsCalendar extends FieldsPlugin { + /** + * Transforms the field into an XML element and appends it as child on the given parent. This + * is the default implementation of a field. Form fields which do support to be transformed into + * an XML Element mut implemet the JFormDomfieldinterface. + * + * @param stdClass $field The field. + * @param DOMElement $parent The field node parent. + * @param JForm $form The form. + * + * @return DOMElement + * + * @since __DEPLOY_VERSION__ + */ + public function onCustomFieldsPrepareDom($field, DOMElement $parent, JForm $form) + { + $fieldNode = parent::onCustomFieldsPrepareDom($field, $parent, $form); + + if (!$fieldNode) + { + return $fieldNode; + } + + // Set filter to user UTC + $fieldNode->setAttribute('filter', 'USER_UTC'); + + return $fieldNode; + } + + /** + * Convert date/time format between `date()` and `strftime()`. + * + * Timezone conversion is done for Unix. Windows users must exchange %z and %Z. + * + * Unsupported date formats : S, n, t, L, B, G, u, e, I, P, Z, c, r + * Unsupported strftime formats : %U, %W, %C, %g, %r, %R, %T, %X, %c, %D, %F, %x + * + * Example: + * Convert `%A, %B %e, %Y, %l:%M %P` to `l, F j, Y, g:i a`, and vice versa for "Saturday, March 10, 2001, 5:16 pm". + * + * @param string $format The format to parse. + * @param string $syntax The format's syntax. Either 'strftime' for `strtime()` or 'date' for `date()`. + * + * @return bool|string Returns a string formatted according $syntax using the given $format or `false`. + * + * @link http://php.net/manual/en/function.strftime.php#96424 + * @link https://gist.github.com/mcaskill/02636e5970be1bb22270 + * + * @since __DEPLOY_VERSION__ + */ + public function changeFormat($format, $syntax = 'strftime') + { + // Syntax from http://php.net/manual/en/function.strftime.php + $strfSyntax = array( + // Day - no strf eq : S (created one called %O) + '%O', '%d', '%a', '%e', '%A', '%u', '%w', '%j', + // Week - no date eq : %U, %W + '%V', + // Month - no strf eq : n, t + '%B', '%m', '%b', '%-m', + // Year - no strf eq : L; no date eq : %C, %g + '%G', '%Y', '%y', + // Time - no strf eq : B, G, u; no date eq : %r, %R, %T, %X + '%P', '%p', '%l', '%I', '%H', '%M', '%S', + // Timezone - no strf eq : e, I, P, Z + '%z', '%Z', + // Full Date / Time - no strf eq : c, r; no date eq : %c, %D, %F, %x + '%s' + ); + + // Syntax from http://php.net/manual/en/function.date.php + $dateSyntax = array( + 'S', 'd', 'D', 'j', 'l', 'N', 'w', 'z', + 'W', + 'F', 'm', 'M', 'n', + 'o', 'Y', 'y', + 'a', 'A', 'g', 'h', 'H', 'i', 's', + 'O', 'T', + 'U' + ); + + // Strftime to date + $from = $strfSyntax; + $to = $dateSyntax; + + if ($syntax == 'date') + { + // Date to strftime + $from = $dateSyntax; + $to = $strfSyntax; + } + + $pattern = array_map( + function ($s) { + return '/(?setTimezone(new DateTimeZone(JFactory::getUser()->getParam('timezone', JFactory::getConfig()->get('offset')))); + +// Get the format for PHP date +$format = $this->changeFormat($fieldParams->get('format', '%Y-%m-%d'), 'strftime'); + +// Transform the value with the date format of the plugin +$value = $date->format($format, true); +// Render the date echo htmlentities($value); diff --git a/plugins/system/fields/fields.php b/plugins/system/fields/fields.php index 3c95f8f1c05e6..822ac524f6656 100644 --- a/plugins/system/fields/fields.php +++ b/plugins/system/fields/fields.php @@ -61,15 +61,13 @@ public function onContentBeforeSave($context, $item, $isNew) $params = new Registry; - // Load the item params from the request - $data = JFactory::getApplication()->input->post->get('jform', array(), 'array'); - - if (key_exists('params', $data)) + // Load the params from the item when it doesn't have such a field + if (isset($item->_params)) { - $params->loadArray($data['params']); + $params->loadArray($item->_params); } - // Load the params from the item itself + // Load the params from the item when it has a params field if (isset($item->params)) { $params->loadString($item->params);