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