Version 5.1 by Ludovic Dubost on 2017/03/08 18:02

Show last authors
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
45 #if ($xwiki.hasAccessLevel('edit',$xcontext.user,${targetAttachDocument.fullName}))
46 #attachmentPicker_displayUploadForm($targetDocument, $targetAttachDocument, $options)
47 #end
48 #attachmentPicker_displayAttachmentGalleryEmptyValue($targetDocument, $targetAttachDocument, $options, $currentValue)
49 #set ($sortedAttachments = $sorttool.sort($targetAttachDocument.getAttachmentList(), "${options.sortAttachmentsBy}") )
50 #foreach ($attachment in $sortedAttachments)
51 #set ($extension = $attachment.getFilename())
52 #set ($extension = $extension.substring($mathtool.add($extension.lastIndexOf('.'), 1)).toLowerCase())
53 #if ($options.filter.size() == 0 || $options.filter.contains($extension))
54 #attachmentPicker_displayAttachmentBox($attachment $targetDocument $targetAttachDocument, $options $currentValue)
55 #end
56 #end
57 )))
58 #end
59
60 #**
61 * Displays an attachment box.
62 *
63 * @param $attachment the target attachment to display
64 * @param $targetDocument the document being modified
65 * @param $options generic picker options
66 * @param $currentValue the currently selected file, used for determining if the box should be highlighted as the current value
67 *#
68 #macro (attachmentPicker_displayAttachmentBox $attachment $targetDocument $targetAttachDocument, $options $currentValue)
69 #if ($options.displayImage && $attachment.isImage())
70 #set ($cssClass = 'gallery_image')
71 #else
72 #set ($cssClass = '')
73 #end
74 #attachmentPicker_displayStartFrame({'value' : $attachment.filename, 'text' : $attachment.filename, 'cssClass' : "$!{cssClass}"} $currentValue)
75 #attachmentPicker_displayAttachmentDetails($attachment $options)
76 #set ($returnURL = $escapetool.url($doc.getURL('view', $request.queryString)))
77 #set ($deleteURL = $targetAttachDocument.getAttachmentURL($attachment.filename, 'delattachment', "xredirect=${returnURL}&form_token=$!{services.csrf.getToken()}") )
78 #set ($viewURL = $targetAttachDocument.getAttachmentURL($attachment.filename) )##{'name' : 'download', 'url' : $viewURL, 'rel' : '__blank'}
79 #set ($selectURL = $targetDocument.getURL(${options.get('docAction')}, "${options.get('classname')}_${options.get('object')}_${options.get('property')}=${attachment.filename}&form_token=$!{services.csrf.getToken()}"))
80 #attachmentPicker_displayEndFrame ([{'name' : 'select', 'url' : $selectURL}, {'name' : 'delete', 'url' : $deleteURL}])
81 #end
82
83 #**
84 * Writes the wiki code used at the start of an attachment box. Outputs the attachment title bar, and opens the inner frame div.
85 *
86 * @param $boxOptions a map of parameters/options for the current attachment, holding, for example, the attachment name (boxOptions.value),
87 * the title to display (boxOptions.text), optional extra CSS classnames to put on the box (boxOptions.cssClass)
88 * @param $currentValue the currently selected file, used for determining if this attachment should be highlighted as the current value
89 *#
90 #macro (attachmentPicker_displayStartFrame $boxOptions $currentValue)
91 (% class="gallery_attachmentbox $!{boxOptions.cssClass} #if ("$!{boxOptions.value}" == $currentValue) current#{end}" %)(((
92 (% class="gallery_attachmenttitle" title="$!{boxOptions.value}" %)((($boxOptions.text)))
93 (% class="gallery_attachmentframe" %)(((
94 #end
95
96 #**
97 * Displays details about an attachment inside the attachment box. If the attachment is an image and the "displayImage" option is on,
98 * then the image is displayed. Otherwise, some basic information is displayed: the version, the size, the date and the author.
99 *
100 * @param $attachment the target attachment to display
101 * @param $options generic picker options
102 *#
103 #macro (attachmentPicker_displayAttachmentDetails $attachment $options)
104 #if ($attachment)
105 #if ($attachment.isImage() && $options.displayImage)
106 #set ($attachmentDocument = $attachment.getDocument())
107 [[[[image:${attachmentDocument.fullName}@${attachment.filename}||width=180]]>>attach:${attachmentDocument.fullName}@${attachment.filename}||rel="lightbox[attachments]"]]
108 #else
109 * (% class="mime" %){{html wiki=false clean=false}}#mimetypeimg($attachment.getMimeType().toLowerCase() $attachment.getFilename().toLowerCase()){{/html}}(%%) (% class="filename" %)$attachment.getFilename()(% %)
110 * v$attachment.getVersion() (#dynamicsize($attachment.longSize))
111 * $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')}])
112 * (% class="buttonwrapper" %)[[${services.localization.render("${translationPrefix}.actions.download")}>>attach:${attachment.getDocument()}@${attachment.filename}||title="$services.localization.render("${translationPrefix}.actions.download")" rel="__blank" class="button"]](%%)
113 #end
114 #end
115 #end
116
117 #**
118 * Writes the wiki code used at the end of an attachment box. Closes the inner frame div, and outputs the attachment actions.
119 *
120 * @param $actions a list of maps defining action buttons, where each entry contains a subset of the following:
121 * <dl>
122 * <dt>name</dt>
123 * <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>
124 * <dt>url</dt>
125 * <dd>the destination of the button</dd>
126 * <dt>rel</dt>
127 * <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>
128 * </dl>
129 *#
130 #macro (attachmentPicker_displayEndFrame $actions)
131 )))## attachmentframe
132 (% class="gallery_actions" %)(((
133 #foreach ($action in $actions)
134 #set( $actionname = $services.localization.render("${translationPrefix}.actions.${action.name}") )
135 [[${actionname}>>path:${action.url}||class="tool ${action.name}" title="${actionname}" #if($action.rel) rel="${action.rel}"#end]]##
136 #end
137 )))## actions
138 )))## attachmentbox
139 #end
140
141 #**
142 * Displays the upload box used for adding and selecting a new attachment.
143 *
144 * @param $targetDocument the document with the property being modified
145 * @param $targetAttachDocument the document to upload the attachment to
146 * @param $options generic picker options
147 *#
148 #macro (attachmentPicker_displayUploadForm $targetDocument, $targetAttachDocument, $options)
149 #attachmentPicker_displayStartFrame({
150 'value' : $services.localization.render("${translationPrefix}.upload.title"),
151 'text' : $services.localization.render("${translationPrefix}.upload.title"),
152 'cssClass' : 'gallery_upload'
153 } $NULL)
154 {{html clean="false"}}
155 <form action="$targetAttachDocument.getURL('upload')" enctype="multipart/form-data" method="post" id="uploadAttachment" class="uploadAttachment xform">
156 <div class="gallery_upload_input">
157 #if (${options.rawfilter} != '')
158 <span class="xHint">$escapetool.xml($services.localization.render("${translationPrefix}.upload.hint", [${options.rawfilter}]))</span>
159 #end
160 <input type="file" name="filepath" id="attachfile" class="attachment" size="30" title="$!{escapetool.xml($options.rawfilter)}"/>
161 <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()}")" />
162 <input type="hidden" name="docname" value="$!{escapetool.xml($targetDocument.fullName)}" />
163 <input type="hidden" name="classname" value="$!{escapetool.xml($options.get('classname'))}" />
164 <input type="hidden" name="object" value="$!{escapetool.xml($options.get('object'))}" />
165 <input type="hidden" name="property" value="$!{escapetool.xml($options.get('property'))}" />
166 <input type="hidden" name="form_token" value="$!{services.csrf.getToken()}" />
167 </div>
168 <div class="buttons">
169 <span class="buttonwrapper">
170 <input type="submit" name="action_upload" class="button " value='$services.localization.render("${translationPrefix}.upload.submit")' title='$services.localization.render("${translationPrefix}.upload.submit")'/>
171 </span>
172 </div>
173 </form>
174 {{/html}}
175 #attachmentPicker_displayEndFrame ([])
176 #end
177
178 #**
179 * Displays the "empty value" box, used for unsetting the field value.
180 *
181 * @param $targetDocument the document being modified
182 * @param $targetAttachDocument the document that the attachments will the loaded from/saved to
183 * @param $options generic picker options
184 * @param $currentValue the currently selected file, used for determining if the empty box should be highlighted as the current value
185 *#
186 #macro (attachmentPicker_displayAttachmentGalleryEmptyValue $targetDocument, $targetAttachDocument, $options, $currentValue)
187 #if ("$!{options.get('defaultValue')}" != '')
188 #set ($reference = ${options.get('defaultValue')})
189 #set ($docNameLimit = $reference.indexOf('@'))
190 #if ($docNameLimit > 0)
191 #set ($docName = $reference.substring(0, $docNameLimit))
192 #else
193 #set ($docName = $targetAttachDocument.fullName)
194 #end
195 #set ($attachmentName = $reference.substring($mathtool.add($docNameLimit, 1)))
196 #set ($defaultAttachment = $xwiki.getDocument($docName).getAttachment($attachmentName))
197 #if ($defaultAttachment.isImage())
198 #set($dcssClass = 'gallery_image')
199 #end
200 #end
201 #attachmentPicker_displayStartFrame({'cssClass' : "gallery_emptyChoice $!{dcssClass}", 'text' : $services.localization.render("${translationPrefix}.default"), 'value' : "${options.defaultValue}"} $currentValue)
202 #attachmentPicker_displayAttachmentDetails($defaultAttachment $options)
203 #set ($returnURL = $escapetool.url($doc.getURL('view', $request.queryString)))
204 #set ($selectURL = $targetDocument.getURL(${options.get('docAction')}, "${options.get('classname')}_${options.get('object')}_${options.get('property')}=&form_token=$!{services.csrf.getToken()}"))
205 #attachmentPicker_displayEndFrame ([{'name' : 'select', 'url' : $selectURL}])
206 #end
207 {{/velocity}}
208
209 {{velocity}}
210 #if ($request.docname)
211 #set ($targetDocument = $xwiki.getDocument($request.docname))
212 #if ($request.targetdocname)
213 ## Use the target document if it exists.
214 #set ($targetAttachDocument = $xwiki.getDocument($request.targetdocname))
215 #else
216 ## Otherwise, just use the current document as the target to save/load attachments
217 #set ($targetAttachDocument = $targetDocument)
218 #end
219 #if ("$!{request.savemode}" == 'direct')
220 #set($docAction = 'save')
221 #else
222 #set($docAction = $targetAttachDocument.getDefaultEditMode())
223 #end
224 #set ($filter = [])
225 #set ($rawfilter = '')
226 #if ("$!{request.filter}" != '')
227 #foreach ($value in $request.filter.trim().split('\s*+[,|; ]\s*+'))
228 #if ("$!value" != '')
229 #set ($discard = $filter.add($value.toLowerCase()))
230 #set ($rawfilter = "${rawfilter}, ${value}")
231 #end
232 #end
233 #if ($rawfilter != '')
234 #set ($rawfilter = $rawfilter.substring(2))
235 #end
236 #end
237 #if ("$!{request.displayImage}" == 'true')
238 #set ($displayImage = true)
239 #else
240 #set ($displayImage = false)
241 #end
242 ### Determine attachment sorting
243 #set($sortAttachmentsBy = "$!{request.sortAttachmentsBy}")
244 #set ($validAttachmentProperties = ['filename', 'date', 'filesize', 'author', 'version', 'mimeType'])
245 #if($sortAttachmentsBy == '' || $validAttachmentProperties.indexOf($sortAttachmentsBy) == -1)
246 ### Default to sorting by filename, sort not requested.
247 #set($sortAttachmentsBy = "filename")
248 #end
249 ### Set attachment sorting direction
250 #if($sortAttachmentsBy == 'date')
251 ### Sort the date descending
252 #set($sortAttachmentsBy = "date:desc")
253 #else
254 ### Sort everything else ascending
255 #set($sortAttachmentsBy = "${sortAttachmentsBy}:asc")
256 #end
257 #set ($options = {
258 'classname' : ${request.get('classname')},
259 'object' : $!{mathtool.toInteger($request.object)},
260 'property' : ${request.property},
261 'displayImage' : ${displayImage},
262 'docAction' : ${docAction},
263 'defaultValue' : "$!{request.defaultValue}",
264 'rawfilter': "$!{rawfilter}",
265 'filter': ${filter},
266 'sortAttachmentsBy': ${sortAttachmentsBy}
267 })
268 $!targetDocument.use($targetDocument.getObject($options.classname, $options.object))##
269 #attachmentPicker_displayAttachmentGallery($targetDocument, $targetAttachDocument, $options)
270
271 (% class="gallery_buttons buttons" %)(((
272 (% class="buttonwrapper secondary" %)[[$services.localization.render("${translationPrefix}.cancel")>>$targetDocument||class="button secondary" id="attachment-picker-close"]]
273 )))
274 #end
275 {{/velocity}}