Wiki source code of GoogleGadgetsImport
Last modified by Anca Luca on 2010/03/23 12:15
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | {{velocity}} | ||
2 | ## | ||
3 | ## Get request parameters | ||
4 | #set($url = "$!{request.url}") ## google gadget url | ||
5 | #set($macroId = "$!{request.id}") | ||
6 | #set($macroName = "$!{request.name}") | ||
7 | #set($macroDocName = "Macros.${macroName}") | ||
8 | ## | ||
9 | ## Check parameters (all mandatory) | ||
10 | #if($url == '' || $macroId == '' || $macroName == '') | ||
11 | {{error}}Bad parameters. All parameters (gadget url, macro id and macro name) are mandatory.{{/error}} | ||
12 | #else | ||
13 | ## | ||
14 | ## Create macro document | ||
15 | #set($macroDoc = $xwiki.getDocument($macroDocName)) | ||
16 | #if(!$macroDoc.isNew()) | ||
17 | {{error}}A macro with this name already exists. See [[$macroDocName]].{{/error}} | ||
18 | ## | ||
19 | ## Check if a macro with the same macroId already exists | ||
20 | #elseif('' != "$!{macros.getMacroDescriptor($macroId)}") | ||
21 | {{error}}A macro with the same id ($macroId) already exists.{{/error}} | ||
22 | #else | ||
23 | ## | ||
24 | ## Create the macro | ||
25 | #set($macroObj = $macroDoc.newObject('XWiki.WikiMacroClass')) | ||
26 | #set($discard = $macroObj.set('id', $macroId)) | ||
27 | #set($discard = $macroObj.set('name', $macroName)) | ||
28 | #set($discard = $macroObj.set('defaultCategory', 'Google Gadget')) | ||
29 | #set($discard = $macroObj.set('visibility', 'Current Wiki')) | ||
30 | #set($discard = $macroObj.set('supportsInlineMode', 0)) | ||
31 | #set($discard = $macroObj.set('contentType', 'No content')) | ||
32 | ## | ||
33 | ## Parse gadget XML for module prefs | ||
34 | #set($gModulePrefs = $gadgets.parseModulePrefs($url)) | ||
35 | #set($discard = $macroObj.set('description', "$!{gModulePrefs.get('description')}")) | ||
36 | #set($thumbnail = "$!{gModulePrefs.get('thumbnail')}") | ||
37 | ## | ||
38 | #set($macroCode = "{{velocity}}\#set($gadgeturl = '" + $url + "') \#set($thumburl = '" + $thumbnail + "'){{/velocity}}" + | ||
39 | "{{include document='MacrosCode.GoogleGadgetsMacroCode'/}}") | ||
40 | #set($discard = $macroObj.set('code', $macroCode)) | ||
41 | ## | ||
42 | ## Create default macro parameters: w, h, title | ||
43 | #set($paramObj = $macroDoc.newObject('XWiki.WikiMacroParameterClass')) | ||
44 | #set($discard = $paramObj.set('name', 'w')) | ||
45 | #set($discard = $paramObj.set('description', 'Width')) | ||
46 | #set($discard = $paramObj.set('mandatory', 0)) | ||
47 | #set($discard = $paramObj.set('defaultValue', '300')) | ||
48 | ## | ||
49 | #set($paramObj = $macroDoc.newObject('XWiki.WikiMacroParameterClass')) | ||
50 | #set($discard = $paramObj.set('name', 'h')) | ||
51 | #set($discard = $paramObj.set('description', 'Height')) | ||
52 | #set($discard = $paramObj.set('mandatory', 0)) | ||
53 | #set($discard = $paramObj.set('defaultValue', '300')) | ||
54 | ## | ||
55 | #set($paramObj = $macroDoc.newObject('XWiki.WikiMacroParameterClass')) | ||
56 | #set($discard = $paramObj.set('name', 'title')) | ||
57 | #set($discard = $paramObj.set('description', 'Title')) | ||
58 | #set($discard = $paramObj.set('mandatory', 0)) | ||
59 | ## | ||
60 | ## Create gadget specific macro parameters | ||
61 | #set($gUserPrefs = $gadgets.parseUserPrefs($url)) | ||
62 | #if("$!gUserPrefs" != '') | ||
63 | #foreach($userPref in $gUserPrefs) | ||
64 | ## | ||
65 | ## Create macro parameters for all UserPrefs except hidden data types | ||
66 | #if("$!{userPref.datatype}" != 'hidden') | ||
67 | #set($paramObj = $macroDoc.newObject('XWiki.WikiMacroParameterClass')) | ||
68 | ## | ||
69 | #set($discard = $paramObj.set('name', "up_$!{userPref.name}")) | ||
70 | ## | ||
71 | #set($mandatory = 0) | ||
72 | #if("$!{userPref.required}" == 'true') #set($mandatory = 1) #end | ||
73 | #set($discard = $paramObj.set('mandatory', $mandatory)) | ||
74 | ## | ||
75 | #set($up_desc = "$!{userPref.displayName}") | ||
76 | ## | ||
77 | ## For enum values, keep the allowed values in the description for now | ||
78 | #if (${userPref.datatype} == 'enum') | ||
79 | #set($up_desc = "${up_desc} - Allowed values:") | ||
80 | #foreach ($ev in ${userPref.enumValues}) | ||
81 | #set($up_desc = "${up_desc} ${ev.value} (${ev.displayValue})") | ||
82 | #end | ||
83 | #end | ||
84 | ## | ||
85 | ## For really long list of enum values, keep only first 400 chars | ||
86 | #if($up_desc.length() > 400) | ||
87 | #set($up_desc = "${up_desc.substring(0, 400)}...") | ||
88 | #end | ||
89 | #set($discard = $paramObj.set('description', $up_desc)) | ||
90 | ## | ||
91 | #set($discard = $paramObj.set('defaultValue', "$!{userPref.defaultValue}")) | ||
92 | #end | ||
93 | #end | ||
94 | ## | ||
95 | #set($discard = $macroDoc.save()) | ||
96 | ## | ||
97 | #set($macroURL = $xwiki.getURL('Macros.Macro', 'view', "id=${macroId}")) | ||
98 | {{info}}Google Gadget imported successfully. See {{html}}<a href='${macroURL}'>$macroName</a>{{/html}}.{{/info}} | ||
99 | #else | ||
100 | {{error}}Error while parsing gadget at URL [[${url}]]{{/error}} | ||
101 | #end | ||
102 | #end | ||
103 | #end | ||
104 | {{/velocity}} |