_id: holds the primary key of the entry in the table (-1 = no entry in table i.e. create not update) * o _tn: holds the tablename for which the primary key is valid * o _type: holds the type of entry (text/boolean/list) - for parsing purposes * * The actual value is held in a form field name * * Copyright (c) 2003 Jam Warehouse http://www.jamwarehouse.com * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * @version $Revision$ * @author Rob Cherry, Jam Warehouse (Pty) Ltd, South Africa * @package lib.visualpatterns * @todo - add client side validation stuff * @todo - add column type 3 -> select field stuff */ 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, 4 = meta data lookup */ var $aColumnDisplayTypes; /** 0 = id, 1 = text, 2 = boolean*/ var $aColumnDatabaseTypes; /** Unique name */ var $sUniqueName; /** message that will be displayed if the table is empty*/ var $sEmptyTableMessage; /** whether or not to include javascript validation rendering automatically */ var $bRenderJavascriptValidation = true; /** temporary array holding the required fields */ var $aRequiredVariables; var $aRequiredColumnNames; var $aRequiredFieldNames; var $aMetaDataFields; 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; $this->aColumnDatabaseTypes = $aTmpColumnDatabaseTypes; } function setUniqueName($sNewValue) { $this->sUniqueName = $sNewValue; } function setMetaDataFields($aNewValue) { $this->aMetaDataFields = $aNewValue; } function setDisplayColumnHeadings($bNewValue) { $this->bDisplayColumnHeadings = $bNewValue; } function setTableCaption($sNewValue) { $this->sTableCaption = $sNewValue; } function setColumnHeaderNames($aNewValue) { $this->aColumnHeaderNames = $aNewValue; } /*function getRequiredVariableNames() { return $this->aRequiredVariableNames; }*/ function setRequiredColumnNames($aNewValue) { $this->aRequiredColumnNames = $aNewValue; } function setEmptyTableMessage($sNewValue) { $this->sEmptyTableMessage = $sNewValue; } function setRenderJavascriptValidation($bNewValue) { $this->bRenderJavascriptValidation = $bNewValue; } function & render() { global $default; $sToRender = "\n"; if (isset($this->sTableCaption)) { $sToRender .= "\n"; } if ($this->bDisplayColumnHeadings) { for ($i = 0; $i < count($this->aColumnHeaderNames); $i++) { $sToRender .= "\n"; } } $this->sUniqueName .= KTUtil::randomString(); $sql = $default->db; $sql->query($this->sQuery); if ($sql->num_rows() == 0) { $sToRender .= "\n"; if (isset($this->sEmptyTableMessage)) { $sToRender .= "\n"; } else { $sToRender .= "\n"; } $sToRender .= "\n"; } else { $this->aRequiredVariables = array(); $iRowCount = 0; while ($sql->next_record()) { $sToRender .= "\n"; $_SESSION["pelfq_" . $this->sUniqueName . $iRowCount . "_id"] = $sql->f("id"); $_SESSION["pelfq_" . $this->sUniqueName . $iRowCount . "_tn"] = $this->sTableName; $_SESSION["pelfq_" . $this->sUniqueName . $iRowCount . "_columns"] = $this->aStoreColumnNames; $sToRender .= "sUniqueName . $iRowCount . "\" value=\"\" />\n"; // $sToRender .= "sUniqueName . "_" . $iRowCount . "\" value=\"" . $sql->f("id") . "\" />\n"; // $sToRender .= "sUniqueName . "_" . $iRowCount . "_tn\" value=\"" . $this->sTableName . "\" />\n"; // check for is_mandatory field validation if ($sql->f("is_mandatory") == "1") { $this->aRequiredVariables[]["formName"] = $this->sUniqueName . "_" . $iRowCount . (count($this->aStoreColumnNames)-1) . "_value"; } for ($i = 0; $i < count($this->aStoreColumnNames); $i++) { if (isset($this->aRequiredColumnNames)) { for ($k = 0; $k < count($this->aRequiredColumnNames); $k++) { if (strcmp($this->aStoreColumnNames[$i], $this->aRequiredColumnNames[$k]) == 0) { $this->aRequiredVariables[count($this->aRequiredVariables)]["formName"] = $this->sUniqueName . "_" . $iRowCount . $i . "_value"; } } } //generate the right kind of editable field $sToRender .= "\t\n"; } $sToRender .= "sUniqueName . $iRowCount . "\" value=\"\" />\n"; $sToRender .= "\n"; $iRowCount++; } } $sToRender .= "
aColumnsSql) . "\" align=\"left\">$this->sTableCaption
" . $this->aColumnHeaderNames[$i] . "
$this->sEmptyTableMessageNo " . (isset($this->sTableCaption) ? $this->sTableCaption : "") . " data
\n"; switch ($this->aColumnDisplayTypes[$i]) { case 1: //output the table column name $sToRender .= "\tsUniqueName . "_" . $iRowCount . $i . "_tc\" value=\"" . $this->aStoreColumnNames[$i] . "\" />\n"; //output the column type $sToRender .= "\tsUniqueName . "_" . $iRowCount . $i . "_type\" value=\"" . $this->aColumnDatabaseTypes[$i] . "\" />\n"; //output the value if ($this->aColumnsVisible[$i]) { if ($this->aColumnsEditable[$i]) { $sToRender .= "\tsUniqueName . "_" . $iRowCount . $i . "_value\" value=\"" . $sql->f($this->aStoreColumnNames[$i]) . "\" />\n"; } else { $sToRender .= "\tsUniqueName . "_" . $iRowCount . $i . "_value\" value=\"" . $sql->f($this->aStoreColumnNames[$i]) . "\" />" . $sql->f($this->aDisplayColumnNames[$i])."\n"; } } else { $sToRender .= "\tsUniqueName . "_" . $iRowCount . $i . "_value\" value=\"" . $sql->f($this->aStoreColumnNames[$i]) . "\" />\n"; } break; case 2: //output the table column name $sToRender .= "\tsUniqueName . "_" . $iRowCount . $i . "_tc\" value=\"" . $this->aStoreColumnNames[$i] . "\" />\n"; //output the column type $sToRender .= "\tsUniqueName . "_" . $iRowCount . $i . "_type\" value=\"" . $this->aColumnDatabaseTypes[$i] . "\" />\n"; //output the value if ($this->aColumnsVisible[$i]) { if ($this->aColumnsEditable[$i]) { $sToRender .= "\tsUniqueName . "_" . $iRowCount . $i . "_value\" value=\"" . ($sql->f($this->aStoreColumnNames[$i] ? "1\" checked" : "0\"")) . " />\n"; } else { $sToRender .= "\tsUniqueName . "_" . $iRowCount . $i . "_value\" value=\"" . $sql->f($this->aStoreColumnNames[$i]) . "\" />".$sql->f($this->aDisplayColumnNames[$i])."\n"; } } else { $sToRender .= "\tsUniqueName . "_" . $iRowCount . $i . "_value\" value=\"" . $sql->f($this->aStoreColumnNames[$i]) . "\" />\n"; } break; case 3: $sToRender .= "The column type for drop downs is not implemented yet\n"; break; case 4: //meta data //could either be a drop down or a text field, depending //output the table column name $sToRender .= "\tsUniqueName . "_" . $iRowCount . $i . "_tc\" value=\"" . $this->aStoreColumnNames[$i] . "\" />\n"; //output the column type $sToRender .= "\tsUniqueName . "_" . $iRowCount . $i . "_type\" value=\"" . $this->aColumnDatabaseTypes[$i] . "\" />\n"; if ($this->aColumnsVisible[$i]) { if ($this->aColumnsEditable[$i]) { $oPattern = & new PatternMetaData($sql->f($this->aMetaDataFields[$i]), $this->sUniqueName . "_" . $iRowCount . $i . "_value", $sql->f($this->aStoreColumnNames[$i])); $sToRender .= $oPattern->render(); } else { $sToRender .= "\tsUniqueName . "_" . $iRowCount . $i . "_value\" value=\"" . $sql->f($this->aStoreColumnNames[$i]) . "\" />".$sql->f($this->aDisplayColumnNames[$i])."\n"; } } else { $sToRender .= "\tsUniqueName . "_" . $iRowCount . $i . "_value\" value=\"" . $sql->f($this->aStoreColumnNames[$i]) . "\" />\n"; } break; break; default: $sToRender .= "You are attempting to render an unknown column type " . ($this->aColumnTypes[$i]) . " in PatternEditableTableSqlQuery\n"; break; } $sToRender .= "\t
"; if (isset($this->aRequiredVariables)) { if ($this->bRenderJavascriptValidation) { $sToRender .= $this->generateRequiredFieldValidation($this->aRequiredVariables); } } return $sToRender; } function generateRequiredFieldValidation($aValidationColumnNames) { $sToRender .= "\n\n\n\n"; return $sToRender; } function getValidationJavascript() { for ($i = 0; $i < count($this->aRequiredVariables); $i++) { $sToRender .= "\tif (!(validRequired(document.MainForm." . $this->aRequiredVariables[$i]["formName"] . ", 'selected'))) {\n"; $sToRender .= "\t\treturn false;\n\t}\n"; } return $sToRender; } } ?>