PatternEditableListFromQuery.inc
6.26 KB
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
<?php
/**
* Class PatternEditableListFromQuery
* Takes a SQL query, an array of column names and and an array of column types
* and displays the data in an editable two column list format
*
* @author Rob Cherry, Jam Warehouse (Pty) Ltd, South Africa
* @date 27 January 2003
* @package lib.visualpatterns
*
* @todo implement HyperLinkURL option
*/
class PatternEditableListFromQuery {
/** SQL query to execute */
var $sQuery;
/** table on which to perform store/create */
var $sTableName;
/** array of columns to display */
var $aDisplayColumns;
/** columns to store */
var $aStoreColumns;
/** names of columns to display */
var $aColumnNames;
/** array of column types (1 = text, 2 = boolean, 3 = dropdown list) */
var $aDisplayColumnTypes;
/** database column types (0 = id, 1 = text, 2 = boolean) */
var $aDatabaseColumnTypes;
/** number of rows for text area if text area option is being used */
var $iTextAreaRows = 6;
/** number of colums for text area if text area option is being used */
var $iTextAreaColums = 20;
/** heading for table */
var $sTableHeading;
/** set the table width */
var $iTableWidth = null;
/** unique name to prepend to form variables */
var $sUniqueName;
/** set the tables to check for display type 3 */
var $aDropDownListTableNames;
/** set whether a column is required or not */
var $aColumnsRequired;
/**
* Default constructor
*
* @param String Query to execute
* @param
*/
function PatternEditableListFromQuery($sNewQuery, $sNewTableName, $aNewDisplayColumns, $aNewStoreColumns, $aNewColumnNames, $aNewDisplayColumnTypes, $aNewDatabaseColumnTypes) {
$this->sQuery = $sNewQuery;
$this->sTableName = $sNewTableName;
$this->aDisplayColumns = $aNewDisplayColumns;
$this->aStoreColumns = $aNewStoreColumns;
$this->aColumnNames = $aNewColumnNames;
$this->aDisplayColumnTypes = $aNewDisplayColumnTypes;
$this->aDatabaseColumnTypes = $aNewDatabaseColumnTypes;
}
function setUniqueName($sNewValue) {
$this->sUniqueName = $sNewValue;
}
function setTableWidth($iNewValue) {
$this->iTableWidth = $iNewValue;
}
function setTextAreaRows($iNewValue) {
$this->iTextAreaRows = $iNewValue;
}
function setTextAreaDisplayColumns($iNewValue) {
$this->iTextAreaDisplayColumns = $iNewValue;
}
function setTableHeading($sNewValue) {
$this->sTableHeading = $sNewValue;
}
function setRenderIndividualTableForEachResult($bNewValue) {
$this->bIndividualTableForEachResult = $bNewValue;
}
function setDropDownListTableNames($aNewValue) {
$this->aDropDownListTableNames = $aNewValue;
}
function setColumnsRequired($aNewValue) {
$this->aColumnsRequired = $aNewValue;
}
function & render() {
global $default;
//records the columns to generate validation javascript for
$aValidationColumnNames = array();
$sql = $default->db;
$sql->query($this->sQuery);
if ($sql->next_record()) {;
$sToRender = "";
$sToRender .= "<table border = 0, cellpadding = 5 " . (isset($this->iTableWidth) ? ", width = $this->iTableWidth" : "") . " >\n";
$sToRender .= "<input type=\"hidden\" name=\"unique_start_" . $this->sUniqueName . "\" value=\"\" />\n";
$sToRender .= "<input type=\"hidden\" name=\"$this->sUniqueName\" value=\"" . $sql->f("id") . "\" />\n";
$sToRender .= "<input type=\"hidden\" name=\"" . $this->sUniqueName . "_tn\" value=\"$this->sTableName\" />\n";
if (isset($this->sTableHeading)) {
$sToRender .= "<caption align=\"top\"><b>$this->sTableHeading</b></caption>\n";
}
for ($i = 0; $i < count($this->aDisplayColumns); $i++) {
$sToRender .= "<tr>\n";
$sToRender .= "<input type=\"hidden\" name=\"" . $this->sUniqueName . "_" . $i . "_tc\" value=\"" . $this->aStoreColumns[$i] . "\" />\n";
$sToRender .= "<input type=\"hidden\" name=\"" . $this->sUniqueName . "_" . $i . "_type\" value=\"" . $this->aDatabaseColumnTypes[$i] . "\" />\n";
$sToRender .= "\t<td>" . $this->aColumnNames[$i] . "</td>\n";
switch ($this->aDisplayColumnTypes[$i]) {
case 1:
//plain text field
$sToRender .= "\t<td><input type=\"text\" name=\"" . $this->sUniqueName . "_" . $i . "_value\" value=\"" . stripslashes($sql->f($this->aDisplayColumns[$i])) . "\"</td>\n";
break;
case 2:
//boolean value
$sToRender .= "\t<td><input type=\"checkbox\" name=\"" . $this->sUniqueName . "_" . $i . "_value\" value=\"1\" " . ($sql->f($this->aDisplayColumns[$i]) ? " CHECKED " : " ") . "/></td>\n";
break;
case 3:
$oPatternListBox = & new PatternListBox($this->aDropDownListTableNames[$i], "name", "id", $this->sUniqueName . "_" . $i . "_value");
$oPatternListBox->setSelectedValue($sql->f($this->aStoreColumns[$i]));
$sToRender .= "\t<td>" . $oPatternListBox->render() . "</td>\t\n";
break;
default:
break;
}
$sToRender .= "</tr>\n";
//check for required columns
if (isset($this->aColumnsRequired) &&($this->aColumnsRequired[$i])) {
$iArrayEntry = count($aValidationColumnNames);
$aValidationColumnNames[$iArrayEntry]["formName"] = $this->sUniqueName . "_" . $i . "_value";
$aValidationColumnNames[$iArrayEntry]["displayName"] = $this->aColumnNames[$i];
}
}
$sToRender .= "<input type=\"hidden\" name=\"unique_end_" . $this->sUniqueName . "\" value=\"\" />\n";
$sToRender .= "</table>\n";
} else {
$sToRender .= "<tr>\n";
$sToRender .= "<td colspan=" . count($this->aDisplayColumns) . ">No " . (isset($this->sTableHeading) ? "$this->sTableHeading" : "") . " data</td>\n";
$sToRender .= "</tr>\n";
}
$sToRender .= $this->generateRequiredFieldValidation($aValidationColumnNames);
return $sToRender;
}
function generateRequiredFieldValidation($aValidationColumnNames) {
$sToRender .= "\n\n<script language=\"javascript\">\n<!--\n";
$sToRender .= "function validateForm(theForm) {\n";
for ($i = 0; $i < count($aValidationColumnNames); $i++) {
$sToRender .= "\tif (!(validRequired(document.MainForm." . $aValidationColumnNames[$i]["formName"] . ", '" . $aValidationColumnNames[$i]["displayName"] . "'))) {\n";
$sToRender .= "\t\treturn false;\n\t}\n";
}
$sToRender .= "return true;\n}\n";
$sToRender .= "//-->\n</script>\n\n";
return $sToRender;
}
}
?>