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

Source for file ViewArticle.php

Documentation is available at ViewArticle.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/controller/AlphaController.inc';
  11. require_once $config->get('sysRoot').'alpha/model/ArticleObject.inc';
  12. require_once $config->get('sysRoot').'alpha/util/InputFilter.inc';
  13. require_once $config->get('sysRoot').'alpha/util/helpers/AlphaValidator.inc';
  14. require_once $config->get('sysRoot').'alpha/controller/AlphaControllerInterface.inc';
  15.  
  16. /**
  17.  * 
  18.  * Controller used to display a Markdown version of an article
  19.  * 
  20.  * @package alpha::controller
  21.  * @since 1.0
  22.  * @author John Collins <dev@alphaframework.org>
  23.  * @version $Id: ViewArticle.php 1453 2011-12-04 15:12:54Z johnc $
  24.  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  25.  * @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
  26.  *  All rights reserved.
  27.  * 
  28.  *  <pre>
  29.  *  Redistribution and use in source and binary forms, with or
  30.  *  without modification, are permitted provided that the
  31.  *  following conditions are met:
  32.  * 
  33.  *  * Redistributions of source code must retain the above
  34.  *    copyright notice, this list of conditions and the
  35.  *    following disclaimer.
  36.  *  * Redistributions in binary form must reproduce the above
  37.  *    copyright notice, this list of conditions and the
  38.  *    following disclaimer in the documentation and/or other
  39.  *    materials provided with the distribution.
  40.  *  * Neither the name of the Alpha Framework nor the names
  41.  *    of its contributors may be used to endorse or promote
  42.  *    products derived from this software without specific
  43.  *    prior written permission.
  44.  *   
  45.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  46.  *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  47.  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  48.  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  49.  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  50.  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  51.  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  52.  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  53.  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  54.  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  55.  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  56.  *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  57.  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  58.  *  </pre>
  59.  *  
  60.  */
  61. class ViewArticle extends AlphaController implements AlphaControllerInterface {
  62.     /**
  63.      * The article to be rendered
  64.      * 
  65.      * @var ArticleObject 
  66.      * @since 1.0
  67.      */
  68.     protected $BO;
  69.     
  70.     /**
  71.      * Trace logger
  72.      * 
  73.      * @var Logger 
  74.      * @since 1.0
  75.      */
  76.     private static $logger null;
  77.     
  78.     /**
  79.      * constructor to set up the object
  80.      * 
  81.      * @since 1.0
  82.      */
  83.     public function __construct({
  84.         self::$logger new Logger('ViewArticle');
  85.         self::$logger->debug('>>__construct()');
  86.         
  87.         global $config;
  88.         
  89.         // ensure that the super class constructor is called, indicating the rights group
  90.         parent::__construct('Public');
  91.         
  92.         $this->BO = new ArticleObject();
  93.         
  94.         self::$logger->debug('<<__construct');
  95.     }
  96.                                 
  97.     /**
  98.      * Handle GET requests
  99.      * 
  100.      * @param array $params 
  101.      * @since 1.0
  102.      * @throws ResourceNotFoundException
  103.      */
  104.     public function doGET($params{
  105.         global $config;
  106.         
  107.         try{
  108.             // check to see if we need to force a re-direct to the mod_rewrite alias URL for the article
  109.             if($config->get('sysForceModRewriteURLs'&& basename($_SERVER['PHP_SELF']== 'ViewArticle.php'{
  110.                 // set the correct HTTP header for the response
  111.                 header('HTTP/1.1 301 Moved Permanently');
  112.                 
  113.                 header('Location: '.$this->BO->get('URL'));
  114.      
  115.                 // we're done here
  116.                 exit;
  117.             }
  118.             
  119.             // load the business object (BO) definition
  120.             if (isset($params['oid']&& AlphaValidator::isInteger($params['oid'])) {
  121.                 $this->BO->load($params['oid']);
  122.                 
  123.                 $BOView AlphaView::getInstance($this->BO);
  124.                 
  125.                 // set up the title and meta details
  126.                 $this->setTitle($this->BO->get('title'));
  127.                 $this->setDescription($this->BO->get('description'));
  128.                 
  129.                 echo AlphaView::displayPageHead($this);
  130.         
  131.                 echo $BOView->markdownView();
  132.             }else{
  133.                 throw new IllegalArguementException('No article available to view!');
  134.             }
  135.         }catch(IllegalArguementException $e{
  136.             self::$logger->error($e->getMessage());
  137.             throw new ResourceNotFoundException($e->getMessage());
  138.         }catch(BONotFoundException $e{
  139.             self::$logger->warn($e->getMessage());
  140.             throw new ResourceNotFoundException('The article that you have requested cannot be found!');
  141.         }
  142.         
  143.         echo AlphaView::displayPageFoot($this);
  144.     }
  145.     
  146.     /**
  147.      * Callback used to inject ArticleObject headerContent into the page
  148.      *
  149.      * @return string 
  150.      * @since 1.0
  151.      */
  152.     public function during_displayPageHead_callback({
  153.         return $this->BO->get('headerContent');
  154.     }
  155.     
  156.     /**
  157.      * Callback that inserts the CMS level header
  158.      * 
  159.      * @return string 
  160.      * @since 1.0
  161.      */
  162.     public function insert_CMSDisplayStandardHeader_callback({
  163.         global $config;
  164.         
  165.         $html '';
  166.         
  167.         if($config->get('sysCMSDisplayStandardHeader')) {
  168.             $html.= '<p><a href="'.$config->get('sysURL').'">'.$config->get('sysTitle').'</a> &nbsp; &nbsp;';
  169.             $html.= 'Date Added: <em>'.$this->BO->getCreateTS()->getDate().'</em> &nbsp; &nbsp;';
  170.             $html.= 'Last Updated: <em>'.$this->BO->getUpdateTS()->getDate().'</em> &nbsp; &nbsp;';
  171.             $html.= 'Revision: <em>'.$this->BO->getVersion().'</em></p>';
  172.         }
  173.         
  174.         $html.= $config->get('sysCMSHeader');
  175.         
  176.         return $html;
  177.     }
  178.     
  179.     /**
  180.      * Callback used to render footer content, including comments, votes and print/PDF buttons when
  181.      * enabled to do so.
  182.      * 
  183.      * @return string 
  184.      * @since 1.0
  185.      */
  186.     public function before_displayPageFoot_callback({
  187.         global $config;
  188.         
  189.         $html '';
  190.         
  191.         if($config->get('sysCMSDisplayComments'))
  192.             $html .= $this->renderComments();
  193.         
  194.         if($config->get('sysCMSDisplayTags')) {
  195.             $tags $this->BO->getPropObject('tags')->getRelatedObjects();
  196.             
  197.             if(count($tags0{
  198.                 $html .= '<p>Tags:';
  199.                 
  200.                 foreach($tags as $tag)
  201.                     $html .= ' <a href="'.$config->get('sysURL').'search/q/'.$tag->get('content').'">'.$tag->get('content').'</a>';
  202.                 $html .= '</p>';
  203.             }
  204.         }
  205.         
  206.         if($config->get('sysCMSDisplayVotes')) {
  207.             $rating $this->BO->getArticleScore();
  208.             $votes $this->BO->getArticleVotes();
  209.             $html .= '<p>Average Article User Rating: <strong>'.$rating.'</strong> out of 10 (based on <strong>'.count($votes).'</strong> votes)</p>';
  210.         }
  211.         
  212.         if(!$this->BO->checkUserVoted(&& $config->get('sysCMSVotingAllowed')) {
  213.             $html .= '<form action="'.$_SERVER['REQUEST_URI'].'" method="post">';
  214.             $html .= '<p>Please rate this article from 1-10 (10 being the best):' .
  215.                     '<select name="user_vote">' .
  216.                     '<option value="1">1' .
  217.                     '<option value="2">2' .
  218.                     '<option value="3">3' .
  219.                     '<option value="4">4' .
  220.                     '<option value="5">5' .
  221.                     '<option value="6">6' .
  222.                     '<option value="7">7' .
  223.                     '<option value="8">8' .
  224.                     '<option value="9">9' .
  225.                     '<option value="10">10' .
  226.                     '</select></p>&nbsp;&nbsp;';
  227.             $temp new Button('submit','Vote!','voteBut');
  228.             $html .= $temp->render();
  229.             
  230.             $html .= AlphaView::renderSecurityFields();
  231.             $html .= '<form>';
  232.         }
  233.         
  234.         AlphaDAO::disconnect();
  235.         
  236.         $html .= '&nbsp;&nbsp;';
  237.         $temp new Button("window.open('".$this->BO->get('printURL')."')",'Open Printer Version','printBut');
  238.         $html .= $temp->render();
  239.         
  240.         $html .= '&nbsp;&nbsp;';
  241.         if($config->get('sysAllowPDFVersions')) {
  242.             $temp new Button("document.location = '".FrontController::generateSecureURL("act=ViewArticlePDF&title=".$this->BO->get("title"))."';",'Open PDF Version','pdfBut');
  243.             $html .= $temp->render();
  244.         }
  245.         
  246.         // render edit button for admins only
  247.         if (isset($_SESSION['currentUser']&& $_SESSION['currentUser']->inGroup('Admin')) {
  248.             $html .= '&nbsp;&nbsp;';
  249.             $button new Button("document.location = '".FrontController::generateSecureURL('act=Edit&bo='.get_class($this->BO).'&oid='.$this->BO->getID())."'",'Edit','editBut');
  250.             $html .= $button->render();
  251.         }
  252.         
  253.         if($config->get('sysCMSDisplayStandardFooter')) {
  254.             $html .= '<p>Article URL: <a href="'.$this->BO->get('URL').'">'.$this->BO->get('URL').'</a><br>';
  255.             $html .= 'Title: '.$this->BO->get('title').'<br>';
  256.             $html .= 'Author: '.$this->BO->get('author').'</p>';
  257.         }
  258.         
  259.         $html .= $config->get('sysCMSFooter');
  260.         
  261.         return $html;
  262.     }
  263.     
  264.     /**
  265.      * Method to handle POST requests
  266.      * 
  267.      * @param array $params 
  268.      * @since 1.0
  269.      */
  270.     public function doPOST($params{
  271.         global $config;
  272.         
  273.         try {
  274.             // check the hidden security fields before accepting the form POST data
  275.             if(!$this->checkSecurityFields())
  276.                 throw new SecurityException('This page cannot accept post data from remote servers!');
  277.  
  278.             
  279.             if(isset($params['voteBut']&& !$this->BO->checkUserVoted()) {
  280.                 $vote new ArticleVoteObject();
  281.                 
  282.                 if(isset($params['oid'])) {
  283.                     $vote->set('articleOID'$params['oid']);
  284.                 }else{
  285.                     // load article by title?                    
  286.                     if (isset($params['title'])) {
  287.                         $title str_replace('_'' '$params['title']);
  288.                     }else{
  289.                         throw new IllegalArguementException('Could not load the article as a title or OID was not supplied!');
  290.                     }
  291.                     
  292.                     $this->BO = new ArticleObject();
  293.                     $this->BO->loadByAttribute('title'$title);
  294.                     $vote->set('articleOID'$this->BO->getOID());
  295.                 }
  296.                 
  297.                 $vote->set('personOID'$_SESSION['currentUser']->getID());
  298.                 $vote->set('score'$params['user_vote']);
  299.                 
  300.                 try {
  301.                     $vote->save();
  302.  
  303.                     AlphaDAO::disconnect();
  304.                     
  305.                     $this->setStatusMessage(AlphaView::displayUpdateMessage('Thank you for rating this article!'));
  306.                     
  307.                     $this->doGET($params);
  308.                 }catch (FailedSaveException $e{
  309.                     self::$logger->error($e->getMessage());
  310.                 }
  311.             }
  312.             
  313.             if(isset($params['createBut'])) {
  314.                 $comment new ArticleCommentObject();
  315.                 
  316.                 // populate the transient object from post data
  317.                 $comment->populateFromPost();
  318.                 
  319.                 // filter the comment before saving                
  320.                 $comment->set('content'InputFilter::encode($comment->get('content')));
  321.                 
  322.                 try {
  323.                     $success $comment->save();
  324.                     
  325.                     AlphaDAO::disconnect();
  326.                     
  327.                     $this->setStatusMessage(AlphaView::displayUpdateMessage('Thank you for your comment!'));
  328.                     
  329.                     $this->doGET($params);
  330.                 }catch (FailedSaveException $e{
  331.                     self::$logger->error($e->getMessage());
  332.                 }                
  333.             }
  334.             
  335.             if(isset($params['saveBut'])) {            
  336.                 $comment new ArticleCommentObject();
  337.                 
  338.                 try {
  339.                     $comment->load($params['article_comment_id']);
  340.                     
  341.                     // re-populates the old object from post data
  342.                     $comment->populateFromPost();            
  343.                     
  344.                     $success $comment->save();
  345.                     
  346.                     AlphaDAO::disconnect();
  347.  
  348.                     $this->setStatusMessage(AlphaView::displayUpdateMessage('Your comment has been updated.'));
  349.                     
  350.                     $this->doGET($params);
  351.                 }catch (AlphaException $e{
  352.                     self::$logger->error($e->getMessage());
  353.                 }
  354.             }
  355.         }catch(SecurityException $e{
  356.             self::$logger->warn($e->getMessage());
  357.             throw new ResourceNotAllowedException($e->getMessage());
  358.         }
  359.     }
  360.     
  361.     /**
  362.      * Method for displaying the user comments for the article.
  363.      * 
  364.      * @return string 
  365.      * @since 1.0
  366.      */
  367.     private function renderComments({
  368.         global $config;
  369.         
  370.         $html '';
  371.         
  372.         $comments $this->BO->getArticleComments();
  373.         $comment_count count($comments);
  374.         
  375.         if($config->get('sysCMSDisplayComments'&& $comment_count 0{
  376.             $html .= '<h2>There are ['.$comment_count.'] user comments for this article</h2>';
  377.             
  378.             ob_start();
  379.             for($i 0$i $comment_count$i++{
  380.                 $view AlphaView::getInstance($comments[$i]);
  381.                 $view->markdownView();
  382.             }
  383.             $html.= ob_get_clean();
  384.         }
  385.         
  386.         if(isset($_SESSION['currentUser']&& $config->get('sysCMSCommentsAllowed')) {
  387.             $comment new ArticleCommentObject();
  388.             $comment->set('articleOID'$this->BO->getID());
  389.             
  390.             ob_start();
  391.             $view AlphaView::getInstance($comment);
  392.             $view->createView();
  393.             $html.= ob_get_clean();
  394.         }
  395.         
  396.         return $html;
  397.     }
  398. }
  399.  
  400. // now build the new controller
  401. if(basename($_SERVER['PHP_SELF']== 'ViewArticle.php'{
  402.     $controller new ViewArticle();
  403.     
  404.     if(!empty($_POST)) {            
  405.         $controller->doPOST($_REQUEST);
  406.     }else{
  407.         $controller->doGET($_GET);
  408.     }
  409. }
  410.  
  411. ?>

Documentation generated on Tue, 13 Dec 2011 20:27:46 +0000 by phpDocumentor 1.4.3