Wiki source code of Category Index
Last modified by wiki admin on 2026/09/01 09:30
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | {{velocity}} | ||
| 2 | ## =========================================================================== | ||
| 3 | ## Main.CategoryIndex -- the article listing every category page includes. | ||
| 4 | ## | ||
| 5 | ## THE PROBLEM IT SOLVES: the Helpjuice migration gave every category page a | ||
| 6 | ## single line - "Category: AI NVR Cameras" - and no listing. Clicking a | ||
| 7 | ## category from the panel, from a home page card, or from a search result | ||
| 8 | ## breadcrumb lands on a page that looks empty even when it holds twenty | ||
| 9 | ## articles. | ||
| 10 | ## | ||
| 11 | ## HOW IT IS USED: one line at the bottom of each category page: | ||
| 12 | ## | ||
| 13 | ## {{include reference="Main.CategoryIndex" context="current"/}} | ||
| 14 | ## | ||
| 15 | ## context="current" is what makes this work - without it $doc refers to THIS | ||
| 16 | ## page and every category would list the contents of Main.CategoryIndex, | ||
| 17 | ## which is nothing. With it, $doc is the including page and the listing is | ||
| 18 | ## about that category. | ||
| 19 | ## | ||
| 20 | ## Kept in ONE page on purpose: 100+ category pages including a shared listing | ||
| 21 | ## can all be improved with a single edit. A hundred copies of the same | ||
| 22 | ## Velocity could not. | ||
| 23 | ## | ||
| 24 | ## SAVE AS AN ADMINISTRATOR - it needs script right for the queries. | ||
| 25 | ## =========================================================================== | ||
| 26 | #set($space = '') | ||
| 27 | #set($space = "$!doc.space") | ||
| 28 | |||
| 29 | #if($space == '') | ||
| 30 | {{warning}} | ||
| 31 | This page lists the contents of whatever page includes it. Included without | ||
| 32 | context="current", it has nothing to describe. | ||
| 33 | {{/warning}} | ||
| 34 | #else | ||
| 35 | ## --------------------------------------------------------------------------- | ||
| 36 | ## Sub-categories: immediate children only. A category three levels down is | ||
| 37 | ## reachable through its parent, and listing every descendant here would repeat | ||
| 38 | ## the whole department on every page. | ||
| 39 | ## --------------------------------------------------------------------------- | ||
| 40 | #set($prefix = "${space}.") | ||
| 41 | #set($allSpaces = []) | ||
| 42 | #set($allSpaces = $services.query.xwql("select distinct doc.space from Document doc where doc.space like :p and doc.name <> 'WebHome' and doc.hidden <> true order by doc.space").bindValue('p', "${prefix}%").execute()) | ||
| 43 | |||
| 44 | #set($kids = []) | ||
| 45 | #foreach($sp in $allSpaces) | ||
| 46 | #set($s = '') | ||
| 47 | #set($s = "$!sp") | ||
| 48 | #if($s.startsWith($prefix)) | ||
| 49 | #set($rest = $s.substring($prefix.length())) | ||
| 50 | ## Immediate child only: no further dot in what remains. Deeper spaces | ||
| 51 | ## arrive in the same result set and would otherwise be listed as if they | ||
| 52 | ## sat directly under this one. | ||
| 53 | #if($rest.indexOf('.') < 0) | ||
| 54 | #set($ignore = $kids.add($s)) | ||
| 55 | #end | ||
| 56 | #end | ||
| 57 | #end | ||
| 58 | |||
| 59 | #if($kids.size() > 0) | ||
| 60 | === Sub-categories === | ||
| 61 | |||
| 62 | {{html clean="false"}} | ||
| 63 | <ul class="cat-index-subs"> | ||
| 64 | #foreach($k in $kids) | ||
| 65 | #set($leaf = $k.substring($prefix.length())) | ||
| 66 | ## The count is what tells a reader whether a sub-category is worth opening. | ||
| 67 | ## doc.hidden <> true keeps redirect stubs from inflating it - a moved | ||
| 68 | ## article leaves one behind at its old address. | ||
| 69 | #set($kn = 0) | ||
| 70 | #set($kn = $services.query.xwql("select count(doc.fullName) from Document doc where (doc.space = :s or doc.space like :p) and doc.name <> 'WebHome' and doc.hidden <> true").bindValue('s', $k).bindValue('p', "${k}.%").execute().get(0)) | ||
| 71 | #if(!$kn) | ||
| 72 | #set($kn = 0) | ||
| 73 | #end | ||
| 74 | <li><a href="$xwiki.getURL("${k}.WebHome")">$escapetool.xml($leaf)</a>#if($kn > 0) <span style="color:#7A8794">($kn)</span>#end</li> | ||
| 75 | #end | ||
| 76 | </ul> | ||
| 77 | {{/html}} | ||
| 78 | #end | ||
| 79 | |||
| 80 | ## --------------------------------------------------------------------------- | ||
| 81 | ## Articles filed directly in this category. | ||
| 82 | ## --------------------------------------------------------------------------- | ||
| 83 | #set($rows = []) | ||
| 84 | #set($rows = $services.query.xwql("select doc.fullName from Document doc where doc.space = :s and doc.name <> 'WebHome' and doc.hidden <> true order by doc.name").bindValue('s', $space).execute()) | ||
| 85 | |||
| 86 | #if($rows.size() > 0) | ||
| 87 | === Articles === | ||
| 88 | |||
| 89 | {{html clean="false"}} | ||
| 90 | <ul class="cat-index-arts"> | ||
| 91 | #foreach($fn in $rows) | ||
| 92 | ## Seeded false before the assignment: #set does nothing when its right-hand | ||
| 93 | ## side is null, so an unguarded assignment would leave the PREVIOUS | ||
| 94 | ## article's document in place and list it twice. | ||
| 95 | #set($d = false) | ||
| 96 | #set($d = $xwiki.getDocument($fn)) | ||
| 97 | #if($d && !$d.isNew()) | ||
| 98 | <li><a href="$d.getURL('view')">$escapetool.xml($d.plainTitle)</a></li> | ||
| 99 | #end | ||
| 100 | #end | ||
| 101 | </ul> | ||
| 102 | {{/html}} | ||
| 103 | #elseif($kids.size() == 0) | ||
| 104 | ## The empty state is read by two completely different audiences and the same | ||
| 105 | ## sentence cannot serve both. A staff member seeing "nothing is filed here" | ||
| 106 | ## reads a job to do. A customer seeing it reads a broken website. | ||
| 107 | #set($wiki = '') | ||
| 108 | #set($wiki = "$!doc.wiki") | ||
| 109 | #if($wiki == 'alder') | ||
| 110 | {{info}} | ||
| 111 | No articles have been published in this category yet. Try the search box at the | ||
| 112 | top of the page, or call customer service on 800.710.1289. | ||
| 113 | {{/info}} | ||
| 114 | #else | ||
| 115 | {{info}} | ||
| 116 | Nothing is filed in this category yet. If an article belongs here, move it in - | ||
| 117 | an empty category is worse than no category. | ||
| 118 | {{/info}} | ||
| 119 | #end | ||
| 120 | #end | ||
| 121 | #end | ||
| 122 | {{/velocity}} |