Commit 0234fe3f43da65ba07043ee8637c4b0b1e99e737

Authored by michael
1 parent c9582c85

(#2674) decoupled pattern POST array handling


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@2415 c91229c3-7414-0410-bfa2-8a42b809f60b
presentation/lookAndFeel/knowledgeTree/store.inc 0 → 100644
  1 +<?php
  2 +/**
  3 + * $Id$
  4 + *
  5 + * Contains the logic for constructing queries from Pattern generated forms
  6 + *
  7 + * This program is free software; you can redistribute it and/or modify
  8 + * it under the terms of the GNU General Public License as published by
  9 + * the Free Software Foundation; either version 2 of the License, or
  10 + * (at your option) any later version.
  11 + *
  12 + * This program is distributed in the hope that it will be useful,
  13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15 + * GNU General Public License for more details.
  16 + *
  17 + * You should have received a copy of the GNU General Public License
  18 + * along with this program; if not, write to the Free Software
  19 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20 + *
  21 + * Copyright (c) 2003 Jam Warehouse - http://www.jamwarehouse.com
  22 + */
  23 +
  24 +function constructQuery($aKeys, $aSuppliedValues = null) {
  25 + global $default;
  26 +
  27 + $aQuery = array();
  28 + for ($i = 0; $i < count($aKeys); $i++) {
  29 + $sRowStart = $aKeys[$i];
  30 + $pos = strncasecmp("unique_start", $sRowStart, 12);
  31 +
  32 + if ($pos == 0) {
  33 + $aColumns = array();
  34 + $aValues = array();
  35 + $aTypes = array();
  36 +
  37 + $iPrimaryKey = $_POST[$aKeys[++$i]];
  38 + $sTableName = $_POST[$aKeys[++$i]];
  39 + $default->log->info("primaryKey=$iPrimaryKey; tableName=$sTableName");
  40 +
  41 + $i++;
  42 + $iColumnCount = 0;
  43 +
  44 + //get all the values for the table
  45 + while ((strncasecmp("unique_end", $sRowStart, 10) != 0) && ($i <= count($aKeys))) {
  46 + $aColumns[$iColumnCount] = $_POST[$aKeys[$i]];
  47 + $aTypes[$iColumnCount]= $_POST[$aKeys[++$i]];
  48 +
  49 + switch ($aTypes[$iColumnCount]) {
  50 + case 0:
  51 + //id's
  52 + $aValues[$iColumnCount] = $_POST[$aKeys[++$i]];
  53 + break;
  54 + case 1:
  55 + //normal text
  56 + $aValues[$iColumnCount] = $_POST[$aKeys[++$i]];
  57 + break;
  58 + case 2:
  59 + //uncheck checkboxes don't generate any name/value pairs
  60 + //so if the next key doesn't contain the word "value" and it's type
  61 + //is checkbox, then we have an unchecked check box
  62 + if (strpos($aKeys[$i + 1], "value") === false) {
  63 + $aValues[$iColumnCount] = false;
  64 + } else {
  65 + $i++;
  66 + $aValues[$iColumnCount] = true;
  67 + }
  68 + //check box
  69 + break;
  70 + case 3:
  71 + //drop down
  72 + case 4:
  73 + // user supplied values
  74 + $aValues[$iColumnCount] = $_POST[$aKeys[++$i]];
  75 + break;
  76 + }
  77 +
  78 + //$aValues[$iColumnCount] = $_POST[$aKeys[$i]];
  79 + $sRowStart = $aKeys[++$i];
  80 + $iColumnCount++;
  81 + }
  82 + $default->log->info("columns: " . arrayToString($aColumns));
  83 + if ($iPrimaryKey < 0) {
  84 + //perform an insert
  85 + $sQuery = "INSERT INTO $sTableName (";
  86 + for ($j = 0; $j < count($aColumns) - 1; $j++) {
  87 + $sQuery .= $aColumns[$j] . ", ";
  88 + }
  89 + $sQuery .= $aColumns[count($aColumns) -1] . ") VALUES (";
  90 +
  91 + for ($j = 0; $j < count($aColumns) - 1; $j++) {
  92 + switch ($aTypes[$j]) {
  93 + case 0 :
  94 + $sQuery .= $aValues[$j] . ", ";
  95 + break;
  96 + case 1:
  97 + //text
  98 + $sQuery .= "'" . addslashes($aValues[$j]) . "', ";
  99 + break;
  100 + case 2:
  101 + //boolean
  102 + $sQuery .= $aValues[$j] . ", ";
  103 + break;
  104 + case 3:
  105 + //drop down list
  106 + $sQuery .= $aValues[$j] . ", ";
  107 + break;
  108 + case 4:
  109 + // user supplied values
  110 + $default->log->info("supplied values: aColumns[j]=" . $aColumns[$j] . "suppliedValues=" . $aSuppliedValues[$aColumns[$j]]);
  111 + $sQuery .= $aSuppliedValues[$aColumns[$j]] . ", ";
  112 + break;
  113 + default:
  114 + break;
  115 + }
  116 + }
  117 + switch ($aTypes[count($aColumns) - 1]) {
  118 + case 0:
  119 + //id
  120 + $sQuery .= $aValues[count($aColumns) - 1] . ") ";
  121 + break;
  122 + case 1:
  123 + //text
  124 + $sQuery .= "'" . addslashes($aValues[count($aColumns) - 1]) . "') ";
  125 + break;
  126 + case 2:
  127 + //boolean
  128 + $sQuery .= ($aValues[count($aColumns) - 1] ? 1 : 0) . ") ";
  129 + break;
  130 + case 3:
  131 + //drop down list
  132 + $sQuery .= $aValues[count($aColumns) - 1] . ") ";
  133 + break;
  134 + case 4:
  135 + // user supplied values
  136 + $default->log->info("supplied values: suppliedValues=" . $aSuppliedValues[$aColumns[count($aColumns) - 1]]);
  137 + $sQuery .= $aSuppliedValues[$aColumns[count($aColumns) - 1]] . ", ";
  138 + break;
  139 + default:
  140 + break;
  141 + }
  142 + // add query to array
  143 + $aQuery[] = $sQuery;
  144 + } else {
  145 + //perform an update
  146 + $sQuery = "UPDATE $sTableName SET ";
  147 + for ($j = 0; $j < count($aColumns) -1; $j++) {
  148 + $sQuery .= $aColumns[$j] . " = ";
  149 + switch ($aTypes[$j]) {
  150 + case 0:
  151 + //id
  152 + $sQuery .= $aValues[$j] . ", ";
  153 + break;
  154 + case 1:
  155 + $sQuery .= "'" . addslashes($aValues[$j]) . "', ";
  156 + break;
  157 + case 2:
  158 + $sQuery .= ($aValues[$j] ? 1 : 0) . ", ";
  159 + break;
  160 + case 3:
  161 + $sQuery .= $aValues[$j] . ", ";
  162 + break;
  163 + default:
  164 + break;
  165 + }
  166 +
  167 + }
  168 + $sQuery .= $aColumns[count($aTypes) -1] . " = ";
  169 + switch ($aTypes[count($aTypes) -1]) {
  170 + case 0:
  171 + //id
  172 + $sQuery .= $aValues[count($aTypes) -1] . " ";
  173 + break;
  174 + case 1:
  175 + $sQuery .= "'" . addslashes($aValues[count($aTypes) -1]) . "' ";
  176 + break;
  177 + case 2:
  178 + $sQuery .= ($aValues[count($aTypes) -1] ? 1 : 0) . " ";
  179 + break;
  180 + case 3:
  181 + $sQuery .= $aValues[count($aTypes) -1] . " ";
  182 + break;
  183 + default:
  184 + break;
  185 + }
  186 + $sQuery .= "WHERE id = $iPrimaryKey";
  187 + // add query to array
  188 + $aQuery[] = $sQuery;
  189 + }
  190 + }
  191 + }
  192 + return $aQuery;
  193 +}
  194 +?>
0 \ No newline at end of file 195 \ No newline at end of file
presentation/lookAndFeel/knowledgeTree/store.php
1 <?php 1 <?php
2 /** 2 /**
3 -* Page used by all editable patterns to actually perform the db insert/updates  
4 -*  
5 -* Expected form variables  
6 -* o fReturnURL  
7 -*  
8 -* @author Rob Cherry, Jam Warehouse (Pty) Ltd, South Africa  
9 -* @date 27 January 2003  
10 -* @package presentation.lookAndFeel.knowledgeTree.documentmanagement  
11 -*/ 3 + * $Id$
  4 + *
  5 + * Page used by all editable patterns to actually perform the db insert/updates
  6 + *
  7 + * Expected form variables
  8 + * o fReturnURL
  9 + *
  10 + * Licensed under the GNU GPL. For full terms see the file COPYING.
  11 + * Copyright (c) 2003 Jam Warehouse - http://www.jamwarehouse.com
  12 +
  13 + * @author Rob Cherry, Jam Warehouse (Pty) Ltd, South Africa
  14 + * @package presentation.lookAndFeel.knowledgeTree.documentmanagement
  15 + */
  16 +
12 require_once("../../../config/dmsDefaults.php"); 17 require_once("../../../config/dmsDefaults.php");
13 require_once("$default->fileSystemRoot/lib/documentmanagement/Document.inc"); 18 require_once("$default->fileSystemRoot/lib/documentmanagement/Document.inc");
14 require_once("$default->fileSystemRoot/lib/foldermanagement/Folder.inc"); 19 require_once("$default->fileSystemRoot/lib/foldermanagement/Folder.inc");
  20 +require_once("store.inc");
15 21
16 -$aKeys = array_keys($_POST);  
17 -$iPrimaryKey;  
18 22
19 -for ($i = 0; $i < count($aKeys); $i++) {  
20 - $sRowStart = $aKeys[$i];  
21 - $pos = strncasecmp("unique_start", $sRowStart, 12); 23 +if (count($_POST) > 0) {
  24 + $aKeys = array_keys($_POST);
  25 + $aQueries = constructQuery($aKeys);
22 26
23 - if ($pos == 0) {  
24 - $aColumns;  
25 - $aValues;  
26 - $aTypes;  
27 - settype($aColumns, "array");  
28 - settype($aValues, "array");  
29 - settype($aTypes, "array");  
30 -  
31 - $i++;  
32 - $iPrimaryKey = $_POST[$aKeys[$i]];  
33 -  
34 - $i++;  
35 - $sTableName = $_POST[$aKeys[$i]];  
36 -  
37 -  
38 - $i++;  
39 - $iColumnCount = 0;  
40 -  
41 - //get all the values for the table  
42 - while ((strncasecmp("unique_end", $sRowStart, 10) != 0) && ($i <= count($aKeys))) {  
43 - $aColumns[$iColumnCount] = $_POST[$aKeys[$i]];  
44 - $i++;  
45 - $aTypes[$iColumnCount]= $_POST[$aKeys[$i]];  
46 -  
47 -  
48 - switch ($aTypes[$iColumnCount]) {  
49 - case 0:  
50 - //id's  
51 - $i++;  
52 - $aValues[$iColumnCount] = $_POST[$aKeys[$i]];  
53 - break;  
54 - case 1:  
55 - //normal text  
56 - $i++;  
57 - $aValues[$iColumnCount] = $_POST[$aKeys[$i]];  
58 - break;  
59 - case 2:  
60 - //uncheck checkboxes don't generate any name/value pairs  
61 - //so if the next key doesn't contain the word "value" and it's type  
62 - //is checkbox, then we have an unchecked check box  
63 - if (strpos($aKeys[$i + 1], "value") === false) {  
64 - $aValues[$iColumnCount] = false;  
65 - } else {  
66 - $i++;  
67 - $aValues[$iColumnCount] = true;  
68 - }  
69 - //check box  
70 - break;  
71 - case 3:  
72 - //drop down  
73 - $i++;  
74 - $aValues[$iColumnCount] = $_POST[$aKeys[$i]];  
75 - break;  
76 - }  
77 -  
78 - //$aValues[$iColumnCount] = $_POST[$aKeys[$i]];  
79 - $i++;  
80 -  
81 - $sRowStart = $aKeys[$i];  
82 -  
83 - $iColumnCount++;  
84 - }  
85 -  
86 - if ($iPrimaryKey < 0) {  
87 - //perform an insert  
88 - $sQuery = "INSERT INTO $sTableName (";  
89 - for ($j = 0; $j < count($aColumns) - 1; $j++) {  
90 - $sQuery .= $aColumns[$j] . ", ";  
91 - }  
92 - $sQuery .= $aColumns[count($aColumns) -1] . ") VALUES (";  
93 -  
94 - for ($j = 0; $j < count($aColumns) - 1; $j++) {  
95 - switch ($aTypes[$j]) {  
96 - case 0 :  
97 - $sQuery .= $aValues[$j] . ", ";  
98 - break;  
99 - case 1:  
100 - //text  
101 - $sQuery .= "'" . addslashes($aValues[$j]) . "', ";  
102 - break;  
103 - case 2:  
104 - $sQuery .= $aValues[$j] . ", ";  
105 - //boolean  
106 - break;  
107 - case 3:  
108 - $sQuery .= $aValues[$j] . ", ";  
109 - //drop down list  
110 - break;  
111 - default:  
112 - break;  
113 - }  
114 - }  
115 - switch ($aTypes[count($aColumns) - 1]) {  
116 - case 0:  
117 - //id  
118 - $sQuery .= $aValues[count($aColumns) - 1] . ") ";  
119 - break;  
120 - case 1:  
121 - //text  
122 - $sQuery .= "'" . addslashes($aValues[count($aColumns) - 1]) . "') ";  
123 - break;  
124 - case 2:  
125 - //boolean  
126 - $sQuery .= ($aValues[count($aColumns) - 1] ? 1 : 0) . ") ";  
127 - break;  
128 - case 3:  
129 - //drop down list  
130 - $sQuery .= $aValues[count($aColumns) - 1] . ") ";  
131 - break;  
132 - default:  
133 - break;  
134 - }  
135 - //execute the query  
136 - $sql = $default->db;  
137 - $sql->query($sQuery);  
138 - $iPrimaryKey = $sql->insert_id();  
139 - } else {  
140 - //perform an update  
141 - $sQuery = "UPDATE $sTableName SET ";  
142 - for ($j = 0; $j < count($aColumns) -1; $j++) {  
143 - $sQuery .= $aColumns[$j] . " = ";  
144 - switch ($aTypes[$j]) {  
145 - case 0:  
146 - //id  
147 - $sQuery .= $aValues[$j] . ", ";  
148 - break;  
149 - case 1:  
150 - $sQuery .= "'" . addslashes($aValues[$j]) . "', ";  
151 - break;  
152 - case 2:  
153 - $sQuery .= ($aValues[$j] ? 1 : 0) . ", ";  
154 - break;  
155 - case 3:  
156 - $sQuery .= $aValues[$j] . ", ";  
157 - break;  
158 - default:  
159 - break;  
160 - }  
161 -  
162 - }  
163 - $sQuery .= $aColumns[count($aTypes) -1] . " = ";  
164 - switch ($aTypes[count($aTypes) -1]) {  
165 - case 0:  
166 - //id  
167 - $sQuery .= $aValues[count($aTypes) -1] . " ";  
168 - break;  
169 - case 1:  
170 - $sQuery .= "'" . addslashes($aValues[count($aTypes) -1]) . "' ";  
171 - break;  
172 - case 2:  
173 - $sQuery .= ($aValues[count($aTypes) -1] ? 1 : 0) . " ";  
174 - break;  
175 - case 3:  
176 - $sQuery .= $aValues[count($aTypes) -1] . " ";  
177 - break;  
178 - default:  
179 - break;  
180 - }  
181 - $sQuery .= "WHERE id = $iPrimaryKey";  
182 - //execute the query  
183 - $sql = $default->db;  
184 - $sql->query($sQuery);  
185 - } 27 + //execute the queries
  28 + for ($i=0; $i<count($aQueries); $i++) {
  29 + $default->log->info("query=" . $aQueries[$i]);
  30 + $sql = $default->db;
  31 + $sql->query($aQueries[$i]);
186 } 32 }
  33 + redirect(urldecode($fReturnURL));
187 } 34 }
188 -redirect(urldecode($fReturnURL));  
189 -  
190 -  
191 -  
192 -  
193 -?> 35 +?>
194 \ No newline at end of file 36 \ No newline at end of file