Version 1.1 by Ludovic Dubost on 2014/03/30 19:46

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