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

Source for file EditTags.php

Documentation is available at EditTags.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/Edit.php';
  10. require_once $config->get('sysRoot').'alpha/model/TagObject.inc';
  11. require_once $config->get('sysRoot').'alpha/controller/AlphaControllerInterface.inc';
  12. require_once $config->get('sysRoot').'alpha/exceptions/IllegalArguementException.inc';
  13. require_once $config->get('sysRoot').'alpha/exceptions/BONotFoundException.inc';
  14. require_once $config->get('sysRoot').'alpha/exceptions/FailedSaveException.inc';
  15. require_once $config->get('sysRoot').'alpha/view/AlphaView.inc';
  16.  
  17. /**
  18.  * 
  19.  * Controller used to edit TagObjects related to the BO indicated in the supplied
  20.  * GET vars (bo and oid).
  21.  * 
  22.  * @package alpha::controller
  23.  * @since 1.0
  24.  * @author John Collins <dev@alphaframework.org>
  25.  * @version $Id: EditTags.php 1453 2011-12-04 15:12:54Z johnc $
  26.  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  27.  * @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
  28.  *  All rights reserved.
  29.  * 
  30.  *  <pre>
  31.  *  Redistribution and use in source and binary forms, with or
  32.  *  without modification, are permitted provided that the
  33.  *  following conditions are met:
  34.  * 
  35.  *  * Redistributions of source code must retain the above
  36.  *    copyright notice, this list of conditions and the
  37.  *    following disclaimer.
  38.  *  * Redistributions in binary form must reproduce the above
  39.  *    copyright notice, this list of conditions and the
  40.  *    following disclaimer in the documentation and/or other
  41.  *    materials provided with the distribution.
  42.  *  * Neither the name of the Alpha Framework nor the names
  43.  *    of its contributors may be used to endorse or promote
  44.  *    products derived from this software without specific
  45.  *    prior written permission.
  46.  *   
  47.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  48.  *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  49.  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  50.  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  51.  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  52.  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53.  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  54.  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  55.  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  56.  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  57.  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  58.  *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  59.  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  60.  *  </pre>
  61.  *  
  62.  */
  63. class EditTags extends Edit implements AlphaControllerInterface {
  64.     /**
  65.      * Trace logger
  66.      * 
  67.      * @var Logger 
  68.      * @since 1.0
  69.      */
  70.     private static $logger null;
  71.     
  72.     /**
  73.      * constructor to set up the object
  74.      * 
  75.      * @since 1.0
  76.      */
  77.     public function __construct({
  78.         self::$logger new Logger('EditTags');
  79.         self::$logger->debug('>>__construct()');
  80.         
  81.         // ensure that the super class constructor is called, indicating the rights group
  82.         parent::__construct('Admin');
  83.         
  84.         // set up the title and meta details
  85.         $this->setTitle('Editing Tags');
  86.         $this->setDescription('Page to edit tags.');
  87.         $this->setKeywords('edit,tags');
  88.         
  89.         $this->BO = new TagObject();
  90.                 
  91.         self::$logger->debug('<<__construct');
  92.     }
  93.     
  94.     /**
  95.      * Handle GET requests
  96.      * 
  97.      * @param array $params 
  98.      * @throws IllegalArguementException
  99.      * @since 1.0
  100.      * @throws FileNotFoundException
  101.      */
  102.     public function doGET($params{
  103.         self::$logger->debug('>>doGET($params=['.var_export($paramstrue).'])');
  104.         
  105.         global $config;
  106.         
  107.         echo AlphaView::displayPageHead($this);
  108.         
  109.         // ensure that a bo is provided
  110.         if (isset($params['bo']))
  111.             $BOName $params['bo'];
  112.         else
  113.             throw new IllegalArguementException('Could not load the tag objects as a bo was not supplied!');
  114.         
  115.         // ensure that a OID is provided
  116.         if (isset($params['oid']))
  117.             $BOoid $params['oid'];
  118.         else
  119.             throw new IllegalArguementException('Could not load the tag objects as an oid was not supplied!');
  120.         
  121.         try {
  122.             AlphaDAO::loadClassDef($BOName);
  123.             $this->BO = new $BOName;
  124.             $this->BO->load($BOoid);
  125.             
  126.             $tags $this->BO->getPropObject('tags')->getRelatedObjects();
  127.             
  128.             AlphaDAO::disconnect();
  129.             
  130.             echo '<table cols="3" class="edit_view">';
  131.             echo '<form action="'.$_SERVER['REQUEST_URI'].'" method="POST">';
  132.             echo '<tr><td colspan="3"><h3>The following tags were found:</h3></td></tr>';
  133.             
  134.             foreach($tags as $tag{
  135.                 echo '<tr><td>';
  136.                 $labels $tag->getDataLabels();
  137.                 echo $labels['content'];
  138.                 echo '</td><td>';
  139.                 
  140.                 $temp new StringBox($tag->getPropObject('content')$labels['content']'content_'.$tag->getID()'');
  141.                 echo $temp->render(false);
  142.                 echo '</td><td>';
  143.                 
  144.                 $js "$('#dialogDiv').text('Are you sure you wish to delete this tag?');
  145.                             $('#dialogDiv').dialog({
  146.                             buttons: {
  147.                                 'OK': function(event, ui) {                        
  148.                                     $('#deleteOID').attr('value', '".$tag->getID()."');
  149.                                     $('#deleteForm').submit();
  150.                                 },
  151.                                 'Cancel': function(event, ui) {
  152.                                     $(this).dialog('close');
  153.                                 }
  154.                             }
  155.                         })
  156.                         $('#dialogDiv').dialog('open');
  157.                         return false;";
  158.                 $button new Button($js"Delete""delete".$tag->getID()."But");
  159.                 echo $button->render().'</td></tr>';
  160.             }
  161.             
  162.             echo '<tr><td colspan="3"><h3>Add a new tag:</h3></td></tr>';
  163.             echo '<tr><td>';            
  164.             echo 'New tag';
  165.             echo '</td><td>';
  166.             $temp new StringBox(new String()'New tag''new_value''');
  167.             echo $temp->render(false);
  168.             echo '</td><td></td></tr>';
  169.         
  170.             echo '<tr><td colspan="3">';
  171.         
  172.             $temp new Button('submit''Save''saveBut');
  173.             echo $temp->render();
  174.             echo '&nbsp;&nbsp;';
  175.             $temp new Button("document.location = '".FrontController::generateSecureURL('act=Edit&bo='.$params['bo'].'&oid='.$params['oid'])."'"'Back to Object''cancelBut');
  176.             echo $temp->render();
  177.             echo '</td></tr>';
  178.  
  179.             echo AlphaView::renderSecurityFields();
  180.         
  181.             echo '</form></table>';
  182.             
  183.             echo AlphaView::renderDeleteForm();
  184.             
  185.         }catch(BONotFoundException $e{
  186.             $msg 'Unable to load the BO of id ['.$params['oid'].'], error was ['.$e->getMessage().']';
  187.             self::$logger->error($msg);
  188.             throw new FileNotFoundException($msg);
  189.         }
  190.         
  191.         echo AlphaView::displayPageFoot($this);
  192.         
  193.         self::$logger->debug('<<doGET');
  194.     }
  195.     
  196.     /**
  197.      * Handle POST requests
  198.      * 
  199.      * @param array $params 
  200.      * @throws SecurityException
  201.      * @throws IllegalArguementException
  202.      * @since 1.0
  203.      */
  204.     public function doPOST($params{
  205.         self::$logger->debug('>>doPOST($params=['.var_export($paramstrue).'])');
  206.         
  207.         try {
  208.             // check the hidden security fields before accepting the form POST data
  209.             if(!$this->checkSecurityFields())
  210.                 throw new SecurityException('This page cannot accept post data from remote servers!');
  211.         
  212.             // ensure that a bo is provided
  213.             if (isset($params['bo']))
  214.                 $BOName $params['bo'];
  215.             else
  216.                 throw new IllegalArguementException('Could not load the tag objects as a bo was not supplied!');
  217.             
  218.             // ensure that a OID is provided
  219.             if (isset($params['oid']))
  220.                 $BOoid $params['oid'];
  221.             else
  222.                 throw new IllegalArguementException('Could not load the tag objects as a bo was not supplied!');
  223.             
  224.             if (isset($params['saveBut'])) {
  225.                 try {
  226.                     AlphaDAO::loadClassDef($BOName);
  227.                     $this->BO = new $BOName;
  228.                     $this->BO->load($BOoid);
  229.             
  230.                     $tags $this->BO->getPropObject('tags')->getRelatedObjects();
  231.             
  232.                     AlphaDAO::begin();
  233.                     
  234.                     foreach ($tags as $tag{
  235.                         $tag->set('content'TagObject::cleanTagContent($params['content_'.$tag->getID()]));
  236.                         $tag->save();
  237.                     }
  238.     
  239.                     // handle new tag if posted
  240.                     if(isset($params['new_value']&& trim($params['new_value']!= ''{
  241.                         $newTag new TagObject();
  242.                         $newTag->set('content'TagObject::cleanTagContent($params['new_value']));
  243.                         $newTag->set('taggedOID'$BOoid);
  244.                         $newTag->set('taggedClass'$BOName);
  245.                         $newTag->save();
  246.                     }
  247.                             
  248.                     AlphaDAO::commit();
  249.                     
  250.                     $this->setStatusMessage(AlphaView::displayUpdateMessage('Tags on '.get_class($this->BO).' '.$this->BO->getID().' saved successfully.'));
  251.                                         
  252.                     $this->doGET($params);
  253.                 }catch (ValidationException $e{
  254.                     /*
  255.                      * The unique key has most-likely been violated because this BO is already tagged with this
  256.                      * value.
  257.                      */
  258.                     AlphaDAO::rollback();
  259.                     
  260.                     $this->setStatusMessage(AlphaView::displayErrorMessage('Tags on '.get_class($this->BO).' '.$this->BO->getID().' not saved due to duplicate tag values, please try again.'));
  261.                     
  262.                     $this->doGET($params);
  263.                 }catch (FailedSaveException $e{
  264.                     self::$logger->error('Unable to save the tags of id ['.$params['oid'].'], error was ['.$e->getMessage().']');
  265.                     AlphaDAO::rollback();
  266.                     
  267.                     $this->setStatusMessage(AlphaView::displayErrorMessage('Tags on '.get_class($this->BO).' '.$this->BO->getID().' not saved, please check the application logs.'));
  268.                     
  269.                     $this->doGET($params);
  270.                 }
  271.                 
  272.                 AlphaDAO::disconnect();
  273.             }
  274.             
  275.             if (!empty($params['deleteOID'])) {                    
  276.                 try {
  277.                     AlphaDAO::loadClassDef($BOName);
  278.                     $this->BO = new $BOName;
  279.                     $this->BO->load($BOoid);
  280.                     
  281.                     $tag new TagObject();
  282.                     $tag->load($params['deleteOID']);
  283.                     $content $tag->get('content');
  284.                     
  285.                     AlphaDAO::begin();
  286.                     
  287.                     $tag->delete();
  288.                                 
  289.                     AlphaDAO::commit();
  290.                     
  291.                     $this->setStatusMessage(AlphaView::displayUpdateMessage('Tag <em>'.$content.'</em> on '.get_class($this->BO).' '.$this->BO->getID().' deleted successfully.'));                    
  292.                     
  293.                     $this->doGET($params);                                    
  294.                 }catch(AlphaException $e{
  295.                     self::$logger->error('Unable to delete the tag of id ['.$params['deleteOID'].'], error was ['.$e->getMessage().']');
  296.                     AlphaDAO::rollback();
  297.                     
  298.                     $this->setStatusMessage(AlphaView::displayErrorMessage('Tag <em>'.$content.'</em> on '.get_class($this->BO).' '.$this->BO->getID().' not deleted, please check the application logs.'));
  299.                     
  300.                     $this->doGET($params);
  301.                 }
  302.                 
  303.                 AlphaDAO::disconnect();
  304.             }
  305.         }catch(SecurityException $e{
  306.             
  307.             $this->setStatusMessage(AlphaView::displayErrorMessage($e->getMessage()));
  308.                                             
  309.             self::$logger->warn($e->getMessage());
  310.         }catch(IllegalArguementException $e{
  311.             self::$logger->error($e->getMessage());
  312.         }catch(BONotFoundException $e{
  313.             self::$logger->warn($e->getMessage());
  314.             
  315.             $this->setStatusMessage(AlphaView::displayErrorMessage('Failed to load the requested item from the database!'));
  316.         }
  317.                 
  318.         self::$logger->debug('<<doPOST');
  319.     }
  320.     
  321.     /**
  322.      * Using this callback to blank the new_value field when the page loads, regardless of anything being posted
  323.      * 
  324.      * @return string 
  325.      * @since 1.0
  326.      */
  327.     public function during_displayPageHead_callback({
  328.         $html '';
  329.         $html .= '<script language="javascript">';
  330.         $html .= 'function clearNewField() {';
  331.         $html .= '    document.getElementById("new_value").value = "";';
  332.         $html .= '}';
  333.         $html .= 'addOnloadEvent(clearNewField);';
  334.         $html .= '</script>';
  335.         return $html;
  336.     }
  337. }
  338.  
  339. // now build the new controller if this file is called directly
  340. if ('EditTags.php' == basename($_SERVER['PHP_SELF'])) {
  341.     $controller new EditTags();
  342.     
  343.     if(!empty($_POST)) {            
  344.         $controller->doPOST($_REQUEST);
  345.     }else{
  346.         $controller->doGET($_GET);
  347.     }
  348. }
  349.  
  350. ?>

Documentation generated on Tue, 13 Dec 2011 20:26:50 +0000 by phpDocumentor 1.4.3