Mercurial > hg > Members > shoshi > webvirt
comparison cake/libs/view/helpers/rss.php @ 0:261e66bd5a0c
hg init
author | Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 24 Jul 2011 21:08:31 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:261e66bd5a0c |
---|---|
1 <?php | |
2 /** | |
3 * RSS Helper class file. | |
4 * | |
5 * Simplifies the output of RSS feeds. | |
6 * | |
7 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | |
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
9 * | |
10 * Licensed under The MIT License | |
11 * Redistributions of files must retain the above copyright notice. | |
12 * | |
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
14 * @link http://cakephp.org CakePHP(tm) Project | |
15 * @package cake | |
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) | |
19 */ | |
20 App::import('Helper', 'Xml'); | |
21 | |
22 /** | |
23 * XML Helper class for easy output of XML structures. | |
24 * | |
25 * XmlHelper encloses all methods needed while working with XML documents. | |
26 * | |
27 * @package cake | |
28 * @subpackage cake.cake.libs.view.helpers | |
29 * @link http://book.cakephp.org/view/1460/RSS | |
30 */ | |
31 class RssHelper extends XmlHelper { | |
32 | |
33 /** | |
34 * Helpers used by RSS Helper | |
35 * | |
36 * @var array | |
37 * @access public | |
38 */ | |
39 var $helpers = array('Time'); | |
40 | |
41 /** | |
42 * Base URL | |
43 * | |
44 * @access public | |
45 * @var string | |
46 */ | |
47 var $base = null; | |
48 | |
49 /** | |
50 * URL to current action. | |
51 * | |
52 * @access public | |
53 * @var string | |
54 */ | |
55 var $here = null; | |
56 | |
57 /** | |
58 * Parameter array. | |
59 * | |
60 * @access public | |
61 * @var array | |
62 */ | |
63 var $params = array(); | |
64 | |
65 /** | |
66 * Current action. | |
67 * | |
68 * @access public | |
69 * @var string | |
70 */ | |
71 var $action = null; | |
72 | |
73 /** | |
74 * POSTed model data | |
75 * | |
76 * @access public | |
77 * @var array | |
78 */ | |
79 var $data = null; | |
80 | |
81 /** | |
82 * Name of the current model | |
83 * | |
84 * @access public | |
85 * @var string | |
86 */ | |
87 var $model = null; | |
88 | |
89 /** | |
90 * Name of the current field | |
91 * | |
92 * @access public | |
93 * @var string | |
94 */ | |
95 var $field = null; | |
96 | |
97 /** | |
98 * Default spec version of generated RSS | |
99 * | |
100 * @access public | |
101 * @var string | |
102 */ | |
103 var $version = '2.0'; | |
104 | |
105 /** | |
106 * Returns an RSS document wrapped in `<rss />` tags | |
107 * | |
108 * @param array $attrib `<rss />` tag attributes | |
109 * @return string An RSS document | |
110 * @access public | |
111 */ | |
112 function document($attrib = array(), $content = null) { | |
113 if ($content === null) { | |
114 $content = $attrib; | |
115 $attrib = array(); | |
116 } | |
117 if (!isset($attrib['version']) || empty($attrib['version'])) { | |
118 $attrib['version'] = $this->version; | |
119 } | |
120 | |
121 return $this->elem('rss', $attrib, $content); | |
122 } | |
123 | |
124 /** | |
125 * Returns an RSS `<channel />` element | |
126 * | |
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 />` | |
131 * @access public | |
132 */ | |
133 function channel($attrib = array(), $elements = array(), $content = null) { | |
134 $view =& ClassRegistry::getObject('view'); | |
135 | |
136 if (!isset($elements['title']) && !empty($view->pageTitle)) { | |
137 $elements['title'] = $view->pageTitle; | |
138 } | |
139 if (!isset($elements['link'])) { | |
140 $elements['link'] = '/'; | |
141 } | |
142 if (!isset($elements['description'])) { | |
143 $elements['description'] = ''; | |
144 } | |
145 $elements['link'] = $this->url($elements['link'], true); | |
146 | |
147 $elems = ''; | |
148 foreach ($elements as $elem => $data) { | |
149 $attributes = array(); | |
150 if (is_array($data)) { | |
151 if (strtolower($elem) == 'cloud') { | |
152 $attributes = $data; | |
153 $data = array(); | |
154 } elseif (isset($data['attrib']) && is_array($data['attrib'])) { | |
155 $attributes = $data['attrib']; | |
156 unset($data['attrib']); | |
157 } else { | |
158 $innerElements = ''; | |
159 foreach ($data as $subElement => $value) { | |
160 $innerElements .= $this->elem($subElement, array(), $value); | |
161 } | |
162 $data = $innerElements; | |
163 } | |
164 } | |
165 $elems .= $this->elem($elem, $attributes, $data); | |
166 } | |
167 return $this->elem('channel', $attrib, $elems . $content, !($content === null)); | |
168 } | |
169 | |
170 /** | |
171 * Transforms an array of data using an optional callback, and maps it to a set | |
172 * of `<item />` tags | |
173 * | |
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 | |
178 * @access public | |
179 */ | |
180 function items($items, $callback = null) { | |
181 if ($callback != null) { | |
182 $items = array_map($callback, $items); | |
183 } | |
184 | |
185 $out = ''; | |
186 $c = count($items); | |
187 | |
188 for ($i = 0; $i < $c; $i++) { | |
189 $out .= $this->item(array(), $items[$i]); | |
190 } | |
191 return $out; | |
192 } | |
193 | |
194 /** | |
195 * Converts an array into an `<item />` element and its contents | |
196 * | |
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 | |
200 * @access public | |
201 */ | |
202 function item($att = array(), $elements = array()) { | |
203 $content = null; | |
204 | |
205 if (isset($elements['link']) && !isset($elements['guid'])) { | |
206 $elements['guid'] = $elements['link']; | |
207 } | |
208 | |
209 foreach ($elements as $key => $val) { | |
210 $attrib = array(); | |
211 | |
212 $escape = true; | |
213 if (is_array($val) && isset($val['convertEntities'])) { | |
214 $escape = $val['convertEntities']; | |
215 unset($val['convertEntities']); | |
216 } | |
217 | |
218 switch ($key) { | |
219 case 'pubDate' : | |
220 $val = $this->time($val); | |
221 break; | |
222 case 'category' : | |
223 if (is_array($val) && !empty($val[0])) { | |
224 foreach ($val as $category) { | |
225 $attrib = array(); | |
226 if (isset($category['domain'])) { | |
227 $attrib['domain'] = $category['domain']; | |
228 unset($category['domain']); | |
229 } | |
230 $categories[] = $this->elem($key, $attrib, $category); | |
231 } | |
232 $elements[$key] = implode('', $categories); | |
233 continue 2; | |
234 } else if (is_array($val) && isset($val['domain'])) { | |
235 $attrib['domain'] = $val['domain']; | |
236 } | |
237 break; | |
238 case 'link': | |
239 case 'guid': | |
240 case 'comments': | |
241 if (is_array($val) && isset($val['url'])) { | |
242 $attrib = $val; | |
243 unset($attrib['url']); | |
244 $val = $val['url']; | |
245 } | |
246 $val = $this->url($val, true); | |
247 break; | |
248 case 'source': | |
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); | |
254 $val = $val[1]; | |
255 } | |
256 break; | |
257 case 'enclosure': | |
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'])); | |
261 } | |
262 if (!isset($val['type']) && function_exists('mime_content_type')) { | |
263 $val['type'] = mime_content_type(WWW_ROOT . $val['url']); | |
264 } | |
265 } | |
266 $val['url'] = $this->url($val['url'], true); | |
267 $attrib = $val; | |
268 $val = null; | |
269 break; | |
270 } | |
271 if (!is_null($val) && $escape) { | |
272 $val = h($val); | |
273 } | |
274 $elements[$key] = $this->elem($key, $attrib, $val); | |
275 } | |
276 if (!empty($elements)) { | |
277 $content = implode('', $elements); | |
278 } | |
279 return $this->elem('item', $att, $content, !($content === null)); | |
280 } | |
281 | |
282 /** | |
283 * Converts a time in any format to an RSS time | |
284 * | |
285 * @param mixed $time | |
286 * @return string An RSS-formatted timestamp | |
287 * @see TimeHelper::toRSS | |
288 */ | |
289 function time($time) { | |
290 return $this->Time->toRSS($time); | |
291 } | |
292 } |