      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 allow an admin to manage tags in the database
     14:  *
     15:  * @package alpha::controller
     16:  * @since 1.0
     17:  * @author John Collins <dev@alphaframework.org>
     18:  * @version $Id: TagManager.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 TagManager extends AlphaController implements AlphaControllerInterface {
     57:     /**
     58:      * Trace logger
     59:      *
     60:      * @var Logger
     61:      * @since 1.0
     62:      */
     63:     private static $logger = null;
     64:
     65:     /**
     66:      * Constructor to set up the object
     67:      *
     68:      * @since 1.0
     69:      */
     70:     public function __construct() {
     71:         self::$logger = new Logger('TagManager');
     72:         self::$logger->debug('>>__construct()');
     73:
     74:         global $config;
     75:
     76:         // ensure that the super class constructor is called, indicating the rights group
     77:         parent::__construct('Admin');
     78:
     79:         $this->setTitle('Tag Manager');
     80:
     81:         self::$logger->debug('<<__construct');
     82:     }
     83:
     84:     /**
     85:      * Handle GET requests
     86:      *
     87:      * @param array $params
     88:      * @since 1.0
     89:      */
     90:     public function doGET($params) {
     91:         self::$logger->debug('>>doGET($params=['.var_export($params, true).'])');
     92:
     93:         global $config;
     94:
     95:         echo AlphaView::displayPageHead($this);
     96:
     97:         $message = $this->getStatusMessage();
     98:         if(!empty($message))
     99:             echo $message;
    100:
    101:         echo '<h3>Listing business objects which are tagged</h3>';
    102:
    103:         $BOs = AlphaDAO::getBOClassNames();
    104:
    105:         foreach ($BOs as $BO) {
    106:             AlphaDAO::loadClassDef($BO);
    107:             $temp = new $BO;
    108:             if($temp->isTagged()) {
    109:                 $tag = new TagObject();
    110:                 $count = count($tag->loadAllByAttribute('taggedClass', $BO));
    111:                 echo '<h4>'.$temp->getFriendlyClassName().' object is tagged ('.$count.' tags found)</h4>';
    112:
    113:                 $fieldname = ($config->get('security.encrypt.http.fieldnames') ? base64_encode(AlphaSecurityUtils::encrypt('clearTaggedClass')) : 'clearTaggedClass');
    114:
    115:                 $js = "if(window.jQuery) {
    116:                     BootstrapDialog.show({
    117:                         title: 'Confirmation',
    118:                         message: 'Are you sure you want to delete all tags attached to the ".$temp->getFriendlyClassName()." class, and have them re-created?',
    119:                         buttons: [
    120:                             {
    121:                                 icon: 'glyphicon glyphicon-remove',
    122:                                 label: 'Cancel',
    123:                                 cssClass: 'btn btn-default btn-xs',
    124:                                 action: function(dialogItself){
    125:                                     dialogItself.close();
    126:                                 }
    127:                             },
    128:                             {
    129:                                 icon: 'glyphicon glyphicon-ok',
    130:                                 label: 'Okay',
    131:                                 cssClass: 'btn btn-default btn-xs',
    132:                                 action: function(dialogItself) {
    133:                                     $('[id=\"".$fieldname."\"]').attr('value', '".$BO."');
    134:                                     $('#clearForm').submit();
    135:                                     dialogItself.close();
    136:                                 }
    137:                             }
    138:                         ]
    139:                     });
    140:                 }";
    141:                 $button = new Button($js, "Re-create tags", "clearBut".$BO);
    142:
    143:                 echo $button->render();
    144:             }
    145:         }
    146:
    147:         AlphaDAO::disconnect();
    148:
    149:         echo '<form action="'.$_SERVER['REQUEST_URI'].'" method="POST" id="clearForm">';
    150:         echo '<input type="hidden" name="'.$fieldname.'" id="'.$fieldname.'"/>';
    151:         echo AlphaView::renderSecurityFields();
    152:         echo '</form>';
    153:
    154:         echo AlphaView::displayPageFoot($this);
    155:
    156:         self::$logger->debug('<<doGET');
    157:     }
    158:
    159:     /**
    160:      * Handle POST requests
    161:      *
    162:      * @param array $params
    163:      * @since 1.0
    164:      */
    165:     public function doPOST($params) {
    166:         self::$logger->debug('>>doPOST($params=['.var_export($params, true).'])');
    167:
    168:         try {
    169:             // check the hidden security fields before accepting the form POST data
    170:             if(!$this->checkSecurityFields())
    171:                 throw new SecurityException('This page cannot accept post data from remote servers!');
    172:
    173:             if (isset($params['clearTaggedClass']) && $params['clearTaggedClass'] != '') {
    174:                 try {
    175:                     self::$logger->info('About to start rebuilding the tags for the class ['.$params['clearTaggedClass'].']');
    176:                     $startTime = microtime(true);
    177:
    178:                     AlphaDAO::loadClassDef($params['clearTaggedClass']);
    179:                     $temp = new $params['clearTaggedClass'];
    180:                     $BOs = $temp->loadAll();
    181:
    182:                     self::$logger->info('Loaded all of the BOs (elapsed time ['.round(microtime(true)-$startTime, 5).'] seconds)');
    183:
    184:                     AlphaDAO::begin();
    185:
    186:                     $tag = new TagObject();
    187:                     $tag->deleteAllByAttribute('taggedClass', $params['clearTaggedClass']);
    188:
    189:                     self::$logger->info('Deleted all of the old tags (elapsed time ['.round(microtime(true)-$startTime, 5).'] seconds)');
    190:
    191:                     $this->regenerateTagsOnBOs($BOs);
    192:
    193:                     self::$logger->info('Saved all of the new tags (elapsed time ['.round(microtime(true)-$startTime, 5).'] seconds)');
    194:
    195:                     self::$logger->action('Tags recreated on the ['.$params['clearTaggedClass'].'] class');
    196:
    197:                     AlphaDAO::commit();
    198:                     $this->setStatusMessage(AlphaView::displayUpdateMessage('Tags recreated on the '.$temp->getFriendlyClassName().' class.'));
    199:
    200:                     self::$logger->info('Tags recreated on the ['.$params['clearTaggedClass'].'] class (time taken ['.round(microtime(true)-$startTime, 5).'] seconds).');
    201:                 }catch (AlphaException $e) {
    202:                     self::$logger->error($e->getMessage());
    203:                     AlphaDAO::rollback();
    204:                 }
    205:
    206:                 AlphaDAO::disconnect();
    207:             }
    208:
    209:             return $this->doGET($params);
    210:         }catch(SecurityException $e) {
    211:             $this->setStatusMessage(AlphaView::displayErrorMessage($e->getMessage()));
    212:
    213:             self::$logger->warn($e->getMessage());
    214:         }catch(IllegalArguementException $e) {
    215:             self::$logger->error($e->getMessage());
    216:             $this->setStatusMessage(AlphaView::displayErrorMessage($e->getMessage()));
    217:         }
    218:
    219:         echo AlphaView::displayPageHead($this);
    220:
    221:         $message = $this->getStatusMessage();
    222:         if(!empty($message))
    223:             echo $message;
    224:
    225:         echo AlphaView::displayPageFoot($this);
    226:         self::$logger->debug('<<doPOST');
    227:     }
    228:
    229:     /**
    230:      * Regenerates the tags on the supplied list of BOs
    231:      *
    232:      * @param array $BOs
    233:      * @since 1.0
    234:      */
    235:     private function regenerateTagsOnBOs($BOs) {
    236:         foreach ($BOs as $BO) {
    237:             foreach($BO->get('taggedAttributes') as $tagged) {
    238:
    239:                 $tags = TagObject::tokenize($BO->get($tagged), get_class($BO), $BO->getOID());
    240:
    241:                 foreach($tags as $tag) {
    242:                     try {
    243:                         $tag->save();
    244:                     }catch(ValidationException $e){
    245:                         /*
    246:                          * The unique key has most-likely been violated because this BO is already tagged with this
    247:                          * value, so we can ignore in this case.
    248:                          */
    249:                     }
    250:                 }
    251:             }
    252:         }
    253:     }
    254:
    255:     /**
    256:      * Use this callback to inject in the admin menu template fragment
    257:      *
    258:      * @since 1.2
    259:      */
    260:     public function after_displayPageHead_callback() {
    261:         $menu = AlphaView::loadTemplateFragment('html', 'adminmenu.phtml', array());
    262:
    263:         return $menu;
    264:     }
    265: }
    266:
    267: // now build the new controller if this file is called directly
    268: if ('TagManager.php' == basename($_SERVER['PHP_SELF'])) {
    269:     $controller = new TagManager();
    270:
    271:     if(!empty($_POST)) {
    272:         $controller->doPOST($_REQUEST);
    273:     }else{
    274:         $controller->doGET($_GET);
    275:     }
    276: }
    277:
    278: ?>