Code source wiki de $services.localization.render('platform.index.attachmentsResults')
Version 1.1 par superadmin le 2022/06/20 08:48
Masquer les derniers auteurs
| author | version | line-number | content |
|---|---|---|---|
| |
1.1 | 1 | {{template name="hierarchy_macros.vm" /}} |
| 2 | |||
| 3 | {{velocity output="false"}} | ||
| 4 | #macro (maybeApplyStringFilter $fieldName $defaultMatchType $constraints $queryParameters) | ||
| 5 | #if ("$!request.getParameter($fieldName)" != '') | ||
| 6 | #set ($fieldNameWithAlias = $fieldName) | ||
| 7 | #if ($fieldName.indexOf('.') < 0) | ||
| 8 | #set ($fieldNameWithAlias = 'attachment.' + $fieldName) | ||
| 9 | #end | ||
| 10 | #set ($matchType = $request.getParameter("${fieldName}_match")) | ||
| 11 | #if ("$!matchType" == '') | ||
| 12 | #set ($matchType = $defaultMatchType) | ||
| 13 | #end | ||
| 14 | #set ($parameterName = $fieldNameWithAlias.replace('.', '_')) | ||
| 15 | #if ($matchType == 'exact') | ||
| 16 | #set ($discard = $constraints.add("$fieldNameWithAlias = :$parameterName")) | ||
| 17 | #set ($parameterValue = $request.getParameter($fieldName)) | ||
| 18 | #else | ||
| 19 | #set ($discard = $constraints.add("lower($fieldNameWithAlias) like lower(:$parameterName)")) | ||
| 20 | #set ($parameterValue = $request.getParameter($fieldName).trim()) | ||
| 21 | #end | ||
| 22 | #set ($discard = $queryParameters.add({ | ||
| 23 | 'name': $parameterName, | ||
| 24 | 'match': $matchType, | ||
| 25 | 'value': $parameterValue | ||
| 26 | })) | ||
| 27 | #end | ||
| 28 | #end | ||
| 29 | |||
| 30 | #macro (maybeApplyIntegerRangeFilter $fieldName $constraints $queryParameters) | ||
| 31 | #set ($fieldValue = $request.getParameter($fieldName)) | ||
| 32 | #if ("$!fieldValue" != '') | ||
| 33 | #set ($range = $fieldValue.split('-')) | ||
| 34 | #set ($range = [$numbertool.toNumber($range.get(0)).intValue(), $numbertool.toNumber($range.get(1)).intValue()]) | ||
| 35 | #applyRangeFilter($fieldName $range $constraints $queryParameters) | ||
| 36 | #end | ||
| 37 | #end | ||
| 38 | |||
| 39 | #macro (maybeApplyDateRangeFilter $fieldName $constraints $queryParameters) | ||
| 40 | #set ($fieldValue = $request.getParameter($fieldName)) | ||
| 41 | #if ("$!fieldValue" != '') | ||
| 42 | #set ($range = $fieldValue.split('-')) | ||
| 43 | #set ($range = [ | ||
| 44 | $datetool.toDate($numbertool.toNumber($range.get(0))), | ||
| 45 | $datetool.toDate($numbertool.toNumber($range.get(1))) | ||
| 46 | ]) | ||
| 47 | #applyRangeFilter($fieldName $range $constraints $queryParameters) | ||
| 48 | #end | ||
| 49 | #end | ||
| 50 | |||
| 51 | #macro (applyRangeFilter $fieldName $range $constraints $queryParameters) | ||
| 52 | #set ($fieldNameWithAlias = $fieldName) | ||
| 53 | #if ($fieldName.indexOf('.') < 0) | ||
| 54 | #set ($fieldNameWithAlias = 'attachment.' + $fieldName) | ||
| 55 | #end | ||
| 56 | #set ($parameterNamePrefix = $fieldNameWithAlias.replace('.', '_')) | ||
| 57 | #set ($start = $range.get(0)) | ||
| 58 | #if ($start) | ||
| 59 | #set ($startParameterName = $parameterNamePrefix + '_start') | ||
| 60 | #set ($discard = $constraints.add("$fieldNameWithAlias >= :$startParameterName")) | ||
| 61 | #set ($discard = $queryParameters.add({ | ||
| 62 | 'name': $startParameterName, | ||
| 63 | 'match': 'exact', | ||
| 64 | 'value': $start | ||
| 65 | })) | ||
| 66 | #end | ||
| 67 | #set ($end = $range.get(1)) | ||
| 68 | #if ($end) | ||
| 69 | #set ($endParameterName = $parameterNamePrefix + '_end') | ||
| 70 | #set ($discard = $constraints.add("$fieldNameWithAlias < :$endParameterName")) | ||
| 71 | #set ($discard = $queryParameters.add({ | ||
| 72 | 'name': $endParameterName, | ||
| 73 | 'match': 'exact', | ||
| 74 | 'value': $end | ||
| 75 | })) | ||
| 76 | #end | ||
| 77 | #end | ||
| 78 | |||
| 79 | #macro (displayAttachmentMimeType $attachment) | ||
| 80 | <div class="mime" data-type="$!escapetool.xml($attachment.mimeType)"> | ||
| 81 | #if ($attachment.isImage()) | ||
| 82 | <span title="$escapetool.xml($services.localization.render('core.viewers.attachments.mime.image'))"> | ||
| 83 | <img src="$xwiki.getURL($attachmentReference, 'download', 'width=48')" | ||
| 84 | alt="$escapetool.xml($attachment.filename)" /> | ||
| 85 | </span> | ||
| 86 | #else | ||
| 87 | #mimetypeimg($attachment.mimeType.toLowerCase() $attachment.filename.toLowerCase()) | ||
| 88 | #end | ||
| 89 | </div> | ||
| 90 | #end | ||
| 91 | |||
| 92 | #macro (displayAttachmentSize $attachment) | ||
| 93 | <span class="size" data-size="$!escapetool.xml($attachment.longSize)">#dynamicsize($attachment.longSize)</span> | ||
| 94 | #end | ||
| 95 | {{/velocity}} | ||
| 96 | |||
| 97 | {{velocity wiki="false"}} | ||
| 98 | ## ======================================================================================== | ||
| 99 | ## This page is requested asynchronously by the Attachments live table and returns a set of | ||
| 100 | ## results serialized in JSON format. | ||
| 101 | ## ======================================================================================== | ||
| 102 | #if ($xcontext.action == 'get') | ||
| 103 | #set ($offset = $numbertool.toNumber($request.offset).intValue()) | ||
| 104 | ## The offset sent by the live table starts at 1. | ||
| 105 | #set ($offset = $offset - 1) | ||
| 106 | #if (!$offset || $offset < 0) | ||
| 107 | #set ($offset = 0) | ||
| 108 | #end | ||
| 109 | #set ($limit = $numbertool.toNumber($request.limit).intValue()) | ||
| 110 | #if (!$limit) | ||
| 111 | #set ($limit = 15) | ||
| 112 | #end | ||
| 113 | ## | ||
| 114 | ## Apply live table filters. | ||
| 115 | ## | ||
| 116 | #set ($constraints = []) | ||
| 117 | #set ($queryParameters = []) | ||
| 118 | #maybeApplyStringFilter('mimeType' 'prefix' $constraints $queryParameters) | ||
| 119 | #maybeApplyStringFilter('filename' 'partial' $constraints $queryParameters) | ||
| 120 | #maybeApplyStringFilter('doc.fullName' 'partial' $constraints $queryParameters) | ||
| 121 | #maybeApplyIntegerRangeFilter('filesize' $constraints $queryParameters) | ||
| 122 | #maybeApplyDateRangeFilter('date' $constraints $queryParameters) | ||
| 123 | #maybeApplyStringFilter('author' 'partial' $constraints $queryParameters) | ||
| 124 | #set ($whereClause = '') | ||
| 125 | #if ($constraints.size() > 0) | ||
| 126 | #set ($whereClause = 'where ' + $stringtool.join($constraints, ' and ')) | ||
| 127 | #end | ||
| 128 | ## | ||
| 129 | ## Determine the sort field and direction. | ||
| 130 | ## | ||
| 131 | #set ($validSortFields = ['mimeType', 'filename', 'doc.fullName', 'filesize', 'date', 'author']) | ||
| 132 | #set ($sortField = $request.sort) | ||
| 133 | #if (!$validSortFields.contains($sortField)) | ||
| 134 | #set ($sortField = 'filename') | ||
| 135 | #end | ||
| 136 | #set ($caseInsensitiveSort = $sortField != 'date' && $sortField != 'filesize') | ||
| 137 | #if (!$sortField.startsWith('doc.')) | ||
| 138 | #set ($sortField = "attachment.$sortField") | ||
| 139 | #end | ||
| 140 | #set ($direction = 'asc') | ||
| 141 | #if ("$!request.dir" == 'desc') | ||
| 142 | #set ($direction = 'desc') | ||
| 143 | #end | ||
| 144 | #if ($caseInsensitiveSort) | ||
| 145 | #set ($orderByClause = "order by lower($sortField) $direction, $sortField $direction") | ||
| 146 | #else | ||
| 147 | #set ($orderByClause = "order by $sortField $direction") | ||
| 148 | #end | ||
| 149 | ## | ||
| 150 | ## Compute the final query. | ||
| 151 | ## | ||
| 152 | #set ($query = $services.query.hql("$whereClause $orderByClause")) | ||
| 153 | #set ($discard = $query.addFilter('attachment').addFilter('hidden')) | ||
| 154 | #set ($discard = $query.setLimit($limit).setOffset($offset)) | ||
| 155 | #foreach ($queryParameter in $queryParameters) | ||
| 156 | #if ($queryParameter.match == 'exact') | ||
| 157 | #set ($discard = $query.bindValue($queryParameter.name, $queryParameter.value)) | ||
| 158 | #elseif ($queryParameter.match == 'prefix') | ||
| 159 | #set ($query = $query.bindValue($queryParameter.name).literal($queryParameter.value).anyChars().query()) | ||
| 160 | #else | ||
| 161 | ## Partial match. | ||
| 162 | #set ($query = $query.bindValue($queryParameter.name).anyChars().literal($queryParameter.value).anyChars().query()) | ||
| 163 | #end | ||
| 164 | #end | ||
| 165 | #set ($attachmentReferences = $query.execute()) | ||
| 166 | #set ($results = { | ||
| 167 | "totalrows": $query.count(), | ||
| 168 | "returnedrows": $mathtool.min($attachmentReferences.size(), $limit), | ||
| 169 | "offset": $mathtool.add($offset, 1), | ||
| 170 | "reqNo": $numbertool.toNumber($request.reqNo).intValue(), | ||
| 171 | "rows": [] | ||
| 172 | }) | ||
| 173 | #foreach ($attachmentReference in $attachmentReferences) | ||
| 174 | #set ($hasAccess = $services.security.authorization.hasAccess('view', $attachmentReference)) | ||
| 175 | #set ($location = "#hierarchy($attachmentReference.parent, { | ||
| 176 | 'limit': 5, | ||
| 177 | 'plain': $hasAccess.equals(false), | ||
| 178 | 'local': true, | ||
| 179 | 'displayTitle': false | ||
| 180 | })") | ||
| 181 | #set ($row = { | ||
| 182 | 'acclev': $hasAccess, | ||
| 183 | 'fullName': $location, | ||
| 184 | 'filename': $attachmentReference.name, | ||
| 185 | 'fileurl': $xwiki.getURL($attachmentReference) | ||
| 186 | }) | ||
| 187 | #if ($hasAccess) | ||
| 188 | #set ($document = $xwiki.getDocument($attachmentReference.parent)) | ||
| 189 | #set ($attachment = $document.getAttachment($attachmentReference.name)) | ||
| 190 | #set ($authorReference = $services.model.resolveDocument($attachment.author)) | ||
| 191 | #set ($discard = $row.putAll({ | ||
| 192 | 'mimeType': "#displayAttachmentMimeType($attachment)", | ||
| 193 | 'filesize': "#displayAttachmentSize($attachment)", | ||
| 194 | 'date': $xwiki.formatDate($attachment.date), | ||
| 195 | 'author': $xwiki.getUserName($attachment.author) | ||
| 196 | })) | ||
| 197 | #end | ||
| 198 | #set ($discard = $results.rows.add($row)) | ||
| 199 | #end | ||
| 200 | #jsonResponse($results) | ||
| 201 | #end | ||
| 202 | {{/velocity}} |