3
* RSS Helper class file.
5
* Simplifies the output of RSS feeds.
7
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
10
* Licensed under The MIT License
11
* Redistributions of files must retain the above copyright notice.
13
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14
* @link http://cakephp.org CakePHP(tm) Project
16
* @subpackage cake.cake.libs.view.helpers
17
* @since CakePHP(tm) v 1.2
18
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
20
App::import('Helper', 'Xml');
23
* XML Helper class for easy output of XML structures.
25
* XmlHelper encloses all methods needed while working with XML documents.
28
* @subpackage cake.cake.libs.view.helpers
29
* @link http://book.cakephp.org/view/1460/RSS
31
class RssHelper extends XmlHelper {
34
* Helpers used by RSS Helper
39
var $helpers = array('Time');
50
* URL to current action.
63
var $params = array();
82
* Name of the current model
90
* Name of the current field
98
* Default spec version of generated RSS
103
var $version = '2.0';
106
* Returns an RSS document wrapped in `<rss />` tags
108
* @param array $attrib `<rss />` tag attributes
109
* @return string An RSS document
112
function document($attrib = array(), $content = null) {
113
if ($content === null) {
117
if (!isset($attrib['version']) || empty($attrib['version'])) {
118
$attrib['version'] = $this->version;
121
return $this->elem('rss', $attrib, $content);
125
* Returns an RSS `<channel />` element
127
* @param array $attrib `<channel />` tag attributes
128
* @param mixed $elements Named array elements which are converted to tags
129
* @param mixed $content Content (`<item />`'s belonging to this channel
130
* @return string An RSS `<channel />`
133
function channel($attrib = array(), $elements = array(), $content = null) {
134
$view =& ClassRegistry::getObject('view');
136
if (!isset($elements['title']) && !empty($view->pageTitle)) {
137
$elements['title'] = $view->pageTitle;
139
if (!isset($elements['link'])) {
140
$elements['link'] = '/';
142
if (!isset($elements['description'])) {
143
$elements['description'] = '';
145
$elements['link'] = $this->url($elements['link'], true);
148
foreach ($elements as $elem => $data) {
149
$attributes = array();
150
if (is_array($data)) {
151
if (strtolower($elem) == 'cloud') {
154
} elseif (isset($data['attrib']) && is_array($data['attrib'])) {
155
$attributes = $data['attrib'];
156
unset($data['attrib']);
159
foreach ($data as $subElement => $value) {
160
$innerElements .= $this->elem($subElement, array(), $value);
162
$data = $innerElements;
165
$elems .= $this->elem($elem, $attributes, $data);
167
return $this->elem('channel', $attrib, $elems . $content, !($content === null));
171
* Transforms an array of data using an optional callback, and maps it to a set
174
* @param array $items The list of items to be mapped
175
* @param mixed $callback A string function name, or array containing an object
176
* and a string method name
177
* @return string A set of RSS `<item />` elements
180
function items($items, $callback = null) {
181
if ($callback != null) {
182
$items = array_map($callback, $items);
188
for ($i = 0; $i < $c; $i++) {
189
$out .= $this->item(array(), $items[$i]);
195
* Converts an array into an `<item />` element and its contents
197
* @param array $attrib The attributes of the `<item />` element
198
* @param array $elements The list of elements contained in this `<item />`
199
* @return string An RSS `<item />` element
202
function item($att = array(), $elements = array()) {
205
if (isset($elements['link']) && !isset($elements['guid'])) {
206
$elements['guid'] = $elements['link'];
209
foreach ($elements as $key => $val) {
213
if (is_array($val) && isset($val['convertEntities'])) {
214
$escape = $val['convertEntities'];
215
unset($val['convertEntities']);
220
$val = $this->time($val);
223
if (is_array($val) && !empty($val[0])) {
224
foreach ($val as $category) {
226
if (isset($category['domain'])) {
227
$attrib['domain'] = $category['domain'];
228
unset($category['domain']);
230
$categories[] = $this->elem($key, $attrib, $category);
232
$elements[$key] = implode('', $categories);
234
} else if (is_array($val) && isset($val['domain'])) {
235
$attrib['domain'] = $val['domain'];
241
if (is_array($val) && isset($val['url'])) {
243
unset($attrib['url']);
246
$val = $this->url($val, true);
249
if (is_array($val) && isset($val['url'])) {
250
$attrib['url'] = $this->url($val['url'], true);
251
$val = $val['title'];
252
} elseif (is_array($val)) {
253
$attrib['url'] = $this->url($val[0], true);
258
if (is_string($val['url']) && is_file(WWW_ROOT . $val['url']) && file_exists(WWW_ROOT . $val['url'])) {
259
if (!isset($val['length']) && strpos($val['url'], '://') === false) {
260
$val['length'] = sprintf("%u", filesize(WWW_ROOT . $val['url']));
262
if (!isset($val['type']) && function_exists('mime_content_type')) {
263
$val['type'] = mime_content_type(WWW_ROOT . $val['url']);
266
$val['url'] = $this->url($val['url'], true);
271
if (!is_null($val) && $escape) {
274
$elements[$key] = $this->elem($key, $attrib, $val);
276
if (!empty($elements)) {
277
$content = implode('', $elements);
279
return $this->elem('item', $att, $content, !($content === null));
283
* Converts a time in any format to an RSS time
286
* @return string An RSS-formatted timestamp
287
* @see TimeHelper::toRSS
289
function time($time) {
290
return $this->Time->toRSS($time);