-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
HelperBase.cfc
executable file
·266 lines (213 loc) · 7.03 KB
/
HelperBase.cfc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<cfcomponent displayname="base" accessors="true" output="no">
<cfproperty type="String" name="tagName">
<cfproperty type="Array" name="params" >
<cfproperty type="Array" name="parts" >
<cfproperty type="Struct" name="attributes" >
<cfscript>
/*
* Store the attributes added to the instance
*/
variables.attributes = {};
/*
* Internal params storage
*/
variables.params = [];
variables.parts = [];
variables.tagname = "";
public HelperBase function init() {
setAttributes(argumentCollection=arguments);
return this;
}
/**
* Add a new param
*/
public HelperBase function addParam() {
ArrayAppend(variables.params,arguments);
return this;
}
/**
* Clear the stored params Array
*/
public HelperBase function clearParams() {
variables.params = [];
return this;
}
/**
* add a new part
*/
public HelperBase function addPart() {
ArrayAppend(variables.parts,arguments);
return this;
}
/**
* Clear the stored parts Array
*/
public HelperBase function clearParts() {
variables.parts = [];
return this;
}
public HelperBase function setAttributes() {
StructAppend(variables.attributes, arguments, true);
return this;
}
public HelperBase function clearAttributes() {
variables.attributes = {};
return this;
}
public HelperBase function clear() {
clearAttributes();
clearParams();
clearParts();
return this;
}
public Struct function getSupportedTagAttributes() {
return getTagData("cf",getTagName());
}
</cfscript>
<!---
invoke Tag
--->
<cffunction name="invokeTag" output="false" access="private" returntype="any" hint="invokes the service tag">
<cfset local.tagname = getTagName()>
<cfset local.tagAttributes = getAttributes()>
<cfset local.tagParams = getParams()>
<cfset local.resultVar = "">
<cfset local.result = new Result()>
<!--- Makes the attributes available in local scope. Es : query of queries --->
<cfset structAppend(local,tagAttributes,true)>
<cfswitch expression="#tagname#">
<!--- cfquery --->
<cfcase value="query">
<!--- get the query array to loop --->
<cfset local.qArray = getQArray()>
<!--- declare the query local var --->
<cfquery name="local.___q" attributeCollection="#tagAttributes#" result="local.tagResult">
<cfloop array="#local.qArray#" index="Local.item"><!---
!---><cfif structKeyExists(item,'type') and item.type eq 'string'><!---
!--->#preserveSingleQuotes(item.value)#<!---
!---><cfelse><!---
!---><cfqueryparam attributecollection="#item#"><!---
!---></cfif></cfloop>
</cfquery>
<cfif !isNull(local.___q)><cfset result.setResult(local.___q)></cfif>
<cfif !isNull(local.tagResult)><cfset result.setPrefix(local.tagResult)></cfif>
<cfreturn result>
</cfcase>
<!--- cfftp --->
<cfcase value="ftp">
<!---
If action = "listdir" we need to provide a name to the cfftp tag where will
be stored the returned query. The recordset will be passed then to the Result
Object.
--->
<cfif structkeyExists(tagAttributes,'action') and tagAttributes.action eq 'listdir'>
<cfset tagAttributes.name = 'local.___q' >
</cfif>
<cfftp attributeCollection="#tagAttributes#" result="local.tagResult"/>
<cfif tagAttributes["action"] eq "listdir">
<cfif !isNull(local.___q)><cfset result.setResult(local.___q)></cfif>
</cfif>
<cfif !isNull(local.tagResult)><cfset result.setPrefix(local.tagResult)></cfif>
</cfcase>
<!--- cfhttp --->
<cfcase value="http">
<cfhttp attributeCollection="#tagAttributes#" result="tagResult">
<cfloop array="#tagParams#" index="param">
<cfhttpParam attributeCollection="#param#">
</cfloop>
</cfhttp>
<cfif structkeyexists(tagAttributes,"name") and tagAttributes["name"] neq "">
<cfset result.setResult(StructFind(variables,tagAttributes["name"]))>
</cfif>
<cfset result.setPrefix(tagResult)>
</cfcase>
<!--- cfmail --->
<cfcase value="mail">
<cfset local.body = "">
<cfif StructKeyExists(tagAttributes, "body")>
<cfset local.body = tagAttributes.body>
<cfset Structdelete(tagAttributes, "body")>
</cfif>
<cfmail attributeCollection="#tagAttributes#">#body#<!---
---><cfloop array="#tagParams#" index="param"><!---
---><cfmailparam attributeCollection="#param#"><!---
---></cfloop><!---
---><cfloop array="#variables.parts#" index="part"><!---
---><cfset partbody = ""><!---
---><cfif structkeyexists(part,"body")><!---
---><cfset partbody = part["body"]><!---
---><cfset structdelete(part,"body")><!---
---></cfif><!---
---><cfmailpart attributeCollection="#part#">#partbody#</cfmailpart><!---
---></cfloop><!---
---></cfmail>
<cfreturn this/>
</cfcase>
<!--- feed --->
<cfcase value="feed">
<!---
fields are optional in read mode
--->
<cfif tagAttributes.action eq 'read'>
<cfset tagAttributes.query = 'query'>
<cfset tagAttributes.name = 'name'>
<cfset tagAttributes.properties = 'properties'>
</cfif>
<!--- the xmlvar is forced for both actions --->
<cfset tagAttributes.xmlvar = 'xmlvar'>
<cffeed attributeCollection="#tagAttributes#">
<cfswitch expression="#tagAttributes.action#">
<cfcase value="read">
<cfset result = {
name = name,
query = query,
properties = properties,
xmlvar = xmlvar
}>
</cfcase>
<cfcase value="create">
<cfset result = xmlvar>
</cfcase>
</cfswitch>
</cfcase>
</cfswitch>
<cfreturn result>
</cffunction>
<!---
onMissingMethod
--->
<cffunction name="onMissingMethod" output="false" access="public" returntype="any"
hint="Allow general get() set() method on the attributes struct and on extra values ( like mail body )">
<cfargument name="methodname" type="string">
<cfargument name="methodArguments" type="Array">
<cfscript>
local.attrName = mid( arguments.methodname, 4 );
local.methodType = left( arguments.methodname, 3 );
local.tagname = getTagName();
local.supportedTagAttributes = getSupportedTagAttributes().attributes;
local.tagAttributes = getAttributes();
local.lAllowedExtra = "";
switch(tagName){
case "mail":
local.lAllowedExtra = "body";
break;
case "query":
local.lAllowedExtra = "sql";
break;
}
if(methodType EQ "get" && (StructKeyExists(supportedTagAttributes, attrName) || ListFindNoCase(lAllowedExtra, attrName))){
if(StructKeyExists(tagAttributes, attrName)){
return tagAttributes[attrName];
}
else{
return "";
}
}
if(methodType EQ "set" && (StructKeyExists(supportedTagAttributes, attrName) || ListFindNoCase(lAllowedExtra, attrName))){
variables.attributes[attrName] = arguments.methodArguments[1];
return this;
}
throw("There is no method with the name #arguments.methodName#", "expression");
</cfscript>
</cffunction>
</cfcomponent>