# Wiki source code of ClassEditSheet

Last modified by Jean Franco on 2022/06/19 21:15
Export
Office Formats
Export as PDF Export as ODT Export as RTF
Other Formats
Export as HTML

Hide last authors

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

# Applications

# Tips

If you're a new XWiki user, check out the Getting Started Guide.

Maila Networks
XWiki Debian 14.4.1