Wiki source code of VelocityMacros

Last modified by Jean Franco on 2022/10/27 21:09

Show last authors
1 {{velocity output="false"}}
2 #set ($LONG_MAX_VALUE = $mathtool.pow(2,63))
3 #set ($licensorId = 'com.xwiki.licensing:application-licensing-licensor-api')
4 #set ($licensingConfigDoc = $xwiki.getDocument('Licenses.Code.LicensingConfig'))
5 #set ($storeBuyURL = $licensingConfigDoc.storeBuyURL)
6 #set ($storeAccessURL = $licensingConfigDoc.storeAccessURL)
7 #set ($ownerObj = $licensingConfigDoc.getObject('Licenses.Code.LicensingOwnerClass'))
8 #set ($userCount = $services.licensing.licensor.userCount)
9
10 #macro (getPaidExtensions)
11 #set ($paidExtensionsByNamespace = $services.extension.installed.getBackwardDependencies($licensorId))
12 ## CollectionsTool is deprecated since 12.1RC1.
13 #if ($collectiontool)
14 #set ($paidExtensions = $collectiontool.set)
15 #else
16 #set ($paidExtensions = $collectionstool.set)
17 #end
18 #foreach ($paidExtensionsOnNamespace in $paidExtensionsByNamespace.values())
19 #set ($discard = $paidExtensions.addAll($paidExtensionsOnNamespace))
20 #end
21 #end
22
23 #macro (getVisiblePaidExtensions)
24 #getPaidExtensions
25 ## CollectionsTool is deprecated since 12.1RC1.
26 #if ($collectiontool)
27 #set ($visiblePaidExtensions = $collectiontool.set)
28 #else
29 #set ($visiblePaidExtensions = $collectionstool.set)
30 #end
31 #foreach ($paidExtension in $paidExtensions)
32 ## Filter out the paid extensions that are dependencies of other paid extensions (because the license of a paid
33 ## extension should cover its paid dependencies).
34 #isRequiredBy($paidExtension $paidExtensions)
35 #if (!$isRequiredBy)
36 #set ($discard = $visiblePaidExtensions.add($paidExtension))
37 #end
38 #end
39 #end
40
41 #macro (isRequiredBy $installedExtension $extensions)
42 #set ($isRequiredBy = false)
43 ## CollectionsTool is deprecated since 12.1RC1.
44 #if ($collectiontool)
45 #set ($backwardDependencies = $collectiontool.queue)
46 #else
47 #set ($backwardDependencies = $collectionstool.queue)
48 #end
49 #addBackwardDependencies($installedExtension $backwardDependencies)
50 ## We can't iterate the queue while modifying it and we can't write a "while" loop in Velocity so we're forced to
51 ## rely on a range loop (limiting the number of backward dependencies that we analyze).
52 #foreach ($i in [0..1000])
53 #set ($backwardDependency = $backwardDependencies.poll())
54 #if ($backwardDependency)
55 #if ($extensions.contains($backwardDependency))
56 #set ($isRequiredBy = true)
57 #break
58 #else
59 #addBackwardDependencies($backwardDependency $backwardDependencies)
60 #end
61 #else
62 #break
63 #end
64 #end
65 #end
66
67 #macro (addBackwardDependencies $installedExtension $backwardDependencies)
68 ## CollectionsTool is deprecated since 12.1RC1.
69 #if ($collectiontool)
70 #set ($backwardDependenciesSet = $collectiontool.set)
71 #else
72 #set ($backwardDependenciesSet = $collectionstool.set)
73 #end
74 #set ($discard = $backwardDependenciesSet.addAll($backwardDependencies))
75 #set ($backwardDependenciesByNamespace = $services.extension.installed.getBackwardDependencies($installedExtension.id))
76 #foreach ($backwardDependenciesOnNamespace in $backwardDependenciesByNamespace.values())
77 #foreach ($backwardDependency in $backwardDependenciesOnNamespace)
78 #if ($backwardDependenciesSet.add($backwardDependency))
79 #set ($discard = $backwardDependencies.add($backwardDependency))
80 #end
81 #end
82 #end
83 #end
84
85 #macro (getLicensesAdminSectionURL)
86 ## We target the main wiki explicitly because licenses are managed globally.
87 #if ($xwiki.exists($services.model.createDocumentReference($xcontext.mainWikiName, 'XWiki', 'ConfigurableClass')))
88 $xwiki.getURL($services.model.createDocumentReference($xcontext.mainWikiName, 'XWiki', 'XWikiPreferences'),
89 'admin', 'editor=globaladmin&section=Licenses')##
90 #else
91 ## Fall-back in case the Administration Application is not installed.
92 $xwiki.getURL($services.model.createDocumentReference($xcontext.mainWikiName, 'Licenses', 'WebHome'))##
93 #end
94 #end
95
96 #macro (getMissingLicenseMessage $extensionName)
97 $services.localization.render('licensor.missingLicense', [$services.localization.render($extensionName), '[[',
98 ">>path:#getLicensesAdminSectionURL]]"])
99 #end
100
101 #macro (getMissingLicenseMessageHTML $extensionName)
102 $services.localization.render('licensor.missingLicense', [$services.localization.render($extensionName),
103 "<a href='#getLicensesAdminSectionURL'>", '</a>'])
104 #end
105
106 #macro (getInstanceIdFormField)
107 <dt>
108 <label for="instanceId">$services.localization.render('licensor.ownerDetails.instanceId.label')</label>
109 <span class="xHint">$services.localization.render('licensor.ownerDetails.instanceId.hint')</span>
110 </dt>
111 <dd>
112 #getInstanceIdInput(false)
113 </dd>
114 #end
115
116 #macro (getInstanceIdInput $hidden)
117 #set ($type = 'text')
118 #if ($hidden)
119 #set ($type = 'hidden')
120 #end
121 <input type="$type" name="instanceId" id="instanceId" value="${services.instance.getInstanceId()}" readonly />
122 #end
123
124 #macro (getOwnerFormFields)
125 #foreach ($prop in $ownerObj.properties)
126 <dt>
127 <label for="${prop.name}">$services.localization.render("licensor.ownerDetails.${prop.name}.label")</label>
128 #set ($hintTranslationKey = "licensor.ownerDetails.${prop.name}.hint")
129 #set ($hint = $services.localization.render($hintTranslationKey))
130 #if ($hint != $hintTranslationKey)
131 <span class="xHint">$!escapetool.xml($hint)</span>
132 #end
133 </dt>
134 #set ($inputType = 'text')
135 #if ($prop.name == 'email')
136 #set ($inputType = 'email')
137 #end
138 <dd><input type="$inputType" name="${prop.name}" id="${prop.name}" value="$!{prop.value}" /></dd>
139 #end
140 #end
141
142 #macro (displayOwnerDetailsForm)
143 {{html clean=false}}
144 <form id="ownerDetails" target="_blank" class="xform half" action="${storeBuyURL}" method="post">
145 <fieldset class="header">
146 <legend>$services.localization.render('licensor.ownerDetails.heading')</legend>
147 <p>$services.localization.render('licensor.ownerDetails.hint')</p>
148 <input type="hidden" name="form_token" value="$!escapetool.xml($services.csrf.token)" />
149 <dl>
150 #getOwnerFormFields
151 #getInstanceIdFormField
152 </dl>
153 </fieldset>
154 </form>
155 {{/html}}
156 #end
157
158 #macro (licenseButton $licenseType $cssClass $iconName $key $jsonData)
159 #set ($jsonData.licenseType = $licenseType)
160 #set ($label = $escapetool.xml($services.localization.render("licensor.${key}License.label")))
161 <a href="#$escapetool.xml($key)" class="action licenseButton licenseButton-$escapetool.xml($key)"
162 data-button="$escapetool.xml($jsontool.serialize($jsonData))" title="$label">
163 <span class="action-icon"><span class="fa fa-$iconName"></span></span>##
164 <span class="action-label">$label</span>
165 </a>
166 #end
167
168 #macro (autoUpgradeButton $extensionId)
169 #set ($licensingConfigDoc = $xwiki.getDocument('Licenses.Code.LicensingConfig'))
170 #set ($autoUpgradesObj = $licensingConfigDoc.getObject('Licenses.Code.AutomaticUpgradesClass'))
171 #set ($isAutoUpgrade = $autoUpgradesObj.getValue('allowlist').contains($extensionId))
172 #set ($upgradeL10nKey = "#if ($isAutoUpgrade)prevent#{else}allow#end")
173 <div class="btn-group">
174 <form target="_blank" class="xform half">
175 <input type="hidden" name="form_token" value="$!escapetool.xml($services.csrf.token)" />
176 <input type="hidden" name="extensionId" value="$!escapetool.xml($extensionId)" />
177 <input type="hidden" name="autoUpgrade" value="$!escapetool.xml($isAutoUpgrade)" />
178 ## The data translations attributes are used for updating the title from js. These can be removed once licensing
179 ## starts depending on a XWiki version >= 13.8 to include XWIKI-18973: Simplify the way JavaScript code loads
180 ## translation messages.
181 <a href="#autoUpgrade" class="action licenseButton-autoUpgrade" title="$escapetool.xml(
182 $services.localization.render("licensor.moreActions.autoUpgrade.${upgradeL10nKey}"))" data-allow-label=
183 "$escapetool.xml($services.localization.render('licensor.moreActions.autoUpgrade.allow'))"
184 data-prevent-label="$escapetool.xml(
185 $services.localization.render('licensor.moreActions.autoUpgrade.prevent'))" >
186 <span class="action-icon #if ($isAutoUpgrade)isAutoUpgrade#end">
187 <span class="fa fa-cloud-download"></span>##
188 </span><span class="action-label">$escapetool.xml(
189 $services.localization.render('licensor.moreActions.autoUpgrade.label'))
190 </span>
191 </a>
192 </form>
193 </div>
194 #end
195
196 #macro (getLicenseActions $license $extension $actions)
197 ## Use the JSON format in order to be parsable in JavaScript.
198 #set ($jsonData = {
199 'featureId': $extension.id.id,
200 'extensionVersion': $extension.id.version.toString(),
201 'userCount': $userCount
202 })
203 #if (!$license || $license.expirationDate == 0)
204 ## No license available.
205 #set ($discard = $actions.add("#licenseButton('PAID' 'success' 'shopping-cart' 'paid' $jsonData)"))
206 #set ($discard = $actions.add("#licenseButton('TRIAL' 'primary' 'hourglass-start' 'trial' $jsonData)"))
207 #elseif ($license.type == 'FREE')
208 #set ($discard = $actions.add("#licenseButton('PAID' 'success' 'shopping-cart' 'paid' $jsonData)"))
209 #elseif ($license.type == 'TRIAL')
210 #set ($discard = $actions.add("#licenseButton('PAID' 'success' 'shopping-cart' 'paid' $jsonData)"))
211 #set ($discard = $actions.add("#licenseButton('TRIAL' 'primary' 'hourglass-end' 'extendTrial' $jsonData)"))
212 #elseif ($license.type == 'PAID')
213 #set ($discard = $actions.add("#licenseButton('PAID' 'success' 'shopping-cart' 'extendPaid' $jsonData)"))
214 #end
215 #end
216
217 #macro (licenseActions $license $extension)
218 #set ($actions = [])
219 ## Pull the resources needed by license actions.
220 #set ($discard = $services.icon.use('Font Awesome'))
221 #getLicenseActions($license $extension $actions)
222 <div class="licenseActions container-fluid">
223 <div class="row">
224 $stringtool.join($actions, '')
225 </div>
226 <div class="row">
227 #autoUpgradeButton($extension.id.id)
228 </div>
229 </div>
230 #end
231 {{/velocity}}
Maila Networks