Source for file RSDEngineDBControllerFileUpdate.php

Documentation is available at RSDEngineDBControllerFileUpdate.php


1 <?php
2 // RSDEngine: The Rapid and Secure Development Engine
3 // Copyright (C) 2003 Lukas Feiler
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 /**Contains just the class RSDEngineDBControllerFileUpdate.
20 *
21 * @package RSDEngine
22 * @author Lukas Feiler <lukas.feiler@chello.at>
23 * @copyright Lukas Feiler 2003
24 * @filesource
25 */
26
27 /**RSDEngineDBControllerFileUpdate extends RSDEngineControllerFile.
28 */
29 require_once('RSDEngine/RSDEngineControllerFile.php');
30
31 /**Generates the controller file for inserting records into a table.
32 *
33 * @author Lukas Feiler <lukas.feiler@chello.at>
34 * @version 0.1.9
35 * @copyright Lukas Feiler 2003
36 * @package RSDEngine
37 */
38 class RSDEngineDBControllerFileUpdate extends RSDEngineControllerFile{
39
40 /**Whether to overwrite an existing file.
41 * @var boolean
42 */
43 var $overwriteExisting = false;
44
45 /**Returns the filename.
46 * @return String
47 */
48 function getFilename()
49 {
50 return "update" . ucfirst($this->config['table']->getTableNameWithoutPrefix()) . ".php";
51 }
52
53 /**Returns the filename of the corresponding template.
54 * @return String
55 */
56 function getTemplateFilename()
57 {
58 return "update" . ucfirst($this->config['table']->getTableNameWithoutPrefix()) . ".tpl";
59 }
60
61 /**Returns the filename of the controller that displays multiple records.
62 * @see RSDEngineDBControllerFileGet
63 * @return String
64 */
65 function getControllerGetFilename()
66 {
67 return "get" . ucfirst($this->config['table']->getTableNameWithoutPrefix()) . ".php";
68 }
69
70 /**Returns the code for the controller file for inserting records into a table.
71 *
72 * @access private
73 * @return String The code for the file.
74 */
75 function getCode()
76 {
77 extract($this->config);
78 $table =& $this->config['table'];
79 $authors = RSArrayUtil::toString(split("\n",$authors),"\n",'* @author %val');
80 $records = ucfirst($this->config['table']->getTableNameWithoutPrefix());
81 $tableInstanceName = $table->getChildClassInstanceName();
82 $appInstanceName = $this->relatedClasses['dbApplicationClass']->getInstanceName();
83
84 $public = $table->getFileOptionProperty('updateController', 'public');
85 $write = $table->getFileOptionProperty('updateController', 'write');
86
87 $this->setWrite($write);
88
89 $data = '';
90 $data .="<?php\n" .
91 '/' . '**This file is the controller for updating records in the table ' . $table->name . ".\n" .
92 '*' . "\n" .
93 $authors . "\n" .
94 '* @copyright ' . $copyright . "\n" .
95 '* @package ' . $projectName . "\n" .
96 '* @filesource' . "\n" .
97 '*/' . "\n" .
98 "\n";
99 if ($this->config['auth'] || $this->config['liveUser']) {
100 if ($public) {
101 $data .='/' . '**This controller requires no authentication.' . "\n" .
102 "*/\n" .
103 "require_once('model/" . $this->relatedFiles['dbApplicationPublicInitFile']->getFilename() . "');\n";
104 } else {
105 $data .='/' . '**This controller requires authentication.' . "\n" .
106 "*/\n" .
107 "require_once('model/" . $this->relatedFiles['dbApplicationPrivateInitFile']->getFilename() . "');\n";
108 }
109 } else {
110 $data .='/' . '**Initializes the application.' . "\n" .
111 "*/\n" .
112 "require_once('model/" . $this->relatedFiles['dbApplicationInitFile']->getFilename() . "');\n";
113 }
114
115 if (($primaryKeyColumn = $table->getPrimaryKeyColumn()) === false) {
116 $data .="/" . "*\n" .
117 "No primary key was defined!\n" .
118 "Without a primary key the RSDEngine cannot generate code to update a single record!\n" .
119 "*/";
120 return "$data?>";
121 }
122
123 $data .= "\n";
124
125 $data .="\$${tableInstanceName} =& \$${appInstanceName}->getTable('$table->name');\n" .
126 "\$error = null;\n";
127
128
129 $data .="\n" .
130 "//we make sure all required fields are initialized\n";
131 reset($table->columns);
132 while (list($key, $column) = each($table->columns)) {
133 $columnName = $column->getColumnNameForVariableName();
134 $isValidFileOptions = $column->getIsValidFileOptions();
135 if ((($column->defaultUpdateValue != "" && $isValidFileOptions == null) || $column->doNotUpdate) && !$column->isPrimaryKey) {
136 continue;
137 }
138 if ($column->inputType == 'checkbox') {
139 if ($column->inputTypeData != '') {
140 $inputTypeData = $column->inputTypeData;
141 } else {
142 $inputTypeData = "array('checked' => '1', 'unchecked' => '0')";
143 }
144 $data .="//if the checkbox was not checked\n" .
145 "\$inputTypeData = $inputTypeData;\n" .
146 "if (isset(\$_REQUEST['$columnName'])) {\n" .
147 " if (isset(\$inputTypeData['checked'])) {\n" .
148 " \$_REQUEST['$columnName'] = \$inputTypeData['checked'];\n" .
149 " } else {\n" .
150 " \$_REQUEST['$columnName'] = '1';\n" .
151 " }\n" .
152 "} else {\n" .
153 " if (isset(\$inputTypeData['unchecked'])) {\n" .
154 " \$_REQUEST['$columnName'] = \$inputTypeData['unchecked'];\n" .
155 " } else {\n" .
156 " \$_REQUEST['$columnName'] = '0';\n" .
157 " }\n" .
158 "}\n";
159 } elseif ($column->inputType == 'date') {
160 $data .="if (!isset(\$_REQUEST['${columnName}Year'])) {\n" .
161 " \$_REQUEST['${columnName}Year'] = '';\n" .
162 "}\n" .
163 "if (!isset(\$_REQUEST['${columnName}Month'])) {\n" .
164 " \$_REQUEST['${columnName}Month'] = '';\n" .
165 "}\n" .
166 "if (!isset(\$_REQUEST['${columnName}Day'])) {\n" .
167 " \$_REQUEST['${columnName}Day'] = '';\n" .
168 "}\n" .
169 "require_once('Date.php');\n" .
170 "\$d = new Date();\n" .
171 "\$d->setYear(\$_REQUEST['${columnName}Year']);\n" .
172 "\$d->setMonth(\$_REQUEST['${columnName}Month']);\n" .
173 "\$d->setDay(\$_REQUEST['${columnName}Day']);\n" .
174 "\$_REQUEST['$columnName'] = \$d->format('%Y-%m-%d');\n";
175 } elseif ($column->inputType == 'time') {
176 $data .="if (!isset(\$_REQUEST['${columnName}Hour'])) {\n" .
177 " \$_REQUEST['${columnName}Hour'] = '';\n" .
178 "}\n" .
179 "if (!isset(\$_REQUEST['${columnName}Minute'])) {\n" .
180 " \$_REQUEST['${columnName}Minute'] = '';\n" .
181 "}\n" .
182 "if (!isset(\$_REQUEST['${columnName}Second'])) {\n" .
183 " \$_REQUEST['${columnName}Second'] = '';\n" .
184 "}\n" .
185 "require_once('Date.php');\n" .
186 "\$d = new Date();\n" .
187 "\$d->setHour(\$_REQUEST['${columnName}Hour']);\n" .
188 "\$d->setMinute(\$_REQUEST['${columnName}Minute']);\n" .
189 "\$d->setSecond(\$_REQUEST['${columnName}Second']);\n" .
190 "\$_REQUEST['$columnName'] = \$d->format('%H:%M:%S');\n";
191 } elseif ($column->inputType == 'datetime') {
192 $data .="if (!isset(\$_REQUEST['${columnName}Year'])) {\n" .
193 " \$_REQUEST['${columnName}Year'] = '';\n" .
194 "}\n" .
195 "if (!isset(\$_REQUEST['${columnName}Month'])) {\n" .
196 " \$_REQUEST['${columnName}Month'] = '';\n" .
197 "}\n" .
198 "if (!isset(\$_REQUEST['${columnName}Day'])) {\n" .
199 " \$_REQUEST['${columnName}Day'] = '';\n" .
200 "}\n" .
201 "if (!isset(\$_REQUEST['${columnName}Hour'])) {\n" .
202 " \$_REQUEST['${columnName}Hour'] = '';\n" .
203 "}\n" .
204 "if (!isset(\$_REQUEST['${columnName}Minute'])) {\n" .
205 " \$_REQUEST['${columnName}Minute'] = '';\n" .
206 "}\n" .
207 "if (!isset(\$_REQUEST['${columnName}Second'])) {\n" .
208 " \$_REQUEST['${columnName}Second'] = '';\n" .
209 "}\n" .
210 "require_once('Date.php');\n" .
211 "\$d = new Date();\n" .
212 "\$d->setYear(\$_REQUEST['${columnName}Year']);\n" .
213 "\$d->setMonth(\$_REQUEST['${columnName}Month']);\n" .
214 "\$d->setDay(\$_REQUEST['${columnName}Day']);\n" .
215 "\$d->setHour(\$_REQUEST['${columnName}Hour']);\n" .
216 "\$d->setMinute(\$_REQUEST['${columnName}Minute']);\n" .
217 "\$d->setSecond(\$_REQUEST['${columnName}Second']);\n" .
218 "\$_REQUEST['$columnName'] = \$d->format('%Y-%m-%d %H:%M:%S');\n";
219 } else {
220 $data .="if (!isset(\$_REQUEST['$columnName'])) {\n" .
221 " \$_REQUEST['$columnName'] = '';\n" .
222 "}\n";
223 }
224 }
225 $data .="if (!isset(\$_REQUEST['action'])) {\n" .
226 " \$_REQUEST['action'] = '';\n" .
227 "}\n" .
228 "if (!isset(\$_REQUEST['orderDescending'])) {\n" .
229 " \$_REQUEST['orderDescending'] = '';\n" .
230 "}\n";
231
232
233 $data .="\$record = \$${tableInstanceName}->" . $table->getGetByPrimaryKeyMethodName() .
234 "(\$_REQUEST['" . $primaryKeyColumn->getColumnNameForVariableName() . "']);\n";
235
236
237 $updates = "";
238 $conditionalUpdates = "";
239 $unsetStoredFiles = "";
240 // $unsetOldStoredFiles = "";
241 $storedErrorCondition = "";
242 reset($table->columns);
243 while (list($key, $column) = each($table->columns)) {
244 $columnName = $column->getColumnNameForVariableName();
245 $isValidFileOptions = $column->getIsValidFileOptions();
246 if (($column->defaultUpdateValue != "" && $isValidFileOptions == null) || $column->doNotUpdate) {
247 continue;
248 }
249
250 if ($isValidFileOptions == null) {
251 $updates .= ($updates == "" ? "" : ",\n") . " '$column->name' => \$_REQUEST['$columnName']";
252 } else {
253 $storedFile = 'stored' . ucfirst($columnName);
254 $storedError = 'stored' . ucfirst($columnName) . 'Error';
255 $conditionalUpdates .= " if (is_uploaded_file(\$_FILES['$columnName']['tmp_name'])) {\n" .
256 " \$$storedFile = '" . $column->table . "_" . $column->name . "_' . \$${tableInstanceName}->" . $column->generateGetNextMethodName() . "();\n" .
257 " \$$storedError = \$${tableInstanceName}->" . $column->generateStoreUploadedMethodName() . "(\$_FILES['$columnName'], \$$storedFile);\n" .
258 " \$updates['$column->name'] = \$$storedFile;\n" .
259 " } else {\n" .
260 " \$$storedFile = null;\n" .
261 " \$$storedError = null;\n" .
262 " }\n";
263 $storedErrorCondition .= ($storedErrorCondition == "" ? "" : " && ") . "!PEAR::isError(\$$storedError)";
264 $unsetStoredFiles .=" if (\$$storedFile != null) {\n" .
265 " \$${tableInstanceName}->" . $column->generateRemoveStoredMethodName() . "(\$$storedFile);\n" .
266 " }\n";
267 /*
268 $unsetOldStoredFiles .= " if (isset(\$updates['$column->name'])) {\n" .
269 " \$${tableInstanceName}->" . $column->generateRemoveStoredMethodName() . "(\$record['$column->name']);\n" .
270 " }\n";
271 */
272 }
273 }
274
275 $data .="\n" .
276 "if(\$_REQUEST['action'] == 1){\n" .
277 " \$updates = array(\n" .
278 "$updates\n" .
279 " );\n" .
280 "$conditionalUpdates\n";
281
282 if ($storedErrorCondition) {
283 $data .= " if ($storedErrorCondition) {\n";
284 }
285
286 $data .=" \$error = \$${tableInstanceName}->" . $table->getUpdateByPrimaryKeyMethodName() . "(\$_REQUEST['" . $primaryKeyColumn->getColumnNameForVariableName() . "'], \$updates);\n";
287
288 $data .="\n" .
289 ' if (!PEAR::isError($error)) {' . "\n" .
290 " header('Location: " . $this->getControllerGetFilename() . "');\n" .
291 " exit;\n" .
292 " }\n";
293 if ($storedErrorCondition) {
294 $data .=" }\n" .
295 "$unsetStoredFiles\n";
296 }
297 reset($table->columns);
298 while (list($key, $column) = each($table->columns)) {
299 $columnName = $column->getColumnNameForVariableName();
300 $isValidFileOptions = $column->getIsValidFileOptions();
301 if ((($column->defaultUpdateValue != "" && $isValidFileOptions == null) || $column->doNotUpdate) && !$column->isPrimaryKey) {
302 continue;
303 }
304 $data .= " \$${appInstanceName}->smarty->assign('$columnName',\$_REQUEST['$columnName']);\n";
305 }
306 $data .="} else {\n";
307
308 reset($table->columns);
309 while (list($key, $column) = each($table->columns)) {
310 $columnName = $column->getColumnNameForVariableName();
311 $isValidFileOptions = $column->getIsValidFileOptions();
312 if ((($column->defaultUpdateValue != "" && $isValidFileOptions == null) || $column->doNotUpdate) && !$column->isPrimaryKey) {
313 continue;
314 }
315 $data .= " \$${appInstanceName}->smarty->assign('$columnName',\$record['$column->name']);\n";
316 }
317
318 $data .= "}\n";
319
320 reset($table->columns);
321 while (list($key, $column) = each($table->columns)) {
322 $columnName = $column->getColumnNameForVariableName();
323 if ($column->inputTypeData != '') {
324 $data .="\$${appInstanceName}->smarty->assign('${columnName}Options', $column->inputTypeData);\n" .
325 "\n";
326 } elseif ($column->isForeignKey) {
327 $foreignKeyTargetTableName = substr($column->foreignKeyTarget,0,strpos($column->foreignKeyTarget,"."));
328 $foreignKeyTargetRSDTable =& $table->rsdEngineDB->getTable($foreignKeyTargetTableName);
329 $foreignKeyTargetTableInstanceName = $foreignKeyTargetRSDTable->getChildClassInstanceName();
330 $data .="\$${foreignKeyTargetTableInstanceName} =& \$${appInstanceName}->getTable('$foreignKeyTargetTableName');\n" .
331 "\$${appInstanceName}->smarty->assign('${columnName}Options',\$${foreignKeyTargetTableInstanceName}->getSmartyHTMLOptionsArray());\n" .
332 "\n";
333 }
334 }
335
336 $data .="\n" .
337 '$' . $appInstanceName . '->smarty->assign("error",PEAR::isError($error) || PEAR::isError($record) || $' . $appInstanceName . '->errorManager->errorsOccurred());' . "\n" .
338 "\n";
339
340 reset($table->columns);
341 while (list($key, $column) = each($table->columns)) {
342 if ($column->defaultUpdateValue != "" || $column->doNotUpdate) {
343 continue;
344 }
345 $columnName = $column->getColumnNameForVariableName();
346 $data .= "\$${appInstanceName}->smarty->assign('" . $column->name . "Error',\$${appInstanceName}->errorManager->errorsOccurredOnProperty('" . $column->name . "'));\n";
347 }
348 $data .="\n" .
349 "\$${appInstanceName}->smarty->display('" . $this->getTemplateFilename() . "');\n" .
350 "?>";
351 return $data;
352 }
353 }
354 ?>

Documentation generated on Mon, 8 Dec 2003 13:11:12 +0100 by phpDocumentor 1.2.3