Documentation is available at RSDEngineDBControllerFileCreate.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 RSDEngineDBControllerFileCreate.
20 *
21 * @package RSDEngine
22 * @author Lukas Feiler <lukas.feiler@chello.at>
23 * @copyright Lukas Feiler 2003
24 * @filesource
25 */
26
27 /**RSDEngineDBControllerFileCreate 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 RSDEngineDBControllerFileCreate 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 "create" . 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 "create" . 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 /**Returns the code for the controller file for inserting records into a table.
70 *
71 * @access private
72 * @return String The code for the file.
73 */
74 function getCode()
75 {
76 extract($this->config);
77 $table =& $this->config['table'];
78 $authors = RSArrayUtil::toString(split("\n",$authors),"\n",'* @author %val');
79 $records = ucfirst($this->config['table']->getTableNameWithoutPrefix());
80 $tableInstanceName = $table->getChildClassInstanceName();
81 $appInstanceName = $this->relatedClasses['dbApplicationClass']->getInstanceName();
82
83 $public = $table->getFileOptionProperty('createController', 'public');
84 $write = $table->getFileOptionProperty('createController', 'write');
85
86 $this->setWrite($write);
87
88 $data = '';
89 $data .="<?php\n" .
90 '/' . '**This file is the controller for inserting records into ' . $table->name . ".\n" .
91 '*' . "\n" .
92 $authors . "\n" .
93 '* @copyright ' . $copyright . "\n" .
94 '* @package ' . $projectName . "\n" .
95 '* @filesource' . "\n" .
96 '*/' . "\n" .
97 "\n";
98 if ($this->config['auth'] || $this->config['liveUser']) {
99 if ($public) {
100 $data .='/' . '**This controller requires no authentication.' . "\n" .
101 "*/\n" .
102 "require_once('model/" . $this->relatedFiles['dbApplicationPublicInitFile']->getFilename() . "');\n";
103 } else {
104 $data .='/' . '**This controller requires authentication.' . "\n" .
105 "*/\n" .
106 "require_once('model/" . $this->relatedFiles['dbApplicationPrivateInitFile']->getFilename() . "');\n";
107 }
108 } else {
109 $data .='/' . '**Initializes the application.' . "\n" .
110 "*/\n" .
111 "require_once('model/" . $this->relatedFiles['dbApplicationInitFile']->getFilename() . "');\n";
112 }
113
114
115
116 $data .="\n" .
117 "//we make sure all required fields are initialized\n";
118 reset($table->columns);
119 while (list($key, $column) = each($table->columns)) {
120 $columnName = $column->getColumnNameForVariableName();
121 $isValidFileOptions = $column->getIsValidFileOptions();
122 if (($column->defaultInsertValue != "" && $isValidFileOptions == null) || $column->doNotInsert) {
123 continue;
124 }
125 if ($column->inputType == 'checkbox') {
126 if ($column->inputTypeData != '') {
127 $inputTypeData = $column->inputTypeData;
128 } else {
129 $inputTypeData = "array('checked' => '1', 'unchecked' => '0')";
130 }
131 $data .="//if the checkbox was not checked\n" .
132 "\$inputTypeData = $inputTypeData;\n" .
133 "if (isset(\$_REQUEST['$columnName'])) {\n" .
134 " if (isset(\$inputTypeData['checked'])) {\n" .
135 " \$_REQUEST['$columnName'] = \$inputTypeData['checked'];\n" .
136 " } else {\n" .
137 " \$_REQUEST['$columnName'] = '1';\n" .
138 " }\n" .
139 "} else {\n" .
140 " if (isset(\$inputTypeData['unchecked'])) {\n" .
141 " \$_REQUEST['$columnName'] = \$inputTypeData['unchecked'];\n" .
142 " } else {\n" .
143 " \$_REQUEST['$columnName'] = '0';\n" .
144 " }\n" .
145 "}\n";
146 } elseif ($column->inputType == 'date') {
147 $data .="if (!isset(\$_REQUEST['${columnName}Year'])) {\n" .
148 " \$_REQUEST['${columnName}Year'] = '';\n" .
149 "}\n" .
150 "if (!isset(\$_REQUEST['${columnName}Month'])) {\n" .
151 " \$_REQUEST['${columnName}Month'] = '';\n" .
152 "}\n" .
153 "if (!isset(\$_REQUEST['${columnName}Day'])) {\n" .
154 " \$_REQUEST['${columnName}Day'] = '';\n" .
155 "}\n" .
156 "require_once('Date.php');\n" .
157 "\$d = new Date();\n" .
158 "\$d->setYear(\$_REQUEST['${columnName}Year']);\n" .
159 "\$d->setMonth(\$_REQUEST['${columnName}Month']);\n" .
160 "\$d->setDay(\$_REQUEST['${columnName}Day']);\n" .
161 "\$_REQUEST['$columnName'] = \$d->format('%Y-%m-%d');\n";
162 } elseif ($column->inputType == 'time') {
163 $data .="if (!isset(\$_REQUEST['${columnName}Hour'])) {\n" .
164 " \$_REQUEST['${columnName}Hour'] = '';\n" .
165 "}\n" .
166 "if (!isset(\$_REQUEST['${columnName}Minute'])) {\n" .
167 " \$_REQUEST['${columnName}Minute'] = '';\n" .
168 "}\n" .
169 "if (!isset(\$_REQUEST['${columnName}Second'])) {\n" .
170 " \$_REQUEST['${columnName}Second'] = '';\n" .
171 "}\n" .
172 "require_once('Date.php');\n" .
173 "\$d = new Date();\n" .
174 "\$d->setHour(\$_REQUEST['${columnName}Hour']);\n" .
175 "\$d->setMinute(\$_REQUEST['${columnName}Minute']);\n" .
176 "\$d->setSecond(\$_REQUEST['${columnName}Second']);\n" .
177 "\$_REQUEST['$columnName'] = \$d->format('%H:%M:%S');\n";
178 } elseif ($column->inputType == 'datetime') {
179 $data .="if (!isset(\$_REQUEST['${columnName}Year'])) {\n" .
180 " \$_REQUEST['${columnName}Year'] = '';\n" .
181 "}\n" .
182 "if (!isset(\$_REQUEST['${columnName}Month'])) {\n" .
183 " \$_REQUEST['${columnName}Month'] = '';\n" .
184 "}\n" .
185 "if (!isset(\$_REQUEST['${columnName}Day'])) {\n" .
186 " \$_REQUEST['${columnName}Day'] = '';\n" .
187 "}\n" .
188 "if (!isset(\$_REQUEST['${columnName}Hour'])) {\n" .
189 " \$_REQUEST['${columnName}Hour'] = '';\n" .
190 "}\n" .
191 "if (!isset(\$_REQUEST['${columnName}Minute'])) {\n" .
192 " \$_REQUEST['${columnName}Minute'] = '';\n" .
193 "}\n" .
194 "if (!isset(\$_REQUEST['${columnName}Second'])) {\n" .
195 " \$_REQUEST['${columnName}Second'] = '';\n" .
196 "}\n" .
197 "require_once('Date.php');\n" .
198 "\$d = new Date();\n" .
199 "\$d->setYear(\$_REQUEST['${columnName}Year']);\n" .
200 "\$d->setMonth(\$_REQUEST['${columnName}Month']);\n" .
201 "\$d->setDay(\$_REQUEST['${columnName}Day']);\n" .
202 "\$d->setHour(\$_REQUEST['${columnName}Hour']);\n" .
203 "\$d->setMinute(\$_REQUEST['${columnName}Minute']);\n" .
204 "\$d->setSecond(\$_REQUEST['${columnName}Second']);\n" .
205 "\$_REQUEST['$columnName'] = \$d->format('%Y-%m-%d %H:%M:%S');\n";
206 } else {
207 $data .="if (!isset(\$_REQUEST['$columnName'])) {\n" .
208 " \$_REQUEST['$columnName'] = '';\n" .
209 "}\n";
210 }
211 }
212 $data .="if (!isset(\$_REQUEST['action'])) {\n" .
213 " \$_REQUEST['action'] = '';\n" .
214 "}\n" .
215 "\n" .
216 "\$error = null;\n" .
217 "\n";
218
219
220 $data .="\n" .
221 "if (\$_REQUEST['action'] == 1) {\n" .
222 " \$${tableInstanceName} =& \$${appInstanceName}->getTable('$table->name');\n" .
223 " \n";
224
225 $insertArgumentString = "";
226 $uploadFileHandling = "";
227 $storedErrorCondition = "";
228 $unsetStoredFiles = "";
229 reset($table->columns);
230 while (list($key, $column) = each($table->columns)) {
231 $columnName = $column->getColumnNameForVariableName();
232 $isValidFileOptions = $column->getIsValidFileOptions();
233
234 if (($column->defaultInsertValue != "" && $isValidFileOptions == null) || $column->doNotInsert) {
235 continue;
236 }
237
238 if ($insertArgumentString != "") {
239 $insertArgumentString .= ",\n";
240 }
241
242 if ($isValidFileOptions == null) {
243 $insertArgumentString .= " '$column->name' => \$_REQUEST['$columnName']";
244 } else {
245 $storedFile = 'stored' . ucfirst($columnName);
246 $storedError = 'stored' . ucfirst($columnName) . 'Error';
247 $uploadFileHandling .= " \$$storedFile = '" . $column->table . "_" . $column->name . "_' . \$${tableInstanceName}->" . $column->generateGetNextMethodName() . "();\n" .
248 " \$$storedError = \$${tableInstanceName}->" . $column->generateStoreUploadedMethodName() . "(\$_FILES['$columnName'], \$$storedFile);\n" .
249 "\n";
250 $storedErrorCondition .= ($storedErrorCondition == "" ? "" : " && ") . "!PEAR::isError(\$$storedError)";
251 $unsetStoredFiles .= " \$${tableInstanceName}->" . $column->generateRemoveStoredMethodName() . "(\$$storedFile);\n";
252 $insertArgumentString .= " '$column->name' => \$$storedFile";
253 }
254 }
255 $insertArgumentString = " array(\n" .
256 "$insertArgumentString\n".
257 " )";
258
259 $data .= "$uploadFileHandling\n";
260
261 if ($storedErrorCondition) {
262 $data .= " if ($storedErrorCondition) {\n";
263 }
264 $data .=" \$error = \$${tableInstanceName}->insert(\n" .
265 "$insertArgumentString\n" .
266 " );\n";
267 $data .="\n" .
268 ' if (!PEAR::isError($error)) {' . "\n" .
269 " header('Location: " . $this->getControllerGetFilename() . "');\n" .
270 " exit;\n" .
271 " }\n" .
272 "\n";
273 if ($storedErrorCondition) {
274 $data .=" }\n" .
275 "$unsetStoredFiles\n";
276 }
277 $data .= "}\n";
278 reset($table->columns);
279 while (list($key, $column) = each($table->columns)) {
280 if ($column->defaultInsertValue != "" || $column->doNotInsert) {
281 continue;
282 }
283 $columnName = $column->getColumnNameForVariableName();
284 $data .= "\$${appInstanceName}->smarty->assign('$columnName',\$_REQUEST['$columnName']);\n";
285
286 if ($column->inputTypeData != '') {
287 $data .="\$${appInstanceName}->smarty->assign('${columnName}Options', $column->inputTypeData);\n" .
288 "\n";
289 } elseif ($column->isForeignKey) {
290 $foreignKeyTargetTableName =& substr($column->foreignKeyTarget,0,strpos($column->foreignKeyTarget,"."));
291 $foreignKeyTargetRSDTable =& $table->rsdEngineDB->getTable($foreignKeyTargetTableName);
292 $foreignKeyTargetTableInstanceName = $foreignKeyTargetRSDTable->getChildClassInstanceName();
293 $data .="\$${foreignKeyTargetTableInstanceName} =& \$${appInstanceName}->getTable('$foreignKeyTargetTableName');\n" .
294 "\$${appInstanceName}->smarty->assign('${columnName}Options',\$${foreignKeyTargetTableInstanceName}->getSmartyHTMLOptionsArray());\n" .
295 "\n";
296 }
297 }
298 $data .="\n" .
299 '$' . $appInstanceName . '->smarty->assign("error",PEAR::isError($error) || $' . $appInstanceName . '->errorManager->errorsOccurred());' . "\n" .
300 "\n";
301
302 reset($table->columns);
303 while (list($key, $column) = each($table->columns)) {
304 if ($column->defaultInsertValue != "" || $column->doNotInsert) {
305 continue;
306 }
307 $columnName = $column->getColumnNameForVariableName();
308 $data .= "\$${appInstanceName}->smarty->assign('" . $column->name . "Error',\$${appInstanceName}->errorManager->errorsOccurredOnProperty('" . $column->name . "'));\n";
309 }
310 $data .="\n" .
311 "\$${appInstanceName}->smarty->display('" . $this->getTemplateFilename() . "');\n" .
312 "?>";
313 return $data;
314 }
315 }
316 ?>
Documentation generated on Mon, 8 Dec 2003 13:11:07 +0100 by phpDocumentor 1.2.3