1: <?php
2:
3:
4: if(!isset($config)) {
5: require_once '../util/AlphaConfig.inc';
6: $config = AlphaConfig::getInstance();
7:
8: require_once $config->get('app.root').'alpha/util/AlphaAutoLoader.inc';
9: }
10:
11: 12: 13: 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: class ListAll extends AlphaController implements AlphaControllerInterface {
57: 58: 59: 60: 61: 62:
63: protected $BOname;
64:
65: 66: 67: 68: 69: 70:
71: protected $BOView;
72:
73: 74: 75: 76: 77: 78:
79: protected $startPoint;
80:
81: 82: 83: 84: 85: 86:
87: protected $BOCount = 0;
88:
89: 90: 91: 92: 93: 94:
95: protected $sort;
96:
97: 98: 99: 100: 101: 102:
103: protected $order;
104:
105: 106: 107: 108: 109: 110:
111: protected $filterField;
112:
113: 114: 115: 116: 117: 118:
119: protected $filterValue;
120:
121: 122: 123: 124: 125: 126:
127: private static $logger = null;
128:
129: 130: 131: 132: 133: 134:
135: public function __construct($visibility='Admin') {
136: self::$logger = new Logger('ListAll');
137: self::$logger->debug('>>__construct()');
138:
139: global $config;
140:
141:
142: parent::__construct($visibility);
143:
144: self::$logger->debug('<<__construct');
145: }
146:
147: 148: 149: 150: 151: 152:
153: public function doGET($params) {
154: self::$logger->debug('>>doGET($params=['.var_export($params, true).'])');
155:
156: try{
157:
158: if (isset($params['bo'])) {
159: $BOname = $params['bo'];
160: $this->BOname = $BOname;
161: }elseif(isset($this->BOname)) {
162: $BOname = $this->BOname;
163: }else{
164: throw new IllegalArguementException('No BO available to list!');
165: }
166:
167: if (isset($params['order'])) {
168: if($params['order'] == 'ASC' || $params['order'] == 'DESC')
169: $this->order = $params['order'];
170: else
171: throw new IllegalArguementException('Order value ['.$params['order'].'] provided is invalid!');
172: }
173:
174: if (isset($params['sort']))
175: $this->sort = $params['sort'];
176:
177: AlphaDAO::loadClassDef($BOname);
178:
179: 180: 181:
182: if($this->getCustomControllerName($BOname, 'list') != null)
183: $this->loadCustomController($BOname, 'list');
184:
185: $this->BO = new $BOname();
186: $this->BOView = AlphaView::getInstance($this->BO);
187:
188: echo AlphaView::displayPageHead($this);
189: }catch(IllegalArguementException $e) {
190: self::$logger->error($e->getMessage());
191: }
192:
193: $this->displayBodyContent();
194:
195: echo AlphaView::displayPageFoot($this);
196:
197: self::$logger->debug('<<doGET');
198: }
199:
200: 201: 202: 203: 204: 205:
206: public function doPOST($params) {
207: self::$logger->debug('>>doPOST($params=['.var_export($params, true).'])');
208:
209: try{
210:
211: if(!$this->checkSecurityFields()) {
212: throw new SecurityException('This page cannot accept post data from remote servers!');
213: self::$logger->debug('<<doPOST');
214: }
215:
216:
217: if (isset($params['bo'])) {
218: $BOname = $params['bo'];
219: $this->BOname = $BOname;
220: }elseif(isset($this->BOname)) {
221: $BOname = $this->BOname;
222: }else{
223: throw new IllegalArguementException('No BO available to list!');
224: }
225:
226: if (isset($params['order'])) {
227: if($params['order'] == 'ASC' || $params['order'] == 'DESC')
228: $this->order = $params['order'];
229: else
230: throw new IllegalArguementException('Order value ['.$params['order'].'] provided is invalid!');
231: }
232:
233: if (isset($params['sort']))
234: $this->sort = $params['sort'];
235:
236: AlphaDAO::loadClassDef($BOname);
237:
238: $this->BO = new $BOname();
239: $this->BOname = $BOname;
240: $this->BOView = AlphaView::getInstance($this->BO);
241:
242: echo AlphaView::displayPageHead($this);
243:
244: if (!empty($params['deleteOID'])) {
245: if(!AlphaValidator::isInteger($params['deleteOID']))
246: throw new IllegalArguementException('Invalid deleteOID ['.$params['deleteOID'].'] provided on the request!');
247:
248: try {
249: $temp = new $BOname();
250: $temp->load($params['deleteOID']);
251:
252: AlphaDAO::begin();
253: $temp->delete();
254: self::$logger->action('Deleted an instance of '.$BOname.' with id '.$params['deleteOID']);
255: AlphaDAO::commit();
256:
257: echo AlphaView::displayUpdateMessage($BOname.' '.$params['deleteOID'].' deleted successfully.');
258:
259: $this->displayBodyContent();
260: }catch(AlphaException $e) {
261: self::$logger->error($e->getMessage());
262: echo AlphaView::displayErrorMessage('Error deleting the BO of OID ['.$params['deleteOID'].'], check the log!');
263: AlphaDAO::rollback();
264: }
265:
266: AlphaDAO::disconnect();
267: }
268: }catch(SecurityException $e) {
269: echo AlphaView::displayErrorMessage($e->getMessage());
270: self::$logger->warn($e->getMessage());
271: }catch(IllegalArguementException $e) {
272: echo AlphaView::displayErrorMessage($e->getMessage());
273: self::$logger->error($e->getMessage());
274: }
275:
276: echo AlphaView::displayPageFoot($this);
277:
278: self::$logger->debug('<<doPOST');
279: }
280:
281: 282: 283: 284: 285:
286: public function before_displayPageHead_callback() {
287:
288: if(!isset($this->title))
289: $this->setTitle('Listing all '.$this->BOname);
290: if(!isset($this->description))
291: $this->setDescription('Page listing all '.$this->BOname.'.');
292: if(!isset($this->keywords))
293: $this->setKeywords('list,all,'.$this->BOname);
294:
295: if (isset($_GET['start']) ? $this->startPoint = $_GET['start']: $this->startPoint = 1);
296: }
297:
298: 299: 300: 301: 302: 303:
304: public function () {
305: $html = $this->renderPageLinks();
306:
307: $html .= '<br>';
308:
309: return $html;
310: }
311:
312: 313: 314: 315: 316: 317:
318: protected function renderPageLinks() {
319: global $config;
320:
321: $html = '';
322:
323: $end = (($this->startPoint-1)+$config->get('app.list.page.amount'));
324:
325: if($end > $this->BOCount)
326: $end = $this->BOCount;
327:
328: if($this->BOCount > 0)
329: $html .= '<p align="center">Displaying '.($this->startPoint).' to '.$end.' of <strong>'.$this->BOCount.'</strong>. ';
330: else
331: $html .= '<p align="center">The list is empty. ';
332:
333: if ($this->startPoint > 1) {
334:
335: if(isset($_GET['tk']))
336: $html .= '<a href="'.FrontController::generateSecureURL('act=ListAll&bo='.$this->BOname.'&start='.($this->startPoint-$config->get('app.list.page.amount'))).'"><<-Previous</a> ';
337: else
338: $html .= '<a href="'.$_SERVER["PHP_SELF"].'?bo='.$this->BOname."&start=".($this->startPoint-$config->get('app.list.page.amount')).'"><<-Previous</a> ';
339: }elseif($this->BOCount > $config->get('app.list.page.amount')){
340: $html .= '<<-Previous ';
341: }
342: $page = 1;
343: for ($i = 0; $i < $this->BOCount; $i+=$config->get('app.list.page.amount')) {
344: if($i != ($this->startPoint-1)) {
345:
346: if(isset($_GET['tk']))
347: $html .= ' <a href="'.FrontController::generateSecureURL('act=ListAll&bo='.$this->BOname.'&start='.($i+1)).'">'.$page.'</a> ';
348: else
349: $html .= ' <a href="'.$_SERVER["PHP_SELF"].'?bo='.$this->BOname."&start=".($i+1).'">'.$page.'</a> ';
350: }elseif($this->BOCount > $config->get('app.list.page.amount')){
351: $html .= ' '.$page.' ';
352: }
353: $page++;
354: }
355: if ($this->BOCount > $end) {
356:
357: if(isset($_GET['tk']))
358: $html .= ' <a href="'.FrontController::generateSecureURL('act=ListAll&bo='.$this->BOname.'&start='.($this->startPoint+$config->get('app.list.page.amount'))).'">Next->></a>';
359: else
360: $html .= ' <a href="'.$_SERVER["PHP_SELF"].'?bo='.$this->BOname."&start=".($this->startPoint+$config->get('app.list.page.amount')).
361: '">Next->></a>';
362: }elseif($this->BOCount > $config->get('app.list.page.amount')){
363: $html .= ' Next->>';
364: }
365: $html .= '</p>';
366:
367: return $html;
368: }
369:
370: 371: 372: 373: 374:
375: protected function displayBodyContent() {
376: global $config;
377:
378:
379: $temp = new $this->BOname;
380:
381: if(isset($this->filterField) && isset($this->filterValue)) {
382: if(isset($this->sort) && isset($this->order)) {
383: $objects = $temp->loadAllByAttribute($this->filterField, $this->filterValue, $this->startPoint-1, $config->get('app.list.page.amount'),
384: $this->sort, $this->order);
385: }else{
386: $objects = $temp->loadAllByAttribute($this->filterField, $this->filterValue, $this->startPoint-1, $config->get('app.list.page.amount'));
387: }
388:
389: $this->BOCount = $temp->getCount(array($this->filterField), array($this->filterValue));
390: }else{
391: if(isset($this->sort) && isset($this->order))
392: $objects = $temp->loadAll($this->startPoint-1, $config->get('app.list.page.amount'), $this->sort, $this->order);
393: else
394: $objects = $temp->loadAll($this->startPoint-1, $config->get('app.list.page.amount'));
395:
396: $this->BOCount = $temp->getCount();
397: }
398:
399: AlphaDAO::disconnect();
400:
401: echo AlphaView::renderDeleteForm();
402:
403: foreach($objects as $object) {
404: $temp = AlphaView::getInstance($object);
405: $temp->listView();
406: }
407: }
408:
409: 410: 411: 412: 413: 414:
415: public function after_displayPageHead_callback() {
416: $menu = '';
417:
418: if (isset($_SESSION['currentUser']) && AlphaDAO::isInstalled() && $_SESSION['currentUser']->inGroup('Admin') && strpos($_SERVER['REQUEST_URI'], '/tk/') !== false) {
419: $menu .= AlphaView::loadTemplateFragment('html', 'adminmenu.phtml', array());
420: }
421:
422: return $menu;
423: }
424: }
425:
426:
427: if(basename($_SERVER['PHP_SELF']) == 'ListAll.php') {
428: $controller = new ListAll();
429:
430: if(!empty($_POST)) {
431: $controller->doPOST($_REQUEST);
432: }else{
433: $controller->doGET($_GET);
434: }
435: }
436:
437: ?>