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

Source for file ViewFeed.php

Documentation is available at ViewFeed.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/feeds/RSS2.inc';
  11. require_once $config->get('sysRoot').'alpha/util/feeds/RSS.inc';
  12. require_once $config->get('sysRoot').'alpha/util/feeds/Atom.inc';
  13. require_once $config->get('sysRoot').'alpha/util/LogFile.inc';
  14. require_once $config->get('sysRoot').'alpha/controller/AlphaControllerInterface.inc';
  15.  
  16. /**
  17.  * Controller for viewing news feeds
  18.  * 
  19.  * @package alpha::controller
  20.  * @since 1.0
  21.  * @author John Collins <dev@alphaframework.org>
  22.  * @version $Id: ViewFeed.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 ViewFeed extends AlphaController implements AlphaControllerInterface {
  61.     /**
  62.      * The name of the BO to render as a feed
  63.      * 
  64.      * @var string 
  65.      * @since 1.0
  66.      */
  67.     private $BOName;
  68.     
  69.     /**
  70.      * The type of feed to render (RSS, RSS2 or Atom)
  71.      * 
  72.      * @var string 
  73.      * @since 1.0
  74.      */
  75.     private $type;
  76.     
  77.     /**
  78.      * The title of the feed
  79.      * 
  80.      * @var string 
  81.      * @since 1.0
  82.      */
  83.     protected $title;
  84.     
  85.     /**
  86.      * The description of the feed
  87.      * 
  88.      * @var string 
  89.      * @since 1.0
  90.      */
  91.     protected $description;
  92.     
  93.     /**
  94.      * The BO to feed field mappings
  95.      * 
  96.      * @var array 
  97.      * @since 1.0
  98.      */
  99.     protected $fieldMappings;
  100.     
  101.     /**
  102.      * The BO field name to sort the feed by (descending), default is OID
  103.      * 
  104.      * @var string 
  105.      * @since 1.0
  106.      */
  107.     private $sortBy 'OID';
  108.     
  109.     /**
  110.      * Trace logger
  111.      * 
  112.      * @var Logger 
  113.      * @since 1.0
  114.      */
  115.     private static $logger null;
  116.     
  117.     /**
  118.      * constructor to set up the object
  119.      * 
  120.      * @since 1.0
  121.      */
  122.     public function __construct({
  123.         self::$logger new Logger('ViewFeed');
  124.         self::$logger->debug('>>__construct()');
  125.         
  126.         global $config;        
  127.         
  128.         // ensure that the super class constructor is called, indicating the rights group
  129.         parent::__construct('Public');
  130.         
  131.         self::$logger->debug('<<__construct');
  132.     }
  133.     
  134.     /**
  135.      * Handle GET requests
  136.      * 
  137.      * @param array $params 
  138.      * @since 1.0
  139.      * @throws ResourceNotFoundException
  140.      */
  141.     public function doGET($params{
  142.         self::$logger->debug('>>doGET($params=['.var_export($paramstrue).'])');
  143.         
  144.         global $config;
  145.         
  146.         try {
  147.             if (isset($params['bo'])) {
  148.                 $BOName $params['bo'];    
  149.             }else{
  150.                 throw new IllegalArguementException('BO not specified to generate feed!');
  151.             }
  152.             
  153.             if (isset($params['type'])) {
  154.                 $type $params['type'];    
  155.             }else{
  156.                 throw new IllegalArguementException('No feed type specified to generate feed!');
  157.             }
  158.         
  159.             $this->BOName $BOName;
  160.             $this->type $type;        
  161.             
  162.             $this->setup();
  163.             
  164.             switch($type{
  165.                 case 'RSS2':
  166.                     $feed new RSS2($BOName$this->titlestr_replace('&''&amp;'$_SERVER['REQUEST_URI'])$this->description);
  167.                     $feed->setFieldMappings($this->fieldMappings[0]$this->fieldMappings[1]$this->fieldMappings[2]$this->fieldMappings[3]);
  168.                 break;
  169.                 case 'RSS':
  170.                     $feed new RSS($BOName$this->titlestr_replace('&''&amp;'$_SERVER['REQUEST_URI'])$this->description);
  171.                     $feed->setFieldMappings($this->fieldMappings[0]$this->fieldMappings[1]$this->fieldMappings[2]$this->fieldMappings[3]);
  172.                 break;
  173.                 case 'Atom':
  174.                     $feed new Atom($BOName$this->titlestr_replace('&''&amp;'$_SERVER['REQUEST_URI'])$this->description);
  175.                     $feed->setFieldMappings($this->fieldMappings[0]$this->fieldMappings[1]$this->fieldMappings[2]$this->fieldMappings[3],
  176.                         $this->fieldMappings[4]);
  177.                     if($config->get('sysAtomFeedAuthor'!= '')
  178.                         $feed->addAuthor($config->get('sysAtomFeedAuthor'));
  179.                 break;
  180.             }
  181.             
  182.             // now add the twenty last items (from newest to oldest) to the feed, and render
  183.             $feed->loadBOs(20$this->sortBy);
  184.             echo $feed->render();
  185.             
  186.             // log the request for this news feed
  187.             $feedLog new LogFile($config->get('sysRoot').'logs/feeds.log');        
  188.             $feedLog->writeLine(array($this->BOName$this->typedate("Y-m-d H:i:s")$_SERVER['HTTP_USER_AGENT']$_SERVER['REMOTE_ADDR']));
  189.         }catch(IllegalArguementException $e{
  190.             self::$logger->error($e->getMessage());
  191.             throw new ResourceNotFoundException($e->getMessage());
  192.         }
  193.         
  194.         self::$logger->debug('<<__doGet');
  195.     }
  196.     
  197.     /**
  198.      * Method to handle POST requests
  199.      * 
  200.      * @param array $params 
  201.      * @since 1.0
  202.      */
  203.     public function doPOST($params{
  204.         self::$logger->debug('>>doPOST($params=['.var_export($paramstrue).'])');
  205.         
  206.         self::$logger->debug('<<doPOST');
  207.     }
  208.     
  209.     /**
  210.      * setup the feed title, field mappings and description based on common BO types
  211.      */
  212.     protected function setup({
  213.         self::$logger->debug('>>setup()');
  214.         
  215.         global $config;        
  216.         
  217.         // set up some BO to feed fields mappings based on common BO types
  218.         switch($this->BOName{
  219.             case 'ArticleObject':
  220.                 $this->title = 'Latest articles from '.$config->get('sysTitle');
  221.                 $this->description = 'News feed containing all of the details on the latest articles published on '.$config->get('sysTitle').'.';
  222.                 $this->fieldMappings = array('title''URL''description''created_ts''OID');
  223.                 $this->sortBy 'created_ts';
  224.             break;
  225.             case 'NewsObject':
  226.                 $this->title = 'Latest news from '.$config->get('sysTitle');
  227.                 $this->description = 'News feed containing all of the latest news items from '.$config->get('sysTitle').'.';
  228.                 $this->fieldMappings = array('title''URL''content''created_ts''OID');
  229.             break;
  230.         }
  231.         
  232.         self::$logger->debug('<<setup');
  233.     }
  234. }
  235.  
  236. // now build the new controller
  237. if(basename($_SERVER['PHP_SELF']== 'ViewFeed.php'{
  238.     $controller new ViewFeed();
  239.     
  240.     if(!empty($_POST)) {            
  241.         $controller->doPOST($_REQUEST);
  242.     }else{
  243.         $controller->doGET($_GET);
  244.     }
  245. }
  246.  
  247. ?>

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