Wiki source code of Attachments

Last modified by Admin on 2023/04/25 09:50

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