General Actions:
Log-in
Register
Wiki:
games
▼
:
Document Index
»
Space:
XWiki
▼
:
Document Index
»
Page:
ConfigurableClassMacros
Search
en
Page Actions:
Export
▼
:
Export as PDF
Export as RTF
Export as HTML
More actions
▼
:
Print preview
View Source
Wiki Home
»
XWiki Space
»
Custom configurable sections
»
Macros for custom configurable sections
Wiki source code of
Macros for custom configurable sections
Last modified by
Administrator
on 2011/11/06 20:09
Content
·
Comments
(0)
·
Annotations
(0)
·
Attachments
(0)
·
History
·
Information
Show line numbers
{{velocity output="false"}} ## Constants: #set($redirectParameter = 'xredirect') #set($nameOfThisDocument = 'XWiki.ConfigurableClass') #* * Any documents which are on the provided list ($documentNames) which are locked by the current user will be unlocked. * If this macro has programming rights, then they are unlocked programmatically, otherwise a javascript tag is * generated with ajax calls to cancel for all of the documents. If there are documents on this list which are not * locked by the current user, then they are ignored. * * @param $documentNames - List<String> - fullNames of documents which should be unlocked if they are locked by the * current user. *### #macro(unlockDocuments $documentNames) #if($documentNames.size() > 0) #set($sql = "doc.fullName=") #foreach($documentName in $documentNames) #set($sql = "${sql}'$documentName' or doc.fullName=") #end ## Trim the dangling ' or doc.fullName=?' #set($sql = $sql.substring(0, $sql.lastIndexOf(' or doc.fullName='))) #set($sql = ", XWikiLock lock where lock.docId=doc.id and lock.userName='$xcontext.getUser()' and (${sql})") #set($namesOfdocumentsToUnlock = $xwiki.searchDocuments($sql)) ## Use ajax and hope the user runs javascript. {{html}} <script type="text/javascript"> document.observe("dom:loaded", function() { #foreach($nameOflockedDocument in $namesOfdocumentsToUnlock) new Ajax.Request("$xwiki.getURL($nameOflockedDocument, 'cancel', 'ajax=1')"); #end }); </script> {{/html}} #end## If output list size > 0 #end #* * Try to determine whether a document was edited by a user who has edit right on this page. This is tricky because * documents are imported with the name XWiki.XWikiGuest who has no access to anything after import. * * @param theDoc - Document who's editor should be checked for edit access on this document. *### #macro(checkDocumentSavedByAuthorizedUser, $docToCheck, $currentDoc, $hasAccess) ## The system is started and the only user is XWikiGuest who has admin right but gives it up when he imports the default ## documents, we are checking to see if this looks like the guest imported the document with the first import. #if($docToCheck.getWiki() == $xcontext.getMainWikiName() && $docToCheck.getVersion() == '1.1' && $docToCheck.getCreator() != $docToCheck.getContentAuthor() && $docToCheck.getContentAuthor() == 'XWiki.XWikiGuest') ## #set($userToCheck = $docToCheck.getCreator()) #else #set($userToCheck = $docToCheck.getAuthor()) #end #set ($hasAccess = $util.null) #setVariable ("$hasAccess" $xwiki.hasAccessLevel('edit', $userToCheck, $currentDoc)) #end #* * Find names of documents which contain objects of the class 'XWiki.ConfigurableClass' * * @param $section - String - Look for apps which specify that they should be configured in this section, * if null or "" then returns them for all sections. * * @param $globaladmin - boolean - If true then we will look for applications which should be configured globally. * * @param $space - String - If not looking for apps which are configured globally, then this is the space where we * will look for apps in. If null or "" or if $globaladmin is true, then all spaces will be * searched. * * @param $outputList - List - The returns from this macro will be put in this list, passing the list as a parameter * a safety measure because macros can't return values. *### #macro(findNamesOfAppsToConfigure, $section, $globaladmin, $space, $outputList) ## Use a parameterized sql query to prevent injection. #set($params = []) #if($section && $section != '') #set($discard = $params.add("$section")) #set($sqlA = ' StringProperty as section,') #set($sqlB = " and section.id=obj.id and section.name='displayInSection' and section.value=?") #else ## Make sure they are "" in case they were set prior to calling the macro. #set($sqlA = '') #set($sqlB = '') #end ## Set up query based on value of $globaladmin #if($globaladmin == true) #set($sqlC = '1') #else #if($space && $space != '') #set($sqlC = '0 and doc.space = ?') #set($discard = $params.add($space)) #else #set($sqlC = '0') #end #end #set($sql = ", BaseObject as obj,$sqlA IntegerProperty as global where " + "doc.fullName=obj.name and obj.className='" + $nameOfThisDocument + "'$sqlB " + "and global.id=obj.id and global.name='configureGlobally' and global.value=$sqlC " + "order by doc.creationDate") ## ## Run the search #set($discard = $outputList.addAll($xwiki.searchDocuments($sql, 0, 0, $params))) ## #end #macro(findCustomSectionsToConfigure $adminMenu) #set ($outputList = []) #set ($global = ($editor == 'globaladmin')) #findNamesOfAppsToConfigure('', $global, $currentSpace, $outputList) #set ($sectionsByName = {}) #set ($categoriesByName = {}) #foreach ($category in $adminMenu) #set ($discard = $categoriesByName.put($category.id, $category)) #foreach ($section in $category.children) #set ($discard = $sectionsByName.put($section.id, $section)) #end #end ## #set ($query = "editor=$escapetool.url(${editor})") #if ($editor != 'globaladmin') #set ($query = $query + "&space=$escapetool.url(${currentSpace})") #end #foreach ($appName in $outputList) ## ## Get the configurable application #set ($app = $xwiki.getDocument($appName)) ## ## If getDocument returns null, then warn the user that they don't have view access to that application. #if (!$app) #set ($discard = $appsUserCannotView.add($appName)) #end ## #foreach ($configurableObject in $app.getObjects($nameOfThisDocument)) #set ($displayInSection = $app.getValue('displayInSection', $configurableObject)) ## ## If there is no section specified in the object, just skip it #if ("$!{displayInSection}" == '') ## Skip, bad object ## If there is no section for this configurable or if the section cannot be edited, then check if the ## application can be edited by the current user, if so then we display the icon from the current app and ## don't display any message to tell the user they can't edit that section. #elseif ($sectionsByName.containsKey($displayInSection)) #set ($appSection = $sectionsByName.get($displayInSection)) #set ($appSection.configurable = true) #set ($newSection = false) #else ## ## If there is no section for this configurable, then we will have to add one. #set ($appSection = {'id' : $displayInSection, 'name' : $msg.get("admin.${displayInSection.toLowerCase()}"), 'url': $xwiki.getURL($currentDoc, $adminAction, "${query}&section=$escapetool.url(${displayInSection})"), 'configurable' : true}) #if ($app.getValue('configureGlobally', $configurableObject) != 1) #set ($appSection.perSpace = true) #end #set ($key = "admin.${displayInSection.toLowerCase()}.description") #if ($msg.get($key) != $key) #set ($appSection.description = $msg.get($key)) #end #set ($discard = $sectionsByName.put($displayInSection, $appSection)) #set ($discard = $categoriesByName.get('applications').children.add($appSection)) #set ($newSection = true) #end ## ## If an attachment by the filename iconAttachment exists and is an image #set ($attachment = $app.getAttachment("$!app.getValue('iconAttachment', $configurableObject)")) #if ($attachment && $attachment.isImage()) ## Set the icon for this section as the attachment URL. #set ($appSection.iconReference = "${appName}@${attachment.getFilename()}") #elseif(!$appSection.iconReference) #set ($appSection.iconReference = 'XWiki.ConfigurableClass@DefaultAdminSectionIcon.png') #end ## ## If the user doesn't have edit access to the application, we want to show a message on the icon #if (!$xcontext.hasAccessLevel('edit', $app.getFullName()) && $newSection) #set ($appSection.readOnly = true) #elseif ($xcontext.hasAccessLevel('edit', $app.getFullName()) && $appSection.readOnly) #set ($appSection.readOnly = false) #end #end## Foreach configurable object in this app. #end## Foreach application which is configurable. #end #* * Show the heading for configuration for a given application. * * $appName (String) Name of the application to show configuration heading for. * * $headingAlreadyShowing (boolean) If true then we don't make another heading. Otherwise it is set to true. *### #macro(showHeading, $appName, $headingAlreadyShowing) #if(!$headingAlreadyShowing) #set($headingAlreadyShowing = true) = $msg.get("admin.customize") __[[$appName>>$appName]]__: = #end #end #define($formHtml) #if ($objClass.getPropertyNames().size() > 0) <dl> #foreach($propName in $objClass.getPropertyNames()) #if($propertiesToShow.size() > 0 && !$propertiesToShow.contains($propName)) ## Silently skip over this property. #else #set($prettyName = "#evaluate($app.displayPrettyName($propName, $obj))") ## ## Further processing of the field display HTML is needed. ## Step 1: Strip <pre> tags which $obj.display inserts, this won't affect content because it's escaped. #set($out = $obj.display($propName, 'edit').replaceAll('<[/]?+pre>', '')) ## Step 2: Select only content between first < and last > because $obj.display inserts html macros. ## Careful not to remove html macros from the content because they are not escaped! #set ($out = $out.substring($out.indexOf('<'), $mathtool.add(1, $out.lastIndexOf('>')))) ## Step 3: Prepend app name to all ID and FOR attributes to prevent id collision with multiple apps on one page. #set ($out = $out.replaceAll( " id='$objClass.getName()_$obj.getNumber()_$propName", " id='${escapedAppName}_$objClass.getName()_$obj.getNumber()_$propName").replaceAll( " for='$objClass.getName()_$obj.getNumber()_$propName", " for='${escapedAppName}_$objClass.getName()_$obj.getNumber()_$propName")) ## App Name is prepended to for= to make label work with id which is modified to prevent collisions. <dt><label for="${escapedAppName}_$objClass.getName()_$obj.getNumber()_$propName"> #if ($out.indexOf('type=''checkbox''') != -1) $out #end $escapetool.xml($prettyName) </label> #if($linkPrefix != '') #set($linkScript = "$linkPrefix$propName") <a href="$escapetool.xml("#evaluate($linkScript)")" class="xHelp" title="$msg.get('admin.documentation')">$msg.get('admin.documentation')</a> #end </dt> #if ($out.indexOf('type=''checkbox''') == -1) <dd>$out</dd> #end #end## If property is in propertiesToShow #end## Foreach property in this class </dl> #end #end## define $formHtml {{/velocity}}