Wiki source code of VelocityMacros

Last modified by leeb chi on 2020/09/02, 17:18

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 $key $jsonData)
159 #set ($jsonData.licenseType = $licenseType)
160 #set ($label = $services.localization.render("licensor.${key}License.label"))
161 <button class="btn btn-$cssClass licenseButton licenseButton-$escapetool.xml($key)"
162 data-button="$escapetool.xml($jsontool.serialize($jsonData))">$label</button>
163 #end
164
165 #macro (getLicenseActions $license $extension $actions)
166 ## Use the JSON format in order to be parsable in JavaScript.
167 #set ($jsonData = {
168 'featureId': $extension.id.id,
169 'extensionVersion': $extension.id.version.toString(),
170 'userCount': $userCount
171 })
172 #if (!$license || $license.expirationDate == 0)
173 ## No license available.
174 #set ($discard = $actions.add("#licenseButton('PAID' 'success' 'paid' $jsonData)"))
175 #set ($discard = $actions.add("#licenseButton('TRIAL' 'primary' 'trial' $jsonData)"))
176 #elseif ($license.type == 'FREE')
177 #set ($discard = $actions.add("#licenseButton('PAID' 'success' 'paid' $jsonData)"))
178 #elseif ($license.type == 'TRIAL')
179 #set ($discard = $actions.add("#licenseButton('PAID' 'success' 'paid' $jsonData)"))
180 #set ($discard = $actions.add("#licenseButton('TRIAL' 'primary' 'extendTrial' $jsonData)"))
181 #elseif ($license.type == 'PAID')
182 #set ($discard = $actions.add("#licenseButton('PAID' 'success' 'extendPaid' $jsonData)"))
183 #end
184 #end
185
186 #macro (licenseActions $license $extension)
187 #set ($actions = [])
188 #getLicenseActions($license $extension $actions)
189 <div class="licenseActions">
190 $stringtool.join($actions, '')
191 </div>
192 #end
193 {{/velocity}}
深圳市艾拓先锋企业管理咨询有限公司