Version 2.1 par Florent Charton le 2024/08/08 18:43

Masquer les derniers auteurs
superadmin 1.1 1 {{template name="hierarchy_macros.vm" /}}
Florent Charton 2.1 2 {{template name="attachment_macros.vm" /}}
superadmin 1.1 3
4 {{velocity wiki="false"}}
5 ## ========================================================================================
6 ## This page is requested asynchronously by the Attachments live table and returns a set of
7 ## results serialized in JSON format.
8 ## ========================================================================================
9 #if ($xcontext.action == 'get')
10 #set ($offset = $numbertool.toNumber($request.offset).intValue())
11 ## The offset sent by the live table starts at 1.
12 #set ($offset = $offset - 1)
13 #if (!$offset || $offset < 0)
14 #set ($offset = 0)
15 #end
16 #set ($limit = $numbertool.toNumber($request.limit).intValue())
17 #if (!$limit)
18 #set ($limit = 15)
19 #end
20 ##
21 ## Apply live table filters.
22 ##
23 #set ($constraints = [])
24 #set ($queryParameters = [])
25 #maybeApplyStringFilter('mimeType' 'prefix' $constraints $queryParameters)
26 #maybeApplyStringFilter('filename' 'partial' $constraints $queryParameters)
27 #maybeApplyStringFilter('doc.fullName' 'partial' $constraints $queryParameters)
28 #maybeApplyIntegerRangeFilter('filesize' $constraints $queryParameters)
29 #maybeApplyDateRangeFilter('date' $constraints $queryParameters)
30 #maybeApplyStringFilter('author' 'partial' $constraints $queryParameters)
31 #set ($whereClause = '')
32 #if ($constraints.size() > 0)
33 #set ($whereClause = 'where ' + $stringtool.join($constraints, ' and '))
34 #end
35 ##
36 ## Determine the sort field and direction.
37 ##
38 #set ($validSortFields = ['mimeType', 'filename', 'doc.fullName', 'filesize', 'date', 'author'])
39 #set ($sortField = $request.sort)
40 #if (!$validSortFields.contains($sortField))
41 #set ($sortField = 'filename')
42 #end
43 #set ($caseInsensitiveSort = $sortField != 'date' && $sortField != 'filesize')
44 #if (!$sortField.startsWith('doc.'))
45 #set ($sortField = "attachment.$sortField")
46 #end
47 #set ($direction = 'asc')
48 #if ("$!request.dir" == 'desc')
49 #set ($direction = 'desc')
50 #end
51 #if ($caseInsensitiveSort)
52 #set ($orderByClause = "order by lower($sortField) $direction, $sortField $direction")
53 #else
54 #set ($orderByClause = "order by $sortField $direction")
55 #end
56 ##
57 ## Compute the final query.
58 ##
59 #set ($query = $services.query.hql("$whereClause $orderByClause"))
60 #set ($discard = $query.addFilter('attachment').addFilter('hidden'))
61 #set ($discard = $query.setLimit($limit).setOffset($offset))
62 #foreach ($queryParameter in $queryParameters)
63 #if ($queryParameter.match == 'exact')
64 #set ($discard = $query.bindValue($queryParameter.name, $queryParameter.value))
65 #elseif ($queryParameter.match == 'prefix')
66 #set ($query = $query.bindValue($queryParameter.name).literal($queryParameter.value).anyChars().query())
67 #else
68 ## Partial match.
69 #set ($query = $query.bindValue($queryParameter.name).anyChars().literal($queryParameter.value).anyChars().query())
70 #end
71 #end
72 #set ($attachmentReferences = $query.execute())
73 #set ($results = {
74 "totalrows": $query.count(),
75 "returnedrows": $mathtool.min($attachmentReferences.size(), $limit),
76 "offset": $mathtool.add($offset, 1),
77 "reqNo": $numbertool.toNumber($request.reqNo).intValue(),
78 "rows": []
79 })
80 #foreach ($attachmentReference in $attachmentReferences)
81 #set ($hasAccess = $services.security.authorization.hasAccess('view', $attachmentReference))
82 #set ($location = "#hierarchy($attachmentReference.parent, {
83 'limit': 5,
84 'plain': $hasAccess.equals(false),
85 'local': true,
86 'displayTitle': false
87 })")
Florent Charton 2.1 88 ## The field "fullName" was used by Livetable and is needed for backwards-compatibility.
89 ## The field "doc.fullName" is needed by LiveData as doc.fullName is the actual field name.
superadmin 1.1 90 #set ($row = {
91 'acclev': $hasAccess,
92 'fullName': $location,
Florent Charton 2.1 93 'doc.fullName': $location,
superadmin 1.1 94 'filename': $attachmentReference.name,
95 'fileurl': $xwiki.getURL($attachmentReference)
96 })
97 #if ($hasAccess)
98 #set ($document = $xwiki.getDocument($attachmentReference.parent))
99 #set ($attachment = $document.getAttachment($attachmentReference.name))
100 #set ($authorReference = $services.model.resolveDocument($attachment.author))
101 #set ($discard = $row.putAll({
102 'mimeType': "#displayAttachmentMimeType($attachment)",
Florent Charton 2.1 103 'filesize': "#displayAttachmentSize($attachment.longSize)",
superadmin 1.1 104 'date': $xwiki.formatDate($attachment.date),
Florent Charton 2.1 105 'author': "#displayUserNameWithAvatar($attachment.author)"
superadmin 1.1 106 }))
107 #end
108 #set ($discard = $results.rows.add($row))
109 #end
110 #jsonResponse($results)
111 #end
112 {{/velocity}}