General Actions:
Log-in
Wiki:
Courses
▼
:
Document Index
»
Space:
Blog
▼
:
Document Index
»
Page:
CategoriesCode
Search
Page Actions:
Export
▼
:
Export as PDF
Export as RTF
Export as HTML
More actions
▼
:
Print preview
View Source
Welcome to L3D's XWiki for Courses
»
The Wiki Blog
»
Macros for the Blog Categories
Wiki source code of
Macros for the Blog Categories
Last modified by
Administrator
on 2008/12/21 20:41
Content
·
Comments
(0)
·
Attachments
(0)
·
History
·
Information
Show line numbers
{{include document="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) #getCategoriesHierarchy('' $tree) #set($subcategories = $util.arrayList) #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($totalEntries = $xwiki.countDocuments(${query}, ${categories})) #preparePagedViewParams($totalEntries 10) #set($entries = $xwiki.searchDocuments("${query} order by publishDate.value desc", $itemsPerPage, $startAt, ${categories})) #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 = $util.hashMap) #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 $xwiki.searchDocuments($query)) #set($categoryDoc = $xwiki.getDocument($category)) #set($categoryParent = "$!categoryDoc.parent") #if($categoryParent == '') #set($categoryParent = $defaultCategoryParent) #end #if(!$tree.containsKey($categoryParent)) #set($discard = $tree.put($categoryParent, $util.arrayList)) #end #set($discard = $tree.get($categoryParent).add($category)) #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 = $util.arrayList) #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 = $util.arrayList) #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 #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($nameUrl = $escapetool.url($name)) #foreach($i in [1..$level])*#end ## <span class="blog-category-level"><span class="blog-category">## <label id='blog_category_${escapetool.xml($name)}' title="#getCategoryDescription($categoryDoc)"><input name="${blogPostClassname}_$!{entryObj.number}_category" value="${escapetool.xml($name)}" type="checkbox"#if($entryObj.getProperty('category').getValue().contains($name)) checked="checked" #end/> #getCategoryName($categoryDoc)</label>## </span>## #if($xwiki.hasAccessLevel('edit', $context.user, $doc.fullName) && ("$!{request.xaction}" != "showAddCategory" || "$!{request.parentCategory}" != $name)) <span class="blog-category-tools">## <a href="$doc.getURL('view', "xaction=showAddCategory&parentCategory=$nameUrl")" 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) <form action="$doc.getURL()" method="post" class="category-add-form"><div class='create-category'> <input type="hidden" name="xaction" value="create"/> <label>$msg.get("xe.blog.categories.new")<br/> <input type="text" name="newCategoryName" class="category-name-input"></input></label><br/>#if("$!{request.parentCategory}" == "")<label>#* $msg.get("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"></input></span> <a 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="xaction" value="rename"/>## <input type="hidden" name="category" value="${escapetool.xml($request.category)}"/>## <label>$msg.get("xe.blog.categories.newName")<br/> <input type="text" name="newCategoryName" class="category-name-input"></input></label><br/>## <span class="buttonwrapper"><input class="button" type="submit" value="Rename"></input></span> ## <a 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="$xwiki.getURL('Blog.CategoryRss', 'view', "xpage=plain&category=$nameUrl")" title="RSS"><img class="icon icon-manage" src="$xwiki.getSkinFile('icons/xwiki/rss-medium.png')" alt="[RSS]"/></a>## [[#getCategoryName($xwiki.getDocument($name)) (% class="itemCount" %)($totalEntries)(%%)>>${name}]]</span> ## <span class="blog-category-tools">## #if($xwiki.hasAccessLevel('delete', $context.user, $name) && ("$!{request.xaction}" != 'showRenameCategory' || "$!{request.category}" != $name))<a href="$xwiki.getURL('Blog.ManageCategories', 'view', "xaction=showRenameCategory&category=$nameUrl")" class="tool rename">#toolImage('pencil' 'Rename ')</a>#end ## #if($xwiki.hasAccessLevel('edit', $context.user, $doc.fullName) && ("$!{request.xaction}" != "showAddCategory" || "$!{request.parentCategory}" != $name))<a href="$xwiki.getURL('Blog.ManageCategories', 'view', "xaction=showAddCategory&parentCategory=$nameUrl")" class="tool add-subcategory">#toolImage('chart_organisation_add' 'Add a subcategory ')</a> #end ## #if($xwiki.hasAccessLevel('delete', $context.user, $name)) <a href="$xwiki.getURL('Blog.ManageCategories', 'view', "xaction=delete&category=$nameUrl")" class="tool delete">#toolImage('cross' 'Delete ')</a>#end ## </span>## #if($xwiki.hasAccessLevel('edit', $context.user, $doc.fullName) && "$!{request.xaction}" == "showRenameCategory" && "$!{request.category}" == $name) #renameCategoryForm() #end## #if($xwiki.hasAccessLevel('edit', $context.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_${escapetool.xml($name)}_option" value="${escapetool.xml($name)}">#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)) #foreach($i in [1..$level])*#end <span class="blog-category-level"><a href="$xwiki.getURL('Blog.CategoryRss', 'view', "xpage=plain&category=$nameUrl")" title="RSS">#toolImage('bullet_feed', '[RSS]')</a> <a href="$xwiki.getURL($name)">#getCategoryName($xwiki.getDocument($name))</a> <span class="itemCount"> ($totalEntries)</span></span> #end ## ## ## #** * Prints the name of a category, indicated by its document. * The result is XML-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 $escapetool.xml($result)## #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="" 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="xaction" value="create"/> <label>$msg.get("xe.blog.categories.new") <input type="text" name="newCategoryName"></input></label> <label>$msg.get("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"></input></span> </div> #end ## ## ## #macro(displayCategoryManagementTree $space $displayType) <div class="blog-categories-list"> #getCategoriesHierarchy("$!{space}" $tree) #displayCategoriesHierarchy($tree $displayType) #if($xwiki.hasAccessLevel('edit', $context.user, $doc.fullName)) * <span class="blog-add-category-label"><a href="$doc.getURL('view', "xaction=showAddCategory&parentCategory=")">$msg.get("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 $xwiki.searchDocuments($query, $parameterValues)) #if($xwiki.hasAccessLevel('edit', $context.user, $item)) #set($subcategoryDoc = $xwiki.getDocument($item)) $subcategoryDoc.setParent($categoryParent) $subcategoryDoc.save($msg.get('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 $xwiki.searchDocuments($query, $parameterValues)) #if($xwiki.hasAccessLevel('edit', $context.user, $item)) #set($blogEntryDoc = $xwiki.getDocument($item)) #set($discard = $blogEntryDoc.getObject(${blogPostClassname}).getProperty('category').value.remove($category)) $blogEntryDoc.save($msg.get('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 $xwiki.searchDocuments($query, $parameterValues)) #if($xwiki.hasAccessLevel('edit', $context.user, $item)) #set($subcategoryDoc = $xwiki.getDocument($item)) $subcategoryDoc.setParent($newCategoryDoc.fullName) $subcategoryDoc.save($msg.get('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 $xwiki.searchDocuments($query, $parameterValues)) #if($xwiki.hasAccessLevel('edit', $context.user, $item)) #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($msg.get('xe.blog.manageCategories.comment.updatedRenamedCategory'), true) #end #end $categoryDoc.getObject('Blog.CategoryClass').set('name', $newCategoryName) $categoryDoc.save($msg.get('xe.blog.manageCategories.comment.updatedCategory'), true) $categoryDoc.rename($newCategoryName) #end {{/velocity}}
Recent Blog Posts
Storm Drain Survey
Computer Science Department Wikipedia Article
Delicious Cookies!!!
Assignment 13 posted
Makeshift Crew Project Survey for Cookies!!!
Assignments 11 & 12 Posted
Assignment 10 Posted
Assignment 9 (Project Progress Report) Posted
Organize Your Projects
A8 Posted
Blog Categories
News
(10)
Other
(2)
Personal
(1)
Survey
(1)
Blog Archive
2009
(1)
Quick Links
DSSF 2008
DCNM 2009
HCCF 2010
Document Index
Sandbox
My Recent Modifications
ASSIGNMENTNAME