Commit 4b2ec61abe7b07570d18992a0d52aaab79b45f37

Authored by nbm
1 parent 36a34b2f

Extend from PatternTableGeneric, passing in an adaptered version of the

PEAR DB result set.


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@3047 c91229c3-7414-0410-bfa2-8a42b809f60b
lib/visualpatterns/PatternTableSqlQuery.inc
... ... @@ -36,192 +36,62 @@
36 36 * @todo $iLinkImageURL is hard coded - change
37 37 * @todo $sLinkPageURL is hard coded - change
38 38 */
39   -class PatternTableSqlQuery {
40   -
41   - /** query to execute*/
42   - var $sQuery;
43   - /* Columns in table to query (ID is included by default) */
44   - var $aColumns;
45   - /* Column types. Possibles are 1 = text, 2 = boolean, 3 = hyperlink url */
46   - var $aColumnTypes;
47   - /** header names to display */
48   - var $aColumnHeaderNames;
49   - /** table width, either in pixels or as a percentage e.g. 600 or 100%*/
50   - var $sWidth;
51   - /** link url used if a column type of 3 is specified */
52   - var $aLinkURLs;
53   - /** database column values to append to link url if a column type of 3 is specified */
54   - var $aDBQueryStringColumns;
55   - /** variables names to give $aDBQueryStringColumns on the query string */
56   - var $aQueryStringVariableNames;
57   - /** display the column headings or not */
58   - var $bDisplayColumnHeadings;
59   - /** table heading */
60   - var $sTableHeading;
61   - /** specify an image at the start of each row in the table */
62   - var $sImageURL;
63   - /** specify whether to use $sImageURL or to get the image url from the sql result set */
64   - var $bUseImageURLFromQuery = false;
65   - /** message that will be displayed if the table is empty*/
66   - var $sEmptyTableMessage;
67   - /** Picture paths */
68   - var $sChkPicPath = "widgets/checked.gif";
69   - var $sUnChkPicPath = "widgets/unchecked.gif";
70   - /** whether to force entries to wrap */
71   - var $bWordWrap = false;
72   -
73   - function PatternTableSqlQuery($sTmpQuery, $aTmpColumns, $aTmpColumnTypes, $aTmpColumnHeaderNames, $sTmpWidth, $aTmpLinkURLs = null, $aTmpDBQueryStringColumns = null, $aNewQueryStringVariableNames = null) {
74   - $this->sQuery = $sTmpQuery;
75   - $this->aColumns = & $aTmpColumns;
76   - $this->aColumnTypes = $aTmpColumnTypes;
77   - $this->aColumnHeaderNames = $aTmpColumnHeaderNames;
78   - $this->sWidth = $sTmpWidth;
79   - $this->bDisplayColumnHeadings = $bTmpDisplayColumnHeadings;
80   - $this->aLinkURLs = $aTmpLinkURLs;
81   - $this->aDBQueryStringColumns = $aTmpDBQueryStringColumns;
82   - $this->aQueryStringVariableNames = $aNewQueryStringVariableNames;
83   - }
84   -
85   - function setEmptyTableMessage($sNewValue) {
86   - $this->sEmptyTableMessage = $sNewValue;
87   - }
88   -
89   - function setTableHeading($sNewValue) {
90   - $this->sTableHeading = $sNewValue;
91   - }
92   -
93   - function setImageURL($sNewValue) {
94   - $this->sImageURL = $sNewValue;
95   - }
96   -
97   - function setUseImageURLFromQuery($bNewValue) {
98   - $this->bUseImageURLFromQuery = $bNewValue;
99   - }
100   -
101   - function setDisplayColumnHeadings($bNewValue) {
102   - $this->bDisplayColumnHeadings = $bNewValue;
103   - }
104   -
105   - function setIncludeBorder($bNewValue) {
106   - $this->bIncludeBorder = $bNewValue;
107   - }
108   -
109   - function setChkPicPath($sNewChkPicPath) {
110   - $this->sChkPicPath = $sNewChkPicPath;
111   - }
112   -
113   - function getChkPicPath() {
114   - return $this->sChkPicPath ;
115   - }
116   -
117   - function setWordWrap($bNewValue) {
118   - $this->bWordWrap = $bNewValue;
119   - }
120   -
121   - /**
122   - * Build the HTML string used to render the object
123   - *
124   - * @return String of HTML used to render object
125   - *
126   - * @todo possibly add in image size restraints for link types 2 and 3
127   - */
128   - function & render() {
  39 +
  40 +require_once('PatternTableGeneric.inc');
  41 +
  42 +class SqlResultSetAdapter {
  43 + // Has there been an error setting up the result set?
  44 + var $broken = false;
  45 +
  46 + function SqlResultSetAdapter ($oResultSet) {
129 47 global $default;
130   -
131   - $sToRender = "<table cellpadding=\"5\" border=\"" . ($this->bIncludeBorder ? "1" : "0") . "\" width=\"" . $this->sWidth . "\">\n";
132   - if (isset($this->sTableHeading)) {
133   - $sToRender .= "<caption align=\"top\" colspan=\"" . count($this->aColumns) . "\" align=\"left\"><b>$this->sTableHeading</b></caption>\n";
134   - }
135   - if ($this->bDisplayColumnHeadings) {
136   - for ($i = 0; $i < count($this->aColumnHeaderNames); $i++) {
137   - $sToRender .= "<th align=left>" . $this->aColumnHeaderNames[$i] . "</th>\n";
138   - }
139   - }
140   - $sql = $default->db;
141   - $sql->query($this->sQuery);
142   - if ($sql->num_rows() == 0) {
143   - $sToRender .= "<tr>\n";
144   - if (isset($this->sEmptyTableMessage)) {
145   - $sToRender .= "<td colspan=" . count($this->aColumns) . ">$this->sEmptyTableMessage</td>\n";
146   - } else {
147   - $sToRender .= "<td colspan=" . count($this->aColumns) . ">No " . (isset($this->sTableHeading) ? $this->sTableHeading : "") . " data</td>\n";
148   - }
149   - $sToRender .= "</tr>\n";
150   - } else {
151   - $iColour = 0;
152   - while ($sql->next_record()) {
153   - $sToRender .= "<tr bgcolor=\"" . getColour($iColour) . "\">\n";
154   - $iColour++;
155   - for ($i = 0; $i < count($this->aColumns); $i++) {
156   - switch ($this->aColumnTypes[$i]) {
157   - case 1:
158   - //text
159   - $sToRender .= "<td>";
160   - if (isset($this->sImageURL)) {
161   - $sToRender .= $this->generateImageURL($this->sImageURL);
162   - } else if ($this->bUseImageURLFromQuery) {
163   - $sToRender .= $this->generateImageURL($sql->f("image_url"));
164   - }
165   - if ($sql->f($this->aColumns[$i]) != null) {
166   - if ($this->bWordWrap) {
167   - $sToRender .= wordwrap($sql->f($this->aColumns[$i]), 25, " ", 1) . "</td>";
168   - } else {
169   - $sToRender .= $sql->f($this->aColumns[$i]) . "</td>";
170   - }
171   - } else {
172   - $sToRender .= "&nbsp;</td>";
173   - }
174   - break;
175   - case 2:
176   - //boolean
177   - $sToRender .= "<td>";
178   - if ($sql->f($this->aColumns[$i]) != null) {
179   - $value = $sql->f($this->aColumns[$i]);
180   - if ($value) {
181   - $sToRender .= "<img src=\"$default->graphicsUrl/" . $this->sChkPicPath . "\">";
182   - } else {
183   - $sToRender .= "<img src=\"$default->graphicsUrl/" . $this->sUnChkPicPath . "\">";
184   - }
185   - $sToRender .= "&nbsp;</td>";
186   - } else {
187   - $sToRender .= "&nbsp;</td>";
188   - }
  48 + $default->log->info('SqlResultSetAdapter called');
  49 + $this->oResultSet = $oResultSet;
  50 + if (PEAR::isError($oResultSet)) {
  51 + $this->broken = true;
  52 + }
  53 + }
189 54  
190   - break;
191   - case 3:
192   - //hyperlink
193   - $sToRender .= "<td><a href=\"";
194   - $sLink = $this->aLinkURLs[$i];
195   - for ($j = 0; $j < count($this->aDBQueryStringColumns); $j++) {
196   - if (strpos($sLink, "?") === false) {
197   - $sLink .= "?" . $this->aQueryStringVariableNames[$j] . "=" . $sql->f($this->aDBQueryStringColumns[$j]);
198   - } else {
199   - $sLink .= "&" . $this->aQueryStringVariableNames[$j] . "=" . $sql->f($this->aDBQueryStringColumns[$j]);
200   - }
201   - }
202   - $sToRender .= $sLink . "\">";
203   -
204   - if (isset($this->sImageURL)) {
205   - $sToRender .= $this->generateImageURL($this->sImageURL);
206   - } else if ($this->bUseImageURLFromQuery) {
207   - $sToRender .= $this->generateImageURL($sql->f("image_url"));
208   - }
209   - $sToRender .= $sql->f($this->aColumns[$i]) . "</a></td>\n";
210   - break;
211   - default:
212   - break;
213   - }
214   - }
215   - $sToRender .= "</tr>\n";
216   - }
217   - }
218   - $sToRender .= "</table>";
219   - return $sToRender;
220   - }
221   -
222   -
223   - function generateImageURL($sURL) {
224   - return "<img src=\"" . $sURL . "\" border=\"0\"/>";
  55 + function isEmpty () {
  56 + global $default;
  57 + $default->log->info('Checked if SqlResultSetAdapter was empty');
  58 + if ($this->broken) {
  59 + return $this->oResultSet;
  60 + }
  61 + return ($this->oResultSet->numRows() == 0);
  62 + }
  63 +
  64 + function next () {
  65 + global $default;
  66 + $default->log->info('Got next result from SqlResultSetAdapter');
  67 + if ($this->broken) {
  68 + return $this->oResultSet;
  69 + }
  70 + $aNextResult = $this->oResultSet->fetchRow(DB_FETCHMODE_ASSOC);
  71 + if ($aNextResult === null) {
  72 + return null;
  73 + }
  74 + return new SqlResultAdapter ($aNextResult);
  75 + }
  76 +}
  77 +
  78 +class SqlResultAdapter {
  79 + function SqlResultAdapter ($oResult) {
  80 + $this->oResult = $oResult;
  81 + }
  82 + function get ($sField) {
  83 + global $default;
  84 + $default->log->info('Getting ' . $sField . ' field from SqlResultAdapter');
  85 + return $this->oResult[$sField];
  86 + }
  87 +}
  88 +
  89 +class PatternTableSqlQuery extends PatternTableGeneric {
  90 + function PatternTableSqlQuery($sTmpQuery, $aTmpColumns, $aTmpColumnTypes, $aTmpColumnHeaderNames, $sTmpWidth, $aTmpLinkURLs = null, $aTmpDBQueryStringColumns = null, $aNewQueryStringVariableNames = null) {
  91 + $oResult = DBUtil::runQuery($sTmpQuery);
  92 + $oResultSet =& new SqlResultSetAdapter ($oResult);
  93 + $this->PatternTableGeneric($oResultSet, $aTmpColumns, $aTmpColumnTypes, $aTmpColumnHeaderNames, $sTmpWidth, $aTmpLinkURLs, $aTmpDBQueryStringColumns, $aNewQueryStringVariableNames);
225 94 }
226 95 }
227   -?>
228 96 \ No newline at end of file
  97 +
  98 +?>
... ...