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
Hide line numbers
1: {{include document="Blog.BlogCode"/}} 2: 3: {{velocity output="false"}} 4: ## 5: ## 6: ## 7: #** 8: * Retrieves the list of blog entries from a given category. Entries belonging to subcategories 9: * are not returned. 10: * 11: * @param category The name of the category (XDocument full name, for example 'MyBlog.Fishing'). 12: * @param articles Return parameter, where the list of entries is placed. 13: * @param total Return parameter, where the total number of entries belonging to this category is 14: * placed. Useful for a paginated view. 15: *### 16: #macro(getEntriesForCategory $category $entries $totalEntries) 17: #getCategoriesHierarchy('' $tree) 18: #set($subcategories = $util.arrayList) 19: #getSubcategories($tree $category $subcategories) 20: #set($categories = [${category}]) 21: #set($discard = $categories.addAll(${subcategories})) 22: #set($parameters = '?') 23: #foreach($subcategory in $subcategories) 24: #set($parameters = $parameters.concat(', ?')) 25: #end 26: #getBlogEntriesBaseQuery($query) 27: #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})") 28: #set($totalEntries = $xwiki.countDocuments(${query}, ${categories})) 29: #preparePagedViewParams($totalEntries 10) 30: #set($entries = $xwiki.searchDocuments("${query} order by publishDate.value desc", $itemsPerPage, $startAt, ${categories})) 31: #end 32: #macro(getSubcategories $tree $category $subcategories) 33: #if(!$subcategories.contains($category)) 34: #foreach($subcategory in $tree.get($category)) 35: #set($discard = $subcategories.add($subcategory)) 36: #getSubcategories($tree $subcategory $subcategories) 37: #end 38: #end 39: #end 40: ## 41: ## 42: ## 43: #** 44: * Builds a tree of categories, respecting the parent<->subcategory relation. Each node holds the 45: * full name of the category's document. The root of the tree is 'Blog.Categories'. 46: * 47: * @param space The space where to search for categories. If this parameter is an emptry string or 48: * null, all the categories in the wiki are returned. 49: * @param tree Return parameter, HashMap<String, List<String>> structure holding the categories 50: * hierarchy, where the key is the name of a category, and the value contains the names of 51: * all its subcategories. To obtain the full hierarchy, start with the key 'Blog.Categories'. 52: *### 53: #macro(getCategoriesHierarchy $space $tree) 54: #set($tree = $util.hashMap) 55: #set($query = ', BaseObject obj where ') 56: #if("$!space" != '') 57: #set($query = "${query}doc.space = '${space}' and ") 58: #end 59: #set($query = "${query}obj.name = doc.fullName and obj.className = '${blogCategoryClassname}' order by doc.name") 60: #foreach($category in $xwiki.searchDocuments($query)) 61: #set($categoryDoc = $xwiki.getDocument($category)) 62: #set($categoryParent = "$!categoryDoc.parent") 63: #if($categoryParent == '') 64: #set($categoryParent = $defaultCategoryParent) 65: #end 66: #if(!$tree.containsKey($categoryParent)) 67: #set($discard = $tree.put($categoryParent, $util.arrayList)) 68: #end 69: #set($discard = $tree.get($categoryParent).add($category)) 70: #end 71: #end 72: ## 73: ## 74: ## 75: #** 76: * Displays the category hierarchy held in the <tt>tree</tt> parameter. 77: * 78: * @param tree The category hierarchy, a HashMap<String, List<String>> structure, where the key 79: * is the name of a category, and the value contains the names of all its subcategories. 80: * @param displayMethod Selects how to display the category tree. Possible values are: 81: * <ul> 82: * <li><em>"simple"</em>: tree with links to the category pages.</li> 83: * <li><em>"selectable"</em>: each category name in the tree is preceded by a checkbox.</li> 84: * <li><em>"option"</em>: wraps each category name in an <option> element, to be used 85: * in a select box.</li> 86: * <li><em>"editable"</em>: displays links to delete and edit each category, if the rights 87: * allow such actions.</li> 88: * </ul> 89: * For any other value, the default ("simple") is considered. 90: *### 91: #macro(displayCategoriesHierarchy $tree $displayMethod) 92: #set($processedCategories = $util.arrayList) 93: #displayCategoriesHierarchyRecursive($tree $defaultCategoryParent 1 $displayMethod) 94: #end 95: ## 96: ## 97: ## 98: #** 99: * Displays recursively the category hierarchy held in the <tt>tree</tt> parameter, starting at 100: * the node indicated by the <tt>root</tt> parameter, which is on the <tt>level</tt>th level in 101: * the tree. 102: * 103: * @param tree The category hierarchy HashMap<String, List<String>> structure, where the key 104: * is the name of a category, and the value contains the names of all its subcategories. 105: * @param root The full name of the document containing the category that is to be considered the 106: * root of the displayed subtree. 107: * @param level The current depth of the tree, used for proper indentation. 108: * @param displayMethod Selects how to display the category tree. Possible values are: 109: * <ul> 110: * <li><em>"simple"</em>: tree with links to the category pages.</li> 111: * <li><em>"selectable"</em>: each category name in the tree is preceded by a checkbox.</li> 112: * <li><em>"option"</em>: wraps each category name in an <option> element, to be used 113: * in a select box.</li> 114: * <li><em>"editable"</em>: displays links to delete and edit each category, if the rights 115: * allow such actions.</li> 116: * </ul> 117: * For any other value, the default ("simple") is considered. 118: *### 119: #macro(displayCategoriesHierarchyRecursive $tree $root $level $displayMethod) 120: #if(!$processedCategories) 121: #set($processedCategories = $util.arrayList) 122: #end 123: #foreach($item in $tree.get($root)) 124: #if(!$processedCategories.contains($item)) 125: #set($discard = $processedCategories.add($item)) 126: #displayCategory($item $level $displayMethod) 127: #displayCategoriesHierarchyRecursive($tree $item $mathtool.add($level, 1) $displayMethod) 128: #end 129: #end 130: #end 131: ## 132: ## 133: ## 134: #** 135: * Displays a category as part of a category hierarchy. 136: * 137: * @param name The full name of the document containing the category to be displayed. 138: * @param level The depth where this category is in the tree, used for proper indentation. 139: * @param displayMethod Selects how to display the category tree. Possible values are: 140: * <ul> 141: * <li><em>"simple"</em>: tree with links to the category pages.</li> 142: * <li><em>"selectable"</em>: each category name in the tree is preceded by a checkbox.</li> 143: * <li><em>"option"</em>: wraps each category name in an <option> element, to be used 144: * in a select box.</li> 145: * <li><em>"editable"</em>: displays links to delete and edit each category, if the rights 146: * allow such actions.</li> 147: * </ul> 148: * For any other value, the default ("simple") is considered. 149: *### 150: #macro(displayCategory $name $level $displayMethod) 151: #if("$!displayMethod" == "option") 152: #displayOptionCategory($name $level) 153: #elseif("$!displayMethod" == "selectable") 154: #displaySelectableCategory($name $level) 155: #elseif("$!displayMethod" == "editable") 156: #displayEditableCategory($name $level) 157: #else 158: #displaySimpleCategory($name $level) 159: #end 160: #end 161: ## 162: ## 163: ## 164: #** 165: * Displays a category as part of a category hierarchy, preceded by a checkbox that allows choosing 166: * this category for a blog entry. 167: * 168: * @param name The full name of the document containing the category to be displayed. 169: * @param level The depth where this category is in the tree, used for proper indentation. 170: *### 171: #macro(displaySelectableCategory $name $level) 172: #set($categoryDoc = $xwiki.getDocument($name)) 173: #set($nameUrl = $escapetool.url($name)) 174: #foreach($i in [1..$level])*#end ## 175: <span class="blog-category-level"><span class="blog-category">## 176: <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>## 177: </span>## 178: #if($xwiki.hasAccessLevel('edit', $context.user, $doc.fullName) && ("$!{request.xaction}" != "showAddCategory" || "$!{request.parentCategory}" != $name)) 179: <span class="blog-category-tools">## 180: <a href="$doc.getURL('view', "xaction=showAddCategory&parentCategory=$nameUrl")" class="tool add-subcategory">#toolImage('chart_organisation_add' 'Add a subcategory ')</a>## 181: </span>## 182: #end 183: </span> 184: #end 185: ## 186: ## 187: ## 188: #** 189: * Displays a form for creating a new category. If a parentCategory parameter is present in the 190: * query string, the parent category is set accordingly. Otherwise, the form provides a selection 191: * control for choosing the parent category among existing categories. 192: *### 193: ## DO NOT CHANGE INDENTATION 194: #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 195: ## 196: ## 197: ## 198: #** 199: * Displays a form for renaming a category. 200: *### 201: ## DO NOT CHANGE INDENTATION 202: #macro(renameCategoryForm)## 203: <form action="$doc.getURL()" method="post" class="category-rename-form"><div class='rename-category'>## 204: <input type="hidden" name="xaction" value="rename"/>## 205: <input type="hidden" name="category" value="${escapetool.xml($request.category)}"/>## 206: <label>$msg.get("xe.blog.categories.newName")<br/> <input type="text" name="newCategoryName" class="category-name-input"></input></label><br/>## 207: <span class="buttonwrapper"><input class="button" type="submit" value="Rename"></input></span> ## 208: <a href="$doc.getURL()">Cancel</a>## 209: </div></form>## 210: #end 211: ## 212: ## 213: ## 214: #** 215: * Displays a category as part of a category hierarchy, followed by links for editing and deleting 216: * this category, if the current user has the rights to perform these actions. 217: * 218: * @param name The full name of the document containing the category to be displayed. 219: * @param level The depth where this category is in the tree, used for proper indentation. 220: *### 221: ## DO NOT CHANGE INDENTATION 222: #macro(displayEditableCategory $name $level) 223: #getEntriesForCategory($name $discard $totalEntries) 224: #set($nameUrl = $escapetool.url($name)) 225: #foreach($i in [1..$level])*#end ## 226: <span class="blog-category-level"><span class="blog-category">## 227: <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>## 228: [[#getCategoryName($xwiki.getDocument($name)) (% class="itemCount" %)($totalEntries)(%%)>>${name}]]</span> ## 229: <span class="blog-category-tools">## 230: #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 ## 231: #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 ## 232: #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 ## 233: </span>## 234: #if($xwiki.hasAccessLevel('edit', $context.user, $doc.fullName) && "$!{request.xaction}" == "showRenameCategory" && "$!{request.category}" == $name) #renameCategoryForm() #end## 235: #if($xwiki.hasAccessLevel('edit', $context.user, $doc.fullName) && "$!{request.xaction}" == "showAddCategory" && "$!{request.parentCategory}" == $name) #addCategoryForm() #end## 236: </span> 237: #end 238: ## 239: ## 240: ## 241: #** 242: * Displays a category as part of a category hierarchy, wrapped in an <option> element, to 243: * be used in a select box. 244: * 245: * @param name The full name of the document containing the category to be displayed. 246: * @param level The depth where this category is in the tree, used for proper indentation. 247: *### 248: #macro(displayOptionCategory $name $level) 249: <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> 250: #end 251: ## 252: ## 253: ## 254: #** 255: * Displays a category as part of a category hierarchy, wrapped in a link. 256: * 257: * @param name The full name of the document containing the category to be displayed. 258: * @param level The depth where this category is in the tree, used for proper indentation. 259: *### 260: #macro(displaySimpleCategory $name $level) 261: #getEntriesForCategory($name $discard $totalEntries) 262: #set($nameUrl = $escapetool.url($name)) 263: #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> 264: #end 265: ## 266: ## 267: ## 268: #** 269: * Prints the name of a category, indicated by its document. 270: * The result is XML-escaped 271: * 272: * @param categoryDoc The document containing the category to be displayed. 273: *### 274: #macro(getCategoryName $categoryDoc) 275: ## Don't indent! 276: #set($result = "$!categoryDoc.getObject(${blogCategoryClassname}).getProperty('name').value.trim()")## 277: #if($result == '') 278: #set($result = $categoryDoc.name) 279: #end 280: $escapetool.xml($result)## 281: #end 282: ## 283: ## 284: ## 285: #** 286: * Prints the description of a category, indicated by its document. 287: * The result is XML-escaped 288: * 289: * @param categoryDoc The document containing the category to be displayed. 290: *### 291: #macro(getCategoryDescription $categoryDoc) 292: ## Don't indent! 293: $escapetool.xml($!categoryDoc.getObject(${blogCategoryClassname}).getProperty('description').value.trim())## 294: #end 295: ## 296: ## 297: ## 298: #** 299: * Generates a form for creating a new category. The form allows to enter the name of the new 300: * category, and select a parent category from the existing ones. 301: * 302: * @param tree The category hierarchy, a HashMap<String, List<String>> structure, where the key 303: * is the name of a category, and the value contains the names of all its subcategories. 304: * @todo When javascript is disabled, a link to "Manage categories" should be displayed instead. 305: * This "form" should be created from javascript. 306: *### 307: #macro(showCreateCategoryBoxWithForm $tree) 308: <form action="" method="post"> 309: #showCreateCategoryBox($tree) 310: </form> 311: #end 312: #** 313: * Generates a box for creating a new category. This allows to enter the name of the new 314: * category, and select a parent category from the existing ones. Note that this does not create 315: * a HTML form element, but requires one to be defined already as its ancestor. 316: * 317: * @param tree The category hierarchy HashMap<String, List<String>> structure, where the key 318: * is the name of a category, and the value contains the names of all its subcategories. 319: * @todo When javascript is disabled, a link to "Manage categories" should be displayed instead. 320: * This "form" should be created from javascript. 321: *### 322: #macro(showCreateCategoryBox $tree) 323: <div class='create-category'> 324: <input type="hidden" name="xaction" value="create"/> 325: <label>$msg.get("xe.blog.categories.new") <input type="text" name="newCategoryName"></input></label> 326: <label>$msg.get("xe.blog.categories.parent") 327: <select name="newCategoryParent" id="blog_category_selectBox"> 328: <option value="${defaultCategoryParent}" selected="selected">None</option> 329: $!processedCategories.clear()## 330: #displayCategoriesHierarchy($tree 'option') 331: </select> 332: </label> 333: <span class="buttonwrapper"><input class="button" type="button" value="Add" id="blog_AddCategoryButton"></input></span> 334: </div> 335: #end 336: ## 337: ## 338: ## 339: #macro(displayCategoryManagementTree $space $displayType) 340: <div class="blog-categories-list"> 341: #getCategoriesHierarchy("$!{space}" $tree) 342: #displayCategoriesHierarchy($tree $displayType) 343: #if($xwiki.hasAccessLevel('edit', $context.user, $doc.fullName)) 344: * <span class="blog-add-category-label"><a href="$doc.getURL('view', "xaction=showAddCategory&parentCategory=")">$msg.get("xe.blog.categories.addcategory")</a></span> 345: #if("$!{request.xaction}" == "showAddCategory" && "$!{request.parentCategory}" == "") #addCategoryForm() #end 346: #end 347: 348: 349: </div> 350: #end 351: ## 352: ## 353: ## 354: #** 355: * Deletes a category, moving all the subcategories to its parent and removing this category from 356: * all existing blog entries. 357: * 358: * @param category The full name of the document containing the category to be deleted. 359: *### 360: #macro(deleteCategory $category) 361: #set($categoryDoc = $xwiki.getDocument($category)) 362: #set($categoryParent = "$!categoryDoc.parent") 363: #if($categoryParent == '') 364: #set($categoryParent = "{$defaultCategoryParent}")) 365: #end 366: #set($parameterValues = ["$!{category}"]) 367: #set($query = ', BaseObject obj where ') 368: #if($space != '') 369: #set($query = "${query}doc.space = '${space}' and ") 370: #end 371: #set($query = "${query}obj.name = doc.fullName and obj.className = '${blogCategoryClassname}' and doc.fullName <> 'Blog.CategoryTemplate' and doc.parent = ? order by doc.name") 372: #foreach($item in $xwiki.searchDocuments($query, $parameterValues)) 373: #if($xwiki.hasAccessLevel('edit', $context.user, $item)) 374: #set($subcategoryDoc = $xwiki.getDocument($item)) 375: $subcategoryDoc.setParent($categoryParent) 376: $subcategoryDoc.save($msg.get('xe.blog.manageCategories.comment.updatedParent'), true) 377: #end 378: #end 379: #set($query = ', BaseObject obj, DBStringListProperty categories join categories.list as category where ') 380: #if($space != '') 381: #set($query = "${query}doc.space = '${space}' and ") 382: #end 383: #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") 384: #foreach($item in $xwiki.searchDocuments($query, $parameterValues)) 385: #if($xwiki.hasAccessLevel('edit', $context.user, $item)) 386: #set($blogEntryDoc = $xwiki.getDocument($item)) 387: #set($discard = $blogEntryDoc.getObject(${blogPostClassname}).getProperty('category').value.remove($category)) 388: $blogEntryDoc.save($msg.get('xe.blog.manageCategories.comment.removedDeletedCategory'), true) 389: #end 390: #end 391: $categoryDoc.delete() 392: #end 393: ## 394: ## 395: ## 396: #** 397: * Renames a category, updating all the subcategories and all existing blog entries. 398: * 399: * @param category The full name of the document containing the category to be renamed. 400: * @param newCategoryName The new name of the category. 401: *### 402: #macro(renameCategory $category $newCategoryName) 403: #set($categoryDoc = $xwiki.getDocument($category)) 404: #set($newCategoryDoc = $xwiki.getDocument($newCategoryName)) 405: #set($parameterValues = ["$!{category}"]) 406: #set($query = ', BaseObject obj where ') 407: #set($query = "${query}obj.name = doc.fullName and obj.className = '${blogCategoryClassname}' and doc.fullName <> 'Blog.CategoryTemplate' and doc.parent = ? order by doc.name") 408: #foreach($item in $xwiki.searchDocuments($query, $parameterValues)) 409: #if($xwiki.hasAccessLevel('edit', $context.user, $item)) 410: #set($subcategoryDoc = $xwiki.getDocument($item)) 411: $subcategoryDoc.setParent($newCategoryDoc.fullName) 412: $subcategoryDoc.save($msg.get('xe.blog.manageCategories.comment.updatedParent'), true) 413: #end 414: #end 415: #set($query = ', BaseObject obj, DBStringListProperty categories join categories.list as category where ') 416: #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") 417: #foreach($item in $xwiki.searchDocuments($query, $parameterValues)) 418: #if($xwiki.hasAccessLevel('edit', $context.user, $item)) 419: #set($blogEntryDoc = $xwiki.getDocument($item)) 420: #set($discard = $blogEntryDoc.getObject(${blogPostClassname}).getProperty('category').value.remove($category)) 421: #set($discard = $blogEntryDoc.getObject(${blogPostClassname}).getProperty('category').value.add($newCategoryDoc.fullName)) 422: $blogEntryDoc.save($msg.get('xe.blog.manageCategories.comment.updatedRenamedCategory'), true) 423: #end 424: #end 425: $categoryDoc.getObject('Blog.CategoryClass').set('name', $newCategoryName) 426: $categoryDoc.save($msg.get('xe.blog.manageCategories.comment.updatedCategory'), true) 427: $categoryDoc.rename($newCategoryName) 428: #end 429: {{/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