PatternTableSqlQuery.inc
5.02 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
<?php
/**
* Class PatternTableSqlQuery
*
* Renders query results in a table.
*
* The first column in the table can be rendered as a link
* to the document/folder using the $iLinkType variable to specify the link type,
* the $sLinkPageURL to specify the page URL to link to and $sLinkImageURL to specify
* the image to display in the case of either a $iLinkType of 2 (image only) or 3 (image + text)
*
* If you wish to include images, there are two ways to do this
* o set the image url - this means that all rows will use the same image
* o set $bUseImageURLFromQuery to true - this will look for a column entitled image_url in
* the sql result set, allowing you to specify different images for each entry
*
* @author Rob Cherry, Jam Warehouse (Pty) Ltd, South Africa
* @date 7 January 2003
*
* @todo $iLinkImageURL is hard coded - change
* @todo $sLinkPageURL is hard coded - change
* @package lib.visualpatterns
*/
class PatternTableSqlQuery {
/** query to execute*/
var $sQuery;
/* Columns in table to query (ID is included by default) */
var $aColumns;
/* Column types. Possibles are 1 = text, 2 = boolean, 3 = hyperlink url */
var $aColumnTypes;
/** header names to display */
var $aColumnHeaderNames;
/** table width, either in pixels or as a percentage e.g. 600 or 100%*/
var $sWidth;
/** link url used if a column type of 3 is specified */
var $sLinkURL;
/** database column value to append to link url if a column type of 3 is specified */
var $sDBQueryStringColumn;
/** display the column headings or not */
var $bDisplayColumnHeadings;
/** table heading */
var $sTableHeading;
/** specify an image at the start of each row in the table */
var $sImageURL;
/** specify whether to use $sImageURL or to get the image url from the sql result set */
var $bUseImageURLFromQuery = false;
/** message that will be displayed if the table is empty*/
var $sEmptyTableMessage;
function PatternTableSqlQuery($sTmpQuery, $aTmpColumns, $aTmpColumnTypes, $aTmpColumnHeaderNames, $sTmpWidth, $sTmpLinkURL = null, $sTmpDBQueryStringColumn = null, $bTmpDisplayColumnHeadings = false) {
$this->sQuery = $sTmpQuery;
$this->aColumns = & $aTmpColumns;
$this->aColumnTypes = $aTmpColumnTypes;
$this->aColumnHeaderNames = $aTmpColumnHeaderNames;
$this->sWidth = $sTmpWidth;
$this->bDisplayColumnHeadings = $bTmpDisplayColumnHeadings;
$this->sLinkURL = $sTmpLinkURL;
$this->sDBQueryStringColumn = $sTmpDBQueryStringColumn;
}
function setEmptyTableMessage($sNewValue) {
$this->sEmptyTableMessage = $sNewValue;
}
function setTableHeading($sNewValue) {
$this->sTableHeading = $sNewValue;
}
function setImageURL($sNewValue) {
$this->sImageURL = $sNewValue;
}
function setUseImageURLFromQuery($bNewValue) {
$this->bUseImageURLFromQuery = $bNewValue;
}
/**
* Build the HTML string used to render the object
*
* @return String of HTML used to render object
*
* @todo possibly add in image size restraints for link types 2 and 3
*/
function & render() {
global $default;
$sToRender = "<table cellpadding = 5, border = 0>\n";
if (isset($this->sTableHeading)) {
$sToRender .= "<caption align=\"top\" colspan = " . count($this->aColumns) . ", align=left><b>$this->sTableHeading</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";
if (isset($this->sEmptyTableMessage)) {
$sToRender .= "<td colspan=" . count($this->aColumns) . ">$this->sEmptyTableMessage</td>\n";
} else {
$sToRender .= "<td colspan=" . count($this->aColumns) . ">No " . (isset($this->sTableHeading) ? $this->sTableHeading : "") . " data</td>\n";
}
$sToRender .= "</tr>\n";
} else {
while ($sql->next_record()) {
$sToRender .= "<tr>\n";
for ($i = 0; $i < count($this->aColumns); $i++) {
switch ($this->aColumnTypes[$i]) {
case 1:
$sToRender .= "<td>";
if (isset($this->sImageURL)) {
$sToRender .= $this->generateImageURL($this->sImageURL);
} else if ($this->bUseImageURLFromQuery) {
$sToRender .= $this->generateImageURL($sql->f("image_url"));
}
$sToRender .= $sql->f($this->aColumns[$i]) . "</td>";
break;
case 3:
$sToRender .= "<td><a href=\"" . $this->sLinkURL . $sql->f($this->sDBQueryStringColumn) . "\">";
if (isset($this->sImageURL)) {
$sToRender .= $this->generateImageURL($this->sImageURL);
} else if ($this->bUseImageURLFromQuery) {
$sToRender .= $this->generateImageURL($sql->f("image_url"));
}
$sToRender .= $sql->f($this->aColumns[$i]) . "</a></td>\n";
break;
default:
break;
}
}
$sToRender .= "</tr>\n";
}
}
$sToRender .= "</table>";
return $sToRender;
}
function generateImageURL($sURL) {
return "<img src=\"" . $sURL . "\" border=\"0\"/>";
}
}
?>