web/enmi/Zend/Feed/Writer/Extension/ITunes/Renderer/Feed.php
changeset 19 1c2f13fd785c
parent 0 4eba9c11703f
equal deleted inserted replaced
18:bd595ad770fc 19:1c2f13fd785c
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Feed_Writer
       
    17  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    19  * @version    $Id: Feed.php 22662 2010-07-24 17:37:36Z mabe $
       
    20  */
       
    21  
       
    22 /**
       
    23  * @see Zend_Feed_Writer_Extension_RendererAbstract
       
    24  */
       
    25 require_once 'Zend/Feed/Writer/Extension/RendererAbstract.php';
       
    26  
       
    27 /**
       
    28  * @category   Zend
       
    29  * @package    Zend_Feed_Writer
       
    30  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    31  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    32  */
       
    33 class Zend_Feed_Writer_Extension_ITunes_Renderer_Feed
       
    34     extends Zend_Feed_Writer_Extension_RendererAbstract
       
    35 {
       
    36     
       
    37     /**
       
    38      * Set to TRUE if a rendering method actually renders something. This
       
    39      * is used to prevent premature appending of a XML namespace declaration
       
    40      * until an element which requires it is actually appended.
       
    41      *
       
    42      * @var bool
       
    43      */
       
    44     protected $_called = false;
       
    45     
       
    46     /**
       
    47      * Render feed
       
    48      * 
       
    49      * @return void
       
    50      */
       
    51     public function render()
       
    52     {
       
    53         $this->_setAuthors($this->_dom, $this->_base);
       
    54         $this->_setBlock($this->_dom, $this->_base);
       
    55         $this->_setCategories($this->_dom, $this->_base);
       
    56         $this->_setImage($this->_dom, $this->_base);
       
    57         $this->_setDuration($this->_dom, $this->_base);
       
    58         $this->_setExplicit($this->_dom, $this->_base);
       
    59         $this->_setKeywords($this->_dom, $this->_base);
       
    60         $this->_setNewFeedUrl($this->_dom, $this->_base);
       
    61         $this->_setOwners($this->_dom, $this->_base);
       
    62         $this->_setSubtitle($this->_dom, $this->_base);
       
    63         $this->_setSummary($this->_dom, $this->_base);
       
    64         if ($this->_called) {
       
    65             $this->_appendNamespaces();
       
    66         }
       
    67     }
       
    68     
       
    69     /**
       
    70      * Append feed namespaces
       
    71      * 
       
    72      * @return void
       
    73      */
       
    74     protected function _appendNamespaces()
       
    75     {
       
    76         $this->getRootElement()->setAttribute('xmlns:itunes',
       
    77             'http://www.itunes.com/dtds/podcast-1.0.dtd');  
       
    78     }
       
    79 
       
    80     /**
       
    81      * Set feed authors
       
    82      * 
       
    83      * @param  DOMDocument $dom 
       
    84      * @param  DOMElement $root 
       
    85      * @return void
       
    86      */
       
    87     protected function _setAuthors(DOMDocument $dom, DOMElement $root)
       
    88     {
       
    89         $authors = $this->getDataContainer()->getItunesAuthors();
       
    90         if (!$authors || empty($authors)) {
       
    91             return;
       
    92         }
       
    93         foreach ($authors as $author) {
       
    94             $el = $dom->createElement('itunes:author');
       
    95             $text = $dom->createTextNode($author);
       
    96             $el->appendChild($text);
       
    97             $root->appendChild($el);
       
    98         }
       
    99         $this->_called = true;
       
   100     }
       
   101     
       
   102     /**
       
   103      * Set feed itunes block
       
   104      * 
       
   105      * @param  DOMDocument $dom 
       
   106      * @param  DOMElement $root 
       
   107      * @return void
       
   108      */
       
   109     protected function _setBlock(DOMDocument $dom, DOMElement $root)
       
   110     {
       
   111         $block = $this->getDataContainer()->getItunesBlock();
       
   112         if ($block === null) {
       
   113             return;
       
   114         }
       
   115         $el = $dom->createElement('itunes:block');
       
   116         $text = $dom->createTextNode($block);
       
   117         $el->appendChild($text);
       
   118         $root->appendChild($el);
       
   119         $this->_called = true;
       
   120     }
       
   121     
       
   122     /**
       
   123      * Set feed categories
       
   124      * 
       
   125      * @param  DOMDocument $dom 
       
   126      * @param  DOMElement $root 
       
   127      * @return void
       
   128      */
       
   129     protected function _setCategories(DOMDocument $dom, DOMElement $root)
       
   130     {
       
   131         $cats = $this->getDataContainer()->getItunesCategories();
       
   132         if (!$cats || empty($cats)) {
       
   133             return;
       
   134         }
       
   135         foreach ($cats as $key=>$cat) {
       
   136             if (!is_array($cat)) {
       
   137                 $el = $dom->createElement('itunes:category');
       
   138                 $el->setAttribute('text', $cat);
       
   139                 $root->appendChild($el);
       
   140             } else {
       
   141                 $el = $dom->createElement('itunes:category');
       
   142                 $el->setAttribute('text', $key);
       
   143                 $root->appendChild($el);
       
   144                 foreach ($cat as $subcat) {
       
   145                     $el2 = $dom->createElement('itunes:category');
       
   146                     $el2->setAttribute('text', $subcat);
       
   147                     $el->appendChild($el2);
       
   148                 }
       
   149             }
       
   150         }
       
   151         $this->_called = true;
       
   152     }
       
   153     
       
   154     /**
       
   155      * Set feed image (icon)
       
   156      * 
       
   157      * @param  DOMDocument $dom 
       
   158      * @param  DOMElement $root 
       
   159      * @return void
       
   160      */
       
   161     protected function _setImage(DOMDocument $dom, DOMElement $root)
       
   162     {
       
   163         $image = $this->getDataContainer()->getItunesImage();
       
   164         if (!$image) {
       
   165             return;
       
   166         }
       
   167         $el = $dom->createElement('itunes:image');
       
   168         $el->setAttribute('href', $image);
       
   169         $root->appendChild($el);
       
   170         $this->_called = true;
       
   171     }
       
   172     
       
   173     /**
       
   174      * Set feed cumulative duration
       
   175      * 
       
   176      * @param  DOMDocument $dom 
       
   177      * @param  DOMElement $root 
       
   178      * @return void
       
   179      */
       
   180     protected function _setDuration(DOMDocument $dom, DOMElement $root)
       
   181     {
       
   182         $duration = $this->getDataContainer()->getItunesDuration();
       
   183         if (!$duration) {
       
   184             return;
       
   185         }
       
   186         $el = $dom->createElement('itunes:duration');
       
   187         $text = $dom->createTextNode($duration);
       
   188         $el->appendChild($text);
       
   189         $root->appendChild($el);
       
   190         $this->_called = true;
       
   191     }
       
   192     
       
   193     /**
       
   194      * Set explicit flag
       
   195      * 
       
   196      * @param  DOMDocument $dom 
       
   197      * @param  DOMElement $root 
       
   198      * @return void
       
   199      */
       
   200     protected function _setExplicit(DOMDocument $dom, DOMElement $root)
       
   201     {
       
   202         $explicit = $this->getDataContainer()->getItunesExplicit();
       
   203         if ($explicit === null) {
       
   204             return;
       
   205         }
       
   206         $el = $dom->createElement('itunes:explicit');
       
   207         $text = $dom->createTextNode($explicit);
       
   208         $el->appendChild($text);
       
   209         $root->appendChild($el);
       
   210         $this->_called = true;
       
   211     }
       
   212     
       
   213     /**
       
   214      * Set feed keywords
       
   215      * 
       
   216      * @param  DOMDocument $dom 
       
   217      * @param  DOMElement $root 
       
   218      * @return void
       
   219      */
       
   220     protected function _setKeywords(DOMDocument $dom, DOMElement $root)
       
   221     {
       
   222         $keywords = $this->getDataContainer()->getItunesKeywords();
       
   223         if (!$keywords || empty($keywords)) {
       
   224             return;
       
   225         }
       
   226         $el = $dom->createElement('itunes:keywords');
       
   227         $text = $dom->createTextNode(implode(',', $keywords));
       
   228         $el->appendChild($text);
       
   229         $root->appendChild($el);
       
   230         $this->_called = true;
       
   231     }
       
   232     
       
   233     /**
       
   234      * Set feed's new URL
       
   235      * 
       
   236      * @param  DOMDocument $dom 
       
   237      * @param  DOMElement $root 
       
   238      * @return void
       
   239      */
       
   240     protected function _setNewFeedUrl(DOMDocument $dom, DOMElement $root)
       
   241     {
       
   242         $url = $this->getDataContainer()->getItunesNewFeedUrl();
       
   243         if (!$url) {
       
   244             return;
       
   245         }
       
   246         $el = $dom->createElement('itunes:new-feed-url');
       
   247         $text = $dom->createTextNode($url);
       
   248         $el->appendChild($text);
       
   249         $root->appendChild($el);
       
   250         $this->_called = true;
       
   251     }
       
   252     
       
   253     /**
       
   254      * Set feed owners 
       
   255      * 
       
   256      * @param  DOMDocument $dom 
       
   257      * @param  DOMElement $root 
       
   258      * @return void
       
   259      */
       
   260     protected function _setOwners(DOMDocument $dom, DOMElement $root)
       
   261     {
       
   262         $owners = $this->getDataContainer()->getItunesOwners();
       
   263         if (!$owners || empty($owners)) {
       
   264             return;
       
   265         }
       
   266         foreach ($owners as $owner) {
       
   267             $el = $dom->createElement('itunes:owner');
       
   268             $name = $dom->createElement('itunes:name');
       
   269             $text = $dom->createTextNode($owner['name']);
       
   270             $name->appendChild($text);
       
   271             $email = $dom->createElement('itunes:email');
       
   272             $text = $dom->createTextNode($owner['email']);
       
   273             $email->appendChild($text);
       
   274             $root->appendChild($el);
       
   275             $el->appendChild($name);
       
   276             $el->appendChild($email);
       
   277         }
       
   278         $this->_called = true;
       
   279     }
       
   280     
       
   281     /**
       
   282      * Set feed subtitle
       
   283      * 
       
   284      * @param  DOMDocument $dom 
       
   285      * @param  DOMElement $root 
       
   286      * @return void
       
   287      */
       
   288     protected function _setSubtitle(DOMDocument $dom, DOMElement $root)
       
   289     {
       
   290         $subtitle = $this->getDataContainer()->getItunesSubtitle();
       
   291         if (!$subtitle) {
       
   292             return;
       
   293         }
       
   294         $el = $dom->createElement('itunes:subtitle');
       
   295         $text = $dom->createTextNode($subtitle);
       
   296         $el->appendChild($text);
       
   297         $root->appendChild($el);
       
   298         $this->_called = true;
       
   299     }
       
   300     
       
   301     /**
       
   302      * Set feed summary
       
   303      * 
       
   304      * @param  DOMDocument $dom 
       
   305      * @param  DOMElement $root 
       
   306      * @return void
       
   307      */
       
   308     protected function _setSummary(DOMDocument $dom, DOMElement $root)
       
   309     {
       
   310         $summary = $this->getDataContainer()->getItunesSummary();
       
   311         if (!$summary) {
       
   312             return;
       
   313         }
       
   314         $el = $dom->createElement('itunes:summary');
       
   315         $text = $dom->createTextNode($summary);
       
   316         $el->appendChild($text);
       
   317         $root->appendChild($el);
       
   318         $this->_called = true;
       
   319     }
       
   320 }