Wiki source code of Attachments Macro

Version 2.1 by Drunk Monkey on 2021-03-01 08:43

Show last authors
1 {{velocity output="false"}}
2 #if ("$!request.xpage" == 'plain')
3 $response.setContentType('application/json')
4 #end
5 ##
6 ## Offset = item # at which to start displaying data
7 ## -------------------------------
8 #set ($offset = "$!request.offset")
9 #if ($offset == '')
10 #set ($offset = '1')
11 #end
12 #set ($offset = $mathtool.max($numbertool.integer($offset), 1))
13 ##
14 ## Limit = # of items to display
15 ## -------------------------------
16 #set ($limit = "$!request.l")
17 #if ($limit == '')
18 #set ($limit = "$!request.limit")
19 #end
20 #if ($limit == '')
21 #set ($limit = '10')
22 #end
23 #set ($limit = $mathtool.max($numbertool.integer($limit), 1))
24 ##
25 ## Document parameter
26 ## -------------------------------
27 #set ($aDocFullName = "$!request.document")
28 #if ($aDocFullName != '' && $xwiki.exists($aDocFullName))
29 #set ($aDoc = $xwiki.getDocument($aDocFullName))
30 #end
31 ##
32 ## Sort parameter
33 ## -------------------------------
34 #set ($sortColumn = "$!request.sort.toLowerCase()")
35 ##
36 ## Filter by name
37 ##
38 #set ($filterByName = false)
39 #set ($namePattern = '')
40 #if ("$!request.name" != '')
41 #set ($filterByName = true)
42 #set ($namePattern = "$!request.name.toLowerCase()")
43 #end
44 ##
45 ## Macros
46 ## -------------------------------
47 #macro (getOrderedAttachmentNameList)
48 #set ($attachmentComposedNameList = [])
49 #set ($orderedAttachmentComposedNameList = [])
50 #foreach ($attach in $aDoc.getAttachmentList())
51 ## Filter by name
52 #if (! $filterByName || ($attach.filename.toLowerCase().indexOf($namePattern) >= 0))
53 #set ($prefix = $attach.author)
54 #if ($sortColumn == 'date')
55 #set ($prefix = $datetool.format('yyyyMMddHHmmss', $attach.date))
56 #elseif ($sortColumn == 'size')
57 #set ($fileSize = "$!attach.filesize")
58 #set ($maxFileSize = "100000000000")
59 #set ($index = $maxFileSize.length() - $fileSize.length())
60 #set ($prefix = $maxFileSize.substring(0, $index) + $fileSize)
61 #end
62 #set ($attachComposedName = $prefix + '|' + $attach.filename)
63 #set ($discard = $attachmentComposedNameList.add($attachComposedName))
64 #end
65 #end
66 #set ($orderedAttachmentComposedNameList = $sorttool.sort($attachmentComposedNameList))
67 #foreach ($attachComposedName in $orderedAttachmentComposedNameList)
68 #set ($authorLastIndex = $attachComposedName.indexOf('|') + 1)
69 #set ($attachName = $attachComposedName.substring($authorLastIndex))
70 #set ($discard = $orderedAttachmentNameList.add($attachName))
71 #end
72 #end
73 ## Get ordered attachments list
74 ## -------------------------------
75 #set ($orderedAttachmentNameList = [])
76 #set ($attachmentList = $aDoc.getAttachmentList())
77 #if ($aDoc && $attachmentList.size() > 0)
78 #if ($sortColumn == 'name')
79 #foreach ($attach in $attachmentList)
80 ## Filter by name
81 #if (! $filterByName || ($attach.filename.toLowerCase().indexOf($namePattern) >= 0))
82 #set ($discard = $orderedAttachmentNameList.add($attach.filename))
83 #end
84 #end
85 #set ($orderedAttachmentNameList = $sorttool.sort($orderedAttachmentNameList))
86 #elseif ($sortColumn == 'type')
87 #set ($attachmentReversedNameList = [])
88 #set ($orderedAttachmentReversedNameList = [])
89 #foreach ($attach in $attachmentList)
90 #set ($attachName = $attach.filename)
91 ## Filter by name
92 #if (! $filterByName || ($attach.filename.toLowerCase().indexOf($namePattern) >= 0))
93 #set ($extensionIndex = $attachName.lastIndexOf('.'))
94 #set ($extension = $attachName.substring($extensionIndex))
95 #set ($fileName = $attachName.substring(0, $extensionIndex))
96 #set ($attachReversedName = $extension + '|' + $fileName)
97 #set ($discard = $attachmentReversedNameList.add($attachReversedName))
98 #end
99 #end
100 #set ($orderedAttachmentReversedNameList = $sorttool.sort($attachmentReversedNameList))
101 #foreach ($attachReversedName in $orderedAttachmentReversedNameList)
102 #set ($extensionLastIndex = $attachReversedName.indexOf('|'))
103 #set ($nameIndex = $extensionLastIndex + 1)
104 #set ($attachName = $attachReversedName.substring($nameIndex) + $attachReversedName.substring(0, $extensionLastIndex))
105 #set ($discard = $orderedAttachmentNameList.add($attachName))
106 #end
107 #else
108 #getOrderedAttachmentNameList()
109 #end
110 #end
111 ##
112 ## Determine the attachments sub list to display.
113 ## -------------------------------
114 #if ($xwiki.hasAccessLevel('view', $context.user, $aDoc.getPrefixedFullName()))
115 #set ($totalRows = $orderedAttachmentNameList.size())
116 #set ($returnedRows = $mathtool.min($limit, $totalRows))
117 #if ($offset + $returnedRows > $totalRows)
118 #set ($returnedRows = $totalRows - $offset + 1)
119 #end
120 #if ($request.dir == 'desc')
121 #set ($discard = $collectionstool.reverse($orderedAttachmentNameList))
122 #end
123 #set ($start = $offset - 1)
124 #set ($end = $start + $returnedRows)
125 #set ($attachNames = $orderedAttachmentNameList.subList($start, $end))
126 #else
127 #set ($totalRows = 0)
128 #set ($returnedRows = 0)
129 #set ($attachNames = [])
130 #end
131 {{/velocity}}
132
133 {{velocity}}
134 ##
135 ## JSON
136 ## -------------------------------
137 #set ($json = {
138 "totalrows": $totalRows,
139 "matchingtags": {},
140 "tags": [],
141 "returnedrows": $returnedRows,
142 "offset": $offset,
143 "reqNo": $numbertool.integer($request.reqNo),
144 "rows": []
145 })
146 ## $type is required by the user display.
147 #set ($type = 'view')
148 #foreach ($attachName in $attachNames)
149 #set ($attach = $aDoc.getAttachment($attachName))
150 ## $value is required by the user display
151 #set ($value = $attach.author)
152 #set ($discard = $json.rows.add({
153 "name": $attachName,
154 "name_url": $aDoc.getAttachmentURL($attachName),
155 "type": "#mimetypeimg($attach.mimeType.toLowerCase() $attach.filename.toLowerCase())",
156 "size": "#dynamicsize($attach.filesize)",
157 "author": "#template('displayer_users.vm')",
158 "date": "$!xwiki.formatDate($attach.date)",
159 "doc_viewable": true
160 }))
161 #end
162 $jsontool.serialize($json)
163 {{/velocity}}