Macros for the Blog Categories
Log-in
|
Blog
:
Welcome to your wiki
>
Wiki blog
>
Macros for the Blog Categories
Top Menu
Show
:
Comments
Attachments
History
Information
Stampa
:
Stampa
Anteprima di stampa
Esporta come PDF
Esporta in formato RTF
Esporta come HTML
Esporta in formato XAR
Wiki source code of
Macros for the Blog Categories
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> <a href="$xwiki.getURL($name)">#getCategoryName($xwiki.getDocument($name)) <span class="itemcount">($totalEntries)</span></a> 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(showCreateCategoryBoxWithForm $tree) 251: <form action="" method="post"> 252: #showCreateCategoryBox($tree) 253: </form> 254: #end 255: #** 256: * Generates a box for creating a new category. This allows to enter the name of the new 257: * category, and select a parent category from the existing ones. Note that this does not create 258: * a HTML form element, but requires one to be defined already as its ancestor. 259: * 260: * @param tree The category hierarchy HashMap<String, List<String>> structure, where the key 261: * is the name of a category, and the value contains the names of all its subcategories. 262: * @todo When javascript is disabled, a link to "Manage categories" should be displayed instead. 263: * This "form" should be created from javascript. 264: *### 265: #macro(showCreateCategoryBox $tree) 266: <div class=’create-category’> 267: <input type="hidden" name="xaction" value="create"/> 268: <label>$msg.get("xe.blog.categories.new") <input type="text" name="newCategoryName"></input></label> 269: <label>$msg.get("xe.blog.categories.parent") 270: <select name="newCategoryParent" id="blog_category_selectBox"> 271: <option value="${defaultCategoryParent}" selected="selected">None</option> 272: $!processedCategories.clear()## 273: #displayCategoriesHierarchy($tree ’option’) 274: </select> 275: </label> 276: <span class="buttonwrapper"><input class="button" type="button" value="Add" id="blog_AddCategoryButton"></input></span> 277: </div> 278: #end 279: ## 280: ## 281: ## 282: #** 283: * Deletes a category, moving all the subcategories to its parent and removing this category from 284: * all existing blog entries. 285: * 286: * @param category The full name of the document containing the category to be deleted. 287: *### 288: #macro(deleteCategory $category) 289: #set($categoryDoc = $xwiki.getDocument($category)) 290: #set($categoryParent = "$!categoryDoc.parent") 291: #if($categoryParent == ’’) 292: #set($categoryParent = "{$defaultCategoryParent}")) 293: #end 294: #set($query = ’, BaseObject obj where ’) 295: #if($space != ’’) 296: #set($query = "${query}doc.space = ’${space}’ and ") 297: #end 298: #set($query = "${query}obj.name = doc.fullName and obj.className = ’${blogCategoryClassname}’ and doc.fullName <> ’Blog.CategoryTemplate’ and doc.parent = ’$category’ order by doc.name") 299: #foreach($item in $xwiki.searchDocuments($query)) 300: #if($xwiki.hasAccessLevel(’edit’, $context.user, $item)) 301: #set($subcategoryDoc = $xwiki.getDocument($item)) 302: $subcategoryDoc.setParent($categoryParent) 303: $subcategoryDoc.save(’Update category parent’) 304: #end 305: #end 306: #set($query = ’, BaseObject obj, DBStringListProperty categories join categories.list as category where ’) 307: #if($space != ’’) 308: #set($query = "${query}doc.space = ’${space}’ and ") 309: #end 310: #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") 311: #foreach($item in $xwiki.searchDocuments($query)) 312: #if($xwiki.hasAccessLevel(’edit’, $context.user, $item)) 313: #set($blogEntryDoc = $xwiki.getDocument($item)) 314: #set($discard = $blogEntryDoc.getObject(${blogPostClassname}).getProperty(’category’).value.remove($category)) 315: $blogEntryDoc.save(’$msg.get("xe.blog.categories.remove")’) 316: #end 317: #end 318: $categoryDoc.delete() 319: #end
Recent Blog Posts
The first post of your wiki's blog
Cannot make recursive include
Blog Categories
News
(1)
Other
(0)
Personal
(0)
Blog Archive
2009
(1)
Search
Quick Links
Wiki Dashboard
Document Index
Blog
Sandbox
My Recent Modifications
dany
|
marco