Show last authors
1 {{include reference="AppWithinMinutes.VelocityMacros" /}}
2
3 {{groovy}}
4 import com.xpn.xwiki.XWikiContext;
5 import com.xpn.xwiki.api.Context;
6 import com.xpn.xwiki.api.Object;
7 import com.xpn.xwiki.api.PropertyClass;
8 import com.xpn.xwiki.doc.XWikiDocument;
9 import com.xpn.xwiki.objects.BaseObject;
10
11 /**
12 * Used to preview class fields that have a custom display associated, before they are actually added/saved to the
13 * class. For instance, when the user drags a Date field from the palette to the field canvas the class editor needs to
14 * display that Date field as if the user would be editing an object with this Date field in "Inline form" edit mode.
15 * This means that if the Date field has a custom display, the custom display should be used (e.g. using a Date picker).
16 */
17 class PropertyCustomDisplayer
18 {
19 private XWikiContext context;
20
21 public PropertyCustomDisplayer(Context context)
22 {
23 this.context = context.getContext();
24 }
25
26 public String display(PropertyClass property, String prefix, com.xpn.xwiki.api.Object object)
27 {
28 HashMap<String, Object> backup = new HashMap<String, Object>();
29 try {
30 XWikiDocument.backupContext(backup, this.context);
31 return this.displayInternal(property.getPropertyClass(), prefix, object.getXWikiObject());
32 } finally {
33 XWikiDocument.restoreContext(backup, this.context);
34 }
35 }
36
37 private String displayInternal(com.xpn.xwiki.objects.classes.PropertyClass property, String prefix, BaseObject object)
38 {
39 StringBuffer result = new StringBuffer();
40 property.displayCustom(result, property.getName(), prefix, "edit", object, this.context);
41 return result.toString();
42 }
43 }
44 xcontext.put('propertyCustomDisplayer', new PropertyCustomDisplayer(xcontext))
45 {{/groovy}}
46
47 {{velocity output="false"}}
48 #**
49 * Constants
50 *#
51 ## Magic date used to mark in AWM that the date field is not set for the current entry. See https://jira.xwiki.org/browse/XWIKI-10296
52 #set($MAGIC_DATE = $datetool.toDate('yyyy-MM-dd', '9999-12-31'))
53
54 #**
55 * Displays the field palette.
56 *#
57 #macro (displayFieldPalette)
58 <div id="palette">
59 <p><strong>$services.localization.render('platform.appwithinminutes.classEditorPaletteTitle')</strong></p>
60 <p class="xHint">$services.localization.render('platform.appwithinminutes.classEditorPaletteHint')</p>
61 ## List all form field types, grouped by category.
62 #set ($formFieldDocs = [])
63 #set ($formFieldClassName = 'AppWithinMinutes.FormFieldClass')
64 #set ($categoryListStatement = 'from doc.object(AppWithinMinutes.FormFieldCategoryClass) as category order by category.priority')
65 <ul>
66 #foreach ($category in $services.query.xwql($categoryListStatement).execute())
67 #set ($categoryDoc = $xwiki.getDocument($category))
68 <li>
69 <div class="category">$categoryDoc.plainTitle</div>
70 #set ($formFieldsForCategoryStatement = "from doc.object($formFieldClassName) as field where field.category = :category order by field.priority")
71 #set ($formFieldsForCategoryQuery = $services.query.xwql($formFieldsForCategoryStatement).bindValue('category', $category))
72 <ul>
73 #foreach ($formField in $formFieldsForCategoryQuery.execute())
74 #set ($formFieldDoc = $xwiki.getDocument($formField))
75 #set ($discard = $formFieldDocs.add($formFieldDoc))
76 #set ($formFieldIcon = $formFieldDoc.getObject($formFieldClassName).getProperty('icon').value)
77 #set ($formFieldIconRendered = $services.icon.renderHTML($formFieldIcon))
78 #if ("$!formFieldIconRendered" == "")
79 #if ($formFieldIcon.contains('/'))
80 #set ($formFieldIconURL = $xwiki.getSkinFile($formFieldIcon))
81 #else
82 #set ($formFieldIconURL = $formFieldDoc.getAttachmentURL($formFieldIcon))
83 #end
84 #set ($formFieldIconRendered = "<img src='$formFieldIconURL' alt='$escapetool.xml($formFieldDoc.plainTitle)' class='icon' />")
85 #end
86 <li class="field">
87 $formFieldIconRendered
88 $escapetool.xml($formFieldDoc.plainTitle)
89 ## FIXME: We should use the 'get' action instead to prevent the stats module from recording this AJAX request.
90 ## The 'edit' action is a temporary solution until the sheet module is modified to allow a sheet to be enforced through
91 ## the query string even if it doesn't match the action (e.g. the 'get' action).
92 ## The sheet parameter is required when editing a new class because the request will be made to a document that doesn't exist.
93 ## FIXME2: In the future don't force the text editor type and instead use the default editor. This means
94 ## that if the WYSIWYG editor is used, we'll need to convert the HTML into the target syntax so that the
95 ## Template in #updateAndSaveTemplate is saved with target syntax and not HTML.
96 ## See https://jira.xwiki.org/browse/XWIKI-13789
97 #set ($fieldURL = $doc.getURL('edit', $escapetool.url({
98 'xpage': 'plain',
99 'sheet': 'AppWithinMinutes.ClassEditSheet',
100 'field': $formFieldDoc.fullName,
101 'xeditmode': 'text'
102 })))
103 <input type="hidden" value="$fieldURL" class="data"/>
104 </li>
105 #end
106 </ul>
107 </li>
108 #end
109 </ul>
110 </div>
111 #end
112
113 #**
114 * Displays the field canvas.
115 *#
116 #macro (displayFieldCanvas)
117 #set ($propertyType2FormField = {})
118 #foreach ($formFieldDoc in $formFieldDocs)
119 ## Use the type of the field template.
120 #set ($type = $formFieldDoc.getxWikiClass().properties.get(0).classType)
121 #set ($discard = $propertyType2FormField.put($type, $formFieldDoc))
122 #end
123 <div id="canvas">
124 <p class="hint">
125 $services.localization.render('platform.appwithinminutes.classEditorCanvasHint')
126 </p>
127 <ul>
128 #set ($unknownFields = [])
129 #foreach ($field in $doc.getxWikiClass().properties)
130 #set ($formFieldDoc = $propertyType2FormField.get($field.classType))
131 #if ($formFieldDoc)
132 <li>#displayField($field $formFieldDoc)</li>
133 #else
134 #set($discard = $unknownFields.add($field))
135 #end
136 #end
137 </ul>
138 <div class="hidden">
139 ## Output the field meta data even if the field is not supported to preserve it when the class is saved.
140 #foreach ($field in $unknownFields)
141 #displayFieldMetaData($field)
142 #end
143 </div>
144 </div>
145 #end
146
147 #**
148 * Display the options to create/update the class template, the class sheet and the class translation bundle.
149 *#
150 #macro (displayClassOptions)
151 #set ($className = $stringtool.removeEnd($doc.fullName, 'Class'))
152 #set ($templateReference = $services.model.resolveDocument("${className}Template"))
153 #set ($translationsReference = $services.model.resolveDocument("${className}Translations"))
154 #set ($classSheets = $services.sheet.getClassSheets($doc))
155 #set ($sheetReference = $null)
156 #if ($classSheets.isEmpty())
157 #set ($sheetReference = $services.model.resolveDocument("${className}Sheet"))
158 #elseif ($classSheets.size() == 1)
159 #set ($sheetReference = $classSheets.get(0))
160 #end
161 ## Hide the options if neither the sheet nor the template nor the translation bundle exists. They don't have to be
162 ## updated, they have to be created.
163 <dl id="options" #if (!$xwiki.exists($sheetReference) && !$xwiki.exists($templateReference)
164 && !$xwiki.exists($translationsReference))class="hidden"#end>
165 <dt>
166 <label for="updateClassTemplate">
167 <input type="checkbox" id="updateClassTemplate" name="updateClassTemplate" checked="checked" />
168 $services.localization.render('platform.appwithinminutes.classEditorUpdateTemplateLabel')
169 </label>
170 </dt>
171 <dd>
172 <span class="xHint">
173 $services.localization.render('platform.appwithinminutes.classEditorUpdateTemplateHint',
174 ["#pageLink($templateReference)"])
175 </span>
176 </dd>
177 <dt>
178 <label for="updateClassSheet">
179 <input type="checkbox" id="updateClassSheet" name="updateClassSheet"
180 #if ($sheetReference)checked="checked" #{else}disabled="disabled" #end/>
181 $services.localization.render('platform.appwithinminutes.classEditorUpdateSheetLabel')
182 </label>
183 </dt>
184 <dd>
185 #if ($sheetReference)
186 <span class="xHint">
187 $services.localization.render('platform.appwithinminutes.classEditorUpdateSheetHint',
188 ["#pageLink($sheetReference)"])
189 </span>
190 #else
191 <span class="warningmessage">
192 $services.localization.render('platform.appwithinminutes.classEditorMultipleSheetsWarning')
193 </span>
194 #end
195 </dd>
196 <dt>
197 <label for="updateClassTranslations">
198 <input type="checkbox" id="updateClassTranslations" name="updateClassTranslations" checked="checked" />
199 $services.localization.render('platform.appwithinminutes.classEditorUpdateTranslationsLabel')
200 </label>
201 </dt>
202 <dd>
203 <span class="xHint">
204 $services.localization.render('platform.appwithinminutes.classEditorUpdateTranslationsHint',
205 ["#pageLink($translationsReference)"])
206 </span>
207 </dd>
208 </dl>
209 #end
210
211 #macro (pageLink $reference)
212 #set ($class = 'wikilink')
213 #set ($action = 'view')
214 #set ($params = {})
215 #if (!$xwiki.exists($reference))
216 #set ($class = 'wikicreatelink')
217 #set ($action = 'create')
218 #set ($discard = $params.put('parent', $doc.fullName))
219 #end
220 <span class="$class"><a href="$escapetool.xml($xwiki.getURL($reference, $action, $escapetool.url($params)))"
221 >$escapetool.xml($reference.name)</a></span>##
222 #end
223
224 #**
225 * Display a form field.
226 *#
227 #macro (displayField $field $formFieldDoc)
228 #if ($formFieldDoc.getObject('XWiki.StyleSheetExtension'))
229 #set ($discard = $xwiki.ssx.use($formFieldDoc.fullName))
230 #end
231 #if ($formFieldDoc.getObject('XWiki.JavaScriptExtension'))
232 #set ($discard = $xwiki.jsx.use($formFieldDoc.fullName))
233 #end
234 <div class="hidden">
235 #displayFieldMetaData($field)
236 ## We need this information to avoid querying and loading all FormField documents twice.
237 ## NOTE: We use a different ID format to avoid collisions with the field meta properties.
238 <input type="hidden" id="template-$field.name" name="template-$field.name"
239 value="$escapetool.xml($formFieldDoc.fullName)"
240 data-propertyName="$escapetool.xml($formFieldDoc.getxWikiClass().propertyNames[0])" />
241 </div>
242 #set ($className = $stringtool.removeEnd($doc.fullName, 'Class'))
243 #set ($templateRef = $services.model.resolveDocument("${className}Template"))
244 #set ($templateDoc = $xwiki.getDocument($templateRef))
245 ## Simulate the editing of the class instance from the template document.
246 ## Note that we can't simply call display on the template document because $field could be a new field that hasn't
247 ## been added to the class yet (so the object from the template doesn't have this field yet).
248 <dl class="field-viewer">
249 #displayFieldProperty($field "${doc.fullName}_0_" $templateDoc.getObject($doc.fullName, true))
250 </dl>
251 #set ($propertyNames = ['name', 'prettyName', 'number', 'required', 'hint'])
252 #set ($formFieldObj = $formFieldDoc.getObject('AppWithinMinutes.FormFieldClass'))
253 #set ($customPropertyNames = $formFieldObj.getProperty('properties').value.split('\s+'))
254 #set ($discard = $customPropertyNames.removeAll($propertyNames))
255 #set ($discard = $propertyNames.addAll($customPropertyNames.subList(0, $customPropertyNames.size())))
256 <dl class="field-config">
257 #foreach ($propertyName in $propertyNames)
258 #set ($propertyDefinition = $field.xWikiClass.get($propertyName))
259 #if ($propertyDefinition)
260 #displayFieldProperty($propertyDefinition "field-${field.name}_" $field)
261 #end
262 #end
263 </dl>
264 #end
265
266 #**
267 * Display the field meta data. This is needed to preserve the field when its type is not supported by the editor.
268 *#
269 #macro (displayFieldMetaData $field)
270 <input type="hidden" id="type-$field.name" name="type-$field.name" value="$field.classType" />
271 #end
272
273 #**
274 * Displays a configuration property of a class field. This macro can also be used to display a property of an object.
275 *#
276 #macro (displayFieldProperty $property $prefix $field)
277 #set ($displayFormType = $property.getProperty('displayFormType'))
278 #if ($property.classType == 'Boolean' && (!$displayFormType || $displayFormType.value == 'checkbox'))
279 <dt>
280 <label for="$!{prefix}$property.name">
281 #displayPropertyEditInput($property, $prefix, $field)$escapetool.xml($property.prettyName)
282 </label>
283 </dt>
284 <dd></dd>
285 #else
286 <dt><label for="${prefix}$property.name">$escapetool.xml($property.prettyName)</label></dt>
287 <dd>#displayPropertyEditInput($property, $prefix, $field)</dd>
288 #end
289 #end
290
291 #**
292 * Displays the input used to edit the specified property of the given object. The given object can be either an
293 * instance of an XWiki class or a class field. In the first case the property represents an object field and in the
294 * second case the property represents a field meta property.
295 *#
296 #macro (displayPropertyEditInput $property $prefix $object)
297 #set ($wrappedProperty = $property.propertyClass)
298 #if ($wrappedProperty.isCustomDisplayed($xcontext.context))
299 $xcontext.get('propertyCustomDisplayer').display($property, $prefix, $object)
300 #else
301 $doc.displayEdit($property, $prefix, $object)
302 #end
303 #end
304
305 #**
306 * Called when a new form field is added via AJAX.
307 *#
308 #macro (displayNewField)
309 ## Output the SkinExtension hooks to allow field displayers to pull JavaScript/CSS resources.
310 ## Output also the LinkExtension hook because $xwiki.linkx.use() is used to load CSS files from WebJars.
311 ## The class editor moves this resource includes in the HTML page head.
312 <!-- com.xpn.xwiki.plugin.skinx.LinkExtensionPlugin -->
313 #skinExtensionHooks
314 #set ($formFieldDoc = $xwiki.getDocument($request.field))
315 #set ($formFieldDocClassFields = $formFieldDoc.getxWikiClass().getXWikiClass().properties)
316 #if ($formFieldDocClassFields.size() > 0)
317 ## Clone the field template.
318 #set ($field = $formFieldDocClassFields.get(0).clone())
319 #if ("$!field.prettyName" == '')
320 #set ($discard = $field.setPrettyName($formFieldDoc.title))
321 #end
322 #set ($xclass = $doc.getxWikiClass().getXWikiClass())
323 #set ($discard = $xclass.addField($field.name, $field))
324 #set ($discard = $field.setObject($xclass))
325 #displayField($doc.getxWikiClass().get($field.name) $formFieldDoc)
326 #else
327 Unsupported form field.
328 #end
329 #end
330
331 #**
332 * Preview a class field (requires Programming Right).
333 *#
334 #macro (previewField)
335 ## Find the request parameter that specifies the field template.
336 #foreach ($paramName in $request.getParameterMap().keySet())
337 #if ($paramName.startsWith('template-'))
338 #set ($fieldName = $paramName.substring(9))
339 #set ($fieldTemplateDoc = $xwiki.getDocument($request.getParameter($paramName)))
340 #break
341 #end
342 #end
343 ##
344 ## Clone the field template.
345 #set ($field = $fieldTemplateDoc.getxWikiClass().getXWikiClass().properties.get(0).clone())
346 ##
347 ## Update the field meta properties based on the submitted data.
348 #set ($valuesFromRequest = $xcontext.context.getForm().getObject("field-$fieldName"))
349 #set ($discard = $field.getxWikiClass().fromMap($valuesFromRequest, $field))
350 ##
351 ## Don't rename the field (ignore the submitted name).
352 #set ($discard = $field.setName($fieldName))
353 ##
354 ## We have to add the field to the class before setting its value.
355 ## (otherwise the field value from the request is ignored).
356 #set ($xclass = $doc.getxWikiClass().getXWikiClass())
357 #set ($discard = $xclass.addField($fieldName, $field))
358 #set ($discard = $field.setObject($xclass))
359 ##
360 ## Create an object that has this field and set its value from request.
361 #set ($object = $fieldTemplateDoc.getObject($doc.fullName, true))
362 ##
363 ## Filter empty values from the request, otherwise the update method could try to select an invalid value.
364 #set ($values = [])
365 #foreach ($value in $request.getParameterValues("${doc.fullName}_0_$fieldName"))
366 #if ($value != '')
367 #set ($discard = $values.add($value))
368 #end
369 #end
370 #if ($values.size() > 0)
371 #set ($stringArray = $request.getParameterValues("template-$fieldName"))
372 #set ($discard = $xclass.fromMap({$fieldName: $values.toArray($stringArray)}, $object.getXWikiObject()))
373 #end
374 ##
375 ## Display the field.
376 #set ($field = $doc.getxWikiClass().get($fieldName))
377 #displayPropertyEditInput($field, "${doc.fullName}_0_", $object)
378 #end
379
380 #**
381 * Display the edit class form.
382 *#
383 #macro (displayEditForm)
384 #set ($discard = $xwiki.jsfx.use('js/scriptaculous/dragdrop.js'))
385 #set ($discard = $xwiki.jsx.use('AppWithinMinutes.ClassEditSheet'))
386 #set ($discard = $xwiki.ssx.use('AppWithinMinutes.ClassEditSheet'))
387 #set ($discard = $xwiki.ssx.use('AppWithinMinutes.ClassSheetGenerator'))
388 #if ("$!request.wizard" == 'true')
389 #appWizardHeader('structure')
390 #end
391 #displayFieldPalette()
392 #displayFieldCanvas()
393 #displayClassOptions()
394 #if("$!request.wizard" == 'true')
395 #appWizardFooter('structure')
396 #end
397 <div class="clearfloats"></div>
398 #end
399
400 #**
401 * Displays either the edit class form or a new form field. The later is used when adding a new form field via AJAX.
402 *#
403 #macro (doEdit)
404 #if ("$!request.field" != '')
405 #displayNewField()
406 #elseif ("$!request.preview" == 'true')
407 #previewField()
408 #else
409 ## Make sure that only the sheet content is rendered when the class is saved using AJAX.
410 <div class="hidden">
411 <input type="hidden" name="xpage" value="plain" />
412 #if ($request.wizard == 'true')
413 ## Preserve the wizard mode.
414 <input type="hidden" name="wizard" value="true" />
415 #end
416 ## Compute the application title to be used as the wizard step title.
417 #getAppTitle
418 </div>
419 #displayEditForm()
420 #end
421 #end
422
423 #**
424 * Create the home page of the application code space, if it doesn't exist already.
425 *#
426 #macro (maybeCreateCodeSpace)
427 #set ($codeHomePageReference = $services.model.resolveDocument('', 'default', $doc.documentReference.parent))
428 #if (!$xwiki.exists($codeHomePageReference))
429 #set ($codeSpaceTemplate = $services.model.resolveDocument('AppWithinMinutes.CodeSpaceTemplate'))
430 #set ($copyAsJob = $services.refactoring.copyAs($codeSpaceTemplate, $codeHomePageReference))
431 #try()
432 #set ($discard = $copyAsJob.join())
433 #set ($copyAsJobStatus = $services.job.getJobStatus($copyAsJob.request.id))
434 #set ($errorLogs = $copyAsJobStatus.log.getLogs('ERROR'))
435 #if ($errorLogs.size() > 0)
436 #set ($errorMessage = $errorLogs.get(0).toString())
437 #end
438 #end
439 #end
440 #end
441
442 #**
443 * Updates and saves the class definition based on the submitted data.
444 *#
445 #macro(updateAndSaveClass)
446 #set($class = $doc.xWikiClass)
447 #set($xclass = $class.getXWikiClass().clone())
448 #set($xdoc = $doc.document)
449 ##
450 ## Handle new fields and field type changes.
451 ##
452 #set($fieldNames = [])
453 #foreach($paramName in $request.getParameterMap().keySet())
454 #if($paramName.startsWith('type-'))
455 #set($fieldName = $paramName.substring(5))
456 #set($fieldType = $request.getParameter($paramName))
457 #set($field = $class.get($fieldName))
458 #if(!$field || $field.classType != $fieldType)
459 #if($field)
460 ## The field type has changed. Remove the field and add a new one with the proper type.
461 #set($discard = $xclass.removeField($fieldName))
462 #end
463 ## Add a new class field with the specified type.
464 #set($fieldTemplateRef = $request.getParameter("template-$fieldName"))
465 #if("$!fieldTemplateRef" != '')
466 #set($fieldTemplateDoc = $xwiki.getDocument($fieldTemplateRef))
467 #set($field = $fieldTemplateDoc.getxWikiClass().getXWikiClass().properties.get(0).clone())
468 #set($discard = $field.setObject($xclass))
469 #set($discard = $xclass.addField($fieldName, $field))
470 #set($discard = $fieldNames.add($fieldName))
471 #set($discard = $xdoc.setMetaDataDirty(true))
472 #end
473 #else
474 #set($discard = $fieldNames.add($fieldName))
475 #end
476 #end
477 #end
478 ##
479 ## Handle deleted fields.
480 ##
481 #foreach($field in $class.properties)
482 #if(!$fieldNames.contains($field.name))
483 #set($discard = $xclass.removeField($field.name))
484 #end
485 #end
486 ##
487 ## Handle field updates.
488 ##
489 #set($fieldsToRename = {})
490 #foreach($fieldName in $xclass.propertyNames)
491 #set($field = $xclass.get($fieldName))
492 #set($valuesFromRequest = $xcontext.context.getForm().getObject("field-$fieldName"))
493 #set($discard = $field.getxWikiClass().fromMap($valuesFromRequest, $field))
494 #if($field.name.matches('^[a-zA-Z_][\w:\-\.]*$'))
495 #if($fieldName != $field.name)
496 ## The field name has changed.
497 #if($xclass.get($field.name))
498 ## There is already a field with the same name.
499 #set($errorMessage = $services.localization.render('platform.appwithinminutes.classEditorDuplicateFieldNameError', [$field.name]))
500 #break
501 #else
502 #set($discard = $xclass.removeField($fieldName))
503 #set($discard = $xclass.addField($field.name, $field))
504 #set($originalField = $class.get($fieldName))
505 #if($originalField)
506 ## This is not a new field.
507 #set($discard = $fieldsToRename.put($fieldName, $field.name))
508 #set($discard = $xclass.addPropertyForRemoval($originalField.propertyClass))
509 #end
510 #end
511 #end
512 #else
513 #set($errorMessage = $services.localization.render('propertynamenotcorrect'))
514 #break
515 #end
516 #end
517 ##
518 ## Save
519 ##
520 #if(!$errorMessage)
521 #set($discard = $xdoc.setXClass($xclass))
522 #set($discard = $xdoc.renameProperties($doc.documentReference, $fieldsToRename))
523 #set($discard = $xdoc.setHidden(true))
524 #set($discard = $xdoc.setMetaDataDirty(true))
525 #set($discard = $doc.save($services.localization.render('core.comment.updateClassProperty'), $minorEdit))
526 #end
527 ##
528 ## Handle field renames.
529 ##
530 #if(!$errorMessage && !$fieldsToRename.isEmpty())
531 ## 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.
532 ## 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.
533 ##
534 ## 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
535 ## as well. Note that there is a possibility that objects in the class' document are automatically updated. Needs checking.
536 ##
537 ## We use HQL because XWQL doesn't allow us to escape the special characters from the class name.
538 #set($instancesStatement = ', BaseObject as obj where doc.fullName = obj.name and obj.className = :className'
539 + ' and doc.fullName not in (:className, :templateName)')
540 #set($className = $stringtool.removeEnd($doc.fullName, 'Class'))
541 #set($instancesQuery = $services.query.hql($instancesStatement).bindValue('className', $doc.fullName).bindValue(
542 'templateName', "${className}Template"))
543 #foreach($instanceDocName in $instancesQuery.execute())
544 #set($instanceDoc = $xwiki.getDocument($instanceDocName))
545 #set($discard = $instanceDoc.document.renameProperties($doc.documentReference, $fieldsToRename))
546 #set($discard = $instanceDoc.save($services.localization.render('core.comment.updateClassPropertyName'), true))
547 #end
548 #end
549 #end
550
551 #**
552 * Handle Date fields that have the "Empty is today" option checked in the class edit form.
553 * See https://jira.xwiki.org/browse/XWIKI-10296
554 **#
555 #macro(handleEmptyIsTodayDateFields $templateDoc)
556 #foreach($property in $doc.xWikiClass.properties)
557 ## We check directly on the request if the user provided an empty date. We can not check from the template
558 ## document's object that we've just parsed from the request using the updateObjectFromRequest method because it
559 ## already applies the emtpyIsToday mechanism and that would not be good for us.
560 #set($newValueRequestParameterName = "${doc.fullName}_0_${property.name}")
561 #set($newDateStringValue = "$!{request.getParameter($newValueRequestParameterName)}")
562 #if($property.classType == 'Date' && $property.getValue('emptyIsToday') == 1 && $newDateStringValue == '')
563 #set($discard = $templateDoc.set($property.name, $MAGIC_DATE))
564 #end
565 #end
566 #end
567
568 #**
569 * Updates and saves the class template based on the submitted data.
570 *#
571 #macro(updateAndSaveTemplate)
572 #if(!$errorMessage && $request.updateClassTemplate)
573 #set($className = $stringtool.removeEnd($doc.fullName, 'Class'))
574 #set($templateRef = $services.model.resolveDocument("${className}Template"))
575 #set($templateDoc = $xwiki.getDocument($templateRef))
576 #set($discard = $templateDoc.setParent($doc.documentReference.name))
577 #if ($request.templateTitle)
578 #set($discard = $templateDoc.setTitle($request.templateTitle))
579 #end
580 #if ($request.templateContent)
581 #set($discard = $templateDoc.setContent($request.templateContent))
582 #end
583 ## Rename the properties of the template's object, if applicable.
584 #set($discard = $templateDoc.document.renameProperties($doc.documentReference, $fieldsToRename))
585 ## Fill the template's object with the default values from the class editor's form.
586 #set($discard = $templateDoc.updateObjectFromRequest($doc.fullName))
587 ##
588 #handleEmptyIsTodayDateFields($templateDoc)
589 #set($discard = $templateDoc.setHidden(true))
590 #set($discard = $templateDoc.save(
591 $services.localization.render('platform.appwithinminutes.classEditorTemplateSaveComment'),
592 $minorEdit))
593 #end
594 #end
595
596 #**
597 * Updates and saves the class sheet based on the submitted data.
598 *#
599 #macro(updateAndSaveSheet)
600 #if(!$errorMessage && $request.updateClassSheet)
601 #set($classSheets = $services.sheet.getClassSheets($doc))
602 #if($classSheets.isEmpty())
603 #set($className = $stringtool.removeEnd($doc.fullName, 'Class'))
604 #set($sheetReference = $services.model.resolveDocument("${className}Sheet"))
605 #set($discard = $services.sheet.bindClassSheet($doc, $sheetReference))
606 #set($discard = $doc.save($services.localization.render('platform.appwithinminutes.classEditorBindSheetSaveComment'),
607 $minorEdit))
608 #elseif($classSheets.size() == 1)
609 #set($sheetReference = $classSheets.get(0))
610 #end
611 #if($sheetReference)
612 #set($sheetDoc = $xwiki.getDocument($sheetReference))
613 #set($sheetGeneratorDoc = $xwiki.getDocument('AppWithinMinutes.ClassSheetGenerator'))
614 #set($discard = $sheetDoc.setParent($doc.documentReference.name))
615 #set($discard = $sheetDoc.setContent($doc.getRenderedContent($sheetGeneratorDoc.content,
616 $sheetGeneratorDoc.syntax.toIdString(), 'plain/1.0')))
617 #set($discard = $sheetDoc.setHidden(true))
618 #set($discard = $sheetDoc.save($services.localization.render('platform.appwithinminutes.classEditorSheetSaveComment'),
619 $minorEdit))
620 #end
621 #end
622 #end
623
624 #**
625 * Updates and saves the class translation bundle based on the submitted data.
626 *#
627 #macro(updateAndSaveTranslations)
628 #if(!$errorMessage && $request.updateClassTranslations)
629 #set($className = $stringtool.removeEnd($doc.fullName, 'Class'))
630 #set($translationsRef = $services.model.resolveDocument("${className}Translations"))
631 #set($translationsDoc = $xwiki.getDocument($translationsRef))
632 #set($translationsObj = $translationsDoc.getObject('XWiki.TranslationDocumentClass', true))
633 #set ($scope = 'USER')
634 #if ($services.security.authorization.hasAccess('admin', $doc.documentReference.wikiReference))
635 #set ($scope = 'WIKI')
636 #end
637 #set($discard = $translationsObj.set('scope', $scope))
638 #set($discard = $translationsDoc.setParent($doc.documentReference.name))
639 #set($translationsGeneratorDoc = $xwiki.getDocument('AppWithinMinutes.ClassTranslationsGenerator'))
640 #set($discard = $translationsDoc.setContent($doc.getRenderedContent($translationsGeneratorDoc.content,
641 $translationsGeneratorDoc.syntax.toIdString(), 'plain/1.0')))
642 #set($discard = $translationsDoc.setHidden(true))
643 #set($discard = $translationsDoc.save(
644 $services.localization.render('platform.appwithinminutes.classEditorTranslationsSaveComment'),
645 $minorEdit))
646 #end
647 #end
648
649 #**
650 * Updates and saves the class definition, the class sheet and the class template.
651 *#
652 #macro (doSave)
653 #set ($minorEdit = "$!request.minorEdit" != '')
654 #maybeCreateCodeSpace
655 #updateAndSaveClass
656 #updateAndSaveTemplate
657 #updateAndSaveSheet
658 #updateAndSaveTranslations
659 #if ($action == 'save')
660 #if ($errorMessage)
661 <div class="box errormessage">$errorMessage</div>
662 #elseif ("$!request.wizard" == 'true')
663 ## Redirect to next wizard step.
664 #set ($className = $stringtool.removeEnd($doc.fullName, 'Class'))
665 #set ($templateProviderReference = $services.model.resolveDocument("${className}TemplateProvider"))
666 #set ($queryString = {
667 'wizard': true,
668 'sheet': 'AppWithinMinutes.TemplateProviderEditSheet'
669 })
670 #if (!$xwiki.exists($templateProviderReference))
671 #set ($discard = $queryString.putAll({
672 'template': 'XWiki.TemplateProviderTemplate',
673 'parent': $doc.fullName
674 }))
675 #end
676 $response.sendRedirect($xwiki.getURL($templateProviderReference, 'edit', $escapetool.url($queryString)))
677 #else
678 ## Redirect to view mode.
679 $response.sendRedirect($doc.getURL())
680 #end
681 #else
682 #if ($errorMessage)
683 $response.sendError(400, $errorMessage)
684 #else
685 $response.setStatus(204)
686 #end
687 #end
688 #end
689 {{/velocity}}
690
691 {{velocity}}
692 #if("$!request.wizard" == 'true')
693 {{include reference="AppWithinMinutes.WizardStep" /}}
694 #end
695 {{/velocity}}
696
697 {{velocity}}
698 {{html clean="false"}}
699 ## Determine the action button that triggered the request
700 #set ($action = 'edit')
701 #foreach ($paramName in $request.getParameterMap().keySet())
702 #if ($paramName.startsWith('xaction_'))
703 #set ($action = $paramName.substring(8))
704 #break
705 #end
706 #end
707 #if ($action == 'edit')
708 #doEdit()
709 #elseif ($action == 'save' || $action == 'saveandcontinue')
710 #if ($services.csrf.isTokenValid($request.form_token))
711 #doSave()
712 #else
713 $response.sendRedirect($services.csrf.getResubmissionURL())
714 #end
715 #end
716 {{/html}}
717 {{/velocity}}