0 Votes

Wiki source code of TourJson

Last modified by Ryan C on 2025/06/28 00:21

Show last authors
1 {{include reference="TourCode.Macros"/}}
2
3 {{velocity}}
4 #if ($xcontext.action == 'get')
5 ##--------------------------------------------------------
6 ## Get the tour steps corresponding to the target page
7 ##--------------------------------------------------------
8 #set ($tours = [])
9 #if ("$!request.tourDoc" != '')
10 #set ($targetRef = $services.model.resolveDocument($request.tourDoc))
11 #set ($targetDoc = $xwiki.getDocument($targetRef))
12 #set ($classes = $targetDoc.getxWikiObjects().keySet())
13 ##-------------------
14 ## Compute the query
15 ##-------------------
16 #set ($extraClause = '')
17 #if (!$classes.isEmpty())
18 ## We need to put this request part in the $extraClause variable otherwise we end up with "tour.targetClass IN ()"
19 ## which causes an hibernate parser error reported in the logs.
20 ## Say differently, "()" cannot be empty in a "IN" clause.
21 #set ($extraClause = 'OR tour.targetClass IN (:classes)')
22 #end
23 #set ($query = $services.query.xwql("from doc.object(TourCode.TourClass) as tour, doc.object(TourCode.StepClass) as step WHERE tour.isActive = 1 AND (tour.targetPage = :targetPage OR step.targetPage = :targetPage ${extraClause})").bindValue('targetPage', $request.tourDoc).addFilter('unique'))
24 #if (!$classes.isEmpty())
25 #set ($query = $query.bindValue('classes', $classes))
26 #end
27 ## -------------------------
28 ## Get the different tours
29 ## -------------------------
30 #foreach ($tourRef in $query.execute())
31 #set ($tourDoc = $xwiki.getDocument($tourRef))
32 #set ($tourObj = $tourDoc.getObject('TourCode.TourClass'))
33 ## the targetPage property takes precedence over targetClass property.
34 ## the targetClass property is used only when targetPage is empty.
35 #if ("$!tourDoc.getValue('targetClass', $tourObj)" != '' && "$!tourDoc.getValue('targetPage', $tourObj)" == '')
36 #set ($targetPage = "$!request.tourDoc")
37 #else
38 #set ($targetPage = "$!tourDoc.getValue('targetPage', $tourObj)")
39 #end
40 ## Verify that the user who has saved this tour has the edit right on the target page
41 #if ($services.security.authorization.hasAccess('edit', $tourDoc.authorReference, $targetRef))
42 #set ($tour = { 'name': $tourRef, 'steps': [] })
43 #set ($discard = $tours.add($tour))
44 ## -------------------------
45 ## Get the tour steps
46 ## -------------------------
47 #foreach($stepObj in $tourDoc.getObjects('TourCode.StepClass'))
48 #set ($backdrop = $tourDoc.getValue('backdrop', $stepObj) == 1)
49 #set ($reflex = $tourDoc.getValue('reflex', $stepObj) == 1)
50 ## If the 'targetPage' value of a step is empty, the path is inherited from the previous step.
51 ## For the first step, the path is inherited from the tour configuration.
52 ## This way the tour stays in the same wiki page.
53 #set ($tempPage = "$!tourDoc.getValue('targetPage', $stepObj)")
54 #set ($tempAction = "$!tourDoc.getValue('action', $stepObj)")
55 #set ($targetParams = "$!tourDoc.getValue('queryString', $stepObj)")
56 #if ($tempPage != '')
57 #set ($targetPage = $tempPage)
58 #end
59 #if ($tempAction != '')
60 #set ($targetAction = $tempAction)
61 #else
62 #set ($targetAction = "view")
63 #end
64 #set ($path = $xwiki.getURL($targetPage, $targetAction, $targetParams))
65 ## ------------------------------
66 ## Render the content of the step
67 ## ------------------------------
68 #set ($content = $stepObj.display('content', 'view'))
69 ## Remove "{{html clean="false" wiki="false"}}" and {{/html}}
70 #set ($endIndex = $content.length() - 9)
71 #set ($content = $content.substring(35, $endIndex))
72 ## -----------------------------
73 ## Create the step map (that will be serialized to JSON afterwards)
74 ## -----------------------------
75 #set ($element = "$!tourDoc.getValue('element', $stepObj)")
76 ## For retro-compatibility and because of a bug (https://github.com/sorich87/bootstrap-tour/issues/557)
77 ## we need to replace the element when it targets the document body by an empty string.
78 #if ($element == 'body')
79 #set ($element = '')
80 #end
81 #set ($orphan = $stringtool.isBlank($element))
82 #set ($step = { 'element' : $element,
83 'title' : $services.localization.render($tourDoc.getValue('title', $stepObj)),
84 'content' : $content,
85 'placement': $tourDoc.getValue('placement', $stepObj),
86 'order' : $tourDoc.getValue('order', $stepObj),
87 'backdrop' : $backdrop,
88 'path' : $path,
89 'orphan' : $orphan,
90 'reflex' : $reflex })
91 #set ($discard = $tour.steps.add($step))
92 #end
93 ##--------------------------------------------------------
94 ## Sort the steps
95 ##--------------------------------------------------------
96 #set ($tour.steps = $properSortTool.sort($tour.steps, 'order'))
97 #end
98 #end
99 #end
100 ##--------------------------------------------------------
101 ## Display the results as JSON
102 ##--------------------------------------------------------
103 #set ($discard = $response.setContentType('application/json'))
104 $jsontool.serialize({'tours': $tours})
105 #end
106 {{/velocity}}