Wiki source code of Macros for the Blog Categories

Last modified by Ludovic Dubost on 2022/01/27 10:59

Show last authors
1 {{include reference="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 entries Return parameter, where the list of entries is placed.
13 * @param totalEntries 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 #set ($entries = $NULL)
18 #set ($totalEntries = $NULL)
19 #if ("$!{blogCategoryEntriesCache.containsKey($!{category})}" == 'true')
20 #setVariable ("$entries" $blogCategoryEntriesCache.get($!{category}).get(0))
21 #setVariable ("$totalEntries" $blogCategoryEntriesCache.get($!{category}).get(1))
22 #preparePagedViewParams ($totalEntries 10)
23 #else
24 #getCategoriesHierarchy ('' $tree)
25 #set ($subcategories = [])
26 #getSubcategories ($tree $category $subcategories)
27 #set ($categories = [])
28 #set ($parameters = '')
29 ## check if it a categories space
30 #set ($categoryDoc = $xwiki.getDocument($category))
31 #if ("$!categoryDoc.getObject($blogCategoryClassname)" != '')
32 #set ($discard = $categories.add($category))
33 #set ($parameters = '?')
34 #end
35 #set ($discard = $categories.addAll(${subcategories}))
36 #foreach ($subcategory in $subcategories)
37 #if ($parameters != '')
38 #set ($parameters = $parameters.concat(', '))
39 #end
40 #set ($parameters = $parameters.concat('?'))
41 #end
42 #getAllBlogPostsQuery($query)
43 #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})")
44 #if ($categories.size() > 0)
45 #set ($totalResult = $services.query.hql($query).bindValues($categories).addFilter("unique").count())
46 #preparePagedViewParams ($totalResult 10)
47 #set ($result = $services.query.hql("${query} order by publishDate.value desc").setLimit($itemsPerPage).setOffset($startAt).bindValues($categories).addFilter("unique").execute())
48 #else
49 #set ($totalResult = 0)
50 #set ($result = [])
51 #end
52 #if ("$!{blogCategoryEntriesCache.containsKey($!{category})}" == '')
53 #set ($blogCategoryEntriesCache = {})
54 #end
55 #set ($discard = $blogCategoryEntriesCache.put("$!{category}", [$result, $totalResult]))
56 #setVariable ("$entries" $result)
57 #setVariable ("$totalEntries" $totalResult)
58 #end
59 #end
60 #macro(getSubcategories $tree $category $subcategories)
61 #foreach($subcategory in $tree.get($category))
62 #set($discard = $subcategories.add($subcategory))
63 #getSubcategories($tree $subcategory $subcategories)
64 #end
65 #end
66 ##
67 ##
68 ##
69 #**
70 * Builds a tree of categories, respecting the parent<->subcategory relation. Each node holds the
71 * full name of the category's document. The root of the tree is 'Blog.Categories' or 'aCategorySpace.WebHome'.
72 *
73 * @param space The space where to search for categories. If this parameter is an emptry string or
74 * null, all the categories in the wiki are returned.
75 * @param tree Return parameter, HashMap<String, List<String>> structure holding the categories
76 * hierarchy, where the key is the name of a category, and the value contains the names of
77 * all its subcategories. To obtain the full hierarchy, start with the key 'Blog.Categories'.
78 *###
79 #macro(getCategoriesHierarchy $space $tree)
80 #set ($tree = $NULL)
81 #if ("$!{blogCategoriesHierarchyCache.containsKey($!{space})}" == 'true')
82 #setVariable ("$tree" $blogCategoriesHierarchyCache.get($!{space}))
83 #else
84 #set ($result = {})
85 #set($query = ', BaseObject obj where ')
86 #if("$!space" != '')
87 #set($query = "${query}doc.space = :space and ")
88 #end
89 #set($query = "${query}obj.name = doc.fullName and obj.className = '${blogCategoryClassname}' order by doc.name")
90 #set($queryObj = $services.query.hql($query))
91 #if("$!space" != '')
92 #set($queryObj = $queryObj.bindValue('space', $space))
93 #end
94 #foreach($category in $queryObj.execute())
95 #set($categoryDoc = $xwiki.getDocument($category))
96 #set($categoryParent = "$!categoryDoc.parent")
97 #if($categoryParent == '')
98 #set($categoryParent = $defaultCategoryParent)
99 #end
100 #if(!$result.containsKey($categoryParent))
101 #set($discard = $result.put($categoryParent, []))
102 #end
103 #set($discard = $result.get($categoryParent).add($category))
104 #end
105 #if ("$!{blogCategoriesHierarchyCache.containsKey($!{space})}" == '')
106 #set ($blogCategoriesHierarchyCache = {})
107 #end
108 #set ($discard = $blogCategoriesHierarchyCache.put("$!{space}", $result))
109 #setVariable ("$tree" $result)
110 #end
111 #end
112 ##
113 ##
114 ##
115 #**
116 * Displays the category hierarchy held in the <tt>tree</tt> parameter.
117 *
118 * @param tree The category hierarchy, a HashMap<String, List<String>> structure, where the key
119 * is the name of a category, and the value contains the names of all its subcategories.
120 * @param displayMethod Selects how to display the category tree. Possible values are:
121 * <ul>
122 * <li><em>"simple"</em>: tree with links to the category pages.</li>
123 * <li><em>"selectable"</em>: each category name in the tree is preceded by a checkbox.</li>
124 * <li><em>"option"</em>: wraps each category name in an &lt;option&gt; element, to be used
125 * in a select box.</li>
126 * <li><em>"editable"</em>: displays links to delete and edit each category, if the rights
127 * allow such actions.</li>
128 * </ul>
129 * For any other value, the default ("simple") is considered.
130 *###
131 #macro(displayCategoriesHierarchy $tree $displayMethod)
132 #set($processedCategories = [])
133 #displayCategoriesHierarchyRecursive($tree $defaultCategoryParent 1 $displayMethod)
134 #end
135 ##
136 ##
137 ##
138 #**
139 * Displays recursively the category hierarchy held in the <tt>tree</tt> parameter, starting at
140 * the node indicated by the <tt>root</tt> parameter, which is on the <tt>level</tt>th level in
141 * the tree.
142 *
143 * @param tree The category hierarchy HashMap<String, List<String>> structure, where the key
144 * is the name of a category, and the value contains the names of all its subcategories.
145 * @param root The full name of the document containing the category that is to be considered the
146 * root of the displayed subtree.
147 * @param level The current depth of the tree, used for proper indentation.
148 * @param displayMethod Selects how to display the category tree. Possible values are:
149 * <ul>
150 * <li><em>"simple"</em>: tree with links to the category pages.</li>
151 * <li><em>"selectable"</em>: each category name in the tree is preceded by a checkbox.</li>
152 * <li><em>"option"</em>: wraps each category name in an &lt;option&gt; element, to be used
153 * in a select box.</li>
154 * <li><em>"editable"</em>: displays links to delete and edit each category, if the rights
155 * allow such actions.</li>
156 * </ul>
157 * For any other value, the default ("simple") is considered.
158 *###
159 #macro(displayCategoriesHierarchyRecursive $tree $root $level $displayMethod)
160 #if(!$processedCategories)
161 #set($processedCategories = [])
162 #end
163 #foreach($item in $tree.get($root))
164 #if(!$processedCategories.contains($item))
165 #set($discard = $processedCategories.add($item))
166 #displayCategory($item $level $displayMethod)
167 #displayCategoriesHierarchyRecursive($tree $item $mathtool.add($level, 1) $displayMethod)
168 #end
169 #end
170 #if($displayMethod == "selectable")
171 #set ($entryObjNumber = 0)
172 #if("$!entryObj.number" != '')
173 #set ($entryObjNumber = $entryObj.number)
174 #end
175 <input type="hidden" name="${blogPostClassname}_$!{entryObjNumber}_category" value="" />
176 #end
177 #end
178 ##
179 ##
180 ##
181 #**
182 * Displays a category as part of a category hierarchy.
183 *
184 * @param name The full name of the document containing the category to be displayed.
185 * @param level The depth where this category is in the tree, used for proper indentation.
186 * @param displayMethod Selects how to display the category tree. Possible values are:
187 * <ul>
188 * <li><em>"simple"</em>: tree with links to the category pages.</li>
189 * <li><em>"selectable"</em>: each category name in the tree is preceded by a checkbox.</li>
190 * <li><em>"option"</em>: wraps each category name in an &lt;option&gt; element, to be used
191 * in a select box.</li>
192 * <li><em>"editable"</em>: displays links to delete and edit each category, if the rights
193 * allow such actions.</li>
194 * </ul>
195 * For any other value, the default ("simple") is considered.
196 *###
197 #macro(displayCategory $name $level $displayMethod)
198 #if("$!displayMethod" == "option")
199 #displayOptionCategory($name $level)
200 #elseif("$!displayMethod" == "selectable")
201 #displaySelectableCategory($name $level)
202 #elseif("$!displayMethod" == "editable")
203 #displayEditableCategory($name $level)
204 #else
205 #displaySimpleCategory($name $level)
206 #end
207 #end
208 ##
209 ##
210 ##
211 #**
212 * Displays a category as part of a category hierarchy, preceded by a checkbox that allows choosing
213 * this category for a blog entry.
214 *
215 * @param name The full name of the document containing the category to be displayed.
216 * @param level The depth where this category is in the tree, used for proper indentation.
217 *###
218 #macro(displaySelectableCategory $name $level)
219 #set($categoryDoc = $xwiki.getDocument($name))
220 #set($addCategURL = $doc.getURL('view', $escapetool.url({
221 'xaction': 'showAddCategory',
222 'parentCategory' : $name
223 })))
224 #set($addEntryParams = false)
225 #if($isBlogPost)
226 #set($entry = $xwiki.getDocument($doc.fullName))
227 #set($entryObj = $isBlogPost)
228 #set($addEntryParams = true)
229 #elseif("$!request.entry" != '' && "$!request.entryObjNb" != '')
230 #set($entry = $xwiki.getDocument($request.entry))
231 #set($entryObj = $entry.getObject($blogPostClassname, $numbertool.toNumber($request.entryObjNb).intValue()))
232 #set($addEntryParams = true)
233 #end
234 #if($isBlogPost || $addEntryParams)
235 ## parentCategory must be the last param
236 #set($addCategURL = $doc.getURL('view', $escapetool.url({
237 'xaction': 'showAddCategory',
238 'entry': $entry.fullName,
239 'entryObjNb': $entryObj.number,
240 'parentCategory': $name
241 })))
242 #end
243 #foreach($i in [1..$level])*#end ##
244 #set ($entryObjNumber = 0)
245 #if("$!entryObj.number" != '')
246 #set ($entryObjNumber = $entryObj.number)
247 #end
248 <span class="blog-category-level"><span class="blog-category">##
249 <label id='blog_category_${services.rendering.escape(${escapetool.xml($name)}, $xwiki.currentContentSyntaxId)}' title="#getCategoryDescription($categoryDoc)"><input name="${blogPostClassname}_$!{entryObjNumber}_category" value="$services.rendering.escape(${escapetool.xml($name)}, $xwiki.currentContentSyntaxId)" type="checkbox"#if($entryObj.getProperty('category').getValue().contains($name)) checked="checked" #end/> #getCategoryName($categoryDoc)</label>##
250 </span>##
251 #if($xwiki.hasAccessLevel('edit', $xcontext.user, $doc.fullName) && ("$!{request.xaction}" != "showAddCategory" || "$!{request.parentCategory}" != $name))
252 <span class="blog-category-tools">##
253 <a href="$escapetool.xml($addCategURL)" class="tool add-subcategory">#toolImage('add')</a>##
254 </span>##
255 #end
256 </span>
257 #end
258 ##
259 ##
260 ##
261 #**
262 * Displays a form for creating a new category. If a parentCategory parameter is present in the
263 * query string, the parent category is set accordingly. Otherwise, the form provides a selection
264 * control for choosing the parent category among existing categories.
265 *###
266 ## DO NOT CHANGE INDENTATION
267 #macro(addCategoryForm) #set($addCategURL = $doc.getURL()) #if("$!request.entry" != '') #set($addCategURL = "${addCategURL}?entry=$escapetool.url($request.entry)&amp;entryObjNb=$escapetool.url($!request.entryObjNb)")#end<form action="${addCategURL}" method="post" class="category-add-form"><div class='create-category'> <input type="hidden" name="form_token" value="$!{services.csrf.getToken()}" /> <input type="hidden" name="xaction" value="create"/> <label>$services.localization.render('blog.categories.new')<br/> <input type="text" name="newCategoryName" class="category-name-input" /></label><br/>#if("$!{request.parentCategory}" == "")<label>#* $services.localization.render('blog.categories.parent')*# $escapetool.xml($services.localization.render('blog.manageCategories.forms.sub_cat_of'))<br/> <select name="newCategoryParent" id="blog_category_selectBox" class="category-add-input"> <option value="${escapetool.xml($defaultCategoryParent)}" selected="selected">$escapetool.xml($services.localization.render('blog.manageCategories.forms.select_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="$escapetool.xml($services.localization.render('blog.manageCategories.forms.add_button_label'))" /></span> <a class="btn btn-default" href="$doc.getURL()">$escapetool.xml($services.localization.render('blog.manageCategories.forms.cancel_button_label'))</a> </div></form> #end
268 ##
269 ##
270 ##
271 #**
272 * Displays a form for renaming a category.
273 *###
274 ## DO NOT CHANGE INDENTATION
275 #macro(renameCategoryForm)##
276 <form action="$doc.getURL()" method="post" class="category-rename-form"><div class='rename-category'>##
277 <input type="hidden" name="form_token" value="$!{services.csrf.getToken()}" />
278 <input type="hidden" name="xaction" value="rename"/>##
279 <input type="hidden" name="category" value="${escapetool.xml($request.category)}"/>##
280 <label>$services.localization.render('blog.categories.newName')<br/> <input type="text" name="newCategoryName" class="category-name-input" /></label><br/>##
281 <span class="buttonwrapper"><input class="button" type="submit" value="$escapetool.xml($services.localization.render('blog.manageCategories.forms.rename_button_label'))" /></span> ##
282 <a class="btn btn-default" href="$doc.getURL()">$escapetool.xml($services.localization.render('blog.manageCategories.forms.cancel_button_label'))</a>##
283 </div></form>##
284 #end
285 ##
286 ##
287 ##
288 #**
289 * Displays a category as part of a category hierarchy, followed by links for editing and deleting
290 * this category, if the current user has the rights to perform these actions.
291 *
292 * @param name The full name of the document containing the category to be displayed.
293 * @param level The depth where this category is in the tree, used for proper indentation.
294 *###
295 ## DO NOT CHANGE INDENTATION
296 #macro(displayEditableCategory $name $level)
297 #getEntriesForCategory($name $discard $totalEntries)
298 #set($nameUrl = $escapetool.url($name))
299 #foreach($i in [1..$level])*#end ##
300 <span class="blog-category-level"><span class="blog-category">##
301 <a href="$services.rendering.escape($xwiki.getURL('Blog.CategoryRss', 'view', "xpage=plain&amp;category=$nameUrl"), $doc.syntax)" title="RSS">#toolImage('rss')</a>&nbsp;##
302 <span class="wikilink"><a href="$services.rendering.escape($xwiki.getURL($name), $doc.syntax)">#getCategoryName($xwiki.getDocument($name)) <span class="itemCount">($totalEntries)</span></a></span></span>##
303 <span class="blog-category-tools">##
304 #if($xwiki.hasAccessLevel('delete', $xcontext.user, $name) && ("$!{request.xaction}" != 'showRenameCategory' || "$!{request.category}" != $name))<a href="$services.rendering.escape($xwiki.getURL('Blog.ManageCategories', 'view', "xaction=showRenameCategory&amp;category=$nameUrl"), $doc.syntax)" class="tool rename">#toolImage('pencil')</a>#end##
305 #if($xwiki.hasAccessLevel('edit', $xcontext.user, $doc.fullName) && ("$!{request.xaction}" != "showAddCategory" || "$!{request.parentCategory}" != $name))<a href="$services.rendering.escape($xwiki.getURL('Blog.ManageCategories', 'view', "xaction=showAddCategory&amp;parentCategory=$nameUrl"), $doc.syntax)" class="tool add-subcategory">#toolImage('add')</a>#end##
306 #if($xwiki.hasAccessLevel('delete', $xcontext.user, $name)) <a href="$services.rendering.escape($xwiki.getURL('Blog.ManageCategories', 'view', "xaction=delete&amp;category=$nameUrl&amp;form_token=$!{services.csrf.getToken()}"), $doc.syntax)" class="tool delete">#toolImage('cross')</a>#end##
307 </span>##
308 #if($xwiki.hasAccessLevel('edit', $xcontext.user, $doc.fullName) && "$!{request.xaction}" == "showRenameCategory" && "$!{request.category}" == $name) #renameCategoryForm() #end##
309 #if($xwiki.hasAccessLevel('edit', $xcontext.user, $doc.fullName) && "$!{request.xaction}" == "showAddCategory" && "$!{request.parentCategory}" == $name) #addCategoryForm() #end##
310 </span>
311 #end
312 ##
313 ##
314 ##
315 #**
316 * Displays a category as part of a category hierarchy, wrapped in an &lt;option&gt; element, to
317 * be used in a select box.
318 *
319 * @param name The full name of the document containing the category to be displayed.
320 * @param level The depth where this category is in the tree, used for proper indentation.
321 *###
322 #macro(displayOptionCategory $name $level)
323 <option id="blog_category_${services.rendering.escape(${escapetool.xml($name)}, $doc.syntax)}_option" value="$services.rendering.escape(${escapetool.xml($name)}, $doc.syntax)">#if($level > 1)#foreach($i in [2..$level])&nbsp;&nbsp;#end#end#getCategoryName($xwiki.getDocument($name))</option>
324 #end
325 ##
326 ##
327 ##
328 #**
329 * Displays a category as part of a category hierarchy, wrapped in a link.
330 *
331 * @param name The full name of the document containing the category to be displayed.
332 * @param level The depth where this category is in the tree, used for proper indentation.
333 *###
334 #macro(displaySimpleCategory $name $level)
335 #getEntriesForCategory($name $discard $totalEntries)
336 #set($nameUrl = $escapetool.url($name))
337 #foreach($i in [1..$level])*#end (% class="blog-category-level" %)((( [[#toolImage('rss')>>$name||queryString="xpage=plain&sheet=Blog.CategoryRss" title="RSS"]] <span class="wikilink"><a href="$services.rendering.escape($xwiki.getURL($name), $xwiki.getCurrentContentSyntaxId())">#getCategoryName($xwiki.getDocument($name)) <span class="itemCount">($totalEntries)</span></a></span>)))
338 #end
339 ##
340 ##
341 ##
342 #**
343 * Prints the name of a category, indicated by its document.
344 * The result is XML-escaped and Wiki syntax escaped.
345 *
346 * @param categoryDoc The document containing the category to be displayed.
347 *###
348 #macro(getCategoryName $categoryDoc)
349 ## Don't indent!
350 #set($result = "$!categoryDoc.getObject(${blogCategoryClassname}).getProperty('name').value.trim()")##
351 #if($result == '')
352 #set($result = $categoryDoc.name)
353 #end
354 ## Escape wiki syntax, if any.
355 #set ($result = "$services.rendering.escape($result, $xwiki.currentContentSyntaxId)")
356 ## Escape HTML, if any.
357 $escapetool.xml($result)##
358 #end
359 ##
360 ##
361 ##
362 #**
363 * Prints the description of a category, indicated by its document.
364 * The result is XML-escaped
365 *
366 * @param categoryDoc The document containing the category to be displayed.
367 *###
368 #macro(getCategoryDescription $categoryDoc)
369 ## Don't indent!
370 $escapetool.xml($!categoryDoc.getObject(${blogCategoryClassname}).getProperty('description').value.trim())##
371 #end
372 ##
373 ##
374 ##
375 #**
376 * Generates a form for creating a new category. The form allows to enter the name of the new
377 * category, and select a parent category from the existing ones.
378 *
379 * @param tree The category hierarchy, a HashMap<String, List<String>> structure, where the key
380 * is the name of a category, and the value contains the names of all its subcategories.
381 * @todo When javascript is disabled, a link to "Manage categories" should be displayed instead.
382 * This "form" should be created from javascript.
383 *###
384 #macro(showCreateCategoryBoxWithForm $tree)
385 <form action="$doc.getURL()" method="post">
386 #showCreateCategoryBox($tree)
387 </form>
388 #end
389 #**
390 * Generates a box for creating a new category. This allows to enter the name of the new
391 * category, and select a parent category from the existing ones. Note that this does not create
392 * a HTML form element, but requires one to be defined already as its ancestor.
393 *
394 * @param tree The category hierarchy HashMap<String, List<String>> structure, where the key
395 * is the name of a category, and the value contains the names of all its subcategories.
396 * @todo When javascript is disabled, a link to "Manage categories" should be displayed instead.
397 * This "form" should be created from javascript.
398 *###
399 #macro(showCreateCategoryBox $tree)
400 <div class='create-category'>
401 <input type="hidden" name="form_token" value="$!{services.csrf.getToken()}" />
402 <input type="hidden" name="xaction" value="create"/>
403 <label>$services.localization.render('blog.categories.new') <input type="text" name="newCategoryName" /></label>
404 <label>$services.localization.render('blog.categories.parent')
405 <select name="newCategoryParent" id="blog_category_selectBox">
406 <option value="${defaultCategoryParent}" selected="selected">None</option>
407 $!processedCategories.clear()##
408 #displayCategoriesHierarchy($tree 'option')
409 </select>
410 </label>
411 <span class="buttonwrapper"><input class="button" type="button" value="Add" id="blog_AddCategoryButton" /></span>
412 </div>
413 #end
414 ##
415 ##
416 ##
417 #macro(displayCategoryManagementTree $space $displayType)
418 <div class="blog-categories-list">
419 #getCategoriesHierarchy("$!{space}" $tree)
420 #if ("$!space" != $defaultBlogSpace)
421 #set ($defaultCategoryParent = "${space}.WebHome")
422 #end
423 #displayCategoriesHierarchy($tree $displayType)
424 #if($xwiki.hasAccessLevel('edit', $xcontext.user, $doc.fullName))
425 #set($addCategURL = $doc.getURL('view', $escapetool.url({
426 'xaction' : 'showAddCategory',
427 'parentCategory' : '',
428 'categoriesSpace': $space
429 })))
430 #if($isBlogPost || ("$!request.entry" != '' && "$!request.entryObjNb" != ''))
431 #set($entryParam = $!doc.fullName)
432 #set($entryObjNbParam = $!entryObj.number)
433 #if(!$isBlogPost)
434 #set($entryParam = $!request.entry)
435 #set($entryObjNbParam = $!request.entryObjNb)
436 #end
437 #set($addCategURL = $doc.getURL('view', $escapetool.url({
438 'xaction' : 'showAddCategory',
439 'parentCategory' : '',
440 'entry' : $entryParam,
441 'entryObjNb' : $entryObjNbParam,
442 'categoriesSpace': $space
443 })))
444 #end
445 * <span class="blog-add-category-label">$services.icon.renderHTML('add')&nbsp;<a href="$escapetool.xml($addCategURL)">$services.localization.render('blog.categories.addcategory')</a></span>
446 #if("$!{request.xaction}" == "showAddCategory" && "$!{request.parentCategory}" == "") #addCategoryForm() #end
447 #end
448
449
450 </div>
451 #end
452 ##
453 ##
454 ##
455 #**
456 * Deletes a category, moving all the subcategories to its parent and removing this category from
457 * all existing blog entries.
458 *
459 * @param category The full name of the document containing the category to be deleted.
460 *###
461 #macro(deleteCategory $category)
462 #set($categoryDoc = $xwiki.getDocument($category))
463 #set($categoryParent = "$!categoryDoc.parent")
464 #if($categoryParent == '')
465 #set($categoryParent = "{$defaultCategoryParent}")
466 #end
467 #set($query = ', BaseObject obj where ')
468 #if($space != '')
469 #set($query = "${query}doc.space = '${space}' and ")
470 #end
471 ## Get the subcategories of the deleted category.
472 #set($query = "${query}obj.name = doc.fullName and obj.className = '${blogCategoryClassname}' and doc.fullName <> 'Blog.CategoryTemplate' and doc.parent = :category order by doc.name")
473
474 #foreach($item in $services.query.hql($query).bindValue('category', $category).execute())
475 #if($xwiki.hasAccessLevel('edit', $xcontext.user, $item) && $!{services.csrf.isTokenValid("$!{request.getParameter('form_token')}")})
476 #set($subcategoryDoc = $xwiki.getDocument($item))
477 $subcategoryDoc.setParent($categoryParent)
478 $subcategoryDoc.save($services.localization.render('blog.manageCategories.comment.updatedParent'), true)
479 #end
480 #end
481 #set($query = ', BaseObject obj, DBStringListProperty categories join categories.list as category where ')
482 #if($space != '')
483 #set($query = "${query}doc.space = '${space}' and ")
484 #end
485 ## Get the blog posts of the deleted category.
486 #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")
487
488 #foreach($item in $services.query.hql($query).bindValue('category', $category).execute())
489 #if($xwiki.hasAccessLevel('edit', $xcontext.user, $item) && $!{services.csrf.isTokenValid("$!{request.getParameter('form_token')}")})
490 #set($blogEntryDoc = $xwiki.getDocument($item))
491 #set($discard = $blogEntryDoc.getObject(${blogPostClassname}).getProperty('category').value.remove($category))
492 $blogEntryDoc.save($services.localization.render('blog.manageCategories.comment.removedDeletedCategory'), true)
493 #end
494 #end
495 $categoryDoc.delete()
496 #end
497 ##
498 ##
499 ##
500 #**
501 * Renames a category, updating all the subcategories and all existing blog entries.
502 *
503 * @param category The full name of the document containing the category to be renamed.
504 * @param newCategoryName The new name of the category.
505 *###
506 #macro(renameCategory $category $newCategoryName)
507 #set($categoryDoc = $xwiki.getDocument($category))
508 #set ($newCategoryFullName = $newCategoryName)
509 #if ($category.space != $defaultBlogSpace)
510 #set ($newCategoryFullName = "${categoryDoc.space}.${newCategoryName}")
511 #end
512 #set($newCategoryDoc = $xwiki.getDocument($newCategoryFullName))
513 #set($parameterValues = ["$!{category}"])
514 #set($query = ', BaseObject obj where ')
515 ## Get the subcategories of the renamed category.
516 #set($query = "${query}obj.name = doc.fullName and obj.className = '${blogCategoryClassname}' and doc.fullName <> 'Blog.CategoryTemplate' and doc.parent = :category order by doc.name")
517 #foreach($item in $services.query.hql($query).bindValue('category', $category).execute())
518 #if($xwiki.hasAccessLevel('edit', $xcontext.user, $item) && $!{services.csrf.isTokenValid("$!{request.getParameter('form_token')}")})
519 #set($subcategoryDoc = $xwiki.getDocument($item))
520 $subcategoryDoc.setParent($newCategoryDoc.fullName)
521 $subcategoryDoc.save($services.localization.render('blog.manageCategories.comment.updatedParent'), true)
522 #end
523 #end
524 #set($query = ', BaseObject obj, DBStringListProperty categories join categories.list as category where ')
525 ## Get the blog posts of the renamed category.
526 #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")
527 #foreach($item in $services.query.hql($query).bindValue('category', $category).execute())
528 #if($xwiki.hasAccessLevel('edit', $xcontext.user, $item) && $!{services.csrf.isTokenValid("$!{request.getParameter('form_token')}")})
529 #set($blogEntryDoc = $xwiki.getDocument($item))
530 #set($discard = $blogEntryDoc.getObject(${blogPostClassname}).getProperty('category').value.remove($category))
531 #set($discard = $blogEntryDoc.getObject(${blogPostClassname}).getProperty('category').value.add($newCategoryDoc.fullName))
532 $blogEntryDoc.save($services.localization.render('blog.manageCategories.comment.updatedRenamedCategory'), true)
533 #end
534 #end
535 #if ($!{services.csrf.isTokenValid("$!{request.getParameter('form_token')}")})
536 $categoryDoc.getObject('Blog.CategoryClass').set('name', $newCategoryName)
537 $categoryDoc.save($services.localization.render('blog.manageCategories.comment.updatedCategory'), true)
538 $categoryDoc.rename($newCategoryFullName)
539 #end
540 #end
541 ##
542 ##
543 ##
544 #**
545 * Dipslay posts of a given category or a categories space.
546 * This macro is used in Blog.CategorySheet and Blog.CategoriesSheet pages
547 *
548 * @param catDoc The document containing the category or the WebHome page of a categories space.
549 * @param catObj The Blog.CategoryClass object attached to the category document, this parameter is null in case of categories space WebHome.
550 *###
551 #macro(displayCategoryPosts $catDoc $catObj)
552 #getEntriesForCategory($catDoc.fullName $discard $totalEntries)
553
554 #if ($totalEntries == 0)
555
556 {{info}}{{translation key="blog.categories.noentries"/}}{{/info}}
557 #else
558 #set ($macro.isCategoriesSpace = $catDoc.getObject('XWiki.DocumentSheetBinding').sheet == 'Blog.CategoriesSheet')
559 #if ($catObj || $macro.isCategoriesSpace)
560
561 (% class="cat-posts-count" %)
562 ==== [[#toolImage('feed')>>Blog.CategoryRss||queryString="xpage=plain&category=$escapetool.url($catDoc.fullName)" title="RSS"]] $services.localization.render('blog.category.posts.count', [$totalEntries]) ====
563 #end
564 ## Keep testing the inline action for backward compatibility with older categories.
565 #if ($xcontext.action != 'edit' && $xcontext.action != 'inline')
566 #getCategoriesHierarchy($catDoc.space $tree)
567 #if ("$!tree.get($catDoc.fullName)" != '')
568 (% class="blog-categories-list subcategories cat-count" %)
569 (((
570 (((
571 **&nbsp;**
572 )))
573 (((
574 #displayCategoriesHierarchyRecursive($tree $catDoc.fullName 1 'simple')
575 )))
576 )))
577 #end
578 (% class="clearfloats" %)((()))
579
580 #set ($macro.layoutParams = 'displayTitle=true|useSummary=true')
581 #getBlogDocumentForCategoriesSpace($catDoc.space $blogDoc)
582 #getBlogPostsLayout($blogDoc $postsLayout)
583 #set ($category = $catDoc.fullName)
584 #if ($macro.isCategoriesSpace)
585 #set ($category = $catDoc.space)
586 #end
587 (% class="hfeed category" %)((({{blogpostlist category="$category.replaceAll('~', '~~').replaceAll('"', '~"')" paginated="yes" layout="$postsLayout.replaceAll('~', '~~').replaceAll('"', '~"')" layoutParams="$!macro.layoutParams.replaceAll('~', '~~').replaceAll('"', '~"')" /}})))
588 #end
589 #end
590 #end
591 {{/velocity}}