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