Changes for page api

Last modified by Ophion Admin on 2026/10/06 19:32

From version 2.1
edited by Ophion Admin
on 2026/10/06 15:59
Change comment: There is no comment for this version
To version 5.1
edited by Ophion Admin
on 2026/10/06 16:20
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -1,7 +1,6 @@
1 1  {{velocity}}
2 2  #set ($appSpace = 'api')
3 3  #set ($appClass = 'api.Code.apiClass')
4 -#set ($templateRef = 'api.Code.apiTemplate')
5 5  ## Lowercase letters and digits, single hyphens only between the parts
6 6  #set ($namePattern = '[a-z0-9]+(-[a-z0-9]+)*')
7 7  ## Collect all AWM apps: data class name -> app title
... ... @@ -17,22 +17,70 @@
17 17   #end
18 18   #end
19 19  #end
19 +## Current state of the form
20 +#set ($name = "$!request.name")
21 +#set ($sourceClass = "$!request.sourceClass")
22 +#set ($filterField = "$!request.filterField")
23 +#set ($filterValue = "$!request.filterValue")
24 +## Preselect the first app on the first page load
25 +#if ($sourceClass == '')
26 + #foreach ($key in $apps.keySet())
27 + #set ($sourceClass = $key)
28 + #break
29 + #end
30 +#end
31 +## Fields of the selected source app
32 +#set ($fields = [])
33 +#if ($apps.containsKey($sourceClass))
34 + #foreach ($p in $xwiki.getDocument($sourceClass).getxWikiClass().properties)
35 + #set ($discard = $fields.add($p.name))
36 + #end
37 +#end
38 +## Reset the filter if the field does not belong to the selected app
39 +#if (!$fields.contains($filterField))
40 + #set ($filterField = '')
41 + #set ($filterValue = '')
42 +#end
43 +## Existing values of the selected field
44 +#set ($values = [])
45 +#if ($filterField != '')
46 + #set ($template = $sourceClass.replaceAll('Class$', 'Template'))
47 + #set ($hql = "select distinct doc.fullName from XWikiDocument doc, BaseObject obj where obj.name = doc.fullName and obj.className = :cls and doc.fullName <> :tpl order by doc.fullName")
48 + #foreach ($entryName in $services.query.hql($hql).bindValue('cls', $sourceClass).bindValue('tpl', $template).execute())
49 + #set ($entry = $xwiki.getDocument($entryName))
50 + #if ($entry)
51 + #set ($value = "$!entry.getObject($sourceClass).getValue($filterField)")
52 + #if ($value != '' && !$values.contains($value))
53 + #set ($discard = $values.add($value))
54 + #end
55 + #end
56 + #end
57 +#end
20 20  #set ($errorMsg = '')
21 21  #if ("$!request.createEntry" == '1')
22 - #set ($name = "$!request.name")
23 - #set ($sourceClass = "$!request.sourceClass")
24 - #if (!$name.matches($namePattern))
60 + #if (!$services.csrf.isTokenValid("$!request.form_token"))
61 + #set ($errorMsg = 'Invalid form token, please try again.')
62 + #elseif (!$name.matches($namePattern))
25 25   #set ($errorMsg = 'Only lowercase letters, digits and hyphens are allowed.')
26 26   #elseif (!$apps.containsKey($sourceClass))
27 27   #set ($errorMsg = 'Invalid source application.')
66 + #elseif ($filterField != '' && !$values.contains($filterValue))
67 + #set ($errorMsg = 'Please select a filter value.')
28 28   #else
29 29   #set ($ref = $services.model.createDocumentReference('', [$appSpace, $name], 'WebHome'))
30 30   #if ($xwiki.exists($ref))
31 31   #set ($errorMsg = "An API named '${name}' already exists.")
32 32   #else
33 - ## The last parameter prefills the sourceClass field of the new entry
34 - #set ($qs = $escapetool.url({'template': $templateRef, 'parent': "${appSpace}.WebHome", 'title': $name, "${appClass}_0_sourceClass": $sourceClass}))
35 - $response.sendRedirect($xwiki.getURL($ref, 'edit', $qs))
73 + ## Create and save the entry directly, no edit form needed
74 + #set ($newDoc = $xwiki.getDocument($ref))
75 + #set ($discard = $newDoc.setTitle($name))
76 + #set ($discard = $newDoc.setParent("${appSpace}.WebHome"))
77 + #set ($newObj = $newDoc.newObject($appClass))
78 + #set ($discard = $newObj.set('sourceClass', $sourceClass))
79 + #set ($discard = $newObj.set('filterField', $filterField))
80 + #set ($discard = $newObj.set('filterValue', $filterValue))
81 + #set ($discard = $newDoc.save('Created API entry'))
82 + $response.sendRedirect($doc.getURL())
36 36   #end
37 37   #end
38 38  #end
... ... @@ -44,16 +44,30 @@
44 44   /* Hide the default AWM "Add new entry" button */
45 45   ul.actions a.action.add, a.action.add { display: none !important; }
46 46  </style>
47 -<form action="$doc.getURL()" method="get" class="form-inline">
48 - <input type="hidden" name="createEntry" value="1"/>
49 - <select name="sourceClass" class="form-control">
94 +<form action="$doc.getURL()" method="post" class="form-inline">
95 + <input type="hidden" name="form_token" value="$!services.csrf.token"/>
96 + <!-- Changing the app or the field reloads the form without creating anything -->
97 + <select name="sourceClass" class="form-control" onchange="this.form.submit()">
50 50   #foreach ($app in $apps.entrySet())
51 - <option value="$escapetool.xml($app.key)">$escapetool.xml($app.value)</option>
99 + <option value="$escapetool.xml($app.key)" #if ($app.key == $sourceClass)selected="selected"#end>$escapetool.xml($app.value)</option>
52 52   #end
53 53   </select>
54 - <input type="text" name="name" class="form-control" required="required"
102 + <select name="filterField" class="form-control" onchange="this.form.submit()">
103 + <option value="">(no filter)</option>
104 + #foreach ($field in $fields)
105 + <option value="$escapetool.xml($field)" #if ($field == $filterField)selected="selected"#end>$escapetool.xml($field)</option>
106 + #end
107 + </select>
108 + #if ($filterField != '')
109 + <select name="filterValue" class="form-control">
110 + #foreach ($value in $values)
111 + <option value="$escapetool.xml($value)" #if ($value == $filterValue)selected="selected"#end>$escapetool.xml($value)</option>
112 + #end
113 + </select>
114 + #end
115 + <input type="text" name="name" class="form-control" required="required" value="$escapetool.xml($name)"
55 55   pattern="$namePattern" title="Lowercase letters, digits and hyphens only"/>
56 - <input type="submit" class="btn btn-primary" value="Create API"/>
117 + <button type="submit" name="createEntry" value="1" class="btn btn-primary">Create API</button>
57 57  </form>
58 58  {{/html}}
59 59  {{/velocity}}
... ... @@ -61,6 +61,7 @@
61 61  {{velocity}}
62 62  #set ($columnsProperties = {
63 63   'doc.title': {"link":"view"},
125 + 'sourceClass': {},
64 64   'doc.location': {"html":true},
65 65   'doc.date': {},
66 66   'doc.author': {"link":"author"},
... ... @@ -75,6 +75,6 @@
75 75   'selectedColumn': 'doc.title',
76 76   'defaultOrder': 'asc'
77 77  })
78 -#set ($columns = ['doc.title', 'doc.location', 'doc.date', 'doc.author', '_actions'])
140 +#set ($columns = ['doc.title', 'sourceClass', 'doc.location', 'doc.date', 'doc.author', '_actions'])
79 79  #livetable('api' $columns $columnsProperties $options)
80 80  {{/velocity}}