Wiki source code of api
Last modified by Ophion Admin on 2026/10/06 19:32
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | {{velocity}} | ||
| 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 | ## Only users with edit right on the app home page may create APIs | ||
| 7 | #set ($canCreate = $xwiki.hasAccessLevel('edit', $xcontext.user, "${appSpace}.WebHome")) | ||
| 8 | ## Collect all AWM apps: data class name -> app title | ||
| 9 | #set ($apps = {}) | ||
| 10 | #set ($xwql = "select doc.fullName from Document doc, doc.object(AppWithinMinutes.LiveTableClass) as app order by doc.fullName") | ||
| 11 | #foreach ($homeName in $services.query.xwql($xwql).execute()) | ||
| 12 | #set ($home = $xwiki.getDocument($homeName)) | ||
| 13 | #if ($home) | ||
| 14 | #set ($cls = "$!home.getObject('AppWithinMinutes.LiveTableClass').getValue('class')") | ||
| 15 | ## Skip empty entries and the API app itself | ||
| 16 | #if ($cls != '' && $cls != $appClass) | ||
| 17 | #set ($discard = $apps.put($cls, $home.plainTitle)) | ||
| 18 | #end | ||
| 19 | #end | ||
| 20 | #end | ||
| 21 | ## Current state of the form | ||
| 22 | #set ($name = "$!request.name") | ||
| 23 | #set ($sourceClass = "$!request.sourceClass") | ||
| 24 | #set ($filterField = "$!request.filterField") | ||
| 25 | #set ($filterValue = "$!request.filterValue") | ||
| 26 | ## Preselect the first app on the first page load | ||
| 27 | #if ($sourceClass == '') | ||
| 28 | #foreach ($key in $apps.keySet()) | ||
| 29 | #set ($sourceClass = $key) | ||
| 30 | #break | ||
| 31 | #end | ||
| 32 | #end | ||
| 33 | ## Fields of the selected source app | ||
| 34 | #set ($fields = []) | ||
| 35 | #if ($apps.containsKey($sourceClass)) | ||
| 36 | #foreach ($p in $xwiki.getDocument($sourceClass).getxWikiClass().properties) | ||
| 37 | #set ($discard = $fields.add($p.name)) | ||
| 38 | #end | ||
| 39 | #end | ||
| 40 | ## Reset the filter if the field does not belong to the selected app | ||
| 41 | #if (!$fields.contains($filterField)) | ||
| 42 | #set ($filterField = '') | ||
| 43 | #set ($filterValue = '') | ||
| 44 | #end | ||
| 45 | ## Existing values of the selected field | ||
| 46 | #set ($values = []) | ||
| 47 | #if ($filterField != '') | ||
| 48 | #set ($template = $sourceClass.replaceAll('Class$', 'Template')) | ||
| 49 | #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") | ||
| 50 | #foreach ($entryName in $services.query.hql($hql).bindValue('cls', $sourceClass).bindValue('tpl', $template).execute()) | ||
| 51 | #set ($entry = $xwiki.getDocument($entryName)) | ||
| 52 | #if ($entry) | ||
| 53 | #set ($value = "$!entry.getObject($sourceClass).getValue($filterField)") | ||
| 54 | #if ($value != '' && !$values.contains($value)) | ||
| 55 | #set ($discard = $values.add($value)) | ||
| 56 | #end | ||
| 57 | #end | ||
| 58 | #end | ||
| 59 | #end | ||
| 60 | #set ($errorMsg = '') | ||
| 61 | #if ("$!request.createEntry" == '1' && $canCreate) | ||
| 62 | #if (!$services.csrf.isTokenValid("$!request.form_token")) | ||
| 63 | #set ($errorMsg = 'Invalid form token, please try again.') | ||
| 64 | #elseif (!$name.matches($namePattern)) | ||
| 65 | #set ($errorMsg = 'Only lowercase letters, digits and hyphens are allowed.') | ||
| 66 | #elseif (!$apps.containsKey($sourceClass)) | ||
| 67 | #set ($errorMsg = 'Invalid source application.') | ||
| 68 | #elseif ($filterField != '' && !$values.contains($filterValue)) | ||
| 69 | #set ($errorMsg = 'Please select a filter value.') | ||
| 70 | #else | ||
| 71 | #set ($ref = $services.model.createDocumentReference('', [$appSpace, $name], 'WebHome')) | ||
| 72 | #if ($xwiki.exists($ref)) | ||
| 73 | #set ($errorMsg = "An API named '${name}' already exists.") | ||
| 74 | #else | ||
| 75 | ## Create and save the entry directly, no edit form needed | ||
| 76 | #set ($newDoc = $xwiki.getDocument($ref)) | ||
| 77 | #set ($discard = $newDoc.setTitle($name)) | ||
| 78 | #set ($discard = $newDoc.setParent("${appSpace}.WebHome")) | ||
| 79 | #set ($newObj = $newDoc.newObject($appClass)) | ||
| 80 | #set ($discard = $newObj.set('sourceClass', $sourceClass)) | ||
| 81 | #set ($discard = $newObj.set('filterField', $filterField)) | ||
| 82 | #set ($discard = $newObj.set('filterValue', $filterValue)) | ||
| 83 | #set ($discard = $newDoc.save('Created API entry')) | ||
| 84 | $response.sendRedirect($doc.getURL()) | ||
| 85 | #end | ||
| 86 | #end | ||
| 87 | #end | ||
| 88 | #if ($errorMsg != '') | ||
| 89 | {{error}}$errorMsg{{/error}} | ||
| 90 | #end | ||
| 91 | {{html clean="false"}} | ||
| 92 | <style> | ||
| 93 | /* Hide the default AWM "Add new entry" button */ | ||
| 94 | ul.actions a.action.add, a.action.add { display: none !important; } | ||
| 95 | </style> | ||
| 96 | #if ($canCreate) | ||
| 97 | <form action="$doc.getURL()" method="post" class="form-inline"> | ||
| 98 | <input type="hidden" name="form_token" value="$!services.csrf.token"/> | ||
| 99 | <!-- Changing the app or the field reloads the form without creating anything --> | ||
| 100 | <select name="sourceClass" class="form-control" onchange="this.form.submit()"> | ||
| 101 | #foreach ($app in $apps.entrySet()) | ||
| 102 | <option value="$escapetool.xml($app.key)" #if ($app.key == $sourceClass)selected="selected"#end>$escapetool.xml($app.value)</option> | ||
| 103 | #end | ||
| 104 | </select> | ||
| 105 | <select name="filterField" class="form-control" onchange="this.form.submit()"> | ||
| 106 | <option value="">(no filter)</option> | ||
| 107 | #foreach ($field in $fields) | ||
| 108 | <option value="$escapetool.xml($field)" #if ($field == $filterField)selected="selected"#end>$escapetool.xml($field)</option> | ||
| 109 | #end | ||
| 110 | </select> | ||
| 111 | #if ($filterField != '') | ||
| 112 | <select name="filterValue" class="form-control"> | ||
| 113 | #foreach ($value in $values) | ||
| 114 | <option value="$escapetool.xml($value)" #if ($value == $filterValue)selected="selected"#end>$escapetool.xml($value)</option> | ||
| 115 | #end | ||
| 116 | </select> | ||
| 117 | #end | ||
| 118 | <input type="text" name="name" class="form-control" required="required" value="$escapetool.xml($name)" | ||
| 119 | pattern="$namePattern" title="Lowercase letters, digits and hyphens only"/> | ||
| 120 | <button type="submit" name="createEntry" value="1" class="btn btn-primary">Create API</button> | ||
| 121 | </form> | ||
| 122 | #end | ||
| 123 | {{/html}} | ||
| 124 | {{/velocity}} | ||
| 125 | |||
| 126 | {{velocity}} | ||
| 127 | #set ($columnsProperties = { | ||
| 128 | 'doc.title': {"link":"view"}, | ||
| 129 | 'sourceClass': {}, | ||
| 130 | 'getUrl': {"html":true,"sortable":false,"filterable":false}, | ||
| 131 | 'doc.location': {"html":true}, | ||
| 132 | 'doc.date': {}, | ||
| 133 | 'doc.author': {"link":"author"}, | ||
| 134 | '_actions': {"sortable":false,"filterable":false,"html":true,"actions":["edit","delete"]} | ||
| 135 | }) | ||
| 136 | #set ($options = { | ||
| 137 | 'className': 'api.Code.apiClass', | ||
| 138 | 'translationPrefix': 'api.livetable.', | ||
| 139 | 'tagCloud': true, | ||
| 140 | 'rowCount': 15, | ||
| 141 | 'maxPages': 10, | ||
| 142 | 'selectedColumn': 'doc.title', | ||
| 143 | 'defaultOrder': 'asc' | ||
| 144 | }) | ||
| 145 | #set ($columns = ['doc.title', 'sourceClass', 'getUrl', 'doc.location', 'doc.date', 'doc.author', '_actions']) | ||
| 146 | #livetable('api' $columns $columnsProperties $options) | ||
| 147 | {{/velocity}} |