Documentation is available at RSDEngineDBControllerFileSearch.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 RSDEngineDBControllerFileSearch.
20 *
21 * @package RSDEngine
22 * @author Lukas Feiler <lukas.feiler@chello.at>
23 * @copyright Lukas Feiler 2003
24 * @filesource
25 */
26
27 /**RSDEngineDBControllerFileSearch extends RSDEngineControllerFile.
28 */
29 require_once('RSDEngine/RSDEngineControllerFile.php');
30
31 /**Generates the controller file for searching records in 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 RSDEngineDBControllerFileSearch 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 "search" . 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 "search" . ucfirst($this->config['table']->getTableNameWithoutPrefix()) . ".tpl";
59 }
60
61 /**Returns the code for the controller file for searching records in a table.
62 *
63 * @access private
64 * @return String The code for the file.
65 */
66 function getCode()
67 {
68 extract($this->config);
69 $table =& $this->config['table'];
70 $authors = RSArrayUtil::toString(split("\n",$authors),"\n",'* @author %val');
71 $records = ucfirst($this->config['table']->getTableNameWithoutPrefix());
72 $tableInstanceName = $table->getChildClassInstanceName();
73 $appInstanceName = $this->relatedClasses['dbApplicationClass']->getInstanceName();
74
75 $allowUserOrderBy = $table->getFileOptionProperty('searchController', 'allowUserOrderBy');
76 $orderBy = $table->getFileOptionProperty('searchController', 'orderBy');
77 $selectMethodName = $table->getFileOptionProperty('searchController', 'selectMethodName');
78 $paging = $table->getFileOptionProperty('searchController', 'paging');
79 $recordsPerPage = $table->getFileOptionProperty('searchController', 'recordsPerPage');
80 $header = $table->getFileOptionProperty('searchController', 'header');
81 $footer = $table->getFileOptionProperty('searchController', 'footer');
82 $public = $table->getFileOptionProperty('searchController', 'public');
83 $write = $table->getFileOptionProperty('searchController', 'write');
84
85 $this->setWrite($write);
86
87 $columns = $table->getSelectMethodColumns($selectMethodName);
88 $data = '';
89 $data .="<?php\n" .
90 '/' . '**This file is the controller for searching records from the table ' . $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 if ($allowUserOrderBy) {
115 $data .="\n" .
116 "if (!isset(\$_REQUEST['orderBy'])) {\n" .
117 " \$_REQUEST['orderBy'] = '$orderBy';\n" .
118 "}\n" .
119 "if (!isset(\$_REQUEST['orderDescending'])) {\n" .
120 " \$_REQUEST['orderDescending'] = '';\n" .
121 "}\n";
122 }
123
124 if ($paging) {
125 $data .= "\$pageNumber = isset(\$_REQUEST['pageNumber']) && is_numeric(\$_REQUEST['pageNumber']) ? \$_REQUEST['pageNumber'] : 1;\n";
126 }
127
128 $data .="\n" .
129 "\$${tableInstanceName} =& \$${appInstanceName}->getTable('$table->name');\n" .
130 "\n" .
131 "\$${appInstanceName}->smarty->assign('canInsertInto" . ucfirst($table->getTableNameForVariableName()) . "', \$${tableInstanceName}->canInsert());\n" .
132 "\n";
133
134 if ($allowUserOrderBy) {
135 $data .="\$${appInstanceName}->smarty->assign('orderBy',\$_REQUEST['orderBy']);\n" .
136 "\$${appInstanceName}->smarty->assign('orderDescending',\$_REQUEST['orderDescending'] ? 1 : 0);\n" .
137 "\$${appInstanceName}->smarty->assign('orderDescendingOptions',array(0 => 'ASC', 1 => 'DESC'));\n";
138 }
139
140 if ($allowUserOrderBy) {
141 $data .="\$orderByClause = '';\n" .
142 "switch (\$_REQUEST['orderBy']) {\n";
143 reset($columns);
144 while (list($key, $column) = each($columns)) {
145 $columnName = $column->getColumnNameForVariableName();
146 $data .=" case '$columnName':\n" .
147 " \$orderByClause = 'ORDER BY $column->name';\n" .
148 " break;\n";
149 }
150 $data .=" default:\n" .
151 " \$orderByClause = '';\n" .
152 " break;\n" .
153 "}\n" .
154 "if (\$orderByClause != '' && \$_REQUEST['orderDescending']) {\n" .
155 " \$orderByClause .= ' DESC';\n" .
156 "}\n";
157 } else {
158 if ($orderBy != '') {
159 $orderBy = "ORDER BY $orderBy";
160 }
161 $data .="\$orderByClause = '$orderBy';\n";
162 }
163
164 $data .="\n" .
165 "\$conditions = array();\n";
166 reset($columns);
167 while (list($key, $column) = each($columns)) {
168 $columnName = $column->getColumnNameForVariableName();
169 $prepareString = $column->isAnyKey() ? "?" : "%";
170 $data .="if (isset(\$_REQUEST['$columnName']) && \$_REQUEST['$columnName'] != '') {\n" .
171 " \$conditions['$column->name'] = array(\$_REQUEST['$columnName'],'$prepareString');\n" .
172 " \$${appInstanceName}->smarty->assign('$columnName',\$_REQUEST['$columnName']);\n" .
173 "}\n";
174 }
175 if ($paging) {
176 $data .="\$page = \$${tableInstanceName}->getPage(\$pageNumber, $recordsPerPage, \$conditions, '$selectMethodName', \$orderByClause, '*');\n" .
177 'if (PEAR::isError($page)) {' . "\n" .
178 " \$${appInstanceName}->smarty->assign('errorMsg',\$page->getMessage());\n" .
179 "} else {\n" .
180 " \$records =& \$page['records'];\n" .
181 " \n" .
182 " \$${appInstanceName}->smarty->assign('from', \$page['from']);\n" .
183 " \$${appInstanceName}->smarty->assign('to', \$page['to']);\n" .
184 " \$${appInstanceName}->smarty->assign('pageNumber', \$page['pageNumber']);\n" .
185 " \$${appInstanceName}->smarty->assign('pageCount', \$page['pageCount']);\n" .
186 " \$${appInstanceName}->smarty->assign('hasNextPage', \$page['hasNextPage']);\n" .
187 " \$${appInstanceName}->smarty->assign('hasPreviousPage', \$page['pageNumber'] > 1);\n" .
188 " \$${appInstanceName}->smarty->assign('recordCount', \$page['recordCount']);\n" .
189 " \$${appInstanceName}->smarty->assign('pageRecordsCount', \$page['pageRecordsCount']);\n" .
190 " \$pages = array();\n" .
191 " for (\$i = 1; \$i <= \$page['pageCount']; \$i++) {\n" .
192 " \$pages[\$i] = \$i;\n" .
193 " }\n" .
194 " \$${appInstanceName}->smarty->assign('pages', \$pages);\n";
195
196 } else {
197 $data .="\$records = \$${tableInstanceName}->get(\$conditions, '$selectMethodName', \$orderByClause, '*');\n" .
198 'if (PEAR::isError($records)) {' . "\n" .
199 " \$${appInstanceName}->smarty->assign('errorMsg',\$records->getMessage());\n" .
200 '} else {' . "\n";
201 }
202
203 if ($liveUser) {
204 $data .=" reset(\$records);\n" .
205 " while (list(\$key, \$val) = each(\$records)) {\n" .
206 " \$${tableInstanceName}->canUpdate(\$records[\$key]);\n" .
207 " \$${tableInstanceName}->canDelete(\$records[\$key]);\n";
208 reset($table->columns);
209 while (list($key, $column) = each($table->columns)) {
210
211 if (count($table->columns[$key]->editLiveUserRightOptions) > 0) {
212 $data .=" \$${tableInstanceName}->" . $table->columns[$key]->generateCanGrantRightToUserMethodName() . "(\$records[\$key]);\n" .
213 " \$${tableInstanceName}->" . $table->columns[$key]->generateCanRevokeRightFromUserMethodName() . "(\$records[\$key]);\n" .
214 " \$${tableInstanceName}->" . $table->columns[$key]->generateCanGrantRightToGroupMethodName() . "(\$records[\$key]);\n" .
215 " \$${tableInstanceName}->" . $table->columns[$key]->generateCanRevokeRightFromGroupMethodName() . "(\$records[\$key]);\n";
216 } elseif (count($table->columns[$key]->editLiveUserAreaOptions) > 0) {
217 $data .=" \$${tableInstanceName}->" . $table->columns[$key]->generateCanAddAdminToAreaMethodName() . "(\$records[\$key]);\n" .
218 " \$${tableInstanceName}->" . $table->columns[$key]->generateCanRemoveAdminFromAreaMethodName() . "(\$records[\$key]);\n" .
219 " \$${tableInstanceName}->" . $table->columns[$key]->generateCanUpdateAreaNameAndCommentMethodName() . "(\$records[\$key]);\n";
220 } elseif (count($table->columns[$key]->editLiveUserGroupOptions) > 0) {
221 $data .=" \$${tableInstanceName}->" . $table->columns[$key]->generateCanAddUserToGroupMethodName() . "(\$records[\$key]);\n" .
222 " \$${tableInstanceName}->" . $table->columns[$key]->generateCanRemoveUserFromGroupMethodName() . "(\$records[\$key]);\n" .
223 " \$${tableInstanceName}->" . $table->columns[$key]->generateCanUpdateGroupNameAndCommentMethodName() . "(\$records[\$key]);\n" .
224 " \$${tableInstanceName}->" . $table->columns[$key]->generateCanActivateGroupMethodName() . "(\$records[\$key]);\n" .
225 " \$${tableInstanceName}->" . $table->columns[$key]->generateCanDeactivateGroupMethodName() . "(\$records[\$key]);\n";
226 }
227
228 }
229 $data .=" }\n";
230 }
231 $data .=" \$${appInstanceName}->smarty->assign('" . $table->getTableNameForVariableName() . "',\$records);\n" .
232 "}\n" .
233 "\$${appInstanceName}->smarty->display('" . $this->getTemplateFilename() . "');\n" .
234 "?>";
235 return $data;
236 }
237 }
238 ?>
Documentation generated on Mon, 8 Dec 2003 13:11:11 +0100 by phpDocumentor 1.2.3