These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Alpha\Controller; |
||
4 | |||
5 | use Alpha\Util\Logging\Logger; |
||
6 | use Alpha\Util\Http\Request; |
||
7 | use Alpha\Util\Http\Response; |
||
8 | use Alpha\View\View; |
||
9 | use Alpha\View\ViewState; |
||
10 | use Alpha\Exception\SecurityException; |
||
11 | use Alpha\Exception\AlphaException; |
||
12 | use Alpha\Model\ActiveRecord; |
||
13 | |||
14 | /** |
||
15 | * Controller used to list all of the active record types in the system. |
||
16 | * |
||
17 | * @since 1.0 |
||
18 | * |
||
19 | * @author John Collins <[email protected]> |
||
20 | * @license http://www.opensource.org/licenses/bsd-license.php The BSD License |
||
21 | * @copyright Copyright (c) 2017, John Collins (founder of Alpha Framework). |
||
22 | * All rights reserved. |
||
23 | * |
||
24 | * <pre> |
||
25 | * Redistribution and use in source and binary forms, with or |
||
26 | * without modification, are permitted provided that the |
||
27 | * following conditions are met: |
||
28 | * |
||
29 | * * Redistributions of source code must retain the above |
||
30 | * copyright notice, this list of conditions and the |
||
31 | * following disclaimer. |
||
32 | * * Redistributions in binary form must reproduce the above |
||
33 | * copyright notice, this list of conditions and the |
||
34 | * following disclaimer in the documentation and/or other |
||
35 | * materials provided with the distribution. |
||
36 | * * Neither the name of the Alpha Framework nor the names |
||
37 | * of its contributors may be used to endorse or promote |
||
38 | * products derived from this software without specific |
||
39 | * prior written permission. |
||
40 | * |
||
41 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||
42 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||
43 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
||
44 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
45 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
||
46 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||
47 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
48 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||
49 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
||
50 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
||
51 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
||
52 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||
53 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
54 | * </pre> |
||
55 | */ |
||
56 | class ListActiveRecordsController extends Controller implements ControllerInterface |
||
57 | { |
||
58 | /** |
||
59 | * Trace logger. |
||
60 | * |
||
61 | * @var \Alpha\Util\Logging\Logger |
||
62 | * |
||
63 | * @since 1.0 |
||
64 | */ |
||
65 | private static $logger = null; |
||
66 | |||
67 | /** |
||
68 | * the constructor. |
||
69 | * |
||
70 | * @since 1.0 |
||
71 | */ |
||
72 | public function __construct() |
||
73 | { |
||
74 | self::$logger = new Logger('ListActiveRecordsController'); |
||
75 | self::$logger->debug('>>__construct()'); |
||
76 | |||
77 | // ensure that the super class constructor is called, indicating the rights group |
||
78 | parent::__construct('Admin'); |
||
79 | |||
80 | // set up the title and meta details |
||
81 | $this->setTitle('Listing all active records in the system'); |
||
82 | $this->setDescription('Page to list all active records.'); |
||
83 | $this->setKeywords('list,all,active,records'); |
||
84 | |||
85 | $viewState = ViewState::getInstance(); |
||
86 | $viewState->set('renderAdminMenu', true); |
||
87 | |||
88 | self::$logger->debug('<<__construct'); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Handle GET requests. |
||
93 | * |
||
94 | * @param alpha\Util\Http\Request $request |
||
95 | * |
||
96 | * @return alpha\Util\Http\Response |
||
97 | * |
||
98 | * @since 1.0 |
||
99 | */ |
||
100 | public function doGET($request) |
||
101 | { |
||
102 | self::$logger->debug('>>doGET($request=['.var_export($request, true).'])'); |
||
103 | |||
104 | $body = View::displayPageHead($this); |
||
105 | |||
106 | $body .= $this->displayBodyContent(); |
||
107 | |||
108 | $body .= View::displayPageFoot($this); |
||
109 | |||
110 | self::$logger->debug('<<doGET'); |
||
111 | |||
112 | return new Response(200, $body, array('Content-Type' => 'text/html')); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Handle POST requests. |
||
117 | * |
||
118 | * @param alpha\Util\Http\Request $request |
||
119 | * |
||
120 | * @return alpha\Util\Http\Response |
||
121 | * |
||
122 | * @since 1.0 |
||
123 | */ |
||
124 | public function doPOST($request) |
||
125 | { |
||
126 | self::$logger->debug('>>doPOST($request=['.var_export($request, true).'])'); |
||
127 | |||
128 | $params = $request->getParams(); |
||
129 | |||
130 | $body = View::displayPageHead($this); |
||
131 | |||
132 | try { |
||
133 | // check the hidden security fields before accepting the form POST data |
||
134 | if (!$this->checkSecurityFields()) { |
||
135 | throw new SecurityException('This page cannot accept post data from remote servers!'); |
||
136 | } |
||
137 | |||
138 | if (isset($params['createTableBut'])) { |
||
139 | try { |
||
140 | $classname = $params['createTableClass']; |
||
141 | |||
142 | $Record = new $classname(); |
||
143 | $Record->makeTable(); |
||
144 | |||
145 | self::$logger->action('Created the table for class '.$classname); |
||
146 | |||
147 | $body .= View::displayUpdateMessage('The table for the class '.$classname.' has been successfully created.'); |
||
148 | } catch (AlphaException $e) { |
||
149 | self::$logger->error($e->getMessage()); |
||
150 | $body .= View::displayErrorMessage('Error creating the table for the class '.$classname.', check the log!'); |
||
151 | } |
||
152 | } |
||
153 | |||
154 | if (isset($params['createHistoryTableBut'])) { |
||
155 | try { |
||
156 | $classname = $params['createTableClass']; |
||
157 | |||
158 | $Record = new $classname(); |
||
159 | $Record->makeHistoryTable(); |
||
160 | |||
161 | self::$logger->action('Created the history table for class '.$classname); |
||
162 | |||
163 | $body .= View::displayUpdateMessage('The history table for the class '.$classname.' has been successfully created.'); |
||
164 | } catch (AlphaException $e) { |
||
165 | self::$logger->error($e->getMessage()); |
||
166 | $body .= View::displayErrorMessage('Error creating the history table for the class '.$classname.', check the log!'); |
||
167 | } |
||
168 | } |
||
169 | |||
170 | if (isset($params['recreateTableClass']) && $params['admin_'.stripslashes($params['recreateTableClass']).'_button_pressed'] == 'recreateTableBut') { |
||
171 | try { |
||
172 | $classname = $params['recreateTableClass']; |
||
173 | $Record = new $classname(); |
||
174 | $Record->rebuildTable(); |
||
175 | |||
176 | self::$logger->action('Recreated the table for class '.$classname); |
||
177 | |||
178 | $body .= View::displayUpdateMessage('The table for the class '.$classname.' has been successfully recreated.'); |
||
179 | } catch (AlphaException $e) { |
||
180 | self::$logger->error($e->getMessage()); |
||
181 | $body .= View::displayErrorMessage('Error recreating the table for the class '.$classname.', check the log!'); |
||
182 | } |
||
183 | } |
||
184 | |||
185 | if (isset($params['updateTableClass']) && $params['admin_'.stripslashes($params['updateTableClass']).'_button_pressed'] == 'updateTableBut') { |
||
186 | try { |
||
187 | $classname = $params['updateTableClass']; |
||
188 | |||
189 | $Record = new $classname(); |
||
190 | $missingFields = $Record->findMissingFields(); |
||
191 | |||
192 | $count = count($missingFields); |
||
193 | |||
194 | for ($i = 0; $i < $count; ++$i) { |
||
195 | $Record->addProperty($missingFields[$i]); |
||
196 | } |
||
197 | |||
198 | self::$logger->action('Updated the table for class '.$classname); |
||
199 | |||
200 | $body .= View::displayUpdateMessage('The table for the class '.$classname.' has been successfully updated.'); |
||
201 | } catch (AlphaException $e) { |
||
202 | self::$logger->error($e->getMessage()); |
||
203 | $body .= View::displayErrorMessage('Error updating the table for the class '.$classname.', check the log!'); |
||
204 | } |
||
205 | } |
||
206 | } catch (SecurityException $e) { |
||
207 | $body .= View::displayErrorMessage($e->getMessage()); |
||
208 | self::$logger->warn($e->getMessage()); |
||
209 | } |
||
210 | |||
211 | $body .= $this->displayBodyContent(); |
||
212 | |||
213 | $body .= View::displayPageFoot($this); |
||
214 | |||
215 | self::$logger->debug('<<doPOST'); |
||
216 | |||
217 | return new Response(200, $body, array('Content-Type' => 'text/html')); |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Private method to generate the main body HTML for this page. |
||
222 | * |
||
223 | * @since 1.0 |
||
224 | * |
||
225 | * @return string |
||
226 | */ |
||
227 | private function displayBodyContent() |
||
228 | { |
||
229 | $classNames = ActiveRecord::getRecordClassNames(); |
||
230 | |||
231 | $body = ''; |
||
232 | |||
233 | $fields = array('formAction' => $this->request->getURI()); |
||
234 | |||
235 | foreach ($classNames as $className) { |
||
236 | try { |
||
237 | $activeRecord = new $className(); |
||
238 | $view = View::getInstance($activeRecord); |
||
239 | $body .= $view->adminView($fields); |
||
240 | } catch (AlphaException $e) { |
||
241 | self::$logger->error("[$classname]:".$e->getMessage()); |
||
0 ignored issues
–
show
|
|||
242 | // its possible that the exception occured due to the table schema being out of date |
||
243 | if ($activeRecord->checkTableExists() && $activeRecord->checkTableNeedsUpdate()) { |
||
244 | $missingFields = $activeRecord->findMissingFields(); |
||
245 | |||
246 | $count = count($missingFields); |
||
247 | |||
248 | for ($i = 0; $i < $count; ++$i) { |
||
249 | $activeRecord->addProperty($missingFields[$i]); |
||
250 | } |
||
251 | |||
252 | // now try again... |
||
253 | $activeRecord = new $className(); |
||
254 | $view = View::getInstance($activeRecord); |
||
255 | $body .= $view->adminView($fields); |
||
256 | } |
||
257 | } catch (\Exception $e) { |
||
258 | self::$logger->error($e->getMessage()); |
||
259 | $body .= View::displayErrorMessage('Error accessing the class ['.$classname.'], check the log!'); |
||
0 ignored issues
–
show
The variable
$classname does not exist. Did you mean $classNames ?
This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name. The variable may have been renamed without also renaming all references. ![]() |
|||
260 | } |
||
261 | } |
||
262 | |||
263 | return $body; |
||
264 | } |
||
265 | } |
||
266 |
This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.
The variable may have been renamed without also renaming all references.