Wiki source code of InvitationCommon

Version 1.1 by Administrator on 2010/05/20 09:16

Show last authors
1 {{velocity}}
2 #*
3 * Invitation Application
4 * This document contains common macros used by all documents in the Invitation Application.
5 *
6 * Macros in this script don't rely on any variables except those which are passed to them and the following:
7 *
8 * $doc the com.xpn.xwiki.api.Document object representing the document containing this code.
9 * $msg the internationalization message provider containing a get(String) and a get(String, List) function
10 * $xcontext the com.xpn.xwiki.Context object for this request
11 * $xwiki an object of the com.xpn.xwiki.api.XWiki class.
12 * $escapetool an object of class org.apache.velocity.tools.generic.EscapeTool
13 * $util used to get a newline character ($util.getNewLine())
14 *
15 * No macros in this script depend on any other macros.
16 *###
17 ##
18 #if($request.getParameter('test') == '1')
19 {{info}}testLoadInvitationConfig{{/info}}
20 #testLoadInvitationConfig()
21 #elseif($doc.getName() == 'InvitationCommon')
22 {{info}}$msg.get("xe.invitation.internalDocument", ["${doc.getSpace()}.WebHome"]){{/info}}
23 #end
24 ##
25 #*
26 * Display a message for the sender (preview) or for the admin (review sent messages)
27 *
28 * $mail (XObject representing email message) the message to view
29 *
30 * $recipients (List<String>) (Optional) the list of email addresses to override the list provided by $mail
31 * used for preview because createMailFromTemplate will exclude addresses
32 * which are invalid and we want to show the invalid addresses to the
33 * user to tell them that they need to correct them.
34 *
35 * $invalidAddresses (List<String>) (Optional) List of invalid email addresses in recipients. Needed if you want to
36 * show the user that they made a mistake on some addresses.
37 * For each email in $invalidEmails, if it is also in $recipients, then it
38 * will be marked but left in the same order, emails in $invlaidEmails but
39 * not found in $recipients will end up at the end of the list.
40 *###
41 #macro(displayMessage, $mail, $recipients, $invalidAddresses)
42
43 {{html wiki=false clean=false}}
44 <div id="invitation-displaymessage" class="invitation">
45 <strong>$msg.get('xe.invitation.previewLabel')</strong>
46 <div class="invitation invitation-preview">
47 #set($recips = [])
48 #set($invalid = [])
49 ## get the lists of valid and invalid email addresses.
50 #if("$!invalidAddresses" == '')
51 #set($invalid = [])
52 #if("$!recipients" == '')
53 #set($recips = [$mail.getProperty('recipient').getValue().trim()])
54 #else
55 #set($discard = $recips.addAll($recipients))
56 #end
57 #else
58 ## Set to local variables to prevent altering input values.
59 #set($discard = $recips.addAll($recipients))
60 #set($discard = $invalid.addAll($invalidAddresses))
61 #end
62 ## Print the email addresses to be sent to.
63 ## To:
64 <strong>$msg.get('xe.invitation.toLabel')</strong>
65 <div id="preview-to-field" class="invitation-preview field">
66 #foreach($recip in $recips)
67 #if($invalid.contains($recip))
68 <span class="invalid-address">$!escapetool.xml($!recip)</span> ##
69 #set($discard = $invalid.remove($recip))
70 #else
71 <span class="valid-address">$!escapetool.xml($!recip)</span> ##
72 #end
73 #end
74 #foreach($recip in $invalid)
75 <span class="invalid-address">$!escapetool.xml($!recip)</span> ##
76 #end
77 &nbsp; ## used to make the field the correct size if it's empty.
78 </div>
79 ## Tell the user that some of the email addresses are invalid.
80 #if($invalidAddresses && $invalidAddresses.size() > 0)
81 <p class="invalid-address-message">
82 <span class="errormessage">
83 #if($recips.size() == 1)
84 ## The email address given is invalid and will not be sent to.
85 $msg.get('xe.invitation.displayMessage.theAddressIsInvalid')
86 #else
87 #if($invalid.size() > 1)
88 $msg.get('xe.invitation.displayMessage.someAddressesAreInvalid', [$invalidAddresses.size()])
89 #else
90 $msg.get('xe.invitation.displayMessage.anAddressesIsInvalid')
91 #end
92 #end
93 </span>
94 </p>
95 #end
96 ## Subject:
97 <strong>$msg.get('xe.invitation.subjectLabel')</strong>
98 <div id="preview-subjectline-field" class="invitation-preview field">
99 $escapetool.xml($mail.getProperty('subjectLine').getValue())
100 </div>
101 ## Message:
102 <strong>$msg.get('xe.invitation.contentLabel')</strong>
103 <div id="preview-messagebody-field" class="invitation-preview field">
104 $mail.getProperty('messageBody').getValue()
105 </div>
106 </div>
107 </div>
108 {{/html}}
109
110 #end
111 ##
112 #*
113 * Load the configuration.
114 * Only works if the script calling this macro has permission to view InvitationConfig
115 * (or create it if it doesn't exist)
116 *
117 * $config (Map<String, String>) will be populated with invitation configuration.
118 *
119 * $configDocName (String) (Optional) will load configuration from this document, if not specified will use
120 * 'InvitationConfig' in the same space as $doc.
121 *
122 * $configClassName (String) (Optional) will load configuration from object of this class, if not specified will use
123 * 'Invitation' in the same space as $doc.
124 *###
125 #macro(loadInvitationConfig, $config, $configDocName, $configClassName)
126 #define($discard)
127 #if("$!configDocName" == '')
128 #set($configDocNameInternal = "${doc.getSpace()}.InvitationConfig")
129 #else
130 #set($configDocNameInternal = $configDocName)
131 #end
132 #if("$!configClassName" == '')
133 #set($configClassNameInternal = "${doc.getSpace()}.WebHome")
134 #else
135 #set($configClassNameInternal = $configClassName)
136 #end
137 ##
138 ## Load some parameters from the configuration.
139 #set($configDoc = $xwiki.getDocumentAsAuthor($configDocNameInternal))
140 ##
141 ## If no configuration document exists, create one and save it.
142 #if($configDoc.isNew())
143 ##
144 ## load the default configuration from this document.
145 #set($thisDocument = $xwiki.getDocumentAsAuthor("${doc.getSpace()}.InvitationCommon"))
146 #set($defaultConfigObj = $thisDocument.getObject($configClassNameInternal))
147 #foreach($element in $defaultConfigObj.getProperties())
148 $config.put($element.getName(), $defaultConfigObj.getProperty($element.getName()).getValue())
149 #end
150 ##
151 #set($configDocContent = '{{velo' + 'city}}{{info}}$msg.get("xe.invitation.internalDocument", ["'
152 + "$!config.get('mainPage')" + '"]){{/info}}{{/velo' + 'city}}')
153 $configDoc.setContent($configDocContent)
154 #set($configObj = $configDoc.newObject($configClassNameInternal))
155 #foreach($key in $config.keySet())
156 $configObj.set($key, $config.get($key))
157 #end
158 ## Now create the configurable objects.
159 #set($cfgable = $configDoc.newObject('XWiki.ConfigurableClass'))
160 $cfgable.set('displayInSection', 'Invitation')
161 $cfgable.set('configurationClass', $configClassNameInternal)
162 $cfgable.set('configureGlobally', 1)
163 #set($propsToShow = 'subjectLineTemplate|messageBodyTemplate|emailRegex|from_address|allowUsersOfOtherWikis'
164 + '|usersMayPersonalizeMessage|usersMaySendToMultiple|emailClass|emailContainer')
165 $cfgable.set('propertiesToShow', $propsToShow)
166 #set($cfgable = $configDoc.newObject('XWiki.ConfigurableClass'))
167 $cfgable.set('displayInSection', 'Invitation')
168 $cfgable.set('heading', '$msg.get("xe.invitation.configuration.smtpHeading")')
169 $cfgable.set('configurationClass', $configClassNameInternal)
170 $cfgable.set('configureGlobally', 1)
171 $cfgable.set('propertiesToShow',
172 'smtp_server_password|smtp_server_username|smtp_port|smtp_server|javamail_extra_props')
173 $configDoc.saveAsAuthor()
174 #else
175 ## load the configuration object...
176 #set($configObj = $configDoc.getObject($configClassNameInternal))
177 #foreach($element in $configObj.getProperties())
178 $config.put($element.getName(), $configObj.getProperty($element.getName()).getValue())
179 #end
180 #end
181 #end## define $discard
182 ## Now invoke the defined code...
183 #set($discard = $discard.toString())
184 #end
185 ##
186 #*
187 * Basic unit testing of the macro above.
188 *###
189 #macro(testLoadInvitationConfig)
190 #set($configDoc = $xwiki.getDocumentAsAuthor("${doc.getSpace()}.InvitationConfig"))
191 #if(!$configDoc.isNew())
192 $configDoc.deleteAsAuthor()
193 #end
194 #set($configClass = $xwiki.getDocumentAsAuthor("${doc.getSpace()}.WebHome"))
195 #if($configClass.isNew())
196 {{error}}Class document [[${doc.getSpace()}.WebHome]] not found. can't run test.{{/error}}
197 #else
198 #set($config = {})
199 #loadInvitationConfig($config, 'HopefullyNonexistantSpace')
200 #if($config.size() < 9)
201 {{error}}Config map too small{{/error}}
202 #end
203 #if($config.get('from_address') != 'no-reply@localhost.localdomain')
204 {{error}}form_address incorrect, expecting "no-reply@localhost.localdomain" got "$config.get('from_address')"{{/error}}
205 #end
206 #set($configDoc = $xwiki.getDocumentAsAuthor("${doc.getSpace()}.InvitationConfig"))
207 #if($configDoc.isNew())
208 {{error}}Config document not created{{/error}}
209 #else
210 #set($configObj = $configDoc.getObject("${doc.getSpace()}.Invitation"))
211 $configObj.set('from_address', 'thisisatest@localhost.localdomain')
212 $configDoc.saveAsAuthor()
213 $config.clear()
214 #loadInvitationConfig($config, 'HopefullyNonexistantSpace')
215 #if($config.get('from_address') != 'thisisatest@localhost.localdomain')
216 {{error}}altering config parameter failed.{{/error}}
217 #end
218 #end
219 #end
220 #set($configDoc = $xwiki.getDocumentAsAuthor("${doc.getSpace()}.InvitationConfig"))
221 #if(!$configDoc.isNew())
222 $configDoc.deleteAsAuthor()
223 #end
224 #end
225 ##
226 #**
227 * Load mail from mail containing document.
228 *
229 * $config (Map<String, String>) will be used to get the name of the email class.
230 *
231 * $emailContainer (Document) the document to get the email from.
232 *
233 * $mail (Map<String, XObject>) will be populated with email message XObjects by their messageID.
234 *###
235 #macro(loadInvitationMail, $config, $emailContainer, $mail)
236 ## If this doesn't already exist, it's created.
237 #if($emailContainer.isNew())
238 #set($emailContainerContent = '{{velo' + 'city}}{{info}}$msg.get("xe.invitation.internalDocument", ["'
239 + "$config.get('emailContainer')" + '"]){{/info}}{{/velo' + 'city}}')
240 #set($discard = $emailContainer.setContent($emailContainerContent))
241 #end
242 ##
243 ## Load messages into a Map by messageID.
244 #foreach($obj in $emailContainer.getObjects($config.get('emailClass')))
245 #set($discard = $mail.put($obj.getProperty('messageID').getValue(), $obj))
246 #end
247 #end
248 ##
249 #**
250 * Is a guest allowed to accept an invitation?
251 *
252 * $guestActionsDoc (Document) the document which guests will use to register.
253 *###
254 #macro(canGuestAcceptInvitation, $guestActionsDoc)
255 #set($out = 'true')
256 #if(!$xwiki.hasAccessLevel('register', 'XWiki.XWikiGuest', 'XWiki.XWikiPreferences')
257 && !$guestActionsDoc.hasProgrammingRights())
258 ##
259 #set($out = 'false')
260 #end
261 $out##
262 #end
263 ##
264 #**
265 * Get the action taken by the user.
266 * This will interpret actions taken using displayActionConfirmationForm
267 *
268 * $parameterMap (Map<String, String>) the parameter map gotten by calling getParameterMap on the servlet request.
269 *
270 * $actionOutList (List<String>) will be populated with a single value (the action)
271 *###
272 #macro(getUserAction, $parameterMap, $actionOutList)
273 #set($actionInternal = 0)
274 #foreach($param in $parameterMap.keySet())
275 #if($param.indexOf('doAction_') != -1)
276 ## Strip 'doAction_'
277 #set($actionInternal = $param.substring(9))
278 #end
279 #end
280 #if($actionInternal != 0)
281 #set($discard = $actionOutList.add($actionInternal))
282 #end
283 #end
284 ##
285 #*
286 * Display a form allowing a user to confirm doing an action.
287 *
288 * $messageIDs (List<String>) the unique IDs of the invitations to act upon.
289 *
290 * $action (String) the action to do.
291 *
292 * $memoLabel (String) what the memo field should be labeled.
293 *
294 * $confirmLabel (String) what the confirm button should say.
295 *
296 * $additionalParameters (Map<String, String>) these parameters will be xml escaped and placed in hidden input fields.
297 *###
298 #macro(displayActionConfirmationForm, $messageIDs, $action, $memoLabel, $confirmLabel, $additionalParameters)
299 {{html wiki=false clean=false}}
300 <div class="invitation action-confirm">
301 <form action="$doc.getURL()" method="POST">
302 ##
303 #foreach($id in $messageIDs)
304 <input type="hidden" name="messageID" value="$escapetool.xml($id)" />
305 #end
306 <input type="hidden" name="confirm" value="y" />
307 #if($additionalParameters && $additionalParameters.size() > 0)
308 #foreach($param in $additionalParameters.keySet())
309 <input type="hidden" name="$escapetool.xml($param)" value="$escapetool.xml($additionalParameters.get($param))" />
310 #end
311 #end
312 ##
313 <dl>
314 <dt><label for="memo">$memoLabel</label></dt>
315 <dd><input type="text" size="54" name="memo" /></dd>
316 </dl>
317 <div class="bottombuttons">
318 <div class="buttons">
319 <span class="buttonwrapper">
320 <input type="submit" class="button" name="doAction_$escapetool.xml($action)" value="$confirmLabel" />
321 </span>
322 </div>
323 </div>
324 </form>
325 </div>
326 {{/html}}
327 #end
328 ##
329 #*
330 * Set the status of a message and log the memo.
331 *
332 * $message (Xobject) the message to act on.
333 *
334 * $status (String) the status to set the message to.
335 *
336 * $memo (String) what to enter in the log.
337 *###
338 #macro(setMessageStatus, $message, $status, $memo)
339 $message.set('status', $status)##
340 #set($history = $message.getProperty('history').getValue())
341 #set($statusWord = "#messageStatusForCode($status)")
342 ## disallow injection of \n which starts a new line or { which may be used for macros.
343 #set($log = $memo.replaceAll('\n', ' ').replaceAll('\{', '~{'))
344 #set($entry = "|$statusWord|[[$xcontext.getUser()]]|$!log")
345 #if("$!history" != '')
346 $message.set('history', "$!history$util.getNewline()$entry")##
347 #else
348 $message.set('history', $entry)##
349 #end
350 #end
351 ##
352 #*
353 * Get the last memo from the message history.
354 *
355 * $message (Xobject) the message to act on.
356 *###
357 #macro(getLastMemo, $message)
358 #set($history = "$!message.getProperty('history').getValue()")
359 #set($indexAfterLastNewline = $mathtool.add($history.lastIndexOf($util.getNewline()), 1))
360 #if($indexAfterLastNewline < $history.length())
361 #set($entry = "$!history.substring($indexAfterLastNewline)")
362 #set($indexAfterLastPipe = $mathtool.add($entry.lastIndexOf('|'), 1))
363 #if($indexAfterLastPipe < $entry.length())
364 $entry.substring($indexAfterLastPipe)##
365 #end
366 #end
367 #end
368 ##
369 #*
370 * Get the status of the current message for it's code.
371 * unsent - not sent yet.
372 * pending - sent and awating a response
373 * accepted - sent and accepted by recipient
374 * declined - sent and declined by recipient
375 * canceled - sent then canceled by sender
376 * reported - as spam (by recipient)
377 * notSpam - reported as spam and investigated (by admin)
378 * sendingFailed - failed to send message
379 * else - unknown status
380 *
381 * $status (String) the status of the message.
382 *###
383 #macro(messageStatusForCode, $status)
384 #if($status == 'unsent')
385 $msg.get('xe.invitation.messageStatus.unsent')##
386 #elseif($status == 'pending')
387 $msg.get('xe.invitation.messageStatus.pending')##
388 #elseif($status == 'accepted')
389 $msg.get('xe.invitation.messageStatus.accepted')##
390 #elseif($status == 'declined')
391 $msg.get('xe.invitation.messageStatus.declined')##
392 #elseif($status == 'canceled')
393 $msg.get('xe.invitation.messageStatus.canceled')##
394 #elseif($status == 'reported')
395 $msg.get('xe.invitation.messageStatus.reported')##
396 #elseif($status == 'notSpam')
397 $msg.get('xe.invitation.messageStatus.investigated')##
398 #elseif($status == 'sendingFailed')
399 $msg.get('xe.invitation.messageStatus.sendingFailed')##
400 #else
401 $msg.get('xe.invitation.messageStatus.unknown', [$escapetool.xml($status)])##
402 #end
403 #end
404 {{/velocity}}