Toggle navigation
Log-in
Wiki Index
Page Index
User Index
Application Index
Blog
Macros for the Blog Categories
Wiki source code of
Macros for the Blog Categories
Last modified by superadmin on 2016/07/29 17:18
Show line numbers
{{include reference="Blog.BlogCode"/}} {{velocity output="false"}} ## ## ## #** * Retrieves the list of blog entries from a given category. Entries belonging to subcategories * are not returned. * * @param category The name of the category (XDocument full name, for example 'MyBlog.Fishing'). * @param articles Return parameter, where the list of entries is placed. * @param total Return parameter, where the total number of entries belonging to this category is * placed. Useful for a paginated view. *### #macro(getEntriesForCategory $category $entries $totalEntries) #set ($entries = $NULL) #set ($totalEntries = $NULL) #if ("$!{blogCategoryEntriesCache.containsKey($!{category})}" == 'true') #setVariable ("$entries" $blogCategoryEntriesCache.get($!{category}).get(0)) #setVariable ("$totalEntries" $blogCategoryEntriesCache.get($!{category}).get(1)) #preparePagedViewParams ($totalEntries 10) #else #getCategoriesHierarchy ('' $tree) #set ($subcategories = []) #getSubcategories ($tree $category $subcategories) #set ($categories = [${category}]) #set ($discard = $categories.addAll(${subcategories})) #set ($parameters = '?') #foreach ($subcategory in $subcategories) #set ($parameters = $parameters.concat(', ?')) #end #getBlogEntriesBaseQuery ($query) #set ($query = ", DBStringListProperty as categories join categories.list as category${query} and obj.id = categories.id.id and categories.id.name='category' and category in (${parameters})") #set ($totalResult = $services.query.hql($query).bindValues($categories).count()) #preparePagedViewParams ($totalResult 10) #set ($result = $services.query.hql("${query} order by publishDate.value desc").setLimit($itemsPerPage).setOffset($startAt).bindValues($categories).execute()) #if ("$!{blogCategoryEntriesCache.containsKey($!{category})}" == '') #set ($blogCategoryEntriesCache = {}) #end #set ($discard = $blogCategoryEntriesCache.put("$!{category}", [$result, $totalResult])) #setVariable ("$entries" $result) #setVariable ("$totalEntries" $totalResult) #end #end #macro(getSubcategories $tree $category $subcategories) #if(!$subcategories.contains($category)) #foreach($subcategory in $tree.get($category)) #set($discard = $subcategories.add($subcategory)) #getSubcategories($tree $subcategory $subcategories) #end #end #end ## ## ## #** * Builds a tree of categories, respecting the parent<->subcategory relation. Each node holds the * full name of the category's document. The root of the tree is 'Blog.Categories'. * * @param space The space where to search for categories. If this parameter is an emptry string or * null, all the categories in the wiki are returned. * @param tree Return parameter, HashMap<String, List<String>> structure holding the categories * hierarchy, where the key is the name of a category, and the value contains the names of * all its subcategories. To obtain the full hierarchy, start with the key 'Blog.Categories'. *### #macro(getCategoriesHierarchy $space $tree) #set ($tree = $NULL) #if ("$!{blogCategoriesHierarchyCache.containsKey($!{space})}" == 'true') #setVariable ("$tree" $blogCategoriesHierarchyCache.get($!{space})) #else #set ($result = {}) #set($query = ', BaseObject obj where ') #if("$!space" != '') #set($query = "${query}doc.space = '${space}' and ") #end #set($query = "${query}obj.name = doc.fullName and obj.className = '${blogCategoryClassname}' order by doc.name") #foreach($category in $services.query.hql($query).execute()) #set($categoryDoc = $xwiki.getDocument($category)) #set($categoryParent = "$!categoryDoc.parent") #if($categoryParent == '') #set($categoryParent = $defaultCategoryParent) #end #if(!$result.containsKey($categoryParent)) #set($discard = $result.put($categoryParent, [])) #end #set($discard = $result.get($categoryParent).add($category)) #end #if ("$!{blogCategoriesHierarchyCache.containsKey($!{space})}" == '') #set ($blogCategoriesHierarchyCache = {}) #end #set ($discard = $blogCategoriesHierarchyCache.put("$!{space}", $result)) #setVariable ("$tree" $result) #end #end ## ## ## #** * Displays the category hierarchy held in the <tt>tree</tt> parameter. * * @param tree The category hierarchy, a HashMap<String, List<String>> structure, where the key * is the name of a category, and the value contains the names of all its subcategories. * @param displayMethod Selects how to display the category tree. Possible values are: * <ul> * <li><em>"simple"</em>: tree with links to the category pages.</li> * <li><em>"selectable"</em>: each category name in the tree is preceded by a checkbox.</li> * <li><em>"option"</em>: wraps each category name in an <option> element, to be used * in a select box.</li> * <li><em>"editable"</em>: displays links to delete and edit each category, if the rights * allow such actions.</li> * </ul> * For any other value, the default ("simple") is considered. *### #macro(displayCategoriesHierarchy $tree $displayMethod) #set($processedCategories = []) #displayCategoriesHierarchyRecursive($tree $defaultCategoryParent 1 $displayMethod) #end ## ## ## #** * Displays recursively the category hierarchy held in the <tt>tree</tt> parameter, starting at * the node indicated by the <tt>root</tt> parameter, which is on the <tt>level</tt>th level in * the tree. * * @param tree The category hierarchy HashMap<String, List<String>> structure, where the key * is the name of a category, and the value contains the names of all its subcategories. * @param root The full name of the document containing the category that is to be considered the * root of the displayed subtree. * @param level The current depth of the tree, used for proper indentation. * @param displayMethod Selects how to display the category tree. Possible values are: * <ul> * <li><em>"simple"</em>: tree with links to the category pages.</li> * <li><em>"selectable"</em>: each category name in the tree is preceded by a checkbox.</li> * <li><em>"option"</em>: wraps each category name in an <option> element, to be used * in a select box.</li> * <li><em>"editable"</em>: displays links to delete and edit each category, if the rights * allow such actions.</li> * </ul> * For any other value, the default ("simple") is considered. *### #macro(displayCategoriesHierarchyRecursive $tree $root $level $displayMethod) #if(!$processedCategories) #set($processedCategories = []) #end #foreach($item in $tree.get($root)) #if(!$processedCategories.contains($item)) #set($discard = $processedCategories.add($item)) #displayCategory($item $level $displayMethod) #displayCategoriesHierarchyRecursive($tree $item $mathtool.add($level, 1) $displayMethod) #end #end #if($displayMethod == "selectable") <input type="hidden" name="${blogPostClassname}_$!{entryObj.number}_category" value="" /> #end #end ## ## ## #** * Displays a category as part of a category hierarchy. * * @param name The full name of the document containing the category to be displayed. * @param level The depth where this category is in the tree, used for proper indentation. * @param displayMethod Selects how to display the category tree. Possible values are: * <ul> * <li><em>"simple"</em>: tree with links to the category pages.</li> * <li><em>"selectable"</em>: each category name in the tree is preceded by a checkbox.</li> * <li><em>"option"</em>: wraps each category name in an <option> element, to be used * in a select box.</li> * <li><em>"editable"</em>: displays links to delete and edit each category, if the rights * allow such actions.</li> * </ul> * For any other value, the default ("simple") is considered. *### #macro(displayCategory $name $level $displayMethod) #if("$!displayMethod" == "option") #displayOptionCategory($name $level) #elseif("$!displayMethod" == "selectable") #displaySelectableCategory($name $level) #elseif("$!displayMethod" == "editable") #displayEditableCategory($name $level) #else #displaySimpleCategory($name $level) #end #end ## ## ## #** * Displays a category as part of a category hierarchy, preceded by a checkbox that allows choosing * this category for a blog entry. * * @param name The full name of the document containing the category to be displayed. * @param level The depth where this category is in the tree, used for proper indentation. *### #macro(displaySelectableCategory $name $level) #set($categoryDoc = $xwiki.getDocument($name)) #set($addCategURL = $doc.getURL('view', $escapetool.url({ 'xaction': 'showAddCategory', 'parentCategory' : $name }))) #set($addEntryParams = false) #if($isBlogPost) #set($entry = $xwiki.getDocument($doc.fullName)) #set($entryObj = $isBlogPost) #set($addEntryParams = true) #elseif("$!request.entry" != '' && "$!request.entryObjNb" != '') #set($entry = $xwiki.getDocument($request.entry)) #set($entryObj = $entry.getObject($blogPostClassname, $mathtool.toInteger($request.entryObjNb))) #set($addEntryParams = true) #end #if($isBlogPost || $addEntryParams) ## parentCategory must be the last param #set($addCategURL = $doc.getURL('view', $escapetool.url({ 'xaction': 'showAddCategory', 'entry': $entry.fullName, 'entryObjNb': $entryObj.number, 'parentCategory': $name }))) #end #foreach($i in [1..$level])*#end ## <span class="blog-category-level"><span class="blog-category">## <label id='blog_category_${services.rendering.escape(${escapetool.xml($name)}, $doc.syntax)}' title="#getCategoryDescription($categoryDoc)"><input name="${blogPostClassname}_$!{entryObj.number}_category" value="$services.rendering.escape(${escapetool.xml($name)}, $doc.syntax)" type="checkbox"#if($entryObj.getProperty('category').getValue().contains($name)) checked="checked" #end/> #getCategoryName($categoryDoc)</label>## </span>## #if($xwiki.hasAccessLevel('edit', $xcontext.user, $doc.fullName) && ("$!{request.xaction}" != "showAddCategory" || "$!{request.parentCategory}" != $name)) <span class="blog-category-tools">## <a href="$escapetool.xml($addCategURL)" class="tool add-subcategory">#toolImage('chart_organisation_add' 'Add a subcategory ')</a>## </span>## #end </span> #end ## ## ## #** * Displays a form for creating a new category. If a parentCategory parameter is present in the * query string, the parent category is set accordingly. Otherwise, the form provides a selection * control for choosing the parent category among existing categories. *### ## DO NOT CHANGE INDENTATION #macro(addCategoryForm) #set($addCategURL = $doc.getURL()) #if("$!request.entry" != '') #set($addCategURL = "${addCategURL}?entry=$escapetool.url($request.entry)&entryObjNb=$escapetool.url($!request.entryObjNb)")#end<form action="${addCategURL}" method="post" class="category-add-form"><div class='create-category'> <input type="hidden" name="form_token" value="$!{services.csrf.getToken()}" /> <input type="hidden" name="xaction" value="create"/> <label>$services.localization.render('xe.blog.categories.new')<br/> <input type="text" name="newCategoryName" class="category-name-input" /></label><br/>#if("$!{request.parentCategory}" == "")<label>#* $services.localization.render('xe.blog.categories.parent')*# Subcategory of:<br/> <select name="newCategoryParent" id="blog_category_selectBox" class="category-add-input"> <option value="${defaultCategoryParent}" selected="selected">None</option> $!processedCategories.clear() #displayCategoriesHierarchy($tree 'option') </select> <br/></label>#else<input type="hidden" name="newCategoryParent" value="${escapetool.xml($request.parentCategory)}"/>#end<span class="buttonwrapper"><input class="button" type="submit" value="Add" /></span> <a class="btn btn-default" href="$doc.getURL()">Cancel</a> </div></form> #end ## ## ## #** * Displays a form for renaming a category. *### ## DO NOT CHANGE INDENTATION #macro(renameCategoryForm)## <form action="$doc.getURL()" method="post" class="category-rename-form"><div class='rename-category'>## <input type="hidden" name="form_token" value="$!{services.csrf.getToken()}" /> <input type="hidden" name="xaction" value="rename"/>## <input type="hidden" name="category" value="${escapetool.xml($request.category)}"/>## <label>$services.localization.render('xe.blog.categories.newName')<br/> <input type="text" name="newCategoryName" class="category-name-input" /></label><br/>## <span class="buttonwrapper"><input class="button" type="submit" value="Rename" /></span> ## <a class="btn btn-default" href="$doc.getURL()">Cancel</a>## </div></form>## #end ## ## ## #** * Displays a category as part of a category hierarchy, followed by links for editing and deleting * this category, if the current user has the rights to perform these actions. * * @param name The full name of the document containing the category to be displayed. * @param level The depth where this category is in the tree, used for proper indentation. *### ## DO NOT CHANGE INDENTATION #macro(displayEditableCategory $name $level) #getEntriesForCategory($name $discard $totalEntries) #set($nameUrl = $escapetool.url($name)) #foreach($i in [1..$level])*#end ## <span class="blog-category-level"><span class="blog-category">## <a href="$services.rendering.escape($xwiki.getURL('Blog.CategoryRss', 'view', "xpage=plain&category=$nameUrl"), $doc.syntax)" title="RSS"><img class="icon icon-manage" src="$xwiki.getSkinFile('icons/xwiki/rss-medium.png')" alt="[RSS]"/></a>## <span class="wikilink"><a href="$services.rendering.escape($xwiki.getURL($name), $doc.syntax)">#getCategoryName($xwiki.getDocument($name)) <span class="itemCount">($totalEntries)</span></a></span></span> ## <span class="blog-category-tools">## #if($xwiki.hasAccessLevel('delete', $xcontext.user, $name) && ("$!{request.xaction}" != 'showRenameCategory' || "$!{request.category}" != $name))<a href="$services.rendering.escape($xwiki.getURL('Blog.ManageCategories', 'view', "xaction=showRenameCategory&category=$nameUrl"), $doc.syntax)" class="tool rename">#toolImage('pencil' 'Rename ')</a>#end ## #if($xwiki.hasAccessLevel('edit', $xcontext.user, $doc.fullName) && ("$!{request.xaction}" != "showAddCategory" || "$!{request.parentCategory}" != $name))<a href="$services.rendering.escape($xwiki.getURL('Blog.ManageCategories', 'view', "xaction=showAddCategory&parentCategory=$nameUrl"), $doc.syntax)" class="tool add-subcategory">#toolImage('chart_organisation_add' 'Add a subcategory ')</a> #end ## #if($xwiki.hasAccessLevel('delete', $xcontext.user, $name)) <a href="$services.rendering.escape($xwiki.getURL('Blog.ManageCategories', 'view', "xaction=delete&category=$nameUrl&form_token=$!{services.csrf.getToken()}"), $doc.syntax)" class="tool delete">#toolImage('cross' 'Delete ')</a>#end ## </span>## #if($xwiki.hasAccessLevel('edit', $xcontext.user, $doc.fullName) && "$!{request.xaction}" == "showRenameCategory" && "$!{request.category}" == $name) #renameCategoryForm() #end## #if($xwiki.hasAccessLevel('edit', $xcontext.user, $doc.fullName) && "$!{request.xaction}" == "showAddCategory" && "$!{request.parentCategory}" == $name) #addCategoryForm() #end## </span> #end ## ## ## #** * Displays a category as part of a category hierarchy, wrapped in an <option> element, to * be used in a select box. * * @param name The full name of the document containing the category to be displayed. * @param level The depth where this category is in the tree, used for proper indentation. *### #macro(displayOptionCategory $name $level) <option id="blog_category_${services.rendering.escape(${escapetool.xml($name)}, $doc.syntax)}_option" value="$services.rendering.escape(${escapetool.xml($name)}, $doc.syntax)">#if($level > 1)#foreach($i in [2..$level]) #end#end#getCategoryName($xwiki.getDocument($name))</option> #end ## ## ## #** * Displays a category as part of a category hierarchy, wrapped in a link. * * @param name The full name of the document containing the category to be displayed. * @param level The depth where this category is in the tree, used for proper indentation. *### #macro(displaySimpleCategory $name $level) #getEntriesForCategory($name $discard $totalEntries) #set($nameUrl = $escapetool.url($name)) #getCallingDocument($callingDocument) #foreach($i in [1..$level])*#end (% class="blog-category-level" %)((( [[#toolImage('bullet_feed', '[RSS]')>>Blog.CategoryRss||queryString="xpage=plain&category=$nameUrl" title="RSS"]] <span class="wikilink"><a href="$services.rendering.escape($xwiki.getURL($name), $callingDocument.syntax)">#getCategoryName($xwiki.getDocument($name)) <span class="itemCount">($totalEntries)</span></a></span>))) #end ## ## ## #** * Prints the name of a category, indicated by its document. * The result is XML-escaped and Wiki syntax escaped. * * @param categoryDoc The document containing the category to be displayed. *### #macro(getCategoryName $categoryDoc) ## Don't indent! #set($result = "$!categoryDoc.getObject(${blogCategoryClassname}).getProperty('name').value.trim()")## #if($result == '') #set($result = $categoryDoc.name) #end ## Escape wiki syntax, if any. #getCallingDocument($callingDocument) #set ($result = "$services.rendering.escape($result, $callingDocument.syntax)") ## Escape HTML, if any. $escapetool.xml($result)## #end ## ## ## #** * Return the calling document to make the various API work when called from the context of a Panel and from a wiki page *### #macro(getCallingDocument $callingDocument) #if ("$!paneldoc" != '') #setVariable ("$callingDocument" $paneldoc) #else #setVariable ("$callingDocument" $doc) #end #end ## ## ## #** * Prints the description of a category, indicated by its document. * The result is XML-escaped * * @param categoryDoc The document containing the category to be displayed. *### #macro(getCategoryDescription $categoryDoc) ## Don't indent! $escapetool.xml($!categoryDoc.getObject(${blogCategoryClassname}).getProperty('description').value.trim())## #end ## ## ## #** * Generates a form for creating a new category. The form allows to enter the name of the new * category, and select a parent category from the existing ones. * * @param tree The category hierarchy, a HashMap<String, List<String>> structure, where the key * is the name of a category, and the value contains the names of all its subcategories. * @todo When javascript is disabled, a link to "Manage categories" should be displayed instead. * This "form" should be created from javascript. *### #macro(showCreateCategoryBoxWithForm $tree) <form action="$doc.getURL()" method="post"> #showCreateCategoryBox($tree) </form> #end #** * Generates a box for creating a new category. This allows to enter the name of the new * category, and select a parent category from the existing ones. Note that this does not create * a HTML form element, but requires one to be defined already as its ancestor. * * @param tree The category hierarchy HashMap<String, List<String>> structure, where the key * is the name of a category, and the value contains the names of all its subcategories. * @todo When javascript is disabled, a link to "Manage categories" should be displayed instead. * This "form" should be created from javascript. *### #macro(showCreateCategoryBox $tree) <div class='create-category'> <input type="hidden" name="form_token" value="$!{services.csrf.getToken()}" /> <input type="hidden" name="xaction" value="create"/> <label>$services.localization.render('xe.blog.categories.new') <input type="text" name="newCategoryName" /></label> <label>$services.localization.render('xe.blog.categories.parent') <select name="newCategoryParent" id="blog_category_selectBox"> <option value="${defaultCategoryParent}" selected="selected">None</option> $!processedCategories.clear()## #displayCategoriesHierarchy($tree 'option') </select> </label> <span class="buttonwrapper"><input class="button" type="button" value="Add" id="blog_AddCategoryButton" /></span> </div> #end ## ## ## #macro(displayCategoryManagementTree $space $displayType) <div class="blog-categories-list"> #getCategoriesHierarchy("$!{space}" $tree) #displayCategoriesHierarchy($tree $displayType) #if($xwiki.hasAccessLevel('edit', $xcontext.user, $doc.fullName)) #set($addCategURL = $doc.getURL('view', $escapetool.url({ 'xaction' : 'showAddCategory', 'parentCategory' : '' }))) #if($isBlogPost || ("$!request.entry" != '' && "$!request.entryObjNb" != '')) #set($entryParam = $!doc.fullName) #set($entryObjNbParam = $!entryObj.number) #if(!$isBlogPost) #set($entryParam = $!request.entry) #set($entryObjNbParam = $!request.entryObjNb) #end #set($addCategURL = $doc.getURL('view', $escapetool.url({ 'xaction' : 'showAddCategory', 'parentCategory' : '', 'entry' : $entryParam, 'entryObjNb' : $entryObjNbParam }))) #end * <span class="blog-add-category-label"><a href="$escapetool.xml($addCategURL)">$services.localization.render('xe.blog.categories.addcategory')</a></span> #if("$!{request.xaction}" == "showAddCategory" && "$!{request.parentCategory}" == "") #addCategoryForm() #end #end </div> #end ## ## ## #** * Deletes a category, moving all the subcategories to its parent and removing this category from * all existing blog entries. * * @param category The full name of the document containing the category to be deleted. *### #macro(deleteCategory $category) #set($categoryDoc = $xwiki.getDocument($category)) #set($categoryParent = "$!categoryDoc.parent") #if($categoryParent == '') #set($categoryParent = "{$defaultCategoryParent}") #end #set($parameterValues = ["$!{category}"]) #set($query = ', BaseObject obj where ') #if($space != '') #set($query = "${query}doc.space = '${space}' and ") #end #set($query = "${query}obj.name = doc.fullName and obj.className = '${blogCategoryClassname}' and doc.fullName <> 'Blog.CategoryTemplate' and doc.parent = ? order by doc.name") #foreach($item in $services.query.hql($query).bindValues($parameterValues).execute()) #if($xwiki.hasAccessLevel('edit', $xcontext.user, $item) && $!{services.csrf.isTokenValid("$!{request.getParameter('form_token')}")}) #set($subcategoryDoc = $xwiki.getDocument($item)) $subcategoryDoc.setParent($categoryParent) $subcategoryDoc.save($services.localization.render('xe.blog.manageCategories.comment.updatedParent'), true) #end #end #set($query = ', BaseObject obj, DBStringListProperty categories join categories.list as category where ') #if($space != '') #set($query = "${query}doc.space = '${space}' and ") #end #set($query = "${query}obj.name = doc.fullName and obj.className = '${blogPostClassname}' and doc.fullName <> 'Blog.BlogPostTemplate' and categories.id.id = obj.id and categories.id.name = 'category' and category = ? order by doc.name") #foreach($item in $services.query.hql($query).bindValues($parameterValues).execute()) #if($xwiki.hasAccessLevel('edit', $xcontext.user, $item) && $!{services.csrf.isTokenValid("$!{request.getParameter('form_token')}")}) #set($blogEntryDoc = $xwiki.getDocument($item)) #set($discard = $blogEntryDoc.getObject(${blogPostClassname}).getProperty('category').value.remove($category)) $blogEntryDoc.save($services.localization.render('xe.blog.manageCategories.comment.removedDeletedCategory'), true) #end #end $categoryDoc.delete() #end ## ## ## #** * Renames a category, updating all the subcategories and all existing blog entries. * * @param category The full name of the document containing the category to be renamed. * @param newCategoryName The new name of the category. *### #macro(renameCategory $category $newCategoryName) #set($categoryDoc = $xwiki.getDocument($category)) #set($newCategoryDoc = $xwiki.getDocument($newCategoryName)) #set($parameterValues = ["$!{category}"]) #set($query = ', BaseObject obj where ') #set($query = "${query}obj.name = doc.fullName and obj.className = '${blogCategoryClassname}' and doc.fullName <> 'Blog.CategoryTemplate' and doc.parent = ? order by doc.name") #foreach($item in $services.query.hql($query).bindValues($parameterValues).execute()) #if($xwiki.hasAccessLevel('edit', $xcontext.user, $item) && $!{services.csrf.isTokenValid("$!{request.getParameter('form_token')}")}) #set($subcategoryDoc = $xwiki.getDocument($item)) $subcategoryDoc.setParent($newCategoryDoc.fullName) $subcategoryDoc.save($services.localization.render('xe.blog.manageCategories.comment.updatedParent'), true) #end #end #set($query = ', BaseObject obj, DBStringListProperty categories join categories.list as category where ') #set($query = "${query}obj.name = doc.fullName and obj.className = '${blogPostClassname}' and doc.fullName <> 'Blog.BlogPostTemplate' and categories.id.id = obj.id and categories.id.name = 'category' and category = ? order by doc.name") #foreach($item in $services.query.hql($query).bindValues($parameterValues).execute()) #if($xwiki.hasAccessLevel('edit', $xcontext.user, $item) && $!{services.csrf.isTokenValid("$!{request.getParameter('form_token')}")}) #set($blogEntryDoc = $xwiki.getDocument($item)) #set($discard = $blogEntryDoc.getObject(${blogPostClassname}).getProperty('category').value.remove($category)) #set($discard = $blogEntryDoc.getObject(${blogPostClassname}).getProperty('category').value.add($newCategoryDoc.fullName)) $blogEntryDoc.save($services.localization.render('xe.blog.manageCategories.comment.updatedRenamedCategory'), true) #end #end #if ($!{services.csrf.isTokenValid("$!{request.getParameter('form_token')}")}) $categoryDoc.getObject('Blog.CategoryClass').set('name', $newCategoryName) $categoryDoc.save($services.localization.render('xe.blog.manageCategories.comment.updatedCategory'), true) $categoryDoc.rename($newCategoryName) #end #end {{/velocity}}