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
Hide line numbers
1: #includeMacros("Blog.BlogCode") 2: ## 3: ## 4: ## 5: #** 6: * Retrieves the list of blog entries from a given category. Entries belonging to subcategories 7: * are not returned. 8: * 9: * @param category The name of the category (XDocument full name, for example 'MyBlog.Fishing'). 10: * @param articles Return parameter, where the list of entries is placed. 11: * @param total Return parameter, where the total number of entries belonging to this category is 12: * placed. Useful for a paginated view. 13: *### 14: #macro(getEntriesForCategory $category $entries $totalEntries) 15: #getCategoriesHierarchy('' $tree) 16: #set($subcategories = $util.arrayList) 17: #getSubcategories($tree $category $subcategories) 18: #set($categories = "'${category}'") 19: #foreach($subcategory in $subcategories) 20: #set($categories = "$!{categories}, '$subcategory'") 21: #end 22: #getBlogEntriesBaseQuery($query) 23: #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})") 24: #set($totalEntries = $xwiki.countDocuments(${query})) 25: #preparePagedViewParams($totalEntries 10) 26: #set($entries = $xwiki.searchDocuments("${query} order by publishDate.value desc", $itemsPerPage, $startAt)) 27: #end 28: #macro(getSubcategories $tree $category $subcategories) 29: #if(!$subcategories.contains($category)) 30: #foreach($subcategory in $tree.get($category)) 31: #set($discard = $subcategories.add($subcategory)) 32: #getSubcategories($tree $subcategory $subcategories) 33: #end 34: #end 35: #end 36: ## 37: ## 38: ## 39: #** 40: * Builds a tree of categories, respecting the parent<->subcategory relation. Each node holds the 41: * full name of the category's document. The root of the tree is 'Blog.Categories'. 42: * 43: * @param space The space where to search for categories. If this parameter is an emptry string or 44: * null, all the categories in the wiki are returned. 45: * @param tree Return parameter, HashMap<String, List<String>> structure holding the categories 46: * hierarchy, where the key is the name of a category, and the value contains the names of 47: * all its subcategories. To obtain the full hierarchy, start with the key 'Blog.Categories'. 48: *### 49: #macro(getCategoriesHierarchy $space $tree) 50: #set($tree = $util.hashMap) 51: #set($query = ', BaseObject obj where ') 52: #if("$!space" != '') 53: #set($query = "${query}doc.space = '${space}' and ") 54: #end 55: #set($query = "${query}obj.name = doc.fullName and obj.className = '${blogCategoryClassname}' order by doc.name") 56: #foreach($category in $xwiki.searchDocuments($query)) 57: #set($categoryDoc = $xwiki.getDocument($category)) 58: #set($categoryParent = "$!categoryDoc.parent") 59: #if ($categoryParent == "") 60: #set($categoryParent = $defaultCategoryParent) 61: #end 62: #if (!$tree.containsKey($categoryParent)) 63: #set($discard = $tree.put($categoryParent, $util.arrayList)) 64: #end 65: #set($discard = $tree.get($categoryParent).add($category)) 66: #end 67: #end 68: ## 69: ## 70: ## 71: #** 72: * Displays the category hierarchy held in the <tt>tree</tt> parameter. 73: * 74: * @param tree The category hierarchy HashMap<String, List<String>> structure, where the key 75: * is the name of a category, and the value contains the names of all its subcategories. 76: * @param displayMethod Selects how to display the category tree. Possible values are: 77: * <ul> 78: * <li><em>"simple"</em>: tree with links to the category pages.</li> 79: * <li><em>"selectable"</em>: each category name in the tree is preceded by a checkbox.</li> 80: * <li><em>"option"</em>: wraps each category name in an <option> element, to be used 81: * in a select box.</li> 82: * <li><em>"editable"</em>: displays links to delete and edit each category, if the rights 83: * allow such actions.</li> 84: * </ul> 85: * For any other value, the default ("simple") is considered. 86: *### 87: #macro(displayCategoriesHierarchy $tree $displayMethod) 88: #displayCategoriesHierarchyRecursive($tree $defaultCategoryParent 1 $displayMethod) 89: #end 90: ## 91: ## 92: ## 93: #** 94: * Displays recursively the category hierarchy held in the <tt>tree</tt> parameter, starting at 95: * the node indicated by the <tt>root</tt> parameter, which is on the <tt>level</tt>th level in 96: * the tree. 97: * 98: * @param tree The category hierarchy HashMap<String, List<String>> structure, where the key 99: * is the name of a category, and the value contains the names of all its subcategories. 100: * @param root The full name of the document containing the category that is to be considered the 101: * root of the displayed subtree. 102: * @param level The current depth of the tree, used for proper indentation. 103: * @param displayMethod Selects how to display the category tree. Possible values are: 104: * <ul> 105: * <li><em>"simple"</em>: tree with links to the category pages.</li> 106: * <li><em>"selectable"</em>: each category name in the tree is preceded by a checkbox.</li> 107: * <li><em>"option"</em>: wraps each category name in an <option> element, to be used 108: * in a select box.</li> 109: * <li><em>"editable"</em>: displays links to delete and edit each category, if the rights 110: * allow such actions.</li> 111: * </ul> 112: * For any other value, the default ("simple") is considered. 113: *### 114: #macro(displayCategoriesHierarchyRecursive $tree $root $level $displayMethod) 115: #if(!$processedCategories) 116: #set($processedCategories = $util.arrayList) 117: #end 118: #foreach($item in $tree.get($root)) 119: #if(!$processedCategories.contains($item)) 120: #set($discard = $processedCategories.add($item)) 121: #displayCategory($item $level $displayMethod) 122: #displayCategoriesHierarchyRecursive($tree $item $mathtool.add($level, 1) $displayMethod) 123: #end 124: #end 125: #end 126: ## 127: ## 128: ## 129: #** 130: * Displays a category as part of a category hierarchy. 131: * 132: * @param name The full name of the document containing the category to be displayed. 133: * @param level The depth where this category is in the tree, used for proper indentation. 134: * @param displayMethod Selects how to display the category tree. Possible values are: 135: * <ul> 136: * <li><em>"simple"</em>: tree with links to the category pages.</li> 137: * <li><em>"selectable"</em>: each category name in the tree is preceded by a checkbox.</li> 138: * <li><em>"option"</em>: wraps each category name in an <option> element, to be used 139: * in a select box.</li> 140: * <li><em>"editable"</em>: displays links to delete and edit each category, if the rights 141: * allow such actions.</li> 142: * </ul> 143: * For any other value, the default ("simple") is considered. 144: *### 145: #macro(displayCategory $name $level $displayMethod) 146: #if("$!displayMethod" == "option") 147: #displayOptionCategory($name $level) 148: #elseif("$!displayMethod" == "selectable") 149: #displaySelectableCategory($name $level) 150: #elseif("$!displayMethod" == "editable") 151: #displayEditableCategory($name $level) 152: #else 153: #displaySimpleCategory($name $level) 154: #end 155: #end 156: ## 157: ## 158: ## 159: #** 160: * Displays a category as part of a category hierarchy, preceded by a checkbox that allows choosing 161: * this category for a blog entry. 162: * 163: * @param name The full name of the document containing the category to be displayed. 164: * @param level The depth where this category is in the tree, used for proper indentation. 165: *### 166: #macro(displaySelectableCategory $name $level) 167: #set($categoryDoc = $xwiki.getDocument($name)) 168: #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> 169: #end 170: ## 171: ## 172: ## 173: #** 174: * Displays a category as part of a category hierarchy, followed by links for editing and deleting 175: * this category, if the current user has the rights to perform these actions. 176: * 177: * @param name The full name of the document containing the category to be displayed. 178: * @param level The depth where this category is in the tree, used for proper indentation. 179: *### 180: #macro(displayEditableCategory $name $level) 181: #set($categoryDoc = $xwiki.getDocument($name)) 182: #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> 183: #end 184: ## 185: ## 186: ## 187: #** 188: * Displays a category as part of a category hierarchy, wrapped in an <option> element, to 189: * be used in a select box. 190: * 191: * @param name The full name of the document containing the category to be displayed. 192: * @param level The depth where this category is in the tree, used for proper indentation. 193: *### 194: #macro(displayOptionCategory $name $level) 195: <option id="blog_category_${name}_option" value="$name">#if($level > 1)#foreach($i in [2..$level]) #end#end#getCategoryName($xwiki.getDocument($name))</option> 196: #end 197: ## 198: ## 199: ## 200: #** 201: * Displays a category as part of a category hierarchy, wrapped in a link. 202: * 203: * @param name The full name of the document containing the category to be displayed. 204: * @param level The depth where this category is in the tree, used for proper indentation. 205: *### 206: #macro(displaySimpleCategory $name $level) 207: #getEntriesForCategory($name $discard $totalEntries) 208: #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] 209: #end 210: ## 211: ## 212: ## 213: #** 214: * Prints the name of a category, indicated by its document. 215: * 216: * @param categoryDoc The document containing the category to be displayed. 217: *### 218: #macro(getCategoryName $categoryDoc) 219: ## Don't indent! 220: #set($result = "$!categoryDoc.getObject(${blogCategoryClassname}).getProperty('name').value.trim()")## 221: #if($result == '') 222: #set($result = $categoryDoc.name) 223: #end 224: $result## 225: #end 226: ## 227: ## 228: ## 229: #** 230: * Prints the description of a category, indicated by its document. 231: * 232: * @param categoryDoc The document containing the category to be displayed. 233: *### 234: #macro(getCategoryDescription $categoryDoc) 235: ## Don't indent! 236: $!categoryDoc.getObject(${blogCategoryClassname}).getProperty('description').value.trim()## 237: #end 238: ## 239: ## 240: ## 241: #** 242: * Generates a form for creating a new category. The form allows to enter the name of the new 243: * category, and select a parent category from the existing ones. 244: * 245: * @param tree The category hierarchy HashMap<String, List<String>> structure, where the key 246: * is the name of a category, and the value contains the names of all its subcategories. 247: * @todo When javascript is disabled, a link to "Manage categories" should be displayed instead. 248: * This "form" should be created from javascript. 249: *### 250: #macro(showCreateCategoryBox $tree) 251: <form action="" method="post"> 252: <div class='create-category'> 253: <input type="hidden" name="action" value="create"/> 254: <label>New category: <input type="text" name="newCategoryName"></input></label> 255: <label>Parent: 256: <select name="newCategoryParent" id="blog_category_selectBox"> 257: <option value="${defaultCategoryParent}" selected="selected">None</option> 258: $!processedCategories.clear()## 259: #displayCategoriesHierarchy($tree 'option') 260: </select> 261: </label> 262: <span class="buttonwrapper"><input class="button" type="button" value="Add" id="blog_AddCategoryButton"></input></span> 263: </div> 264: </form> 265: #end 266: ## 267: ## 268: ## 269: #** 270: * Deletes a category, moving all the subcategories to its parent and removing this category from 271: * all existing blog entries. 272: * 273: * @param category The full name of the document containing the category to be deleted. 274: *### 275: #macro(deleteCategory $category) 276: #set($categoryDoc = $xwiki.getDocument($category)) 277: #set($categoryParent = "$!categoryDoc.parent") 278: #if($categoryParent == '') 279: #set($categoryParent = "{$defaultCategoryParent}")) 280: #end 281: #set($query = ', BaseObject obj where ') 282: #if($space != '') 283: #set($query = "${query}doc.space = '${space}' and ") 284: #end 285: #set($query = "${query}obj.name = doc.fullName and obj.className = '${blogCategoryClassname}' and doc.fullName <> 'Blog.CategoryTemplate' and doc.parent = '$category' order by doc.name") 286: #foreach($item in $xwiki.searchDocuments($query)) 287: #if($xwiki.hasAccessLevel('edit', $context.user, $item)) 288: #set($subcategoryDoc = $xwiki.getDocument($item)) 289: $subcategoryDoc.setParent($categoryParent) 290: $subcategoryDoc.save('Update category parent') 291: #end 292: #end 293: #set($query = ', BaseObject obj, DBStringListProperty categories join categories.list as category where ') 294: #if($space != '') 295: #set($query = "${query}doc.space = '${space}' and ") 296: #end 297: #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") 298: #foreach($item in $xwiki.searchDocuments($query)) 299: #if($xwiki.hasAccessLevel('edit', $context.user, $item)) 300: #set($blogEntryDoc = $xwiki.getDocument($item)) 301: #set($discard = $blogEntryDoc.getObject(${blogPostClassname}).getProperty('category').value.remove($category)) 302: $blogEntryDoc.save('Remove deleted category') 303: #end 304: #end 305: $categoryDoc.delete() 306: #end
Quick Links
L3D Calendar
Wiki Dashboard
Document Index
Blog
Sandbox