Alpha Framework alpha--controller
[ class tree: alpha--controller ] [ index: alpha--controller ] [ all elements ]

Source for file TagManager.php

Documentation is available at TagManager.php

  1. <?php
  2.  
  3. // include the config file
  4. if(!isset($config)) {
  5.     require_once '../util/AlphaConfig.inc';
  6.     $config AlphaConfig::getInstance();
  7. }
  8.  
  9. require_once $config->get('sysRoot').'alpha/controller/AlphaController.inc';
  10. require_once $config->get('sysRoot').'alpha/util/AlphaFileUtil.inc';
  11. require_once $config->get('sysRoot').'alpha/controller/AlphaControllerInterface.inc';
  12. require_once $config->get('sysRoot').'alpha/view/AlphaView.inc';
  13.  
  14. /**
  15.  * 
  16.  * Controller used to allow an admin to manage tags in the database
  17.  * 
  18.  * @package alpha::controller
  19.  * @since 1.0
  20.  * @author John Collins <dev@alphaframework.org>
  21.  * @version $Id: TagManager.php 1341 2011-03-17 15:02:02Z johnc $
  22.  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  23.  * @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
  24.  *  All rights reserved.
  25.  * 
  26.  *  <pre>
  27.  *  Redistribution and use in source and binary forms, with or
  28.  *  without modification, are permitted provided that the
  29.  *  following conditions are met:
  30.  * 
  31.  *  * Redistributions of source code must retain the above
  32.  *    copyright notice, this list of conditions and the
  33.  *    following disclaimer.
  34.  *  * Redistributions in binary form must reproduce the above
  35.  *    copyright notice, this list of conditions and the
  36.  *    following disclaimer in the documentation and/or other
  37.  *    materials provided with the distribution.
  38.  *  * Neither the name of the Alpha Framework nor the names
  39.  *    of its contributors may be used to endorse or promote
  40.  *    products derived from this software without specific
  41.  *    prior written permission.
  42.  *   
  43.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  44.  *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  45.  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  46.  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  47.  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  48.  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  49.  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  50.  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  51.  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  52.  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  53.  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  54.  *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  55.  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56.  *  </pre>
  57.  *  
  58.  */
  59. class TagManager extends AlphaController implements AlphaControllerInterface {    
  60.     /**
  61.      * Trace logger
  62.      * 
  63.      * @var Logger 
  64.      * @since 1.0
  65.      */
  66.     private static $logger null;
  67.     
  68.     /**
  69.      * constructor to set up the object
  70.      * 
  71.      * @since 1.0
  72.      */
  73.     public function __construct({
  74.         self::$logger new Logger('TagManager');
  75.         self::$logger->debug('>>__construct()');
  76.         
  77.         global $config;
  78.         
  79.         // ensure that the super class constructor is called, indicating the rights group
  80.         parent::__construct('Admin');
  81.         
  82.         $this->setTitle('Tag Manager');        
  83.         
  84.         self::$logger->debug('<<__construct');
  85.     }
  86.     
  87.     /**
  88.      * Handle GET requests
  89.      * 
  90.      * @param array $params 
  91.      * @since 1.0
  92.      */
  93.     public function doGET($params{
  94.         self::$logger->debug('>>doGET($params=['.var_export($paramstrue).'])');
  95.         
  96.         global $config;
  97.         
  98.         echo AlphaView::displayPageHead($this);
  99.         
  100.         echo '<h2>Listing business objects which are tagged</h2>';
  101.         
  102.         $BOs AlphaDAO::getBOClassNames();
  103.         
  104.         foreach ($BOs as $BO{
  105.             AlphaDAO::loadClassDef($BO);
  106.             $temp new $BO;
  107.             if($temp->isTagged()) {
  108.                 $tag new TagObject();
  109.                 $count count($tag->loadAllByAttribute('taggedClass'$BO));
  110.                 echo '<h3>'.$temp->getFriendlyClassName().' object is tagged ('.$count.' tags found)</h3>';
  111.                 
  112.                 $js "$('#dialogDiv').text('Are you sure you want to delete all tags attached to the ".$temp->getFriendlyClassName().
  113.                     " class, and have them re-created?');
  114.                         $('#dialogDiv').dialog({
  115.                         buttons: {
  116.                             'OK': function(event, ui) {                        
  117.                                 $('#clearTaggedClass').attr('value', '".$BO."');
  118.                                 $('#clearForm').submit();
  119.                             },
  120.                             'Cancel': function(event, ui) {
  121.                                 $(this).dialog('close');
  122.                             }
  123.                         }
  124.                     })
  125.                     $('#dialogDiv').dialog('open');
  126.                     return false;";
  127.                 $button new Button($js"Re-create tags""clearBut");
  128.                 
  129.                    echo $button->render();
  130.             }
  131.         }
  132.  
  133.         AlphaDAO::disconnect();
  134.         
  135.            echo '<form action="'.$_SERVER['REQUEST_URI'].'" method="POST" id="clearForm">';
  136.            echo '<input type="hidden" name="clearTaggedClass" id="clearTaggedClass"/>';
  137.            echo AlphaView::renderSecurityFields();
  138.            echo '</form>';
  139.         
  140.         echo AlphaView::displayPageFoot($this);
  141.         
  142.         self::$logger->debug('<<doGET');
  143.     }
  144.     
  145.     /**
  146.      * Handle POST requests
  147.      * 
  148.      * @param array $params 
  149.      * @since 1.0
  150.      * @throws ResourceNotAllowedException
  151.      */
  152.     public function doPOST($params{
  153.         self::$logger->debug('>>doPOST($params=['.var_export($paramstrue).'])');
  154.         
  155.         try {
  156.             // check the hidden security fields before accepting the form POST data
  157.             if(!$this->checkSecurityFields())
  158.                 throw new SecurityException('This page cannot accept post data from remote servers!');
  159.             
  160.             if (isset($params['clearTaggedClass']&& $params['clearTaggedClass'!= ''{
  161.                 try {
  162.                     self::$logger->info('About to start rebuilding the tags for the class ['.$params['clearTaggedClass'].']');
  163.                     $startTime microtime(true);
  164.                     
  165.                     AlphaDAO::loadClassDef($params['clearTaggedClass']);
  166.                     $temp new $params['clearTaggedClass'];
  167.                     $BOs $temp->loadAll();
  168.                     
  169.                     self::$logger->info('Loaded all of the BOs (elapsed time ['.round(microtime(true)-$startTime5).'] seconds)');
  170.                     
  171.                     AlphaDAO::begin();
  172.                     
  173.                     $tag new TagObject();
  174.                     $tag->deleteAllByAttribute('taggedClass'$params['clearTaggedClass']);
  175.                     
  176.                     self::$logger->info('Deleted all of the old tags (elapsed time ['.round(microtime(true)-$startTime5).'] seconds)');
  177.                     
  178.                     $this->regenerateTagsOnBOs($BOs);
  179.  
  180.                     self::$logger->info('Saved all of the new tags (elapsed time ['.round(microtime(true)-$startTime5).'] seconds)');
  181.                     
  182.                     AlphaDAO::commit();
  183.                     $this->setStatusMessage(AlphaView::displayUpdateMessage('Tags recreated on the '.$temp->getFriendlyClassName().' class.'));
  184.                     
  185.                     self::$logger->info('Tags recreated on the ['.$params['clearTaggedClass'].'] class (time taken ['.round(microtime(true)-$startTime5).'] seconds).');
  186.                 }catch (AlphaException $e{
  187.                     self::$logger->error($e->getMessage());
  188.                     AlphaDAO::rollback();
  189.                 }
  190.                 
  191.                 AlphaDAO::disconnect();
  192.             }
  193.             
  194.             $this->doGET($params);
  195.         }catch(SecurityException $e{
  196.             self::$logger->warn($e->getMessage());
  197.             throw new ResourceNotAllowedException($e->getMessage());
  198.         }catch(IllegalArguementException $e{
  199.             self::$logger->error($e->getMessage());
  200.             throw new ResourceNotFoundException($e->getMessage());
  201.         }
  202.         
  203.         echo AlphaView::displayPageFoot($this);
  204.         self::$logger->debug('<<doPOST');
  205.     }
  206.     
  207.     /**
  208.      * Regenerates the tags on the supplied list of BOs
  209.      * 
  210.      * @param array $BOs 
  211.      * @since 1.0
  212.      */
  213.     private function regenerateTagsOnBOs($BOs{
  214.         foreach ($BOs as $BO{
  215.             foreach($BO->get('taggedAttributes'as $tagged{
  216.                 
  217.                 $tags TagObject::tokenize($BO->get($tagged)get_class($BO)$BO->getOID());
  218.                 
  219.                 foreach($tags as $tag{
  220.                     try {
  221.                         $tag->save();
  222.                     }catch(ValidationException $e){
  223.                         /*
  224.                          * The unique key has most-likely been violated because this BO is already tagged with this
  225.                          * value, so we can ignore in this case.
  226.                          */
  227.                     }
  228.                 }
  229.             }
  230.         }
  231.     }
  232. }
  233.  
  234. // now build the new controller if this file is called directly
  235. if ('TagManager.php' == basename($_SERVER['PHP_SELF'])) {
  236.     $controller new TagManager();
  237.     
  238.     if(!empty($_POST)) {            
  239.         $controller->doPOST($_REQUEST);
  240.     }else{
  241.         $controller->doGET($_GET);
  242.     }
  243. }
  244.  
  245. ?>

Documentation generated on Thu, 17 Mar 2011 16:44:55 +0000 by phpDocumentor 1.4.3