General Actions:
Log-in
Register
Wiki:
games
▼
:
Document Index
»
Space:
Invitation
▼
:
Document Index
»
Page:
InvitationCommon
Search
en
Page Actions:
Export
▼
:
Export as PDF
Export as RTF
Export as HTML
More actions
▼
:
Print preview
View Source
Invitation.WebHome
»
InvitationCommon
Wiki source code of
InvitationCommon
Last modified by
Administrator
on 2011/11/06 20:09
Content
·
Comments
(0)
·
Annotations
(0)
·
Attachments
(0)
·
History
·
Information
Show line numbers
{{velocity}} #* * Invitation Application * This document contains common macros used by all documents in the Invitation Application. * * Macros in this script don't rely on any variables except those which are passed to them and the following: * * $doc the com.xpn.xwiki.api.Document object representing the document containing this code. * $msg the internationalization message provider containing a get(String) and a get(String, List) function * $xcontext the com.xpn.xwiki.Context object for this request * $xwiki an object of the com.xpn.xwiki.api.XWiki class. * $escapetool an object of class org.apache.velocity.tools.generic.EscapeTool * $util used to get a newline character ($util.getNewLine()) * * No macros in this script depend on any other macros. *### ## #if($request.getParameter('test') == '1') {{info}}testLoadInvitationConfig{{/info}} #testLoadInvitationConfig() #elseif($doc.getName() == 'InvitationCommon') {{info}}$msg.get("xe.invitation.internalDocument", ["${doc.getSpace()}.WebHome"]){{/info}} #end ## #* * Display a message for the sender (preview) or for the admin (review sent messages) * * $mail (XObject representing email message) the message to view * * $recipients (List<String>) (Optional) the list of email addresses to override the list provided by $mail * used for preview because createMailFromTemplate will exclude addresses * which are invalid and we want to show the invalid addresses to the * user to tell them that they need to correct them. * * $invalidAddresses (List<String>) (Optional) List of invalid email addresses in recipients. Needed if you want to * show the user that they made a mistake on some addresses. * For each email in $invalidEmails, if it is also in $recipients, then it * will be marked but left in the same order, emails in $invlaidEmails but * not found in $recipients will end up at the end of the list. *### #macro(displayMessage, $mail, $recipients, $invalidAddresses) {{html wiki=false clean=false}} <div id="invitation-displaymessage" class="invitation"> <strong>$msg.get('xe.invitation.previewLabel')</strong> <div class="invitation invitation-preview"> #set($recips = []) #set($invalid = []) ## get the lists of valid and invalid email addresses. #if("$!invalidAddresses" == '') #set($invalid = []) #if("$!recipients" == '') #set($recips = [$mail.getProperty('recipient').getValue().trim()]) #else #set($discard = $recips.addAll($recipients)) #end #else ## Set to local variables to prevent altering input values. #set($discard = $recips.addAll($recipients)) #set($discard = $invalid.addAll($invalidAddresses)) #end ## Print the email addresses to be sent to. ## To: <strong>$msg.get('xe.invitation.toLabel')</strong> <div id="preview-to-field" class="invitation-preview field"> #foreach($recip in $recips) #if($invalid.contains($recip)) <span class="invalid-address">$!escapetool.xml($!recip)</span> ## #set($discard = $invalid.remove($recip)) #else <span class="valid-address">$!escapetool.xml($!recip)</span> ## #end #end #foreach($recip in $invalid) <span class="invalid-address">$!escapetool.xml($!recip)</span> ## #end ## used to make the field the correct size if it's empty. </div> ## Tell the user that some of the email addresses are invalid. #if($invalidAddresses && $invalidAddresses.size() > 0) <p class="invalid-address-message"> <span class="errormessage"> #if($recips.size() == 1) ## The email address given is invalid and will not be sent to. $msg.get('xe.invitation.displayMessage.theAddressIsInvalid') #else #if($invalid.size() > 1) $msg.get('xe.invitation.displayMessage.someAddressesAreInvalid', [$invalidAddresses.size()]) #else $msg.get('xe.invitation.displayMessage.anAddressesIsInvalid') #end #end </span> </p> #end ## Subject: <strong>$msg.get('xe.invitation.subjectLabel')</strong> <div id="preview-subjectline-field" class="invitation-preview field"> $escapetool.xml($mail.getProperty('subjectLine').getValue()) </div> ## Message: <strong>$msg.get('xe.invitation.contentLabel')</strong> <div id="preview-messagebody-field" class="invitation-preview field"> $mail.getProperty('messageBody').getValue() </div> </div> </div> {{/html}} #end ## #* * Load the configuration. * Only works if the script calling this macro has permission to view InvitationConfig * (or create it if it doesn't exist) * * $config (Map<String, String>) will be populated with invitation configuration. * * $configDocName (String) (Optional) will load configuration from this document, if not specified will use * 'InvitationConfig' in the same space as $doc. * * $configClassName (String) (Optional) will load configuration from object of this class, if not specified will use * 'Invitation' in the same space as $doc. *### #macro(loadInvitationConfig, $config, $configDocName, $configClassName) #define($discard) #if("$!configDocName" == '') #set($configDocNameInternal = "${doc.getSpace()}.InvitationConfig") #else #set($configDocNameInternal = $configDocName) #end #if("$!configClassName" == '') #set($configClassNameInternal = "${doc.getSpace()}.WebHome") #else #set($configClassNameInternal = $configClassName) #end ## ## Load some parameters from the configuration. #set($configDoc = $xwiki.getDocumentAsAuthor($configDocNameInternal)) ## ## If no configuration document exists, create one and save it. #if($configDoc.isNew()) ## ## load the default configuration from this document. #set($thisDocument = $xwiki.getDocumentAsAuthor("${doc.getSpace()}.InvitationCommon")) #set($defaultConfigObj = $thisDocument.getObject($configClassNameInternal)) #foreach($element in $defaultConfigObj.getProperties()) $config.put($element.getName(), $defaultConfigObj.getProperty($element.getName()).getValue()) #end ## #set($configDocContent = '{{velo' + 'city}}{{info}}$msg.get("xe.invitation.internalDocument", ["' + "$!config.get('mainPage')" + '"]){{/info}}{{/velo' + 'city}}') $configDoc.setContent($configDocContent) $configDoc.setParent($configClassNameInternal) #set($configObj = $configDoc.newObject($configClassNameInternal)) #foreach($key in $config.keySet()) $configObj.set($key, $config.get($key)) #end ## Now create the configurable objects. #set($cfgable = $configDoc.newObject('XWiki.ConfigurableClass')) $cfgable.set('displayInSection', 'Invitation') $cfgable.set('configurationClass', $configClassNameInternal) $cfgable.set('configureGlobally', 1) #set($propsToShow = 'subjectLineTemplate|messageBodyTemplate|emailRegex|from_address|allowUsersOfOtherWikis' + '|usersMayPersonalizeMessage|usersMaySendToMultiple|emailClass|emailContainer') $cfgable.set('propertiesToShow', $propsToShow) #set($cfgable = $configDoc.newObject('XWiki.ConfigurableClass')) $cfgable.set('displayInSection', 'Invitation') $cfgable.set('heading', '$msg.get("xe.invitation.configuration.smtpHeading")') $cfgable.set('configurationClass', $configClassNameInternal) $cfgable.set('configureGlobally', 1) $cfgable.set('propertiesToShow', 'smtp_server_password|smtp_server_username|smtp_port|smtp_server|javamail_extra_props') $configDoc.saveAsAuthor() #else ## load the configuration object... #set($configObj = $configDoc.getObject($configClassNameInternal)) #foreach($element in $configObj.getProperties()) $config.put($element.getName(), $configObj.getProperty($element.getName()).getValue()) #end #end #end## define $discard ## Now invoke the defined code... #set($discard = $discard.toString()) #end ## #* * Basic unit testing of the macro above. *### #macro(testLoadInvitationConfig) #set($configDoc = $xwiki.getDocumentAsAuthor("${doc.getSpace()}.InvitationConfig")) #if(!$configDoc.isNew()) $configDoc.deleteAsAuthor() #end #set($configClass = $xwiki.getDocumentAsAuthor("${doc.getSpace()}.WebHome")) #if($configClass.isNew()) {{error}}Class document [[${doc.getSpace()}.WebHome]] not found. can't run test.{{/error}} #else #set($config = {}) #loadInvitationConfig($config, 'HopefullyNonexistantSpace') #if($config.size() < 9) {{error}}Config map too small{{/error}} #end #if($config.get('from_address') != 'no-reply@localhost.localdomain') {{error}}form_address incorrect, expecting "no-reply@localhost.localdomain" got "$config.get('from_address')"{{/error}} #end #set($configDoc = $xwiki.getDocumentAsAuthor("${doc.getSpace()}.InvitationConfig")) #if($configDoc.isNew()) {{error}}Config document not created{{/error}} #else #set($configObj = $configDoc.getObject("${doc.getSpace()}.Invitation")) $configObj.set('from_address', 'thisisatest@localhost.localdomain') $configDoc.saveAsAuthor() $config.clear() #loadInvitationConfig($config, 'HopefullyNonexistantSpace') #if($config.get('from_address') != 'thisisatest@localhost.localdomain') {{error}}altering config parameter failed.{{/error}} #end #end #end #set($configDoc = $xwiki.getDocumentAsAuthor("${doc.getSpace()}.InvitationConfig")) #if(!$configDoc.isNew()) $configDoc.deleteAsAuthor() #end #end ## #** * Load mail from mail containing document. * * $config (Map<String, String>) will be used to get the name of the email class. * * $emailContainer (Document) the document to get the email from. * * $mail (Map<String, XObject>) will be populated with email message XObjects by their messageID. *### #macro(loadInvitationMail, $config, $emailContainer, $mail) ## If this doesn't already exist, it's created. #if($emailContainer.isNew()) #set($emailContainerContent = '{{velo' + 'city}}{{info}}$msg.get("xe.invitation.internalDocument", ["' + "$config.get('emailContainer')" + '"]){{/info}}{{/velo' + 'city}}') #set($discard = $emailContainer.setContent($emailContainerContent)) #end ## ## Load messages into a Map by messageID. #foreach($obj in $emailContainer.getObjects($config.get('emailClass'))) #set($discard = $mail.put($obj.getProperty('messageID').getValue(), $obj)) #end #end ## #** * Is a guest allowed to accept an invitation? * * $guestActionsDoc (Document) the document which guests will use to register. *### #macro(canGuestAcceptInvitation, $guestActionsDoc) #set($out = 'true') #if(!$xwiki.hasAccessLevel('register', 'XWiki.XWikiGuest', 'XWiki.XWikiPreferences') && !$guestActionsDoc.hasProgrammingRights()) ## #set($out = 'false') #end $out## #end ## #** * Get the action taken by the user. * This will interpret actions taken using displayActionConfirmationForm * * $parameterMap (Map<String, String>) the parameter map gotten by calling getParameterMap on the servlet request. * * $actionOutList (List<String>) will be populated with a single value (the action) *### #macro(getUserAction, $parameterMap, $actionOutList) #set($actionInternal = 0) #foreach($param in $parameterMap.keySet()) #if($param.indexOf('doAction_') != -1) ## Strip 'doAction_' #set($actionInternal = $param.substring(9)) #end #end #if($actionInternal != 0) #set($discard = $actionOutList.add($actionInternal)) #end #end ## #* * Display a form allowing a user to confirm doing an action. * * $messageIDs (List<String>) the unique IDs of the invitations to act upon. * * $action (String) the action to do. * * $memoLabel (String) what the memo field should be labeled. * * $confirmLabel (String) what the confirm button should say. * * $additionalParameters (Map<String, String>) these parameters will be xml escaped and placed in hidden input fields. *### #macro(displayActionConfirmationForm, $messageIDs, $action, $memoLabel, $confirmLabel, $additionalParameters) {{html wiki=false clean=false}} <div class="invitation action-confirm"> <form action="$doc.getURL()" method="POST"> ## #foreach($id in $messageIDs) <input type="hidden" name="messageID" value="$escapetool.xml($id)" /> #end <input type="hidden" name="confirm" value="y" /> <input type="hidden" name="form_token" value="$!{services.csrf.getToken()}" /> #if($additionalParameters && $additionalParameters.size() > 0) #foreach($param in $additionalParameters.keySet()) <input type="hidden" name="$escapetool.xml($param)" value="$escapetool.xml($additionalParameters.get($param))" /> #end #end ## <dl> <dt><label for="memo">$memoLabel</label></dt> <dd><input type="text" size="54" name="memo" /></dd> </dl> <div class="bottombuttons"> <div class="buttons"> <span class="buttonwrapper"> <input type="submit" class="button" name="doAction_$escapetool.xml($action)" value="$confirmLabel" /> </span> </div> </div> </form> </div> {{/html}} #end ## #* * Set the status of a message and log the memo. * * $message (Xobject) the message to act on. * * $status (String) the status to set the message to. * * $memo (String) what to enter in the log. *### #macro(setMessageStatus, $message, $status, $memo) $message.set('status', $status)## #set($history = $message.getProperty('history').getValue()) #set($statusWord = "#messageStatusForCode($status)") ## disallow injection of \n which starts a new line or { which may be used for macros. #set($log = $memo.replaceAll('\n', ' ').replaceAll('\{', '~{')) #set($entry = "|$statusWord|[[$xcontext.getUser()]]|$!log") #if("$!history" != '') $message.set('history', "$!history$util.getNewline()$entry")## #else $message.set('history', $entry)## #end #end ## #* * Get the last memo from the message history. * * $message (Xobject) the message to act on. *### #macro(getLastMemo, $message) #set($history = "$!message.getProperty('history').getValue()") #set($indexAfterLastNewline = $mathtool.add($history.lastIndexOf($util.getNewline()), 1)) #if($indexAfterLastNewline < $history.length()) #set($entry = "$!history.substring($indexAfterLastNewline)") #set($indexAfterLastPipe = $mathtool.add($entry.lastIndexOf('|'), 1)) #if($indexAfterLastPipe < $entry.length()) $entry.substring($indexAfterLastPipe)## #end #end #end ## #* * Get the status of the current message for it's code. * unsent - not sent yet. * pending - sent and awating a response * accepted - sent and accepted by recipient * declined - sent and declined by recipient * canceled - sent then canceled by sender * reported - as spam (by recipient) * notSpam - reported as spam and investigated (by admin) * sendingFailed - failed to send message * else - unknown status * * $status (String) the status of the message. *### #macro(messageStatusForCode, $status) #if($status == 'unsent') $msg.get('xe.invitation.messageStatus.unsent')## #elseif($status == 'pending') $msg.get('xe.invitation.messageStatus.pending')## #elseif($status == 'accepted') $msg.get('xe.invitation.messageStatus.accepted')## #elseif($status == 'declined') $msg.get('xe.invitation.messageStatus.declined')## #elseif($status == 'canceled') $msg.get('xe.invitation.messageStatus.canceled')## #elseif($status == 'reported') $msg.get('xe.invitation.messageStatus.reported')## #elseif($status == 'notSpam') $msg.get('xe.invitation.messageStatus.investigated')## #elseif($status == 'sendingFailed') $msg.get('xe.invitation.messageStatus.sendingFailed')## #else $msg.get('xe.invitation.messageStatus.unknown', [$escapetool.xml($status)])## #end #end {{/velocity}}