      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 1548 2012-07-29 17:07:07Z alphadevx $
     19:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
     20:  * @copyright Copyright (c) 2012, 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:         echo '<h2>Listing business objects which are tagged</h2>';
     98:
     99:         $BOs = AlphaDAO::getBOClassNames();
    100:
    101:         foreach ($BOs as $BO) {
    102:             AlphaDAO::loadClassDef($BO);
    103:             $temp = new $BO;
    104:             if($temp->isTagged()) {
    105:                 $tag = new TagObject();
    106:                 $count = count($tag->loadAllByAttribute('taggedClass', $BO));
    107:                 echo '<h3>'.$temp->getFriendlyClassName().' object is tagged ('.$count.' tags found)</h3>';
    108:
    109:                 $js = "$('#dialogDiv').text('Are you sure you want to delete all tags attached to the ".$temp->getFriendlyClassName().
    110:                     " class, and have them re-created?');
    111:                         $('#dialogDiv').dialog({
    112:                         buttons: {
    113:                             'OK': function(event, ui) {
    114:                                 $('#clearTaggedClass').attr('value', '".$BO."');
    115:                                 $('#clearForm').submit();
    116:                             },
    117:                             'Cancel': function(event, ui) {
    118:                                 $(this).dialog('close');
    119:                             }
    120:                         }
    121:                     })
    122:                     $('#dialogDiv').dialog('open');
    123:                     return false;";
    124:                 $button = new Button($js, "Re-create tags", "clearBut".$BO);
    125:
    126:                 echo $button->render();
    127:             }
    128:         }
    129:
    130:         AlphaDAO::disconnect();
    131:
    132:         echo '<form action="'.$_SERVER['REQUEST_URI'].'" method="POST" id="clearForm">';
    133:         echo '<input type="hidden" name="clearTaggedClass" id="clearTaggedClass"/>';
    134:         echo AlphaView::renderSecurityFields();
    135:         echo '</form>';
    136:
    137:         echo AlphaView::displayPageFoot($this);
    138:
    139:         self::$logger->debug('<<doGET');
    140:     }
    141:
    142:     /**
    143:      * Handle POST requests
    144:      *
    145:      * @param array $params
    146:      * @since 1.0
    147:      * @throws ResourceNotAllowedException
    148:      */
    149:     public function doPOST($params) {
    150:         self::$logger->debug('>>doPOST($params=['.var_export($params, true).'])');
    151:
    152:         try {
    153:             // check the hidden security fields before accepting the form POST data
    154:             if(!$this->checkSecurityFields())
    155:                 throw new SecurityException('This page cannot accept post data from remote servers!');
    156:
    157:             if (isset($params['clearTaggedClass']) && $params['clearTaggedClass'] != '') {
    158:                 try {
    159:                     self::$logger->info('About to start rebuilding the tags for the class ['.$params['clearTaggedClass'].']');
    160:                     $startTime = microtime(true);
    161:
    162:                     AlphaDAO::loadClassDef($params['clearTaggedClass']);
    163:                     $temp = new $params['clearTaggedClass'];
    164:                     $BOs = $temp->loadAll();
    165:
    166:                     self::$logger->info('Loaded all of the BOs (elapsed time ['.round(microtime(true)-$startTime, 5).'] seconds)');
    167:
    168:                     AlphaDAO::begin();
    169:
    170:                     $tag = new TagObject();
    171:                     $tag->deleteAllByAttribute('taggedClass', $params['clearTaggedClass']);
    172:
    173:                     self::$logger->info('Deleted all of the old tags (elapsed time ['.round(microtime(true)-$startTime, 5).'] seconds)');
    174:
    175:                     $this->regenerateTagsOnBOs($BOs);
    176:
    177:                     self::$logger->info('Saved all of the new tags (elapsed time ['.round(microtime(true)-$startTime, 5).'] seconds)');
    178:
    179:                     AlphaDAO::commit();
    180:                     $this->setStatusMessage(AlphaView::displayUpdateMessage('Tags recreated on the '.$temp->getFriendlyClassName().' class.'));
    181:
    182:                     self::$logger->info('Tags recreated on the ['.$params['clearTaggedClass'].'] class (time taken ['.round(microtime(true)-$startTime, 5).'] seconds).');
    183:                 }catch (AlphaException $e) {
    184:                     self::$logger->error($e->getMessage());
    185:                     AlphaDAO::rollback();
    186:                 }
    187:
    188:                 AlphaDAO::disconnect();
    189:             }
    190:
    191:             $this->doGET($params);
    192:         }catch(SecurityException $e) {
    193:             self::$logger->warn($e->getMessage());
    194:             throw new ResourceNotAllowedException($e->getMessage());
    195:         }catch(IllegalArguementException $e) {
    196:             self::$logger->error($e->getMessage());
    197:             throw new ResourceNotFoundException($e->getMessage());
    198:         }
    199:
    200:         echo AlphaView::displayPageFoot($this);
    201:         self::$logger->debug('<<doPOST');
    202:     }
    203:
    204:     /**
    205:      * Regenerates the tags on the supplied list of BOs
    206:      *
    207:      * @param array $BOs
    208:      * @since 1.0
    209:      */
    210:     private function regenerateTagsOnBOs($BOs) {
    211:         foreach ($BOs as $BO) {
    212:             foreach($BO->get('taggedAttributes') as $tagged) {
    213:
    214:                 $tags = TagObject::tokenize($BO->get($tagged), get_class($BO), $BO->getOID());
    215:
    216:                 foreach($tags as $tag) {
    217:                     try {
    218:                         $tag->save();
    219:                     }catch(ValidationException $e){
    220:                         /*
    221:                          * The unique key has most-likely been violated because this BO is already tagged with this
    222:                          * value, so we can ignore in this case.
    223:                          */
    224:                     }
    225:                 }
    226:             }
    227:         }
    228:     }
    229:
    230:     /**
    231:      * Use this callback to inject in the admin menu template fragment
    232:      *
    233:      * @since 1.2
    234:      */
    235:     public function after_displayPageHead_callback() {
    236:         $menu = AlphaView::loadTemplateFragment('html', 'adminmenu.phtml', array());
    237:
    238:         return $menu;
    239:     }
    240: }
    241:
    242: // now build the new controller if this file is called directly
    243: if ('TagManager.php' == basename($_SERVER['PHP_SELF'])) {
    244:     $controller = new TagManager();
    245:
    246:     if(!empty($_POST)) {
    247:         $controller->doPOST($_REQUEST);
    248:     }else{
    249:         $controller->doGET($_GET);
    250:     }
    251: }
    252:
    253: ?>