General Actions:
Log-in
Wiki:
L3D's Wiki
▼
:
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 your wiki
»
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
#includeMacros("Blog.BlogCode") ## ## ## #** * 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}'") #foreach($subcategory in $subcategories) #set($categories = "$!{categories}, '$subcategory'") #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 (${categories})") #set($totalEntries = $xwiki.countDocuments(${query})) #preparePagedViewParams($totalEntries 10) #set($entries = $xwiki.searchDocuments("${query} order by publishDate.value desc", $itemsPerPage, $startAt)) #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 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) #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)) #foreach($i in [1..$level])*#end <label id='blog_category_${name}' title="#getCategoryDescription($categoryDoc)"><input name="${blogPostClassname}_$!{entryObj.number}_category" value="$name" type="checkbox"#if($entryObj.getProperty('category').getValue().contains($name)) checked="checked" #end/> #getCategoryName($categoryDoc)</label> #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. *### #macro(displayEditableCategory $name $level) #set($categoryDoc = $xwiki.getDocument($name)) #foreach($i in [1..$level])*#end #getCategoryName($categoryDoc) <a href="$doc.getURL('view', "action=delete&category=$name")">#toolImage('delete.png' 'delete ')</a> <a href="$categoryDoc.getURL('inline')">#toolImage('edit.png' 'edit ')</a> #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_${name}_option" value="$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) #foreach($i in [1..$level])*#end <a href="$xwiki.getURL('Blog.CategoryRss', 'view', "xpage=plain&category=$name")" title=""><img src="$xwiki.getSkinFile('icons/black-rss-mini.png')" alt="RSS"/></a> [#getCategoryName($xwiki.getDocument($name)) ($totalEntries)>$name] #end ## ## ## #** * Prints the name of a category, indicated by its document. * * @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 $result## #end ## ## ## #** * Prints the description of a category, indicated by its document. * * @param categoryDoc The document containing the category to be displayed. *### #macro(getCategoryDescription $categoryDoc) ## Don't indent! $!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 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) <form action="" method="post"> <div class='create-category'> <input type="hidden" name="action" value="create"/> <label>New category: <input type="text" name="newCategoryName"></input></label> <label>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> </form> #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($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 = '$category' order by doc.name") #foreach($item in $xwiki.searchDocuments($query)) #if($xwiki.hasAccessLevel('edit', $context.user, $item)) #set($subcategoryDoc = $xwiki.getDocument($item)) $subcategoryDoc.setParent($categoryParent) $subcategoryDoc.save('Update category parent') #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 = '$category' order by doc.name") #foreach($item in $xwiki.searchDocuments($query)) #if($xwiki.hasAccessLevel('edit', $context.user, $item)) #set($blogEntryDoc = $xwiki.getDocument($item)) #set($discard = $blogEntryDoc.getObject(${blogPostClassname}).getProperty('category').value.remove($category)) $blogEntryDoc.save('Remove deleted category') #end #end $categoryDoc.delete() #end
Quick Links
L3D Calendar
Wiki Dashboard
Document Index
Blog
Sandbox