Version 7.1 by teamwire004 on 2024/07/12 13:05

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