PatternEditableTableSqlQuery.inc
7.06 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
<?php
/**
* Class PatternEditableTableSqlQuery
*
* Will take a query that generates a table like
* result and create and editable HTML table from it that can
* be used as part of a form.
*
* 1 columns is REQUIRED in the Sql query:
* o column giving primary key of entry in table (primary key column name specified in $aPrimaryKey)
*
* 3 hidden fields are generated for each editable entry:
* o <uniquename>_id: holds the primary key of the entry in the table (-1 = no entry in table i.e. create not update)
* o <uniquename>_tn: holds the tablename for which the primary key is valid
* o <uniquename>_type: holds the type of entry (text/boolean/list) - for parsing purposes
*
* The actual value is held in a form field name <uniquename>
*
* @author Rob Cherry, Jam Warehouse (Pty) Ltd, South Africa
* @date 25 January 2003
* @todo - add client side validation stuff
* @todo - add column type 3 -> select field stuff
* @package lib.visualpatterns
*/
class PatternEditableTableSqlQuery {
/** query to execute*/
var $sQuery;
/** table to perform insert/update on */
var $sTableName;
/** columns names in $sQuery to display */
var $aDisplayColumnNames;
/** column names in $sQuery that will be stored/updated */
var $aStoreColumnNames;
/** Array of boolean values dictating column editability */
var $aColumnsEditable;
/** columns visible or not (store columns) */
var $aColumnsVisible;
/** column types: 1 = text, 2 = boolean, 3 = drop down list */
var $aColumnDisplayTypes;
/** 0 = id, 1 = text, 2 = boolean*/
var $aColumnDatabaseTypes;
/** Unique name */
var $sUniqueName;
function PatternEditableTableSqlQuery($sTmpQuery, $sTmpTableName, $aTmpStoreColumnNames, $aTmpDisplayColumnNames, $aTmpColumnsEditable, $aTmpColumnsVisible, $aTmpColumnDisplayTypes, $aTmpColumnDatabaseTypes) {
$this->sQuery = $sTmpQuery;
$this->sTableName = $sTmpTableName;
$this->aStoreColumnNames = $aTmpStoreColumnNames;
$this->aDisplayColumnNames = $aTmpDisplayColumnNames;
$this->aColumnsEditable = $aTmpColumnsEditable;
$this->sUniqueName = $sTmpUniqueName;
$this->aColumnsVisible = $aTmpColumnsVisible;
$this->aColumnDisplayTypes = $aTmpColumnDisplayTypes;
//echo "Types: " . $aTmpDatabaseColumnTypes;
$this->aColumnDatabaseTypes = $aTmpColumnDatabaseTypes;
}
function setUniqueName($sNewValue) {
$this->sUniqueName = $sNewValue;
}
function setDisplayColumnHeadings($bNewValue) {
$this->bDisplayColumnHeadings = $bNewValue;
}
function setTableCaption($sNewValue) {
$this->sTableCaption = $sNewValue;
}
function setColumnHeaderNames($aNewValue) {
$this->aColumnHeaderNames = $aNewValue;
}
function getRequiredVariableNames() {
return $this->aRequiredVariableNames;
}
function & render() {
global $default;
//$sToRender = "<table border = 0, width=$this->sWidth>\n";
$sToRender = "<table cellpadding = 5, border = 0>\n";
if (isset($this->sTableCaption)) {
$sToRender .= "<caption align=\"top\" colspan = " . count($this->aColumnsSql) . ", align=left><b>$this->sTableCaption</b></caption>\n";
}
if ($this->bDisplayColumnHeadings) {
for ($i = 0; $i < count($this->aColumnHeaderNames); $i++) {
$sToRender .= "<th align=left>" . $this->aColumnHeaderNames[$i] . "</th>\n";
}
}
$sql = $default->db;
$sql->query($this->sQuery);
if ($sql->num_rows() == 0) {
$sToRender .= "<tr>\n";
$sToRender .= "<td colspan=" . count($this->aColumnsSql) . ">No " . (isset($this->sTableCaption) ? $this->sTableCaption : "") . " data</td>\n";
$sToRender .= "</tr>\n";
} else {
$iRowCount = 0;
while ($sql->next_record()) {
$sToRender .= "<tr>\n";
$sToRender .= "<input type=\"hidden\" name=\"unique_start_" . $this->sUniqueName . $iRowCount . "\" value=\"\" />\n";
$sToRender .= "<input type=\"hidden\" name=\"" . $this->sUniqueName . "_" . $iRowCount . "\" value=\"" . $sql->f("id") . "\" />\n";
$sToRender .= "<input type=\"hidden\" name=\"" . $this->sUniqueName . "_" . $iRowCount . "_tn\" value=\"" . $this->sTableName . "\" />\n";
for ($i = 0; $i < count($this->aStoreColumnNames); $i++) {
//generate the right kind of editable field
$sToRender .= "\t<td>\n";
switch ($this->aColumnDisplayTypes[$i]) {
case 1:
//output the table column name
$sToRender .= "\t<input type=\"hidden\" name=\"" . $this->sUniqueName . "_" . $iRowCount . $i . "_tc\" value=\"" . $this->aStoreColumnNames[$i] . "\" />\n";
//output the column type
$sToRender .= "\t<input type=\"hidden\" name=\"" . $this->sUniqueName . "_" . $iRowCount . $i . "_type\" value=\"" . $this->aColumnDatabaseTypes[$i] . "\" />\n";
//output the value
if ($this->aColumnsVisible[$i]) {
if ($this->aColumnsEditable[$i]) {
$sToRender .= "\t<input type=\"text\" name=\"" . $this->sUniqueName . "_" . $iRowCount . $i . "_value\" value=\"" . stripslashes($sql->f($this->aStoreColumnNames[$i])) . "\" />\n";
} else {
$sToRender .= "\t<input type=\"hidden\" name=\"" . $this->sUniqueName . "_" . $iRowCount . $i . "_value\" value=\"" . $sql->f($this->aStoreColumnNames[$i]) . "\" />".$sql->f($this->aDisplayColumnNames[$i])."\n";
}
} else {
$sToRender .= "\t<input type=\"hidden\" name=\"" . $this->sUniqueName . "_" . $iRowCount . $i . "_value\" value=\"" . $sql->f($this->aStoreColumnNames[$i]) . "\" />\n";
}
break;
case 2:
//output the table column name
$sToRender .= "\t<input type=\"hidden\" name=\"tc_" . $this->sUniqueName . "_" . $iRowCount . $i . "_tc\" value=\"" . $this->aStoreColumnNames[$i] . "\" />\n";
//output the column type
$sToRender .= "\t<input type=\"hidden\" name=\"" . $this->sUniqueName . "_" . $iRowCount . $i . "_type\" value=\"" . $this->aColumnDatabaseTypes[$i] . "\" />\n";
//output the value
if ($this->aColumnsVisible[$i]) {
if ($this->aColumnsEditable[$i]) {
$sToRender .= "\t<input type=\"checkbox\" name=\"" . $this->sUniqueName . "_" . $iRowCount . $i . "_value\" value=\"" . ($sql->f($this->aStoreColumnNames[$i] ? "1\" checked" : "0\"")) . " />\n";
} else {
$sToRender .= "\t<input type=\"hidden\" name=\"" . $this->sUniqueName . "_" . $iRowCount . $i . "_value\" value=\"" . $sql->f($this->aStoreColumnNames[$i]) . "\" />".$sql->f($this->aDisplayColumnNames[$i])."\n";
}
} else {
$sToRender .= "\t<input type=\"hidden\" name=\"" . $this->sUniqueName . "_" . $iRowCount . $i . "_value\" value=\"" . $sql->f($this->aStoreColumnNames[$i]) . "\" />\n";
}
break;
case 3:
$sToRender .= "<b>The column type for drop downs is not implemented yet</b>\n";
break;
default:
$sToRender .= "<b>You are attempting to render an unknown column type " . ($this->aColumnTypes[$i]) . " in PatternEditableTableSqlQuery</b>\n";
break;
}
$sToRender .= "\t</td>\n";
}
$sToRender .= "<input type=\"hidden\" name=\"unique_end_" . $this->sUniqueName . $iRowCount . "\" value=\"\" />\n";
$sToRender .= "</tr>\n";
$iRowCount++;
}
}
$sToRender .= "</table>";
return $sToRender;
}
}
?>