Skip to content
Snippets Groups Projects
Commit 74c35f45 authored by Eduard Moraru's avatar Eduard Moraru
Browse files

XWIKI-10296: The default value of a date field should be empty or today, not...

XWIKI-10296: The default value of a date field should be empty or today, not the date when the class has been created
- Using a magic date to mark inside an application's template that a default value is not actually set and that the emptyIsToday mechanism should still be applied when creating new entries.
- Used a custom displayer for Date fields that displays an empty value in the AWM class editor or the entry's edit/create form when the magic value is set to the template so that the user is never exposed to this technical detail.
parent a827346b
No related branches found
No related tags found
No related merge requests found
...@@ -83,6 +83,12 @@ ...@@ -83,6 +83,12 @@
{{/groovy}} {{/groovy}}
{{velocity output="false"}} {{velocity output="false"}}
#**
* Constants
*#
## Magic date used to mark in AWM that the date field is not set for the current entry. See http://jira.xwiki.org/browse/XWIKI-10296
#set($MAGIC_DATE = $datetool.toDate('yyyy-MM-dd', '9999-12-31'))
#** #**
* Displays the field palette. * Displays the field palette.
*# *#
...@@ -472,9 +478,17 @@ ...@@ -472,9 +478,17 @@
## Handle field renames. ## Handle field renames.
## ##
#if(!$errorMessage && !$fieldsToRename.isEmpty()) #if(!$errorMessage && !$fieldsToRename.isEmpty())
## We need to load all documents that have objects of this class and rename their properties. ## We need to load all documents (except the class and template, which we handle below) that have objects of this class and rename their properties.
#set($instancesStatement = "from doc.object($doc.fullName) as obj where doc.fullName <> :className") ## If we don`t skip the template, we can not control the behaviour of emptyIsToday for date fields, which we want to handle in #updateAndSaveTemplate only once.
#set($instancesQuery = $services.query.xwql($instancesStatement).bindValue('className', $doc.fullName)) ##
## FIXME: even if it is not a good practice to have an object in the class document, it is still possible. We should handle field renames for the class document
## as well. Note that there is a possibility that objects in the class' document are automatically updated. Needs checking.
##
#set($instancesStatement = "from doc.object($doc.fullName) as obj where doc.fullName not in (:className,:templateName)")
#set($className = $stringtool.removeEnd($doc.name, 'Class'))
#set($templateRef = $services.model.createDocumentReference($doc.wiki, $doc.space, "${className}Template"))
#set($templateFullName = $services.model.serialize($templateRef, 'local'))
#set($instancesQuery = $services.query.xwql($instancesStatement).bindValue('className', $doc.fullName).bindValue('templateName', $templateFullName))
#foreach($instanceDocName in $instancesQuery.execute()) #foreach($instanceDocName in $instancesQuery.execute())
#set($instanceDoc = $xwiki.getDocument($instanceDocName)) #set($instanceDoc = $xwiki.getDocument($instanceDocName))
#set($discard = $instanceDoc.document.renameProperties($doc.documentReference, $fieldsToRename)) #set($discard = $instanceDoc.document.renameProperties($doc.documentReference, $fieldsToRename))
...@@ -489,6 +503,21 @@ ...@@ -489,6 +503,21 @@
#end #end
#end #end
#**
* Handle Date fields that have the "Empty is today" option checked in the class edit form. See http://jira.xwiki.org/browse/XWIKI-10296
**#
#macro(handleEmptyIsTodayDateFields $templateDoc)
#foreach($property in $doc.xWikiClass.properties)
## We check directly on the request if the user provided an empty date. We can not check from the template document's object that we've just
## parsed from the request using the updateObjectFromRequest method because it already applies the emtpyIsToday mechanism and that would not be good for us.
#set($newValueRequestParameterName = "${doc.fullName}_0_${property.name}")
#set($newDateStringValue = "$!{request.getParameter($newValueRequestParameterName)}")
#if($property.classType == 'Date' && $property.getValue('emptyIsToday') == 1 && $newDateStringValue == '')
#set($discard = $templateDoc.set($property.name, $MAGIC_DATE))
#end
#end
#end
#** #**
* Updates and saves the class template based on the submitted data. * Updates and saves the class template based on the submitted data.
*# *#
...@@ -504,7 +533,12 @@ ...@@ -504,7 +533,12 @@
#if ($request.templateContent) #if ($request.templateContent)
#set($discard = $templateDoc.setContent($request.templateContent)) #set($discard = $templateDoc.setContent($request.templateContent))
#end #end
## Rename the properties of the template's object, if applicable.
#set($discard = $templateDoc.document.renameProperties($doc.documentReference, $fieldsToRename))
## Fill the template's object with the default values from the class editor's form.
#set($discard = $templateDoc.updateObjectFromRequest($doc.fullName)) #set($discard = $templateDoc.updateObjectFromRequest($doc.fullName))
##
#handleEmptyIsTodayDateFields($templateDoc)
#set($discard = $templateDoc.setHidden(true)) #set($discard = $templateDoc.setHidden(true))
#set($discard = $templateDoc.save($services.localization.render('platform.appwithinminutes.classEditorTemplateSaveComment'), $minorEdit)) #set($discard = $templateDoc.save($services.localization.render('platform.appwithinminutes.classEditorTemplateSaveComment'), $minorEdit))
#end #end
......
...@@ -72,7 +72,7 @@ ...@@ -72,7 +72,7 @@
<nameField/> <nameField/>
<validationScript/> <validationScript/>
<date> <date>
<customDisplay/> <customDisplay>{{include reference="AppWithinMinutes.DateDisplayer" /}}</customDisplay>
<dateFormat>dd/MM/yyyy HH:mm:ss</dateFormat> <dateFormat>dd/MM/yyyy HH:mm:ss</dateFormat>
<disabled>0</disabled> <disabled>0</disabled>
<emptyIsToday>1</emptyIsToday> <emptyIsToday>1</emptyIsToday>
......
<?xml version="1.0" encoding="UTF-8"?>
<!--
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
-->
<xwikidoc version="1.1">
<web>AppWithinMinutes</web>
<name>DateDisplayer</name>
<language/>
<defaultLanguage/>
<translation>0</translation>
<creator>xwiki:XWiki.Admin</creator>
<creationDate>1411997494000</creationDate>
<parent>AppWithinMinutes.Date</parent>
<author>xwiki:XWiki.Admin</author>
<contentAuthor>xwiki:XWiki.Admin</contentAuthor>
<date>1412001721000</date>
<contentUpdateDate>1412001721000</contentUpdateDate>
<version>1.1</version>
<title>Date Displayer</title>
<comment/>
<minorEdit>false</minorEdit>
<syntaxId>xwiki/2.1</syntaxId>
<hidden>true</hidden>
<content>{{velocity}}
#set ($MAGIC_DATE = $datetool.toDate('yyyy-MM-dd', '9999-12-31'))
#if ($xcontext.action == 'edit' &amp;&amp; $field.classType == 'Date' &amp;&amp; $field.getValue('emptyIsToday') == 1 &amp;&amp; $MAGIC_DATE.equals($value))
#set ($value = $NULL)
#end
{{html}}#template('displayer_date.vm'){{/html}}
{{/velocity}}
</content>
</xwikidoc>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment