Commit 36a34b2f757e662be91c18bb35e0c96eb94bd963

Authored by nbm
1 parent 9ca33aba

PatternTableGeneric is a generic form of PatternTableSqlQuery, and takes

a ResultSet from which it expects Results that handle 'get' method
queries, so that we can also pass in standard arrays with dictionaries
inside (suitably adapterised).


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@3046 c91229c3-7414-0410-bfa2-8a42b809f60b
lib/visualpatterns/PatternTableGeneric.inc 0 → 100644
  1 +<?php
  2 +/**
  3 + * $Id$
  4 + * $BasedOnId: PatternTableSqlQuery.inc,v 1.39 2003/09/09 09:20:36 michael Exp $
  5 + *
  6 + * Renders an iterable result set in a table.
  7 + *
  8 + * The first column in the table can be rendered as a link
  9 + * to the document/folder using the $iLinkType variable to specify the link type,
  10 + * the $sLinkPageURL to specify the page URL to link to and $sLinkImageURL to specify
  11 + * the image to display in the case of either a $iLinkType of 2 (image only) or 3 (image + text)
  12 + *
  13 + * If you wish to include images, there are two ways to do this
  14 + * o set the image url - this means that all rows will use the same image
  15 + * o set $bUseImageURLFromQuery to true - this will look for a column entitled image_url in
  16 + * the sql result set, allowing you to specify different images for each entry
  17 + *
  18 + * Copyright (c) 2004 Jam Warehouse http://www.jamwarehouse.com
  19 + *
  20 + * This program is free software; you can redistribute it and/or modify
  21 + * it under the terms of the GNU General Public License as published by
  22 + * the Free Software Foundation; either version 2 of the License, or
  23 + * (at your option) any later version.
  24 + *
  25 + * This program is distributed in the hope that it will be useful,
  26 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28 + * GNU General Public License for more details.
  29 + *
  30 + * You should have received a copy of the GNU General Public License
  31 + * along with this program; if not, write to the Free Software
  32 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  33 + *
  34 + * @version $Revision$
  35 + * @author Neil Blakey-Milner, Jam Warehouse (Pty) Ltd, South Africa
  36 + * @package lib.visualpatterns
  37 + * @todo $iLinkImageURL is hard coded - change
  38 + * @todo $sLinkPageURL is hard coded - change
  39 + */
  40 +class PatternTableGeneric {
  41 + /** query to execute*/
  42 + var $oResultSet;
  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 + /** whether the table cannot be edited */
  73 + var $bDisabled = false;
  74 +
  75 + function PatternTableGeneric($oResultSet, $aTmpColumns, $aTmpColumnTypes, $aTmpColumnHeaderNames, $sTmpWidth, $aTmpLinkURLs = null, $aTmpDBQueryStringColumns = null, $aNewQueryStringVariableNames = null) {
  76 + $this->oResultSet =& $oResultSet;
  77 + $this->aColumns = & $aTmpColumns;
  78 + $this->aColumnTypes = $aTmpColumnTypes;
  79 + $this->aColumnHeaderNames = $aTmpColumnHeaderNames;
  80 + $this->sWidth = $sTmpWidth;
  81 + $this->bDisplayColumnHeadings = $bTmpDisplayColumnHeadings;
  82 + $this->aLinkURLs = $aTmpLinkURLs;
  83 + $this->aDBQueryStringColumns = $aTmpDBQueryStringColumns;
  84 + $this->aQueryStringVariableNames = $aNewQueryStringVariableNames;
  85 + }
  86 +
  87 + function setEmptyTableMessage($sNewValue) { $this->sEmptyTableMessage = $sNewValue; }
  88 + function setTableHeading($sNewValue) { $this->sTableHeading = $sNewValue; }
  89 + function setImageURL($sNewValue) { $this->sImageURL = $sNewValue; }
  90 + function setUseImageURLFromQuery($bNewValue) { $this->bUseImageURLFromQuery = $bNewValue; }
  91 + function setDisplayColumnHeadings($bNewValue) { $this->bDisplayColumnHeadings = $bNewValue; }
  92 + function setIncludeBorder($bNewValue) { $this->bIncludeBorder = $bNewValue; }
  93 + function setChkPicPath($sNewChkPicPath) { $this->sChkPicPath = $sNewChkPicPath; }
  94 + function getChkPicPath() { return $this->sChkPicPath ; }
  95 + function setWordWrap($bNewValue) { $this->bWordWrap = $bNewValue; }
  96 + function setDisabled($bNewValue) { $this->bDisabled = $bNewValue; }
  97 +
  98 + /**
  99 + * Build the HTML string used to render the object
  100 + *
  101 + * @return String of HTML used to render object
  102 + *
  103 + * @todo possibly add in image size restraints for link types 2 and 3
  104 + */
  105 + function & render() {
  106 + global $default;
  107 +
  108 + $sToRender = "<table cellpadding=\"5\" border=\"" . ($this->bIncludeBorder ? "1" : "0") . "\" width=\"" . $this->sWidth . "\">\n";
  109 + if (isset($this->sTableHeading)) {
  110 + $sToRender .= "<caption align=\"top\" colspan=\"" . count($this->aColumns) . "\" align=\"left\"><b>$this->sTableHeading</b></caption>\n";
  111 + }
  112 + if ($this->bDisplayColumnHeadings) {
  113 + for ($i = 0; $i < count($this->aColumnHeaderNames); $i++) {
  114 + $sToRender .= "<th align=left>" . $this->aColumnHeaderNames[$i] . "</th>\n";
  115 + }
  116 + }
  117 + # $sql->query($this->sQuery);
  118 + $oResultSet =& $this->oResultSet;
  119 + if ($oResultSet->isEmpty()) {
  120 + $sToRender .= "<tr>\n";
  121 + if (isset($this->sEmptyTableMessage)) {
  122 + $sToRender .= "<td colspan=" . count($this->aColumns) . ">$this->sEmptyTableMessage</td>\n";
  123 + } else {
  124 + $sToRender .= "<td colspan=" . count($this->aColumns) . ">No " . (isset($this->sTableHeading) ? $this->sTableHeading : "") . " data</td>\n";
  125 + }
  126 + $sToRender .= "</tr>\n";
  127 + } else {
  128 + $iColour = 0;
  129 + while ($oResult = $oResultSet->next()) {
  130 + $sToRender .= "<tr bgcolor=\"" . getColour($iColour) . "\">\n";
  131 + $iColour++;
  132 + for ($i = 0; $i < count($this->aColumns); $i++) {
  133 + switch ($this->aColumnTypes[$i]) {
  134 + case 1:
  135 + //text
  136 + $sToRender .= "<td>";
  137 + if (isset($this->sImageURL)) {
  138 + $sToRender .= $this->generateImageURL($this->sImageURL);
  139 + } else if ($this->bUseImageURLFromQuery) {
  140 + $sToRender .= $this->generateImageURL($oResult->get("image_url"));
  141 + }
  142 + if ($oResult->get($this->aColumns[$i]) != null) {
  143 + if ($this->bWordWrap) {
  144 + $sToRender .= wordwrap($oResult->get($this->aColumns[$i]), 25, " ", 1) . "</td>";
  145 + } else {
  146 + $sToRender .= $oResult->get($this->aColumns[$i]) . "</td>";
  147 + }
  148 + } else {
  149 + $sToRender .= "&nbsp;</td>";
  150 + }
  151 + break;
  152 + case 2:
  153 + //boolean
  154 + $sToRender .= "<td>";
  155 + if ($oResult->get($this->aColumns[$i]) != null) {
  156 + $value = $oResult->get($this->aColumns[$i]);
  157 + if ($value) {
  158 + $sToRender .= "<img src=\"$default->graphicsUrl/" . $this->sChkPicPath . "\">";
  159 + } else {
  160 + $sToRender .= "<img src=\"$default->graphicsUrl/" . $this->sUnChkPicPath . "\">";
  161 + }
  162 + $sToRender .= "&nbsp;</td>";
  163 + } else {
  164 + $sToRender .= "&nbsp;</td>";
  165 + }
  166 +
  167 + break;
  168 + case 3:
  169 + if ($this->bDisabled === true) {
  170 + $sToRender .= '<td><span style="background-color: #CCCCCC; color: #222222;">';
  171 + $sToRender .= $oResult->get($this->aColumns[$i]);
  172 + $sToRender .= '</span></td>';
  173 + $sToRender .= "\n";
  174 + break;
  175 + }
  176 + //hyperlink
  177 + $sToRender .= "<td><a href=\"";
  178 + $sLink = $this->aLinkURLs[$i];
  179 + for ($j = 0; $j < count($this->aDBQueryStringColumns); $j++) {
  180 + if (strpos($sLink, "?") === false) {
  181 + $sLink .= "?" . $this->aQueryStringVariableNames[$j] . "=" . $oResult->get($this->aDBQueryStringColumns[$j]);
  182 + } else {
  183 + $sLink .= "&" . $this->aQueryStringVariableNames[$j] . "=" . $oResult->get($this->aDBQueryStringColumns[$j]);
  184 + }
  185 + }
  186 + $sToRender .= $sLink . "\">";
  187 +
  188 + if (isset($this->sImageURL)) {
  189 + $sToRender .= $this->generateImageURL($this->sImageURL);
  190 + } else if ($this->bUseImageURLFromQuery) {
  191 + $sToRender .= $this->generateImageURL($oResult->get("image_url"));
  192 + }
  193 + $sToRender .= $oResult->get($this->aColumns[$i]) . "</a></td>\n";
  194 + break;
  195 + default:
  196 + break;
  197 + }
  198 + }
  199 + $sToRender .= "</tr>\n";
  200 + }
  201 + }
  202 + $sToRender .= "</table>";
  203 + return $sToRender;
  204 + }
  205 +
  206 + function generateImageURL($sURL) {
  207 + return "<img src=\"" . $sURL . "\" border=\"0\"/>";
  208 + }
  209 +}
  210 +?>
... ...