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

Source for file AlphaFeed.inc

Documentation is available at AlphaFeed.inc

  1. <?php
  2.  
  3. require_once $config->get('sysRoot').'alpha/util/MarkdownFacade.inc';
  4.  
  5. /**
  6.  * Base feed class for generating syndication feeds
  7.  * 
  8.  * @package alpha::util::feeds
  9.  * @since 1.0
  10.  * @author John Collins <dev@alphaframework.org>
  11.  * @version $Id: AlphaFeed.inc 1341 2011-03-17 15:02:02Z johnc $
  12.  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  13.  * @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
  14.  *  All rights reserved.
  15.  * 
  16.  *  <pre>
  17.  *  Redistribution and use in source and binary forms, with or
  18.  *  without modification, are permitted provided that the
  19.  *  following conditions are met:
  20.  * 
  21.  *  * Redistributions of source code must retain the above
  22.  *    copyright notice, this list of conditions and the
  23.  *    following disclaimer.
  24.  *  * Redistributions in binary form must reproduce the above
  25.  *    copyright notice, this list of conditions and the
  26.  *    following disclaimer in the documentation and/or other
  27.  *    materials provided with the distribution.
  28.  *  * Neither the name of the Alpha Framework nor the names
  29.  *    of its contributors may be used to endorse or promote
  30.  *    products derived from this software without specific
  31.  *    prior written permission.
  32.  *   
  33.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  34.  *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  35.  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  36.  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  37.  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  38.  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39.  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  40.  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  41.  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  42.  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  43.  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  44.  *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  45.  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46.  *  </pre>
  47.  *  
  48.  */
  49. abstract class AlphaFeed {
  50.     /**
  51.      * The DOMDocument object used to create the feed
  52.      * 
  53.      * @var DOMDocument 
  54.      * @since 1.0
  55.      */
  56.     protected $rssDoc;
  57.     
  58.     /**
  59.      * The DOMElement object used to hold the item or entry elements
  60.      * 
  61.      * @var DOMElement 
  62.      * @since 1.0
  63.      */
  64.     protected $docElement;
  65.     
  66.     /**
  67.      * Holds the DOMElement to which metadata is added for the feed
  68.      * 
  69.      * @var DOMElement 
  70.      * @since 1.0
  71.      */
  72.     protected $root;
  73.     
  74.     /**
  75.      * The actual root tag used in each feed type
  76.      * 
  77.      * @var string 
  78.      * @since 1.0
  79.      */
  80.     protected $rootTag;
  81.     
  82.     /**
  83.      * An array of feed items
  84.      * 
  85.      * @var array 
  86.      * @since 1.0
  87.      */
  88.     protected $items;
  89.     
  90.     /**
  91.      * If the feed format has a channel or not
  92.      * 
  93.      * @var boolean 
  94.      * @since 1.0
  95.      */
  96.     protected $hasChannel = true;
  97.     
  98.     /**
  99.      * Maps the tags to the feed-specific tags
  100.      * 
  101.      * @var array 
  102.      * @since 1.0
  103.      */
  104.     protected $tagMap = array('item'=>'item','feeddesc'=>'description','itemdesc'=>'description');
  105.     
  106.     /**
  107.      * The BO which we will serve up in this feed
  108.      * 
  109.      * @var AlphaDAO 
  110.      * @since 1.0
  111.      */
  112.     private $BO;
  113.     
  114.     /**
  115.      * An array containing BO field names -> RSS field name mappings
  116.      * 
  117.      * @var array 
  118.      * @since 1.0
  119.      */
  120.     protected $fieldNameMappings;
  121.     
  122.     /**
  123.      * The XML namespace to use in the generated feed
  124.      * 
  125.      * @var string 
  126.      */
  127.     protected $nameSpace;
  128.     
  129.     /**
  130.      * Trace logger
  131.      * 
  132.      * @var Logger 
  133.      * @since 1.0
  134.      */
  135.     private static $logger null;        
  136.     
  137.     /**
  138.      * The constructor
  139.      * 
  140.      * @param string $BOName The classname of the BO to render a feed for.
  141.      * @param string $title The title of the feed.
  142.      * @param string $url The base URL for the feed.
  143.      * @param string $description The description of the feed.
  144.      * @param string $pubDate The publish date, only used in Atom feeds.
  145.      * @param integer $id The feed id, only used in Atom feeds.
  146.      * @param integer $limit The amount of items to render in the feed.
  147.      * @throws IllegalArguementException
  148.      * @since 1.0
  149.      */
  150.     public function __construct($BOName$title$url$description$pubDate null$id null$limit 10{
  151.         self::$logger new Logger('AlphaFeed');
  152.         self::$logger->debug('>>__construct(BOName=['.$BOName.'], title=['.$title.'], url=['.$url.'], description=['.$description.'], pubDate=['.$pubDate.'], id=['.$id.'], limit=['.$limit.'])');
  153.         
  154.         $this->rssDoc = new DOMDocument();
  155.         $this->rssDoc->loadXML($this->rootTag);
  156.         $this->docElement = $this->rssDoc->documentElement;
  157.         
  158.         try {
  159.             AlphaDAO::loadClassDef($BOName);
  160.             $this->BO new $BOName;
  161.         }catch (IllegalArguementException $e{
  162.             self::$logger->error('Unable to load the class definition for the class ['.$BOName.'] while trying to generate a feed!');
  163.             throw $e;
  164.         }                    
  165.         
  166.         if ($this->hasChannel{
  167.             $root $this->createFeedElement('channel');
  168.             $this->root = $this->docElement->appendChild($root);
  169.         }else{
  170.             $this->root = $this->docElement;
  171.         }
  172.         
  173.         $this->createRSSNode('feed'$this->root$title$url$description$pubDate$id);
  174.         
  175.         self::$logger->debug('<<__construct');
  176.     }
  177.     
  178.     /**
  179.      * Method to load all of the BO items to the feed from the database, from the newest to the
  180.      * $limit provided
  181.      * 
  182.      * @param integer $limit The amount of items to render in the feed.
  183.      * @param string $sortBy The name of the field to sort the feed by.
  184.      * @since 1.0
  185.      */
  186.     public function loadBOs($limit$sortBy{        
  187.         
  188.         $BOs $this->BO->loadAll(0$limit$sortBy'DESC');
  189.         
  190.         AlphaDAO::disconnect();
  191.         
  192.         foreach($BOs as $BO{
  193.             $this->addBO($BO);
  194.         }
  195.     }
  196.     
  197.     /**
  198.      * Method for adding a BO to the current feed
  199.      * 
  200.      * @param AlphaDAO $BO 
  201.      */
  202.     public function addBO($BO{
  203.         $title $BO->get($this->fieldNameMappings['title']);            
  204.         $url $BO->get($this->fieldNameMappings['url']);
  205.             
  206.         if(isset($this->fieldNameMappings['description'])) {
  207.             $description $BO->get($this->fieldNameMappings['description']);
  208.         }else{
  209.             $description '';
  210.         }
  211.             
  212.         if(isset($this->fieldNameMappings['pubDate'])) {
  213.             $dateTS strtotime($BO->get($this->fieldNameMappings['pubDate']));
  214.             $pubDate date(DATE_ATOM$dateTS);
  215.         }else{
  216.             $pubDate '';
  217.         }
  218.             
  219.         if(isset($this->fieldNameMappings['id']))
  220.             $id $BO->get($this->fieldNameMappings['id']);
  221.         else
  222.             $id '';
  223.             
  224.         $this->addItem($title$url$description$pubDate$id);
  225.     }
  226.     
  227.     /**
  228.      * Method for mapping BO fieldnames to feed field names
  229.      * 
  230.      * @param string $title The title of the feed.
  231.      * @param string $url The base URL for the feed.
  232.      * @param string $description The description of the feed.
  233.      * @param string $pubDate The publish date, only used in Atom feeds.
  234.      * @param integer $id The feed id, only used in Atom feeds.
  235.      * @since 1.0
  236.      */
  237.     public function setFieldMappings($title$url$description=null$pubDate=null$id=null{
  238.         $this->fieldNameMappings = array(
  239.             'title' => $title,
  240.             'url' => $url            
  241.         );
  242.         
  243.         if(isset($description))
  244.             $this->fieldNameMappings['description'$description;
  245.             
  246.         if(isset($pubDate))
  247.             $this->fieldNameMappings['pubDate'$pubDate;
  248.             
  249.         if(isset($id))
  250.             $this->fieldNameMappings['id'$id;
  251.     }
  252.     
  253.     /**
  254.      * Method for creating a new feed element
  255.      * 
  256.      * @param string $name The name of the element.
  257.      * @param string $value The value of the element.
  258.      * @return DOMElement 
  259.      * @since 1.0
  260.      */
  261.     protected function createFeedElement($name$value=null{
  262.         $value htmlspecialchars($value);
  263.         
  264.         if($this->nameSpace == null{            
  265.             return $this->rssDoc->createElement($name$value);
  266.         }else{
  267.             return $this->rssDoc->createElementNS($this->nameSpace$name$value);
  268.         }
  269.     }
  270.     
  271.     /**
  272.      * Method for creating link elements (note that Atom has a different format)
  273.      * 
  274.      * @param DOMElement $parent The parent element.
  275.      * @param string $url The URL for the link.
  276.      * @since 1.0
  277.      */
  278.     protected function createLink($parent$url{
  279.         $link $this->createFeedElement('link'$url);
  280.         $parent->appendChild($link);
  281.     }
  282.     
  283.     /**
  284.      * Method for creating an RSS node with a title, url and description
  285.      * 
  286.      * @param integer $type Can be either (item|feed) to indicate the type of node we are creating.
  287.      * @param DOMElement $parent The parent element.
  288.      * @param string $title The title of the feed.
  289.      * @param string $url The base URL for the feed.
  290.      * @param string $description The description of the feed.
  291.      * @param string $pubDate The publish date, only used in Atom feeds.
  292.      * @param integer $id The feed id, only used in Atom feeds.
  293.      * @since 1.0
  294.      * @throws IllegalArguementException
  295.      */
  296.     protected function createRSSNode($type$parent$title$url$description$pubDate=null$id null{
  297.         $this->createLink($parent$url);        
  298.         $title $this->createFeedElement('title'$title);        
  299.         $parent->appendChild($title);
  300.         
  301.         if ($type == 'item'{
  302.             $titletag $this->tagMap['itemdesc'];
  303.         }else if ($type == 'feed'{
  304.             $titletag $this->tagMap['feeddesc'];
  305.         }else{
  306.             throw new IllegalArguementException('The type paramater ['.$type.'] provided is invalid!');
  307.         }
  308.         
  309.         $description $this->createFeedElement($titletag$description);
  310.         $parent->appendChild($description);
  311.         
  312.         // id elements and updated elements are just for Atom!
  313.         if ($id != null{
  314.             $idnode $this->createFeedElement('id'$id);
  315.             $parent->appendChild($idnode);
  316.         }
  317.         
  318.         if ($pubDate != null{
  319.             $datenode $this->createFeedElement('updated'$pubDate);
  320.             $parent->appendChild($datenode);
  321.         }
  322.     }
  323.     
  324.     /**
  325.      * Method for adding an item to a feed
  326.      * 
  327.      * @param string $title The title of the feed.
  328.      * @param string $url The base URL for the feed.
  329.      * @param string $description The description of the feed.
  330.      * @param string $pubDate The publish date, only used in Atom feeds.
  331.      * @param integer $id The feed id, only used in Atom feeds.
  332.      * @since 1.0
  333.      */
  334.     protected function addItem($title$url$description=null$pubDate=null$id=null{        
  335.         $item $this->createFeedElement($this->tagMap['item']);
  336.         
  337.         if ($this->docElement->appendChild($item)) {            
  338.             $this->createRSSNode('item'$item$title$url$description$pubDate$id);
  339.         }
  340.     }
  341.     
  342.     /**
  343.      * Returns the formatted XML for the feed as a string
  344.      * 
  345.      * @return string 
  346.      * @since 1.0
  347.      */
  348.     public function render({
  349.         if ($this->rssDoc{
  350.             $this->rssDoc->formatOutput true;
  351.             return $this->rssDoc->saveXML();
  352.         }else{
  353.             return '';
  354.         }
  355.     }
  356. }
  357.  
  358. ?>

Documentation generated on Thu, 17 Mar 2011 16:43:30 +0000 by phpDocumentor 1.4.3