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

Source for file EditArticle.php

Documentation is available at EditArticle.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/view/AlphaView.inc';
  10. require_once $config->get('sysRoot').'alpha/view/ViewState.inc';
  11. require_once $config->get('sysRoot').'alpha/controller/AlphaController.inc';
  12. require_once $config->get('sysRoot').'alpha/model/ArticleObject.inc';
  13. require_once $config->get('sysRoot').'alpha/controller/AlphaControllerInterface.inc';
  14.  
  15. /**
  16.  * 
  17.  * Controller used to edit an existing article
  18.  * 
  19.  * @package alpha::controller
  20.  * @since 1.0
  21.  * @author John Collins <dev@alphaframework.org>
  22.  * @version $Id: EditArticle.php 1341 2011-03-17 15:02:02Z johnc $
  23.  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  24.  * @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
  25.  *  All rights reserved.
  26.  * 
  27.  *  <pre>
  28.  *  Redistribution and use in source and binary forms, with or
  29.  *  without modification, are permitted provided that the
  30.  *  following conditions are met:
  31.  * 
  32.  *  * Redistributions of source code must retain the above
  33.  *    copyright notice, this list of conditions and the
  34.  *    following disclaimer.
  35.  *  * Redistributions in binary form must reproduce the above
  36.  *    copyright notice, this list of conditions and the
  37.  *    following disclaimer in the documentation and/or other
  38.  *    materials provided with the distribution.
  39.  *  * Neither the name of the Alpha Framework nor the names
  40.  *    of its contributors may be used to endorse or promote
  41.  *    products derived from this software without specific
  42.  *    prior written permission.
  43.  *   
  44.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  45.  *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  46.  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  47.  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  48.  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  49.  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  50.  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  51.  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  52.  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  53.  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  54.  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  55.  *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  56.  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  57.  *  </pre>
  58.  *  
  59.  */
  60. class EditArticle extends AlphaController implements AlphaControllerInterface {
  61.     /**
  62.      * The new article to be edited
  63.      * 
  64.      * @var ArticleObject 
  65.      * @since 1.0
  66.      */
  67.     protected $BO;
  68.                                 
  69.     /**
  70.      * Trace logger
  71.      * 
  72.      * @var Logger 
  73.      * @since 1.0
  74.      */
  75.     private static $logger null;
  76.     
  77.     /**
  78.      * constructor to set up the object
  79.      * 
  80.      * @since 1.0
  81.      */
  82.     public function __construct({
  83.         self::$logger new Logger('EditArticle');
  84.         self::$logger->debug('>>__construct()');
  85.         
  86.         global $config;
  87.         
  88.         // ensure that the super class constructor is called, indicating the rights group
  89.         parent::__construct('Standard');
  90.         
  91.         $this->BO = new ArticleObject();
  92.         
  93.         self::$logger->debug('<<__construct');
  94.     }
  95.     
  96.     /**
  97.      * Handle GET requests
  98.      * 
  99.      * @param array $params 
  100.      * @since 1.0
  101.      */
  102.     public function doGET($params{
  103.         self::$logger->debug('>>doGET(params=['.var_export($paramstrue).'])');
  104.         
  105.         try{
  106.             // load the business object (BO) definition
  107.             if (isset($params['oid'])) {                
  108.                 if(!AlphaValidator::isInteger($params['oid']))
  109.                     throw new IllegalArguementException('Article ID provided ['.$params['oid'].'] is not valid!');
  110.                 
  111.                 $this->BO->load($params['oid']);
  112.                 
  113.                 AlphaDAO::disconnect();
  114.                 
  115.                 $BOView AlphaView::getInstance($this->BO);
  116.                 
  117.                 // set up the title and meta details
  118.                 $this->setTitle($this->BO->get('title').' (editing)');
  119.                 $this->setDescription('Page to edit '.$this->BO->get('title').'.');
  120.                 $this->setKeywords('edit,article');
  121.                 
  122.                 echo AlphaView::displayPageHead($this);
  123.         
  124.                 echo $BOView->editView();
  125.             }else{
  126.                 throw new IllegalArguementException('No valid article ID provided!');
  127.             }
  128.         }catch(IllegalArguementException $e{
  129.             self::$logger->error($e->getMessage());
  130.         }catch(BONotFoundException $e{
  131.             self::$logger->warn($e->getMessage());
  132.             echo '<div class="ui-state-error ui-corner-all" style="padding: 0pt 0.7em;"> 
  133.                 <p><span class="ui-icon ui-icon-alert" style="float: left; margin-right: 0.3em;"></span> 
  134.                 <strong>Error:</strong> Failed to load the requested article from the database!</p></div>';
  135.         }
  136.         
  137.         echo AlphaView::renderDeleteForm();
  138.         
  139.         echo AlphaView::displayPageFoot($this);
  140.         
  141.         self::$logger->debug('<<doGET');
  142.     }
  143.     
  144.     /**
  145.      * Method to handle POST requests
  146.      * 
  147.      * @param array $params 
  148.      * @since 1.0
  149.      */
  150.     public function doPOST($params{
  151.         self::$logger->debug('>>doPOST(params=['.var_export($paramstrue).'])');
  152.         
  153.         global $config;
  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.                 self::$logger->debug('<<doPOST');
  160.             }
  161.             
  162.             if(isset($params['markdownTextBoxRows']&& $params['markdownTextBoxRows'!= ''{
  163.                 $viewState ViewState::getInstance();
  164.                 $viewState->set('markdownTextBoxRows'$params['markdownTextBoxRows']);
  165.             }
  166.  
  167.             if (isset($params['oid'])) {
  168.                 if(!AlphaValidator::isInteger($params['oid']))
  169.                     throw new IllegalArguementException('Article ID provided ['.$params['oid'].'] is not valid!');
  170.                                     
  171.                 $this->BO->load($params['oid']);
  172.                 
  173.                 $BOView AlphaView::getInstance($this->BO);
  174.                     
  175.                 // set up the title and meta details
  176.                 $this->setTitle($this->BO->get('title').' (editing)');
  177.                 $this->setDescription('Page to edit '.$this->BO->get('title').'.');
  178.                 $this->setKeywords('edit,article');
  179.                 
  180.                 echo AlphaView::displayPageHead($this);
  181.         
  182.                 if (isset($params['saveBut'])) {
  183.                                         
  184.                     // populate the transient object from post data
  185.                     $this->BO->populateFromPost();
  186.                     
  187.                     try {
  188.                         $success $this->BO->save();            
  189.                         echo AlphaView::displayUpdateMessage('Article '.$this->BO->getID().' saved successfully.');
  190.                     }catch (LockingException $e{
  191.                         $this->BO->reload();                        
  192.                         echo AlphaView::displayErrorMessage($e->getMessage());
  193.                     }
  194.  
  195.                     AlphaDAO::disconnect();
  196.                     echo $BOView->editView();
  197.                 }
  198.                 
  199.                 if (!empty($params['deleteOID'])) {
  200.                     
  201.                     $this->BO->load($params['deleteOID']);
  202.                     
  203.                     try {
  204.                         $this->BO->delete();
  205.                         
  206.                         AlphaDAO::disconnect();
  207.                                 
  208.                         echo AlphaView::displayUpdateMessage('Article '.$params['deleteOID'].' deleted successfully.');
  209.                                         
  210.                         echo '<center>';
  211.                         
  212.                         $temp new Button("document.location = '".FrontController::generateSecureURL('act=ListAll&bo='.get_class($this->BO))."'",
  213.                             'Back to List','cancelBut');
  214.                         echo $temp->render();
  215.                         
  216.                         echo '</center>';
  217.                     }catch(AlphaException $e{
  218.                         self::$logger->error($e->getTraceAsString());                        
  219.                         echo AlphaView::displayErrorMessage('Error deleting the article, check the log!');
  220.                     }
  221.                 }
  222.                 
  223.                 if(isset($params['uploadBut'])) {
  224.                                                 
  225.                     // upload the file to the attachments directory
  226.                     $success move_uploaded_file($_FILES['userfile']['tmp_name']$this->BO->getAttachmentsLocation().'/'.$_FILES['userfile']['name']);
  227.                     
  228.                     if(!$success)
  229.                         throw new AlphaException('Could not move the uploaded file ['.$_FILES['userfile']['name'].']');
  230.                     
  231.                     // set read/write permissions on the file
  232.                     $success chmod($this->BO->getAttachmentsLocation().'/'.$_FILES['userfile']['name']0666);
  233.                     
  234.                     if (!$success)
  235.                         throw new AlphaException('Unable to set read/write permissions on the uploaded file ['.$this->BO->getAttachmentsLocation().'/'.$_FILES['userfile']['name'].'].');
  236.                     
  237.                     if($success{                        
  238.                         echo AlphaView::displayUpdateMessage('File uploaded successfully.');
  239.                     }
  240.                     
  241.                     $view AlphaView::getInstance($this->BO);
  242.                 
  243.                     echo $view->editView();
  244.                 }
  245.                 
  246.                 if (!empty($params['file_to_delete'])) {
  247.                                                 
  248.                     $success unlink($this->BO->getAttachmentsLocation().'/'.$params['file_to_delete']);
  249.                     
  250.                     if(!$success)
  251.                         throw new AlphaException('Could not delete the file ['.$params['file_to_delete'].']');
  252.                     
  253.                     if($success{                        
  254.                         echo AlphaView::displayUpdateMessage($params['file_to_delete'].' deleted successfully.');
  255.                     }
  256.                     
  257.                     $view AlphaView::getInstance($this->BO);
  258.                 
  259.                     echo $view->editView();
  260.                 }
  261.             }else{
  262.                 throw new IllegalArguementException('No valid article ID provided!');
  263.             }
  264.         }catch(SecurityException $e{
  265.             echo AlphaView::displayErrorMessage($e->getMessage());
  266.             self::$logger->warn($e->getMessage());
  267.         }catch(IllegalArguementException $e{
  268.             echo AlphaView::displayErrorMessage($e->getMessage());
  269.             self::$logger->error($e->getMessage());
  270.         }catch(BONotFoundException $e{
  271.             self::$logger->warn($e->getMessage());
  272.             echo AlphaView::displayErrorMessage('Failed to load the requested article from the database!');
  273.         }catch(AlphaException $e{
  274.             echo AlphaView::displayErrorMessage($e->getMessage());
  275.             self::$logger->error($e->getMessage());
  276.         }
  277.         
  278.         echo AlphaView::renderDeleteForm();
  279.         
  280.         echo AlphaView::displayPageFoot($this);
  281.         
  282.         self::$logger->debug('<<doPOST');
  283.     }
  284.     
  285.     /**
  286.      * Renders the Javascript required in the header by markItUp!
  287.      *
  288.      * @return string 
  289.      * @since 1.0
  290.      */
  291.     public function during_displayPageHead_callback({
  292.         global $config;
  293.         
  294.         $html '
  295.             <script type="text/javascript">
  296.             var previewURL = "'.FrontController::generateSecureURL('act=PreviewArticle&bo=ArticleObject&oid='.$this->BO->getOID()).'";
  297.             </script>            
  298.             <script type="text/javascript" src="'.$config->get('sysURL').'alpha/lib/markitup/jquery.markitup.js"></script>
  299.             <script type="text/javascript" src="'.$config->get('sysURL').'alpha/lib/markitup/sets/markdown/set.js"></script>
  300.             <link rel="stylesheet" type="text/css" href="'.$config->get('sysURL').'alpha/lib/markitup/skins/simple/style.css" />
  301.             <link rel="stylesheet" type="text/css" href="'.$config->get('sysURL').'alpha/lib/markitup/sets/markdown/style.css" />
  302.             <script type="text/javascript">
  303.             $(document).ready(function() {
  304.                 $("#text_field_content_0").markItUp(mySettings);
  305.                 
  306.                 var dialogCoords = [(screen.width/2)-400, (screen.height/2)-300];
  307.                 
  308.                 var dialogOpts = {
  309.                     title: "Help Page",
  310.                     modal: true,
  311.                     resizable: false,
  312.                     draggable: false,
  313.                     autoOpen: false,
  314.                     height: 400,
  315.                     width: 800,
  316.                     position: dialogCoords,
  317.                     buttons: {},
  318.                     open: function() {
  319.                         //display correct dialog content
  320.                         $("#helpPage").load("'.FrontController::generateSecureURL('act=ViewArticleFile&file=Markdown_Help.text').'");
  321.                     },
  322.                     close: function() {
  323.                     
  324.                         $("#helpPage").dialog(dialogOpts);
  325.                         
  326.                         $(".markItUpButton15").click(
  327.                             function (){
  328.                                 $("#helpPage").dialog("open");
  329.                                 return false;
  330.                             }
  331.                         );
  332.                     }
  333.                 };
  334.                     
  335.                 $("#helpPage").dialog(dialogOpts);
  336.     
  337.                 $(".markItUpButton15").click(
  338.                     function (){
  339.                         $("#helpPage").dialog("open");
  340.                         return false;
  341.                     }
  342.                 );
  343.             });
  344.             </script>';
  345.         
  346.         return $html;
  347.     }
  348. }
  349.  
  350. // now build the new controller
  351. if(basename($_SERVER['PHP_SELF']== 'EditArticle.php'{
  352.     $controller new EditArticle();
  353.     
  354.     if(!empty($_POST)) {            
  355.         $controller->doPOST($_REQUEST);
  356.     }else{
  357.         $controller->doGET($_GET);
  358.     }
  359. }
  360.  
  361. ?>

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