      1: <?php
      2:
      3: // include the config file
      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:  * Controller used to list a BO, which must be supplied in GET vars
     14:  *
     15:  * @package alpha::controller
     16:  * @since 1.0
     17:  * @author John Collins <dev@alphaframework.org>
     18:  * @version $Id: ListAll.php 1745 2014-03-29 15:19:05Z alphadevx $
     19:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
     20:  * @copyright Copyright (c) 2014, John Collins (founder of Alpha Framework).
     21:  * All rights reserved.
     22:  *
     23:  * <pre>
     24:  * Redistribution and use in source and binary forms, with or
     25:  * without modification, are permitted provided that the
     26:  * following conditions are met:
     27:  *
     28:  * * Redistributions of source code must retain the above
     29:  *   copyright notice, this list of conditions and the
     30:  *   following disclaimer.
     31:  * * Redistributions in binary form must reproduce the above
     32:  *   copyright notice, this list of conditions and the
     33:  *   following disclaimer in the documentation and/or other
     34:  *   materials provided with the distribution.
     35:  * * Neither the name of the Alpha Framework nor the names
     36:  *   of its contributors may be used to endorse or promote
     37:  *   products derived from this software without specific
     38:  *   prior written permission.
     39:  *
     40:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
     41:  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
     42:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     43:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     44:  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     45:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     46:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     47:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     48:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     49:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     50:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     51:  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     52:  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     53:  * </pre>
     54:  *
     55:  */
     56: class ListAll extends AlphaController implements AlphaControllerInterface {
     57:     /**
     58:      * The name of the BO
     59:      *
     60:      * @var string
     61:      * @since 1.0
     62:      */
     63:     protected $BOname;
     64:
     65:     /**
     66:      * The new default AlphaView object used for rendering the onjects to list
     67:      *
     68:      * @var AlphaView
     69:      * @since 1.0
     70:      */
     71:     protected $BOView;
     72:
     73:     /**
     74:      * The start number for list pageination
     75:      *
     76:      * @var integer
     77:      * @since 1.0
     78:      */
     79:     protected $startPoint;
     80:
     81:     /**
     82:      * The count of the BOs of this type in the database
     83:      *
     84:      * @var integer
     85:      * @since 1.0
     86:      */
     87:     protected $BOCount = 0;
     88:
     89:     /**
     90:      * The field name to sort the list by (optional, default is OID)
     91:      *
     92:      * @var string
     93:      * @since 1.0
     94:      */
     95:     protected $sort;
     96:
     97:     /**
     98:      * The order to sort the list by (optional, should be ASC or DESC, default is ASC)
     99:      *
    100:      * @var string
    101:      * @since 1.0
    102:      */
    103:     protected $order;
    104:
    105:     /**
    106:      * The name of the BO field to filter the list by (optional)
    107:      *
    108:      * @var string
    109:      * @since 1.0
    110:      */
    111:     protected $filterField;
    112:
    113:     /**
    114:      * The value of the filterField to filter by (optional)
    115:      *
    116:      * @var string
    117:      * @since 1.0
    118:      */
    119:     protected $filterValue;
    120:
    121:     /**
    122:      * Trace logger
    123:      *
    124:      * @var Logger
    125:      * @since 1.0
    126:      */
    127:     private static $logger = null;
    128:
    129:     /**
    130:      * Constructor to set up the object
    131:      *
    132:      * @param string $visibility
    133:      * @since 1.0
    134:      */
    135:     public function __construct($visibility='Admin') {
    136:         self::$logger = new Logger('ListAll');
    137:         self::$logger->debug('>>__construct()');
    138:
    139:         global $config;
    140:
    141:         // ensure that the super class constructor is called, indicating the rights group
    142:         parent::__construct($visibility);
    143:
    144:         self::$logger->debug('<<__construct');
    145:     }
    146:
    147:     /**
    148:      * Handle GET requests
    149:      *
    150:      * @param array $params
    151:      * @since 1.0
    152:      */
    153:     public function doGET($params) {
    154:         self::$logger->debug('>>doGET($params=['.var_export($params, true).'])');
    155:
    156:         try{
    157:             // load the business object (BO) definition
    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:              *  check and see if a custom create controller exists for this BO, and if it does use it otherwise continue
    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:      * Handle POST requests
    202:      *
    203:      * @param array $params
    204:      * @since 1.0
    205:      */
    206:     public function doPOST($params) {
    207:         self::$logger->debug('>>doPOST($params=['.var_export($params, true).'])');
    208:
    209:         try{
    210:             // check the hidden security fields before accepting the form POST data
    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:             // load the business object (BO) definition
    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:      * Sets up the title etc. and pagination start point
    283:      *
    284:      * @since 1.0
    285:      */
    286:     public function before_displayPageHead_callback() {
    287:         // set up the title and meta details
    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:         // set the start point for the list pagination
    295:         if (isset($_GET['start']) ? $this->startPoint = $_GET['start']: $this->startPoint = 1);
    296:     }
    297:
    298:     /**
    299:      * Method to display the page footer with pageination links
    300:      *
    301:      * @return string
    302:      * @since 1.0
    303:      */
    304:     public function before_displayPageFoot_callback() {
    305:         $html = $this->renderPageLinks();
    306:
    307:         $html .= '<br>';
    308:
    309:         return $html;
    310:     }
    311:
    312:     /**
    313:      * Method for rendering the pagination links
    314:      *
    315:      * @return string
    316:      * @since 1.0
    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 .= '<ul class="pagination">';
    330:         }else{
    331:             $html .= '<p align="center">The list is empty.&nbsp;&nbsp;</p>';
    332:
    333:             return $html;
    334:         }
    335:
    336:         if ($this->startPoint > 1) {
    337:             // handle secure URLs
    338:             if(isset($_GET['tk']))
    339:                 $html .= '<li><a href="'.FrontController::generateSecureURL('act=ListAll&bo='.$this->BOname.'&start='.($this->startPoint-$config->get('app.list.page.amount'))).'">&lt;&lt;-Previous</a></li>';
    340:             else
    341:                 $html .= '<li><a href="'.$_SERVER["PHP_SELF"].'?bo='.$this->BOname."&start=".($this->startPoint-$config->get('app.list.page.amount')).'">&lt;&lt;-Previous</a></li>';
    342:         }elseif($this->BOCount > $config->get('app.list.page.amount')){
    343:             $html .= '<li class="disabled"><a href="#">&lt;&lt;-Previous</a></li>';
    344:         }
    345:         $page = 1;
    346:         for ($i = 0; $i < $this->BOCount; $i+=$config->get('app.list.page.amount')) {
    347:             if($i != ($this->startPoint-1)) {
    348:                 // handle secure URLs
    349:                 if(isset($_GET['tk']))
    350:                     $html .= '<li><a href="'.FrontController::generateSecureURL('act=ListAll&bo='.$this->BOname.'&start='.($i+1)).'">'.$page.'</a></li>';
    351:                 else
    352:                     $html .= '<li><a href="'.$_SERVER["PHP_SELF"].'?bo='.$this->BOname."&start=".($i+1).'">'.$page.'</a></li>';
    353:             }elseif($this->BOCount > $config->get('app.list.page.amount')){
    354:                 $html .= '<li class="active"><a href="#">'.$page.'</a></li>';
    355:             }
    356:             $page++;
    357:         }
    358:         if ($this->BOCount > $end) {
    359:             // handle secure URLs
    360:             if(isset($_GET['tk']))
    361:                 $html .= '<li><a href="'.FrontController::generateSecureURL('act=ListAll&bo='.$this->BOname.'&start='.($this->startPoint+$config->get('app.list.page.amount'))).'">Next-&gt;&gt;</a></li>';
    362:             else
    363:                 $html .= '<li><a href="'.$_SERVER["PHP_SELF"].'?bo='.$this->BOname."&start=".($this->startPoint+$config->get('app.list.page.amount')).
    364:                     '">Next-&gt;&gt;</a></li>';
    365:         }elseif($this->BOCount > $config->get('app.list.page.amount')){
    366:             $html .= '<li class="disabled"><a href="#">Next-&gt;&gt;</a></li>';
    367:         }
    368:         $html .= '</ul>';
    369:
    370:         return $html;
    371:     }
    372:
    373:     /**
    374:      * Method to display the main body HTML for this page
    375:      *
    376:      * @since 1.0
    377:      */
    378:     protected function displayBodyContent() {
    379:         global $config;
    380:
    381:         // get all of the BOs and invoke the listView on each one
    382:         $temp = new $this->BOname;
    383:
    384:         if(isset($this->filterField) && isset($this->filterValue)) {
    385:             if(isset($this->sort) && isset($this->order)) {
    386:                 $objects = $temp->loadAllByAttribute($this->filterField, $this->filterValue, $this->startPoint-1, $config->get('app.list.page.amount'),
    387:                     $this->sort, $this->order);
    388:             }else{
    389:                 $objects = $temp->loadAllByAttribute($this->filterField, $this->filterValue, $this->startPoint-1, $config->get('app.list.page.amount'));
    390:             }
    391:
    392:             $this->BOCount = $temp->getCount(array($this->filterField), array($this->filterValue));
    393:         }else{
    394:             if(isset($this->sort) && isset($this->order))
    395:                 $objects = $temp->loadAll($this->startPoint-1, $config->get('app.list.page.amount'), $this->sort, $this->order);
    396:             else
    397:                 $objects = $temp->loadAll($this->startPoint-1, $config->get('app.list.page.amount'));
    398:
    399:             $this->BOCount = $temp->getCount();
    400:         }
    401:
    402:         AlphaDAO::disconnect();
    403:
    404:         echo AlphaView::renderDeleteForm();
    405:
    406:         foreach($objects as $object) {
    407:             $temp = AlphaView::getInstance($object);
    408:             $temp->listView();
    409:         }
    410:     }
    411:
    412:     /**
    413:      * Use this callback to inject in the admin menu template fragment for admin users of
    414:      * the backend only.
    415:      *
    416:      * @since 1.2
    417:      */
    418:     public function after_displayPageHead_callback() {
    419:         $menu = '';
    420:
    421:         if (isset($_SESSION['currentUser']) && AlphaDAO::isInstalled() && $_SESSION['currentUser']->inGroup('Admin') && mb_strpos($_SERVER['REQUEST_URI'], '/tk/') !== false) {
    422:             $menu .= AlphaView::loadTemplateFragment('html', 'adminmenu.phtml', array());
    423:         }
    424:
    425:         return $menu;
    426:     }
    427: }
    428:
    429: // now build the new controller
    430: if(basename($_SERVER['PHP_SELF']) == 'ListAll.php') {
    431:     $controller = new ListAll();
    432:
    433:     if(!empty($_POST)) {
    434:         $controller->doPOST($_REQUEST);
    435:     }else{
    436:         $controller->doGET($_GET);
    437:     }
    438: }
    439:
    440: ?>