CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.2 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.2
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • CacheHelper
  • FormHelper
  • HtmlHelper
  • JqueryEngineHelper
  • JsBaseEngineHelper
  • JsHelper
  • MootoolsEngineHelper
  • NumberHelper
  • PaginatorHelper
  • PrototypeEngineHelper
  • RssHelper
  • SessionHelper
  • TextHelper
  • TimeHelper
  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-2012, 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-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 14:  * @link          http://cakephp.org CakePHP(tm) Project
 15:  * @package       Cake.View.Helper
 16:  * @since         CakePHP(tm) v 1.2
 17:  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 18:  */
 19: 
 20: App::uses('AppHelper', 'View/Helper');
 21: App::uses('Xml', 'Utility');
 22: 
 23: /**
 24:  * RSS Helper class for easy output RSS structures.
 25:  *
 26:  * @package       Cake.View.Helper
 27:  * @property      TimeHelper $Time
 28:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/rss.html
 29:  */
 30: class RssHelper extends AppHelper {
 31: 
 32: /**
 33:  * Helpers used by RSS Helper
 34:  *
 35:  * @var array
 36:  */
 37:     public $helpers = array('Time');
 38: 
 39: /**
 40:  * Base URL
 41:  *
 42:  * @var string
 43:  */
 44:     public $base = null;
 45: 
 46: /**
 47:  * URL to current action.
 48:  *
 49:  * @var string
 50:  */
 51:     public $here = null;
 52: 
 53: /**
 54:  * Parameter array.
 55:  *
 56:  * @var array
 57:  */
 58:     public $params = array();
 59: 
 60: /**
 61:  * Current action.
 62:  *
 63:  * @var string
 64:  */
 65:     public $action = null;
 66: 
 67: /**
 68:  * POSTed model data
 69:  *
 70:  * @var array
 71:  */
 72:     public $data = null;
 73: 
 74: /**
 75:  * Name of the current model
 76:  *
 77:  * @var string
 78:  */
 79:     public $model = null;
 80: 
 81: /**
 82:  * Name of the current field
 83:  *
 84:  * @var string
 85:  */
 86:     public $field = null;
 87: 
 88: /**
 89:  * Default spec version of generated RSS
 90:  *
 91:  * @var string
 92:  */
 93:     public $version = '2.0';
 94: 
 95: /**
 96:  * Returns an RSS document wrapped in `<rss />` tags
 97:  *
 98:  * @param array $attrib `<rss />` tag attributes
 99:  * @param string $content
100:  * @return string An RSS document
101:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/rss.html#RssHelper::document
102:  */
103:     public function document($attrib = array(), $content = null) {
104:         if ($content === null) {
105:             $content = $attrib;
106:             $attrib = array();
107:         }
108:         if (!isset($attrib['version']) || empty($attrib['version'])) {
109:             $attrib['version'] = $this->version;
110:         }
111: 
112:         return $this->elem('rss', $attrib, $content);
113:     }
114: 
115: /**
116:  * Returns an RSS `<channel />` element
117:  *
118:  * @param array $attrib `<channel />` tag attributes
119:  * @param array $elements Named array elements which are converted to tags
120:  * @param string $content Content (`<item />`'s belonging to this channel
121:  * @return string An RSS `<channel />`
122:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/rss.html#RssHelper::channel
123:  */
124:     public function channel($attrib = array(), $elements = array(), $content = null) {
125:         if (!isset($elements['title']) && !empty($this->_View->pageTitle)) {
126:             $elements['title'] = $this->_View->pageTitle;
127:         }
128:         if (!isset($elements['link'])) {
129:             $elements['link'] = '/';
130:         }
131:         if (!isset($elements['description'])) {
132:             $elements['description'] = '';
133:         }
134:         $elements['link'] = $this->url($elements['link'], true);
135: 
136:         $elems = '';
137:         foreach ($elements as $elem => $data) {
138:             $attributes = array();
139:             if (is_array($data)) {
140:                 if (strtolower($elem) == 'cloud') {
141:                     $attributes = $data;
142:                     $data = array();
143:                 } elseif (isset($data['attrib']) && is_array($data['attrib'])) {
144:                     $attributes = $data['attrib'];
145:                     unset($data['attrib']);
146:                 } else {
147:                     $innerElements = '';
148:                     foreach ($data as $subElement => $value) {
149:                         $innerElements .= $this->elem($subElement, array(), $value);
150:                     }
151:                     $data = $innerElements;
152:                 }
153:             }
154:             $elems .= $this->elem($elem, $attributes, $data);
155:         }
156:         return $this->elem('channel', $attrib, $elems . $content, !($content === null));
157:     }
158: 
159: /**
160:  * Transforms an array of data using an optional callback, and maps it to a set
161:  * of `<item />` tags
162:  *
163:  * @param array $items The list of items to be mapped
164:  * @param string|array $callback A string function name, or array containing an object
165:  *     and a string method name
166:  * @return string A set of RSS `<item />` elements
167:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/rss.html#RssHelper::items
168:  */
169:     public function items($items, $callback = null) {
170:         if ($callback != null) {
171:             $items = array_map($callback, $items);
172:         }
173: 
174:         $out = '';
175:         $c = count($items);
176: 
177:         for ($i = 0; $i < $c; $i++) {
178:             $out .= $this->item(array(), $items[$i]);
179:         }
180:         return $out;
181:     }
182: 
183: /**
184:  * Converts an array into an `<item />` element and its contents
185:  *
186:  * @param array $att The attributes of the `<item />` element
187:  * @param array $elements The list of elements contained in this `<item />`
188:  * @return string An RSS `<item />` element
189:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/rss.html#RssHelper::item
190:  */
191:     public function item($att = array(), $elements = array()) {
192:         $content = null;
193: 
194:         if (isset($elements['link']) && !isset($elements['guid'])) {
195:             $elements['guid'] = $elements['link'];
196:         }
197: 
198:         foreach ($elements as $key => $val) {
199:             $attrib = array();
200: 
201:             $escape = true;
202:             if (is_array($val) && isset($val['convertEntities'])) {
203:                 $escape = $val['convertEntities'];
204:                 unset($val['convertEntities']);
205:             }
206: 
207:             switch ($key) {
208:                 case 'pubDate' :
209:                     $val = $this->time($val);
210:                 break;
211:                 case 'category' :
212:                     if (is_array($val) && !empty($val[0])) {
213:                         foreach ($val as $category) {
214:                             $attrib = array();
215:                             if (is_array($category) && isset($category['domain'])) {
216:                                 $attrib['domain'] = $category['domain'];
217:                                 unset($category['domain']);
218:                             }
219:                             $categories[] = $this->elem($key, $attrib, $category);
220:                         }
221:                         $elements[$key] = implode('', $categories);
222:                         continue 2;
223:                     } elseif (is_array($val) && isset($val['domain'])) {
224:                         $attrib['domain'] = $val['domain'];
225:                     }
226:                 break;
227:                 case 'link':
228:                 case 'guid':
229:                 case 'comments':
230:                     if (is_array($val) && isset($val['url'])) {
231:                         $attrib = $val;
232:                         unset($attrib['url']);
233:                         $val = $val['url'];
234:                     }
235:                     $val = $this->url($val, true);
236:                 break;
237:                 case 'source':
238:                     if (is_array($val) && isset($val['url'])) {
239:                         $attrib['url'] = $this->url($val['url'], true);
240:                         $val = $val['title'];
241:                     } elseif (is_array($val)) {
242:                         $attrib['url'] = $this->url($val[0], true);
243:                         $val = $val[1];
244:                     }
245:                 break;
246:                 case 'enclosure':
247:                     if (is_string($val['url']) && is_file(WWW_ROOT . $val['url']) && file_exists(WWW_ROOT . $val['url'])) {
248:                         if (!isset($val['length']) && strpos($val['url'], '://') === false) {
249:                             $val['length'] = sprintf("%u", filesize(WWW_ROOT . $val['url']));
250:                         }
251:                         if (!isset($val['type']) && function_exists('mime_content_type')) {
252:                             $val['type'] = mime_content_type(WWW_ROOT . $val['url']);
253:                         }
254:                     }
255:                     $val['url'] = $this->url($val['url'], true);
256:                     $attrib = $val;
257:                     $val = null;
258:                 break;
259:                 default:
260:                     $attrib = $att;
261:             }
262:             if (!is_null($val) && $escape) {
263:                 $val = h($val);
264:             }
265:             $elements[$key] = $this->elem($key, $attrib, $val);
266:         }
267:         if (!empty($elements)) {
268:             $content = implode('', $elements);
269:         }
270:         return $this->elem('item', (array)$att, $content, !($content === null));
271:     }
272: 
273: /**
274:  * Converts a time in any format to an RSS time
275:  *
276:  * @param integer|string|DateTime $time
277:  * @return string An RSS-formatted timestamp
278:  * @see TimeHelper::toRSS
279:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/rss.html#RssHelper::time
280:  */
281:     public function time($time) {
282:         return $this->Time->toRSS($time);
283:     }
284: 
285: /**
286:  * Generates an XML element
287:  *
288:  * @param string $name The name of the XML element
289:  * @param array $attrib The attributes of the XML element
290:  * @param string|array $content XML element content
291:  * @param boolean $endTag Whether the end tag of the element should be printed
292:  * @return string XML
293:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/rss.html#RssHelper::elem
294:  */
295:     public function elem($name, $attrib = array(), $content = null, $endTag = true) {
296:         $namespace = null;
297:         if (isset($attrib['namespace'])) {
298:             $namespace = $attrib['namespace'];
299:             unset($attrib['namespace']);
300:         }
301:         $cdata = false;
302:         if (is_array($content) && isset($content['cdata'])) {
303:             $cdata = true;
304:             unset($content['cdata']);
305:         }
306:         if (is_array($content) && array_key_exists('value', $content)) {
307:             $content = $content['value'];
308:         }
309:         $children = array();
310:         if (is_array($content)) {
311:             $children = $content;
312:             $content = null;
313:         }
314: 
315:         $xml = '<' . $name;
316:         if (!empty($namespace)) {
317:             $xml .= ' xmlns';
318:             if (is_array($namespace)) {
319:                 $xml .= ':' . $namespace['prefix'];
320:                 $namespace = $namespace['url'];
321:             }
322:             $xml .= '="' . $namespace . '"';
323:         }
324:         $bareName = $name;
325:         if (strpos($name, ':') !== false) {
326:             list($prefix, $bareName) = explode(':', $name, 2);
327:             switch ($prefix) {
328:                 case 'atom':
329:                     $xml .= ' xmlns:atom="http://www.w3.org/2005/Atom"';
330:                     break;
331:             }
332:         }
333:         if ($cdata && !empty($content)) {
334:             $content = '<![CDATA[' . $content . ']]>';
335:         }
336:         $xml .= '>' . $content . '</' . $name . '>';
337:         $elem = Xml::build($xml, array('return' => 'domdocument'));
338:         $nodes = $elem->getElementsByTagName($bareName);
339:         if ($attrib) {
340:             foreach ($attrib as $key => $value) {
341:                 $nodes->item(0)->setAttribute($key, $value);
342:             }
343:         }
344:         foreach ($children as $child) {
345:             $child = $elem->createElement($name, $child);
346:             $nodes->item(0)->appendChild($child);
347:         }
348: 
349:         $xml = $elem->saveXml();
350:         $xml = trim(substr($xml, strpos($xml, '?>') + 2));
351:         return $xml;
352:     }
353: 
354: }
355: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs