-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathQueryView.php
412 lines (335 loc) · 11.2 KB
/
QueryView.php
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?php
/*******************************************************************************
*
* filename : QueryView.php
* last change : 2003-06-09
* website : http://www.infocentral.org
* copyright : Copyright 2001, 2002 Deane Barker
*
* InfoCentral is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
******************************************************************************/
//Include the function library
require "Include/Config.php";
require "Include/Functions.php";
//Set the page title
$sPageTitle = gettext("Query View");
//Get the QueryID from the querystring
$iQueryID = FilterInput($_GET["QueryID"],'int');
$aFinanceQueries = explode(',', $aFinanceQueries);
if (!$_SESSION['bFinance'] && in_array($iQueryID,$aFinanceQueries))
{
Redirect("Menu.php");
exit;
}
//Include the header
require "Include/Header.php";
//Get the query information
$sSQL = "SELECT * FROM query_qry WHERE qry_ID = " . $iQueryID;
$rsSQL = RunQuery($sSQL);
extract(mysql_fetch_array($rsSQL));
//Get the parameters for this query
$sSQL = "SELECT * FROM queryparameters_qrp WHERE qrp_qry_ID = " . $iQueryID;
$rsParameters = RunQuery($sSQL);
//If the form was submitted or there are no parameters, run the query
if (isset($_POST["Submit"]) || mysql_num_rows($rsParameters) == 0)
{
//Check that all validation rules were followed
ValidateInput();
//Any errors?
if (count($aErrorText) == 0)
{
//No errors; process the SQL, run the query, and display the results
ProcessSQL();
DoQuery();
}
else
{
//Yes, there were errors; re-display the parameter form (the DisplayParameterForm function will
//pick up and display any error messages)
DisplayQueryInfo();
DisplayParameterForm();
}
}
else
{
//Display the parameter form
DisplayQueryInfo();
DisplayParameterForm();
}
//Loops through all the parameters and ensures validation rules have been followed
function ValidateInput()
{
global $rsParameters;
global $_POST;
global $vPOST;
global $aErrorText;
//Initialize the validated post array, error text array, and the error flag
$vPOST = array();
$aErrorText = array();
$bError = false;
//Are there any parameters to loop through?
if (mysql_num_rows($rsParameters)) { mysql_data_seek($rsParameters,0); }
while ($aRow = mysql_fetch_array($rsParameters))
{
extract($aRow);
//Is the value required?
if ($qrp_Required && strlen(trim($_POST[$qrp_Alias])) < 1)
{
$bError = true;
$aErrorText[$qrp_Alias] = gettext("This value is required.");
}
//Assuming there was no error above...
else
{
//Validate differently depending on the contents of the qrp_Validation field
switch ($qrp_Validation)
{
//Numeric validation
case "n":
//Is it a number?
if (!is_numeric($_POST[$qrp_Alias]))
{
$bError = true;
$aErrorText[$qrp_Alias] = gettext("This value must be numeric.");
}
else
{
//Is it more than the minimum?
if ($_POST[$qrp_Alias] < $qrp_NumericMin)
{
$bError = true;
$aErrorText[$qrp_Alias] = gettext("This value must be at least ") . $qrp_NumericMin;
}
//Is it less than the maximum?
elseif ($_POST[$qrp_Alias] > $qrp_NumericMax)
{
$bError = true;
$aErrorText[$qrp_Alias] = gettext("This value cannot be more than ") . $qrp_NumericMax;
}
}
$vPOST[$qrp_Alias] = FilterInput($_POST[$qrp_Alias],'int');
break;
//Alpha validation
case "a":
//Is the length less than the maximum?
if (strlen($_POST[$qrp_Alias]) > $qrp_AlphaMaxLength)
{
$bError = true;
$aErrorText[$qrp_Alias] = gettext("This value cannot be more than ") . $qrp_AlphaMaxLength . gettext(" characters long");
}
//is the length more than the minimum?
elseif (strlen($_POST[$qrp_Alias]) < $qrp_AlphaMinLength)
{
$bError = true;
$aErrorText[$qrp_Alias] = gettext("This value cannot be less than ") . $qrp_AlphaMinLength . gettext(" characters long");
}
$vPOST[$qrp_Alias] = FilterInput($_POST[$qrp_Alias]);
break;
default:
$vPOST[$qrp_Alias] = FilterInput($_POST[$qrp_Alias]);
break;
}
}
}
}
//Loops through the list of parameters and replaces their alias in the SQL with the value given for the parameter
function ProcessSQL()
{
global $vPOST;
global $qry_SQL;
global $rsParameters;
//Loop through the list of parameters
if (mysql_num_rows($rsParameters)) {mysql_data_seek($rsParameters,0); }
while ($aRow = mysql_fetch_array($rsParameters))
{
extract($aRow);
//Debugging code
//echo "--" . $qry_SQL . "<br>--" . "~" . $qrp_Alias . "~" . "<br>--" . $vPOST[$qrp_Alias] . "<p>";
//Replace the placeholder with the parameter value
$qry_SQL = str_replace("~" . $qrp_Alias . "~",$vPOST[$qrp_Alias],$qry_SQL);
}
}
//Checks if a count is to be displayed, and displays it if required
function DisplayRecordCount()
{
global $qry_Count;
global $rsQueryResults;
//Are we supposed to display a count for this query?
if ($qry_Count == 1)
{
//Display the count of the recordset
echo "<p align=\"center\">";
echo mysql_num_rows($rsQueryResults) . gettext(" record(s) returned");
echo "</p>";
}
}
//Runs the parameterized SQL and display the results
function DoQuery()
{
global $cnInfoCentral;
global $aRowClass;
global $rsQueryResults;
global $qry_SQL;
global $iQueryID;
//Run the SQL
$rsQueryResults = RunQuery($qry_SQL);
//Set the first row style
$sRowClass = "RowColorA";
//Check for a count display
DisplayRecordCount();
//Start the table and the header row
echo "<table align=\"center\" cellpadding=\"5\" cellspacing=\"0\">";
echo "<tr class=\"TableHeader\">";
//Loop through the fields and write the header row
for ($iCount = 0; $iCount < mysql_num_fields($rsQueryResults); $iCount++)
{
//If this field is called "AddToCart", don't display this field...
if (mysql_field_name($rsQueryResults,$iCount) != "AddToCart")
{
echo "<td>" . mysql_field_name($rsQueryResults,$iCount) . "</td>";
}
}
//Close the header row
echo "</tr>";
//Loop through the recordset
while($aRow = mysql_fetch_array($rsQueryResults))
{
//Alternate the background color of the row
$sRowClass = AlternateRowStyle($sRowClass);
//Begin the row
echo "<tr class=\"" . $sRowClass . "\">";
//Loop through the fields and write each one
for ($iCount = 0; $iCount < mysql_num_fields($rsQueryResults); $iCount++)
{
//If this field is called "AddToCart", add this to the hidden form field...
if (mysql_field_name($rsQueryResults,$iCount) == "AddToCart")
{
$aHiddenFormField[] = $aRow[$iCount];
}
//...otherwise just render the field
else
{
//Write the actual value of this row
echo "<td>" . $aRow[$iCount] . " </td>";
}
}
//Close the row
echo "</tr>";
}
//Close the table and allow a link to run the query again
echo "</table>";
echo "<p align=\"center\">";
if (is_array($aHiddenFormField) && count($aHiddenFormField) > 0)
{
?>
<form method="post" action="CartView.php"><p align="center">
<input type="hidden" value="<?php echo join(",",$aHiddenFormField); ?>" name="BulkAddToCart">
<input type="submit" class="icButton" name="AddToCartSubmit" value="<?php echo gettext("Add Results To Cart");?>">
<input type="submit" class="icButton" name="AndToCartSubmit" value="<?php echo gettext("Intersect Results With Cart");?>">
<input type="submit" class="icButton" name="NotToCartSubmit" value="<?php echo gettext("Remove Results From Cart");?>">
</p></form>
<?php
}
echo "<p align=\"center\"><a href=\"QueryView.php?QueryID=" . $iQueryID . "\">" . gettext("Run Query Again") . "</a></p>";
echo "<p align=\"center\"><a href=\"QueryList.php\">". gettext("Return to Query Menu") . "</a></p>";
//Print the SQL to make debugging easier
echo "<br><p class=\"ShadedBox\"><span class=\"SmallText\">" . str_replace(Chr(13),"<br>",htmlspecialchars($qry_SQL)) . "</span></p>";
}
//Displays the name and description of the query
function DisplayQueryInfo()
{
global $rsSQL;
global $qry_Name;
global $qry_Description;
//Display the information about this query
echo "<p align=\"center\">";
echo "<b>" . $qry_Name . "</b><br>" . $qry_Description;
echo "</p>";
}
//Displays a form to enter values for each parameter, creating INPUT boxes and SELECT drop-downs as necessary
function DisplayParameterForm()
{
global $rsParameters;
global $iQueryID;
global $aErrorText;
global $cnInfoCentral;
//Start the form and the table
echo '<form method="post" action="QueryView.php?QueryID=' . $iQueryID . '">';
echo '<table align="center" cellpadding="5" cellspacing="1" border="0">';
//Loop through the parameters and display an entry box for each one
if (mysql_num_rows($rsParameters)) {mysql_data_seek($rsParameters,0); }
while ($aRow = mysql_fetch_array($rsParameters))
{
extract($aRow);
//Begin the row, giving the name of the parameter
echo "<tr>";
echo "<td class=\"LabelColumn\">" . $qrp_Name . ":</td>";
//Determine the type of parameter we're dealing with
switch ($qrp_Type)
{
//Standard INPUT box
case 0:
//Begin the table cell, disoplay the INPUT tag, close the table cell
echo "<td class=\"TextColumn\">";
echo "<input size=\"" . $qrp_InputBoxSize . "\" name=\"" . $qrp_Alias . "\" type=\"text\" value=\"" . $qrp_Default . "\">";
echo "</td>";
break;
//SELECT box with OPTION tags supplied in the queryparameteroptions_qpo table
case 1:
//Get the query parameter options for this parameter
$sSQL = "SELECT * FROM queryparameteroptions_qpo WHERE qpo_qrp_ID = " . $qrp_ID;
$rsParameterOptions = RunQuery($sSQL);
//Begin the table cell and the SELECT tag
echo "<td class=\"TextColumn\">";
echo "<select name=\"" . $qrp_Alias . "\">";
//Loop through the parameter options
while ($ThisRow = mysql_Fetch_array($rsParameterOptions))
{
extract($ThisRow);
//Display the OPTION tag
echo "<option value=\"" . $qpo_Value . "\">" . $qpo_Display . "</option>";
}
//Close the SELECT tag and table cell
echo "</select>";
echo "</td>";
break;
//SELECT box with OPTION tags provided via a SQL query
case 2:
//Run the SQL to get the options
$rsParameterOptions = RunQuery($qrp_OptionSQL);
echo "<td class=\"TextColumn\">";
echo "<select name=\"" . $qrp_Alias . "\">";
while ($ThisRow = mysql_Fetch_array($rsParameterOptions))
{
extract($ThisRow);
echo "<option value=\"" . $Value . "\">" . $Display . "</option>";
}
//Close the SELECT tag and table cell
echo "</select>";
echo "</td>";
break;
}
//Display the query description and close the row
echo "<td valign=\"top\" class=\"SmallText\">" . $qrp_Description . "</td>";
echo "</tr>";
//If we are re-rendering this form due to a validation error, display the error
if (isset($aErrorText[$qrp_Alias]))
{
echo "<tr><td colspan=\"3\" style=\"color: red;\">" . $aErrorText[$qrp_Alias] . "</td></tr>";
}
}
?>
<td colspan="3" align="center">
<br>
<input class="icButton" type="Submit" value="<?php echo gettext("Execute Query"); ?>" name="Submit">
</p>
</table>
</form>
<?php
}
require "Include/Footer.php";
?>