Wiki source code of api
Version 6.1 by Ophion Admin on 2026/10/06 17:01
Hide last authors
| author | version | line-number | content |
|---|---|---|---|
| |
1.1 | 1 | {{velocity}} |
| |
2.1 | 2 | #set ($appSpace = 'api') |
| 3 | #set ($appClass = 'api.Code.apiClass') | ||
| 4 | ## Lowercase letters and digits, single hyphens only between the parts | ||
| 5 | #set ($namePattern = '[a-z0-9]+(-[a-z0-9]+)*') | ||
| 6 | ## Collect all AWM apps: data class name -> app title | ||
| 7 | #set ($apps = {}) | ||
| 8 | #set ($xwql = "select doc.fullName from Document doc, doc.object(AppWithinMinutes.LiveTableClass) as app order by doc.fullName") | ||
| 9 | #foreach ($homeName in $services.query.xwql($xwql).execute()) | ||
| 10 | #set ($home = $xwiki.getDocument($homeName)) | ||
| 11 | #if ($home) | ||
| 12 | #set ($cls = "$!home.getObject('AppWithinMinutes.LiveTableClass').getValue('class')") | ||
| 13 | ## Skip empty entries and the API app itself | ||
| 14 | #if ($cls != '' && $cls != $appClass) | ||
| 15 | #set ($discard = $apps.put($cls, $home.plainTitle)) | ||
| 16 | #end | ||
| 17 | #end | ||
| 18 | #end | ||
| |
5.1 | 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 | ||
| |
2.1 | 58 | #set ($errorMsg = '') |
| 59 | #if ("$!request.createEntry" == '1') | ||
| |
4.1 | 60 | #if (!$services.csrf.isTokenValid("$!request.form_token")) |
| 61 | #set ($errorMsg = 'Invalid form token, please try again.') | ||
| 62 | #elseif (!$name.matches($namePattern)) | ||
| |
2.1 | 63 | #set ($errorMsg = 'Only lowercase letters, digits and hyphens are allowed.') |
| 64 | #elseif (!$apps.containsKey($sourceClass)) | ||
| 65 | #set ($errorMsg = 'Invalid source application.') | ||
| |
5.1 | 66 | #elseif ($filterField != '' && !$values.contains($filterValue)) |
| 67 | #set ($errorMsg = 'Please select a filter value.') | ||
| |
2.1 | 68 | #else |
| 69 | #set ($ref = $services.model.createDocumentReference('', [$appSpace, $name], 'WebHome')) | ||
| 70 | #if ($xwiki.exists($ref)) | ||
| 71 | #set ($errorMsg = "An API named '${name}' already exists.") | ||
| 72 | #else | ||
| |
4.1 | 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)) | ||
| |
5.1 | 79 | #set ($discard = $newObj.set('filterField', $filterField)) |
| 80 | #set ($discard = $newObj.set('filterValue', $filterValue)) | ||
| |
4.1 | 81 | #set ($discard = $newDoc.save('Created API entry')) |
| 82 | $response.sendRedirect($doc.getURL()) | ||
| |
2.1 | 83 | #end |
| 84 | #end | ||
| 85 | #end | ||
| 86 | #if ($errorMsg != '') | ||
| 87 | {{error}}$errorMsg{{/error}} | ||
| 88 | #end | ||
| 89 | {{html clean="false"}} | ||
| 90 | <style> | ||
| 91 | /* Hide the default AWM "Add new entry" button */ | ||
| 92 | ul.actions a.action.add, a.action.add { display: none !important; } | ||
| 93 | </style> | ||
| |
4.1 | 94 | <form action="$doc.getURL()" method="post" class="form-inline"> |
| 95 | <input type="hidden" name="form_token" value="$!services.csrf.token"/> | ||
| |
5.1 | 96 | <!-- Changing the app or the field reloads the form without creating anything --> |
| 97 | <select name="sourceClass" class="form-control" onchange="this.form.submit()"> | ||
| |
2.1 | 98 | #foreach ($app in $apps.entrySet()) |
| |
5.1 | 99 | <option value="$escapetool.xml($app.key)" #if ($app.key == $sourceClass)selected="selected"#end>$escapetool.xml($app.value)</option> |
| |
2.1 | 100 | #end |
| 101 | </select> | ||
| |
5.1 | 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)" | ||
| |
2.1 | 116 | pattern="$namePattern" title="Lowercase letters, digits and hyphens only"/> |
| |
5.1 | 117 | <button type="submit" name="createEntry" value="1" class="btn btn-primary">Create API</button> |
| |
2.1 | 118 | </form> |
| 119 | {{/html}} | ||
| 120 | {{/velocity}} | ||
| 121 | |||
| 122 | {{velocity}} | ||
| |
1.1 | 123 | #set ($columnsProperties = { |
| 124 | 'doc.title': {"link":"view"}, | ||
| |
3.1 | 125 | 'sourceClass': {}, |
| |
6.1 | 126 | 'getUrl': {"html":true,"sortable":false,"filterable":false}, |
| |
1.1 | 127 | 'doc.location': {"html":true}, |
| 128 | 'doc.date': {}, | ||
| 129 | 'doc.author': {"link":"author"}, | ||
| 130 | '_actions': {"sortable":false,"filterable":false,"html":true,"actions":["edit","delete"]} | ||
| 131 | }) | ||
| 132 | #set ($options = { | ||
| 133 | 'className': 'api.Code.apiClass', | ||
| 134 | 'translationPrefix': 'api.livetable.', | ||
| 135 | 'tagCloud': true, | ||
| 136 | 'rowCount': 15, | ||
| 137 | 'maxPages': 10, | ||
| 138 | 'selectedColumn': 'doc.title', | ||
| 139 | 'defaultOrder': 'asc' | ||
| 140 | }) | ||
| |
6.1 | 141 | #set ($columns = ['doc.title', 'sourceClass', 'getUrl', 'doc.location', 'doc.date', 'doc.author', '_actions']) |
| |
1.1 | 142 | #livetable('api' $columns $columnsProperties $options) |
| 143 | {{/velocity}} |