Failed to execute template. Cause: [Access denied when checking [script] access to [xwiki:Collaboratory.UX.HbpSkin.WebHome] for user [xwiki:XWiki.Admin]]. Click on this message for details.

Attachments - HBP Wiki

IAM21 instance, do not create collab nor modify a team, your changes will be lost


Wiki source code of Attachments

Last modified by superadmin on 2024/11/04 17:33

Hide last authors
superadmin 1.1 1 {{velocity output="false"}}
2 #if ($request.xaction == 'postUpload')
3 #set ($targetDocument = $xwiki.getDocument($request.get('docname')))
4 #set ($targetAttachDocument = $xwiki.getDocument($request.get('targetdocname')))
5
6 #set ($fieldname = $request.get('fieldname'))
7 #set ($docAction = $request.get('docAction'))
8 #set ($attachmentList = $targetAttachDocument.getAttachmentList())
9 #if ($attachmentList && $attachmentList.size() > 0)
10 #set ($sortedAttachments = $sorttool.sort($attachmentList, 'date:desc'))
11 #set ($lastAttachment = $sortedAttachments.get(0))
12 #end
13 $response.sendRedirect($targetDocument.getURL($docAction, $escapetool.url({
14 $fieldname: $lastAttachment.filename,
15 'form_token': $request.form_token
16 })))
17 #stop
18 #end
19 {{/velocity}}
20
21 {{velocity output="false"}}
22 ##
23 ## Macros
24 ##
25 #set ($translationPrefix = 'xe.attachmentSelector')
26 #set ($attachmentPickerDocName = 'XWiki.AttachmentSelector')
27
28 $xwiki.ssx.use($attachmentPickerDocName)
29 $xwiki.jsx.use($attachmentPickerDocName)
30
31 #**
32 * Displays the attachment gallery as a list of attachment boxes, starting with special boxes for uploading a new attachment and for setting a default value.
33 *
34 * @param $targetDocument the document to recieve the field value being modified
35 * @param $targetAttachDocument the document to list/save attachments to
36 * @param $options generic picker options
37 *#
38 #macro (attachmentPicker_displayAttachmentGallery $targetDocument, $targetAttachDocument, $options)
39 #set ($currentValue = $targetDocument.getValue($options.property))
40 #if ("$!{targetAttachDocument.getAttachment($currentValue)}" == '')
41 #set ($currentValue = "$!{options.defaultValue}")
42 #end
43 (% class="gallery" %)(((
44 ## Only display the upload form if they have edit permission on targetAttachDocument
superadmin 5.1 45 #attachmentPicker_displayUploadForm($targetDocument, $targetAttachDocument, $options)
superadmin 1.1 46 #attachmentPicker_displayAttachmentGalleryEmptyValue($targetDocument, $targetAttachDocument, $options, $currentValue)
47 #set ($sortedAttachments = $sorttool.sort($targetAttachDocument.getAttachmentList(), "${options.sortAttachmentsBy}") )
48 #foreach ($attachment in $sortedAttachments)
49 #set ($extension = $attachment.getFilename())
50 #set ($extension = $extension.substring($mathtool.add($extension.lastIndexOf('.'), 1)).toLowerCase())
51 #if ($options.filter.size() == 0 || $options.filter.contains($extension))
52 #attachmentPicker_displayAttachmentBox($attachment $targetDocument $targetAttachDocument, $options $currentValue)
53 #end
54 #end
55 )))
56 #end
57
58 #**
59 * Displays an attachment box.
60 *
61 * @param $attachment the target attachment to display
62 * @param $targetDocument the document being modified
63 * @param $options generic picker options
64 * @param $currentValue the currently selected file, used for determining if the box should be highlighted as the current value
65 *#
66 #macro (attachmentPicker_displayAttachmentBox $attachment $targetDocument $targetAttachDocument, $options $currentValue)
superadmin 5.1 67 #set ($hasTemporaryAttachment = "$!services.temporaryAttachments" != '')
68 #set ($canEdit = $xwiki.hasAccessLevel('edit', $xcontext.user, ${targetAttachDocument.fullName}))
69 #set ($isTemporaryAttachment = false)
70 #if(!$hasTemporaryAttachment)
71 #set ($canDeleteAttachment = $canEdit)
superadmin 1.1 72 #else
superadmin 5.1 73 #set ($isTemporaryAttachment = $services.temporaryAttachments.temporaryAttachmentExists($attachment))
74 ## TODO: Update once it is made possible to delete temporary attachments (see XWIKI-20225).
75 #set ($canDeleteAttachment = !$isTemporaryAttachment && $canEdit)
superadmin 1.1 76 #end
superadmin 5.1 77 #set ($cssClasses = [])
78 #if ($options.displayImage && $attachment.isImage())
79 #set ($discard = $cssClasses.add('gallery_image'))
80 #end
81 #if ($isTemporaryAttachment)
82 #set ($discard = $cssClasses.add('temporary_attachment'))
83 #end
84 #attachmentPicker_displayStartFrame({'value' : $attachment.filename, 'text' : $attachment.filename, 'cssClass' : "${stringtool.join($cssClasses, ' ')}"} $currentValue)
superadmin 1.1 85 #attachmentPicker_displayAttachmentDetails($attachment $options)
86 #set ($returnURL = $escapetool.url($doc.getURL('view', $request.queryString)))
87 #set ($deleteURL = $targetAttachDocument.getAttachmentURL($attachment.filename, 'delattachment', "xredirect=${returnURL}&form_token=$!{services.csrf.getToken()}") )
88 #set ($viewURL = $targetAttachDocument.getAttachmentURL($attachment.filename) )##{'name' : 'download', 'url' : $viewURL, 'rel' : '__blank'}
superadmin 5.1 89 #set ($selectURL = $targetDocument.getURL(${options.get('docAction')}, $escapetool.url({
90 "${options.get('classname')}_${options.get('object')}_${options.get('property')}": ${attachment.filename},
91 'form_token': $!{services.csrf.getToken()}
92 })))
93 ## Delete action is only proposed for users with the edit right on the document.
94 ## If the temporary attachment is available, the delete action is only allowed for non-temporary attachments.
95 #set ($attachmentActions = [{'name' : 'select', 'url' : $selectURL}])
96 #if($canDeleteAttachment)
97 #set ($discard = $attachmentActions.add({'name' : 'delete', 'url' : $deleteURL}))
98 #end
99 #define($additionalContent)
100 #if ($isTemporaryAttachment)
101 #set ($titleMessage = $services.localization.render('attachment.attachmentSelector.attachmentBox.temporaryAttachmentTitle'))
102 #set ($titleMessage = $services.rendering.escape($titleMessage, 'xwiki/2.1'))
103 (% title="$titleMessage" %)$services.icon.render('clock')(%%)
104 #end
105 #end
106 #attachmentPicker_displayEndFrame ($attachmentActions $additionalContent)
superadmin 1.1 107 #end
108
109 #**
110 * Writes the wiki code used at the start of an attachment box. Outputs the attachment title bar, and opens the inner frame div.
111 *
112 * @param $boxOptions a map of parameters/options for the current attachment, holding, for example, the attachment name (boxOptions.value),
113 * the title to display (boxOptions.text), optional extra CSS classnames to put on the box (boxOptions.cssClass)
114 * @param $currentValue the currently selected file, used for determining if this attachment should be highlighted as the current value
115 *#
116 #macro (attachmentPicker_displayStartFrame $boxOptions $currentValue)
117 (% class="gallery_attachmentbox $!{boxOptions.cssClass} #if ("$!{boxOptions.value}" == $currentValue) current#{end}" %)(((
superadmin 5.1 118 (% class="gallery_attachmenttitle" title="$services.rendering.escape($!{boxOptions.value}, 'xwiki/2.1')" %)(((
119 $services.rendering.escape($boxOptions.text, 'xwiki/2.1')
120 )))
superadmin 1.1 121 (% class="gallery_attachmentframe" %)(((
122 #end
123
124 #**
125 * Displays details about an attachment inside the attachment box. If the attachment is an image and the "displayImage" option is on,
126 * then the image is displayed. Otherwise, some basic information is displayed: the version, the size, the date and the author.
127 *
128 * @param $attachment the target attachment to display
129 * @param $options generic picker options
130 *#
131 #macro (attachmentPicker_displayAttachmentDetails $attachment $options)
132 #if ($attachment)
133 ## Compute the attachment reference because there's no getter.
134 #set ($attachmentReference = $services.model.createAttachmentReference($attachment.document.documentReference,
135 $attachment.filename))
superadmin 5.1 136 #set ($attachmentStringReference = $services.rendering.escape($services.model.serialize($attachmentReference, 'default'), 'xwiki/2.1'))
superadmin 1.1 137 #if ($attachment.isImage() && $options.displayImage)
138 ## We add the version to the query string in order to invalidate the cache when an image attachment is replaced.
139 #set ($queryString = $escapetool.url({'version': $attachment.version}))
140 [[[[image:$attachmentStringReference||width=180 queryString="$queryString"]]>>attach:$attachmentStringReference]]
141 #else
superadmin 5.1 142 * (% class="mime" %){{html wiki=false clean=false}}#mimetypeimg($attachment.getMimeType().toLowerCase() $attachment.getFilename().toLowerCase()){{/html}}(%%) (% class="filename" %)$services.rendering.escape($attachment.getFilename(), 'xwiki/2.1')(% %)
superadmin 1.1 143 * v$attachment.getVersion() (#dynamicsize($attachment.longSize))
144 * $services.localization.render('core.viewers.attachments.author', [$!{xwiki.getUserName($attachment.author, false)}]) $services.localization.render('core.viewers.attachments.date', [$!{xwiki.formatDate($attachment.date, 'dd/MM/yyyy hh:mm')}])
145 * (% class="buttonwrapper" %)[[${services.localization.render("${translationPrefix}.actions.download")}>>attach:$attachmentStringReference||title="$services.localization.render("${translationPrefix}.actions.download")" rel="__blank" class="button"]](%%)
146 #end
147 #end
148 #end
149
150 #**
151 * Writes the wiki code used at the end of an attachment box. Closes the inner frame div, and outputs the attachment actions.
152 *
153 * @param $actions a list of maps defining action buttons, where each entry contains a subset of the following:
154 * <dl>
155 * <dt>name</dt>
156 * <dd>identifies the action; the name is used as a CSS classname, and in the translation key for the display text, as "xe.attachmentSelector.actions.<name>"</dd>
157 * <dt>url</dt>
158 * <dd>the destination of the button</dd>
159 * <dt>rel</dt>
160 * <dd>an optional parameter to be used in the "rel" HTML attribute; for example "__blank" can be used to open the link in a new tab/window</dd>
161 * </dl>
superadmin 5.1 162 * @param $additionalContent optional additional content that does not follow the structure of the actions
superadmin 1.1 163 *#
superadmin 5.1 164 #macro (attachmentPicker_displayEndFrame $actions $additionalContent)
superadmin 1.1 165 )))## attachmentframe
166 (% class="gallery_actions" %)(((
167 #foreach ($action in $actions)
168 #set( $actionname = $services.localization.render("${translationPrefix}.actions.${action.name}") )
169 [[${actionname}>>path:${action.url}||class="tool ${action.name}" title="${actionname}" #if($action.rel) rel="${action.rel}"#end]]##
170 #end
superadmin 5.1 171 $!additionalContent
superadmin 1.1 172 )))## actions
173 )))## attachmentbox
174 #end
175
176 #**
177 * Displays the upload box used for adding and selecting a new attachment.
178 *
179 * @param $targetDocument the document with the property being modified
180 * @param $targetAttachDocument the document to upload the attachment to
181 * @param $options generic picker options
182 *#
183 #macro (attachmentPicker_displayUploadForm $targetDocument, $targetAttachDocument, $options)
184 #attachmentPicker_displayStartFrame({
185 'value' : $services.localization.render("${translationPrefix}.upload.title"),
186 'text' : $services.localization.render("${translationPrefix}.upload.title"),
187 'cssClass' : 'gallery_upload'
188 } $NULL)
189 {{html clean="false"}}
190 <form action="$targetAttachDocument.getURL('upload')" enctype="multipart/form-data" method="post" id="uploadAttachment" class="uploadAttachment xform">
191 <div class="gallery_upload_input">
192 #if (${options.rawfilter} != '')
193 <span class="xHint">$escapetool.xml($services.localization.render("${translationPrefix}.upload.hint", [${options.rawfilter}]))</span>
194 #end
195 <input type="file" name="filepath" id="attachfile" class="noitems" title="$!{escapetool.xml($options.rawfilter)}"/>
196 <input type="hidden" name="xredirect" value="$xwiki.getDocument($attachmentPickerDocName).getURL('get', "xaction=postUpload&amp;docAction=$!{escapetool.url($options.get('docAction'))}&amp;targetdocname=$!{escapetool.url($targetAttachDocument.fullName)}&amp;docname=$!{escapetool.url($targetDocument.fullName)}&amp;fieldname=$!{escapetool.url($options.get('classname'))}_$!{escapetool.url($options.get('object'))}_$!{escapetool.url($options.get('property'))}&amp;form_token=$!{services.csrf.getToken()}")" />
197 <input type="hidden" name="docname" value="$!{escapetool.xml($targetDocument.fullName)}" />
198 <input type="hidden" name="classname" value="$!{escapetool.xml($options.get('classname'))}" />
199 <input type="hidden" name="object" value="$!{escapetool.xml($options.get('object'))}" />
200 <input type="hidden" name="property" value="$!{escapetool.xml($options.get('property'))}" />
201 <input type="hidden" name="form_token" value="$!{services.csrf.getToken()}" />
202 </div>
203 #if ("$!currentValue" != '' && $currentValue != $options.defaultValue)
204 <div>
205 <label>
206 <input type="checkbox" name="filename" value="$!escapetool.xml($currentValue)"
207 />$services.localization.render('attachmentSelector.replace',
208 ["<strong>$!escapetool.xml($currentValue)</strong>"])
209 </label>
210 <span class="xHint">$escapetool.xml($services.localization.render('attachmentSelector.replace.hint'))</span>
211 </div>
212 #end
213 #if ($xwiki.hasEditComment() && $options.versionSummary)
214 <div>
215 #if ($xwiki.isEditCommentFieldHidden())
216 <input type="hidden" name="comment" value="$!escapetool.xml($request.comment)" />
217 #else
218 <label for="commentinput">$services.localization.render('core.comment')</label>
219 <input type="text" name="comment" id="commentinput" value="$!escapetool.xml($request.comment)"
220 title="$services.localization.render('core.comment.tooltip')" />
221 #end
222 </div>
223 #end
224 <div class="buttons">
225 <span class="buttonwrapper">
226 <input type="submit" name="action_upload" class="button " value='$services.localization.render("${translationPrefix}.upload.submit")' title='$services.localization.render("${translationPrefix}.upload.submit")'/>
227 </span>
228 </div>
229 </form>
230 {{/html}}
231 #attachmentPicker_displayEndFrame ([])
232 #end
233
234 #**
235 * Displays the "empty value" box, used for unsetting the field value.
236 *
237 * @param $targetDocument the document being modified
238 * @param $targetAttachDocument the document that the attachments will the loaded from/saved to
239 * @param $options generic picker options
240 * @param $currentValue the currently selected file, used for determining if the empty box should be highlighted as the current value
241 *#
242 #macro (attachmentPicker_displayAttachmentGalleryEmptyValue $targetDocument, $targetAttachDocument, $options, $currentValue)
243 #if ("$!{options.get('defaultValue')}" != '')
244 #set ($reference = ${options.get('defaultValue')})
245 #set ($docNameLimit = $reference.indexOf('@'))
246 #if ($docNameLimit > 0)
247 #set ($docName = $reference.substring(0, $docNameLimit))
248 #else
249 #set ($docName = $targetAttachDocument.fullName)
250 #end
251 #set ($attachmentName = $reference.substring($mathtool.add($docNameLimit, 1)))
252 #set ($defaultAttachment = $xwiki.getDocument($docName).getAttachment($attachmentName))
253 #if ($defaultAttachment.isImage())
254 #set($dcssClass = 'gallery_image')
255 #end
256 #end
257 #attachmentPicker_displayStartFrame({'cssClass' : "gallery_emptyChoice $!{dcssClass}", 'text' : $services.localization.render("${translationPrefix}.default"), 'value' : "${options.defaultValue}"} $currentValue)
258 #attachmentPicker_displayAttachmentDetails($defaultAttachment $options)
259 #set ($returnURL = $escapetool.url($doc.getURL('view', $request.queryString)))
260 #set ($selectURL = $targetDocument.getURL(${options.get('docAction')}, "${options.get('classname')}_${options.get('object')}_${options.get('property')}=&form_token=$!{services.csrf.getToken()}"))
261 #attachmentPicker_displayEndFrame ([{'name' : 'select', 'url' : $selectURL}])
262 #end
263 {{/velocity}}
264
265 {{velocity}}
266 #if ($request.docname)
267 #set ($targetDocument = $xwiki.getDocument($request.docname))
268 #if ($request.targetdocname)
269 ## Use the target document if it exists.
270 #set ($targetAttachDocument = $xwiki.getDocument($request.targetdocname))
271 #else
272 ## Otherwise, just use the current document as the target to save/load attachments
273 #set ($targetAttachDocument = $targetDocument)
274 #end
275 #if ("$!{request.savemode}" == 'direct')
276 #set($docAction = 'save')
277 #else
278 #set($docAction = $targetAttachDocument.getDefaultEditMode())
279 #end
280 #set ($filter = [])
281 #set ($rawfilter = '')
282 #if ("$!{request.filter}" != '')
283 #foreach ($value in $request.filter.trim().split('\s*+[,|; ]\s*+'))
284 #if ("$!value" != '')
285 #set ($discard = $filter.add($value.toLowerCase()))
286 #set ($rawfilter = "${rawfilter}, ${value}")
287 #end
288 #end
289 #if ($rawfilter != '')
290 #set ($rawfilter = $rawfilter.substring(2))
291 #end
292 #end
293 #if ("$!{request.displayImage}" == 'true')
294 #set ($displayImage = true)
295 #else
296 #set ($displayImage = false)
297 #end
298 ### Determine attachment sorting
299 #set($sortAttachmentsBy = "$!{request.sortAttachmentsBy}")
300 #set ($validAttachmentProperties = ['filename', 'date', 'filesize', 'author', 'version', 'mimeType'])
301 #if($sortAttachmentsBy == '' || $validAttachmentProperties.indexOf($sortAttachmentsBy) == -1)
302 ### Default to sorting by filename, sort not requested.
303 #set($sortAttachmentsBy = "filename")
304 #end
305 ### Set attachment sorting direction
306 #if($sortAttachmentsBy == 'date')
307 ### Sort the date descending
308 #set($sortAttachmentsBy = "date:desc")
309 #else
310 ### Sort everything else ascending
311 #set($sortAttachmentsBy = "${sortAttachmentsBy}:asc")
312 #end
313 #set ($options = {
314 'classname' : ${request.get('classname')},
315 'object' : $!{mathtool.toInteger($request.object)},
316 'property' : ${request.property},
317 'displayImage' : ${displayImage},
318 'docAction' : ${docAction},
319 'defaultValue' : "$!{request.defaultValue}",
320 'rawfilter': "$!{rawfilter}",
321 'filter': ${filter},
322 'sortAttachmentsBy': ${sortAttachmentsBy},
323 'versionSummary': $request.versionSummary.equals('true')
324 })
325 $!targetDocument.use($targetDocument.getObject($options.classname, $options.object))##
326 #attachmentPicker_displayAttachmentGallery($targetDocument, $targetAttachDocument, $options)
327
superadmin 5.1 328 #set ($cancelLinkName = $services.rendering.escape($services.rendering.escape($services.localization.render("${translationPrefix}.cancel"), 'xwiki/2.1'), 'xwiki/2.1'))
329 #set ($cancelLinkTarget = $services.rendering.escape($services.model.serialize($targetDocument), 'xwiki/2.1'))
superadmin 1.1 330 (% class="gallery_buttons buttons" %)(((
331 (% class="buttonwrapper secondary" %)[[$services.localization.render("${translationPrefix}.cancel")>>$targetDocument||class="button secondary" id="attachment-picker-close"]]
332 )))
333 #end
334 {{/velocity}}