1: <?php
2:
3: namespace Alpha\Controller;
4:
5: use Alpha\Util\Logging\Logger;
6: use Alpha\Util\Config\ConfigProvider;
7: use Alpha\Util\Http\Request;
8: use Alpha\Util\Http\Response;
9: use Alpha\View\View;
10: use Alpha\View\ViewState;
11: use Alpha\Exception\SecurityException;
12: use Alpha\Exception\AlphaException;
13: use Alpha\Model\ActiveRecord;
14:
15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56:
57: class ListActiveRecordsController extends Controller implements ControllerInterface
58: {
59: 60: 61: 62: 63: 64: 65:
66: private static $logger = null;
67:
68: 69: 70: 71: 72:
73: public function __construct()
74: {
75: self::$logger = new Logger('ListActiveRecordsController');
76: self::$logger->debug('>>__construct()');
77:
78: $config = ConfigProvider::getInstance();
79:
80:
81: parent::__construct('Admin');
82:
83:
84: $this->setTitle('Listing all active records in the system');
85: $this->setDescription('Page to list all active records.');
86: $this->setKeywords('list,all,active,records');
87:
88: $viewState = ViewState::getInstance();
89: $viewState->set('renderAdminMenu', true);
90:
91: self::$logger->debug('<<__construct');
92: }
93:
94: 95: 96: 97: 98: 99: 100: 101: 102:
103: public function doGET($request)
104: {
105: self::$logger->debug('>>doGET($request=['.var_export($request, true).'])');
106:
107: $body = View::displayPageHead($this);
108:
109: $body .= $this->displayBodyContent();
110:
111: $body .= View::displayPageFoot($this);
112:
113: self::$logger->debug('<<doGET');
114:
115: return new Response(200, $body, array('Content-Type' => 'text/html'));
116: }
117:
118: 119: 120: 121: 122: 123: 124: 125: 126:
127: public function doPOST($request)
128: {
129: self::$logger->debug('>>doPOST($request=['.var_export($request, true).'])');
130:
131: $params = $request->getParams();
132:
133: $config = ConfigProvider::getInstance();
134:
135: $body = View::displayPageHead($this);
136:
137: try {
138:
139: if (!$this->checkSecurityFields()) {
140: throw new SecurityException('This page cannot accept post data from remote servers!');
141: }
142:
143: if (isset($params['createTableBut'])) {
144: try {
145: $classname = $params['createTableClass'];
146:
147: $BO = new $classname();
148: $BO->makeTable();
149:
150: self::$logger->action('Created the table for class '.$classname);
151:
152: $body .= View::displayUpdateMessage('The table for the class '.$classname.' has been successfully created.');
153: } catch (AlphaException $e) {
154: self::$logger->error($e->getMessage());
155: $body .= View::displayErrorMessage('Error creating the table for the class '.$classname.', check the log!');
156: }
157: }
158:
159: if (isset($params['createHistoryTableBut'])) {
160: try {
161: $classname = $params['createTableClass'];
162:
163: $BO = new $classname();
164: $BO->makeHistoryTable();
165:
166: self::$logger->action('Created the history table for class '.$classname);
167:
168: $body .= View::displayUpdateMessage('The history table for the class '.$classname.' has been successfully created.');
169: } catch (AlphaException $e) {
170: self::$logger->error($e->getMessage());
171: $body .= View::displayErrorMessage('Error creating the history table for the class '.$classname.', check the log!');
172: }
173: }
174:
175: if (isset($params['recreateTableClass']) && $params['admin_'.stripslashes($params['recreateTableClass']).'_button_pressed'] == 'recreateTableBut') {
176: try {
177: $classname = $params['recreateTableClass'];
178: $BO = new $classname();
179: $BO->rebuildTable();
180:
181: self::$logger->action('Recreated the table for class '.$classname);
182:
183: $body .= View::displayUpdateMessage('The table for the class '.$classname.' has been successfully recreated.');
184: } catch (AlphaException $e) {
185: self::$logger->error($e->getMessage());
186: $body .= View::displayErrorMessage('Error recreating the table for the class '.$classname.', check the log!');
187: }
188: }
189:
190: if (isset($params['updateTableClass']) && $params['admin_'.stripslashes($params['updateTableClass']).'_button_pressed'] == 'updateTableBut') {
191: try {
192: $classname = $params['updateTableClass'];
193:
194: $BO = new $classname();
195: $missingFields = $BO->findMissingFields();
196:
197: $count = count($missingFields);
198:
199: for ($i = 0; $i < $count; ++$i) {
200: $BO->addProperty($missingFields[$i]);
201: }
202:
203: self::$logger->action('Updated the table for class '.$classname);
204:
205: $body .= View::displayUpdateMessage('The table for the class '.$classname.' has been successfully updated.');
206: } catch (AlphaException $e) {
207: self::$logger->error($e->getMessage());
208: $body .= View::displayErrorMessage('Error updating the table for the class '.$classname.', check the log!');
209: }
210: }
211: } catch (SecurityException $e) {
212: $body .= View::displayErrorMessage($e->getMessage());
213: self::$logger->warn($e->getMessage());
214: }
215:
216: $body .= $this->displayBodyContent();
217:
218: $body .= View::displayPageFoot($this);
219:
220: self::$logger->debug('<<doPOST');
221:
222: return new Response(200, $body, array('Content-Type' => 'text/html'));
223: }
224:
225: 226: 227: 228: 229: 230: 231:
232: private function displayBodyContent()
233: {
234: $classNames = ActiveRecord::getBOClassNames();
235:
236: $body = '';
237:
238: $fields = array('formAction' => $this->request->getURI());
239:
240: foreach ($classNames as $className) {
241: try {
242: $activeRecord = new $className();
243: $view = View::getInstance($activeRecord);
244: $body .= $view->adminView($fields);
245: } catch (AlphaException $e) {
246: self::$logger->error("[$classname]:".$e->getMessage());
247:
248: if ($activeRecord->checkTableExists() && $activeRecord->checkTableNeedsUpdate()) {
249: $missingFields = $activeRecord->findMissingFields();
250:
251: $count = count($missingFields);
252:
253: for ($i = 0; $i < $count; ++$i) {
254: $activeRecord->addProperty($missingFields[$i]);
255: }
256:
257:
258: $activeRecord = new $className();
259: $view = View::getInstance($activeRecord);
260: $body .= $view->adminView($fields);
261: }
262: } catch (\Exception $e) {
263: self::$logger->error($e->getMessage());
264: $body .= View::displayErrorMessage('Error accessing the class ['.$classname.'], check the log!');
265: }
266: }
267:
268: return $body;
269: }
270: }
271: