1 |
<?php |
---|
2 |
/** |
---|
3 |
* SQLQuery Widget class |
---|
4 |
* |
---|
5 |
* @project CWC2 |
---|
6 |
* @revision $Id: |
---|
7 |
* @purpose SQLQuery Widget class |
---|
8 |
* @author DM Solutions Group (lacroix@dmsolutions.ca) |
---|
9 |
* @copyright |
---|
10 |
* <b>Copyright (c) 2003, DM Solutions Group Inc.</b> |
---|
11 |
* Permission is hereby granted, free of charge, to any person obtaining a |
---|
12 |
* copy of this software and associated documentation files (the "Software"), |
---|
13 |
* to deal in the Software without restriction, including without limitation |
---|
14 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
15 |
* and/or sell copies of the Software, and to permit persons to whom the |
---|
16 |
* Software is furnished to do so, subject to the following conditions: |
---|
17 |
* |
---|
18 |
* The above copyright notice and this permission notice shall be included |
---|
19 |
* in all copies or substantial portions of the Software. |
---|
20 |
* |
---|
21 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
22 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
23 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
---|
24 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
25 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
26 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
27 |
* DEALINGS IN THE SOFTWARE. |
---|
28 |
*/ |
---|
29 |
|
---|
30 |
|
---|
31 |
include_once(dirname(__FILE__)."/../Widget.php"); |
---|
32 |
include_once(dirname(__FILE__)."/../SharedResource/SharedResource.widget.php"); |
---|
33 |
|
---|
34 |
/** |
---|
35 |
* SQLQueryWidget |
---|
36 |
* |
---|
37 |
* @desc Widget That return the result of a given SQL query |
---|
38 |
*/ |
---|
39 |
class SQLQuery extends CWCWidget |
---|
40 |
{ |
---|
41 |
var $mszDBType = "MYSQL"; |
---|
42 |
var $mszSQLQuery = ""; |
---|
43 |
var $mszServer = ""; |
---|
44 |
var $mszDatabase = ""; |
---|
45 |
var $mszUserName = ""; |
---|
46 |
var $mszPassword = ""; |
---|
47 |
var $mszSharedResourceName = "SQLQuery"; |
---|
48 |
var $mbSaveQuery = true; |
---|
49 |
var $mbRandomResult = false; |
---|
50 |
|
---|
51 |
var $maszFilters = array(); |
---|
52 |
|
---|
53 |
/** |
---|
54 |
* SQLQueryWidget |
---|
55 |
* |
---|
56 |
* Constructor method for the SQLQuery widget. |
---|
57 |
*/ |
---|
58 |
function SQLQuery() |
---|
59 |
{ |
---|
60 |
// invoke constructor of parent |
---|
61 |
parent::CWCWidget(); |
---|
62 |
|
---|
63 |
// set the description for this widget |
---|
64 |
$this->szWidgetDescription = <<<EOT |
---|
65 |
SQLQueryWidget is a simple widget that simply return the return value of a given SQL query. |
---|
66 |
EOT; |
---|
67 |
|
---|
68 |
if ($this->mszSQLQuery == "" ) |
---|
69 |
$this->maAttributes["SQLQUERY"] = new StringAttribute( "SQLQUERY", true ); |
---|
70 |
|
---|
71 |
if ($this->mszServer == "") |
---|
72 |
$this->maAttributes["SERVER"] = new StringAttribute( "SERVER", true ); |
---|
73 |
|
---|
74 |
if ($this->mszDatabase == "") |
---|
75 |
$this->maAttributes["DATABASE"] = new StringAttribute( "DATABASE", true ); |
---|
76 |
|
---|
77 |
if ($this->mszUserName == "") |
---|
78 |
$this->maAttributes["USERNAME"] = new StringAttribute( "USERNAME", true ); |
---|
79 |
|
---|
80 |
if ($this->mszPassword == "") |
---|
81 |
$this->maAttributes["PASSWORD"] = new StringAttribute( "PASSWORD", true ); |
---|
82 |
|
---|
83 |
$this->maAttributes["DBTYPE"] = new StringAttribute( "DBTYPE", false ); |
---|
84 |
$this->maAttributes["SHAREDRESOURCENAME"] = new StringAttribute( "SHAREDRESOURCENAME", false ); |
---|
85 |
$this->maAttributes["SAVEQUERY"] = new BooleanAttribute( "SAVEQUERY", false ); |
---|
86 |
|
---|
87 |
$this->mnPriority = PRIORITY_SUPER; |
---|
88 |
$this->mnMaturityLevel = MATURITY_BETA; |
---|
89 |
} |
---|
90 |
|
---|
91 |
/** |
---|
92 |
* initialize respectable defaults |
---|
93 |
*/ |
---|
94 |
function InitDefaults() |
---|
95 |
{ |
---|
96 |
parent::InitDefaults(); |
---|
97 |
|
---|
98 |
if(isset($this->maParams["DBTYPE"])) |
---|
99 |
$this->mszDBType = strtoupper($this->maParams["DBTYPE"]); |
---|
100 |
if(isset($this->maParams["SQLQUERY"])) |
---|
101 |
$this->mszSQLQuery = $this->maParams["SQLQUERY"]; |
---|
102 |
if(isset($this->maParams["SERVER"])) |
---|
103 |
$this->mszServer = $this->maParams["SERVER"]; |
---|
104 |
if(isset($this->maParams["DATABASE"])) |
---|
105 |
$this->mszDatabase = $this->maParams["DATABASE"]; |
---|
106 |
if(isset($this->maParams["USERNAME"])) |
---|
107 |
$this->mszUserName = $this->maParams["USERNAME"]; |
---|
108 |
if(isset($this->maParams["PASSWORD"])) |
---|
109 |
$this->mszPassword = $this->maParams["PASSWORD"]; |
---|
110 |
if(isset($this->maParams["SHAREDRESOURCENAME"])) |
---|
111 |
$this->mszSharedResourceName=$this->maParams["SHAREDRESOURCENAME"]; |
---|
112 |
|
---|
113 |
if (isset($this->maParams["SAVEQUERY"])) |
---|
114 |
$this->mbSaveQuery = (strtoupper($this->maParams["SAVEQUERY"]) == "FALSE") ? false:true; |
---|
115 |
|
---|
116 |
if (isset($this->maParams["RANDOMRESULT"])) |
---|
117 |
$this->mbRandomResult = |
---|
118 |
(strtoupper($this->maParams["RANDOMRESULT"])=="TRUE") ? true : false; |
---|
119 |
|
---|
120 |
// Get all filters |
---|
121 |
if (isset($this->maszContents['FILTERS'])) |
---|
122 |
{ |
---|
123 |
foreach ($this->maszContents['FILTERS'] as $oFilter) |
---|
124 |
{ |
---|
125 |
if (isset($oFilter['ID']) && isset($oFilter['FILTER'])) |
---|
126 |
{ |
---|
127 |
$this->maszFilters[$oFilter['ID']] = array("FILTER" => $oFilter['FILTER']); |
---|
128 |
|
---|
129 |
if (isset($oFilter['TABLE']) && isset($oFilter['LINK'])) |
---|
130 |
{ |
---|
131 |
// array_push($this->maszFilter[$oFilter['ID']], "TABLE" => $oFilter['TABLE'], |
---|
132 |
// "LINK" => $oFilter['LINK']); |
---|
133 |
} |
---|
134 |
} |
---|
135 |
} |
---|
136 |
} |
---|
137 |
} |
---|
138 |
|
---|
139 |
/** |
---|
140 |
* Connect to the database and run the query |
---|
141 |
*/ |
---|
142 |
function ParseURL() |
---|
143 |
{ |
---|
144 |
// Replace dynamic values in the SQL query |
---|
145 |
$this->ParseSQLQuery(); |
---|
146 |
|
---|
147 |
$szQueryType = strtoupper(substr($this->mszSQLQuery, 0, 6)); |
---|
148 |
|
---|
149 |
if($this->mbSaveQuery && $szQueryType == "SELECT" && |
---|
150 |
isset($_SESSION["SessSQLQuery"][$this->mszSQLQuery])) |
---|
151 |
{ |
---|
152 |
$this->maSharedResourceWidgets[$this->mszSharedResourceName] = |
---|
153 |
$_SESSION["SessSQLQuery"][$this->mszSQLQuery]; |
---|
154 |
|
---|
155 |
// Add the number of Row/Col in the SharedResource |
---|
156 |
$this->maSharedResourceWidgets[$this->mszSharedResourceName."_NumRows"] = |
---|
157 |
count($this->maSharedResourceWidgets[$this->mszSharedResourceName]); |
---|
158 |
if(isset($this->maSharedResourceWidgets[$this->mszSharedResourceName][0])) |
---|
159 |
$this->maSharedResourceWidgets[$this->mszSharedResourceName."_NumCols"] = |
---|
160 |
count($this->maSharedResourceWidgets[$this->mszSharedResourceName][0]); |
---|
161 |
else |
---|
162 |
$this->maSharedResourceWidgets[$this->mszSharedResourceName."_NumCols"] = 0; |
---|
163 |
|
---|
164 |
if(count($this->maSharedResourceWidgets[$this->mszSharedResourceName]) == 0) |
---|
165 |
{ |
---|
166 |
$szErrorLevel = $this->ProcessEvent("NoRowReturned"); |
---|
167 |
if($szErrorLevel == ONEVENT_FATAL) |
---|
168 |
return false; |
---|
169 |
} |
---|
170 |
|
---|
171 |
return true; |
---|
172 |
} |
---|
173 |
|
---|
174 |
switch ($this->mszDBType) |
---|
175 |
{ |
---|
176 |
case "MYSQL": |
---|
177 |
// Connect to the database |
---|
178 |
$link = @mysql_connect($this->mszServer, $this->mszUserName, |
---|
179 |
$this->mszPassword); |
---|
180 |
if($link == false) |
---|
181 |
{ |
---|
182 |
$this->ProcessEvent("ConnectFailed"); |
---|
183 |
return false; |
---|
184 |
} |
---|
185 |
if(!mysql_select_db($this->mszDatabase, $link)) |
---|
186 |
{ |
---|
187 |
$this->ProcessEvent("SelectDBFailed"); |
---|
188 |
return false; |
---|
189 |
} |
---|
190 |
|
---|
191 |
// Run the SQL query |
---|
192 |
$hRes = mysql_query($this->mszSQLQuery, $link); |
---|
193 |
|
---|
194 |
if($hRes == false) |
---|
195 |
{ |
---|
196 |
$this->ProcessEvent("QueryFailed"); |
---|
197 |
return false; |
---|
198 |
} |
---|
199 |
|
---|
200 |
switch($szQueryType) |
---|
201 |
{ |
---|
202 |
case "SELECT": |
---|
203 |
// Get all returned rows and put them in a Shared Resource |
---|
204 |
$i = 0; |
---|
205 |
$aMySQLResult = array(); |
---|
206 |
while ($aRow = mysql_fetch_array($hRes, MYSQL_ASSOC)) |
---|
207 |
{ |
---|
208 |
$aMySQLResult[$i] = $aRow; |
---|
209 |
$i++; |
---|
210 |
} |
---|
211 |
|
---|
212 |
if ($this->mbRandomResult) |
---|
213 |
{ |
---|
214 |
srand((float)microtime()*1000000); |
---|
215 |
shuffle($aMySQLResult); |
---|
216 |
} |
---|
217 |
|
---|
218 |
$this->maSharedResourceWidgets[$this->mszSharedResourceName] = |
---|
219 |
$aMySQLResult; |
---|
220 |
|
---|
221 |
// Add the number of Row/Col in the SharedResource |
---|
222 |
$this->maSharedResourceWidgets[$this->mszSharedResourceName."_NumRows"] = |
---|
223 |
count($this->maSharedResourceWidgets[$this->mszSharedResourceName]); |
---|
224 |
|
---|
225 |
if (count($this->maSharedResourceWidgets[$this->mszSharedResourceName])>0) |
---|
226 |
$this->maSharedResourceWidgets[$this->mszSharedResourceName."_NumCols"] = |
---|
227 |
count($this->maSharedResourceWidgets[$this->mszSharedResourceName][0]); |
---|
228 |
else |
---|
229 |
$this->maSharedResourceWidgets[$this->mszSharedResourceName."_NumCols"] = 0; |
---|
230 |
|
---|
231 |
if($this->mbSaveQuery) |
---|
232 |
{ |
---|
233 |
$_SESSION["SessSQLQuery"][$this->mszSQLQuery] = |
---|
234 |
$aMySQLResult; |
---|
235 |
} |
---|
236 |
|
---|
237 |
if(($numRows = mysql_num_rows($hRes)) <= 0) |
---|
238 |
{ |
---|
239 |
$szErrorLevel = $this->ProcessEvent("NoRowReturned"); |
---|
240 |
if($szErrorLevel == ONEVENT_FATAL) |
---|
241 |
return false; |
---|
242 |
} |
---|
243 |
|
---|
244 |
break; |
---|
245 |
case "INSERT": |
---|
246 |
case "UPDATE": |
---|
247 |
case "DELETE": |
---|
248 |
if(($numRows = mysql_affected_rows($link)) <= 0) |
---|
249 |
{ |
---|
250 |
$szErrorLevel = $this->ProcessEvent("NoRowReturned"); |
---|
251 |
if($szErrorLevel == ONEVENT_FATAL) |
---|
252 |
return false; |
---|
253 |
} |
---|
254 |
break; |
---|
255 |
} |
---|
256 |
|
---|
257 |
// Close the connection |
---|
258 |
mysql_close($link); |
---|
259 |
break; |
---|
260 |
case "PGSQL": |
---|
261 |
// Connect to the database |
---|
262 |
$szConnect = ''; |
---|
263 |
$szConnect .= ($this->mszServer != '')?"host=$this->mszServer ":''; |
---|
264 |
$szConnect .= ($this->mszServer != '')?"user=$this->mszUserName ":''; |
---|
265 |
$szConnect .= ($this->mszServer != '')?"password=$this->mszPassword ":''; |
---|
266 |
$szConnect .= ($this->mszServer != '')?"dbname=$this->mszDatabase ":''; |
---|
267 |
$link = @pg_connect($szConnect); |
---|
268 |
if($link == false) |
---|
269 |
{ |
---|
270 |
$this->ProcessEvent("ConnectFailed"); |
---|
271 |
return false; |
---|
272 |
} |
---|
273 |
// if(!mysql_select_db($this->mszDatabase, $link)) |
---|
274 |
// { |
---|
275 |
// $this->ProcessEvent("SelectDBFailed"); |
---|
276 |
// return false; |
---|
277 |
// } |
---|
278 |
|
---|
279 |
// Run the SQL query |
---|
280 |
$hRes = pg_query($link, $this->mszSQLQuery); |
---|
281 |
|
---|
282 |
if($hRes == false) |
---|
283 |
{ |
---|
284 |
$this->ProcessEvent("QueryFailed"); |
---|
285 |
return false; |
---|
286 |
} |
---|
287 |
|
---|
288 |
switch($szQueryType) |
---|
289 |
{ |
---|
290 |
case "SELECT": |
---|
291 |
// Get all returned rows and put them in a Shared Resource |
---|
292 |
$i = 0; |
---|
293 |
$aResult = array(); |
---|
294 |
while ($aRow = pg_fetch_assoc($hRes)) |
---|
295 |
{ |
---|
296 |
$aResult[$i] = $aRow; |
---|
297 |
$i++; |
---|
298 |
} |
---|
299 |
|
---|
300 |
if ($this->mbRandomResult) |
---|
301 |
{ |
---|
302 |
srand((float)microtime()*1000000); |
---|
303 |
shuffle($aResult); |
---|
304 |
} |
---|
305 |
|
---|
306 |
$this->maSharedResourceWidgets[$this->mszSharedResourceName] = |
---|
307 |
$aResult; |
---|
308 |
|
---|
309 |
// Add the number of Row/Col in the SharedResource |
---|
310 |
$this->maSharedResourceWidgets[$this->mszSharedResourceName."_NumRows"] = |
---|
311 |
count($this->maSharedResourceWidgets[$this->mszSharedResourceName]); |
---|
312 |
|
---|
313 |
if (count($this->maSharedResourceWidgets[$this->mszSharedResourceName])>0) |
---|
314 |
$this->maSharedResourceWidgets[$this->mszSharedResourceName."_NumCols"] = |
---|
315 |
count($this->maSharedResourceWidgets[$this->mszSharedResourceName][0]); |
---|
316 |
else |
---|
317 |
$this->maSharedResourceWidgets[$this->mszSharedResourceName."_NumCols"] = 0; |
---|
318 |
|
---|
319 |
if($this->mbSaveQuery) |
---|
320 |
{ |
---|
321 |
$_SESSION["SessSQLQuery"][$this->mszSQLQuery] = |
---|
322 |
$aResult; |
---|
323 |
} |
---|
324 |
|
---|
325 |
if(($numRows = pg_num_rows($hRes)) <= 0) |
---|
326 |
{ |
---|
327 |
$szErrorLevel = $this->ProcessEvent("NoRowReturned"); |
---|
328 |
if($szErrorLevel == ONEVENT_FATAL) |
---|
329 |
return false; |
---|
330 |
} |
---|
331 |
|
---|
332 |
break; |
---|
333 |
case "INSERT": |
---|
334 |
case "UPDATE": |
---|
335 |
case "DELETE": |
---|
336 |
if(($numRows = pg_affected_rows($link)) <= 0) |
---|
337 |
{ |
---|
338 |
$szErrorLevel = $this->ProcessEvent("NoRowReturned"); |
---|
339 |
if($szErrorLevel == ONEVENT_FATAL) |
---|
340 |
return false; |
---|
341 |
} |
---|
342 |
break; |
---|
343 |
} |
---|
344 |
|
---|
345 |
// Close the connection |
---|
346 |
pg_close($link); |
---|
347 |
break; |
---|
348 |
} |
---|
349 |
|
---|
350 |
return true; |
---|
351 |
} |
---|
352 |
|
---|
353 |
/** |
---|
354 |
* DrawPublish() |
---|
355 |
* |
---|
356 |
* return an empty string. Should be redefined for Widgets |
---|
357 |
* returning HTML code. |
---|
358 |
*/ |
---|
359 |
function DrawPublish() |
---|
360 |
{ |
---|
361 |
$szReturn = parent::DrawPublish(); |
---|
362 |
return $szReturn; |
---|
363 |
} |
---|
364 |
|
---|
365 |
/** |
---|
366 |
* ParseSQLQuery() |
---|
367 |
* |
---|
368 |
* Replace the dynamic values (SharedResource) in the SQL query |
---|
369 |
*/ |
---|
370 |
function ParseSQLQuery() |
---|
371 |
{ |
---|
372 |
foreach ($this->maSharedResourceWidgets as $szSRName => $szValue) |
---|
373 |
{ |
---|
374 |
if (stristr($this->mszSQLQuery, "[$".$szSRName) !== false) |
---|
375 |
{ |
---|
376 |
if(is_array($szValue)) |
---|
377 |
{ |
---|
378 |
$aKeys = array_keys($szValue); |
---|
379 |
$numRow = count($szValue); |
---|
380 |
$oSR = $szValue; |
---|
381 |
for($i=0; $i<$numRow; $i++) |
---|
382 |
{ |
---|
383 |
if(stristr($this->mszSQLQuery, |
---|
384 |
"[$".$szSRName.".".$aKeys[$i]) !== false) |
---|
385 |
{ |
---|
386 |
$szSRName .= ".".$aKeys[$i]; |
---|
387 |
if(!is_array($oSR[$aKeys[$i]])) |
---|
388 |
{ |
---|
389 |
$szValue = $oSR[$aKeys[$i]]; |
---|
390 |
break; |
---|
391 |
} |
---|
392 |
$oSR = $oSR[$aKeys[$i]]; |
---|
393 |
$i = -1; |
---|
394 |
$aKeys = array_keys($oSR); |
---|
395 |
$numRow = count($oSR); |
---|
396 |
} |
---|
397 |
} |
---|
398 |
} |
---|
399 |
if(!is_array($szValue)) |
---|
400 |
$this->mszSQLQuery = str_replace("[$".$szSRName."]", |
---|
401 |
$szValue, $this->mszSQLQuery); |
---|
402 |
} |
---|
403 |
} |
---|
404 |
} |
---|
405 |
} |
---|
406 |
?> |
---|