Daan is sweet to Kelley and delegates himself TestCMS code reflection
[ssproject1617.git] / testcms-final-anon / system / classes / rss.php
1 <?php defined('IN_CMS') or die('No direct access allowed.');
2
3 class Rss {
4
5 private static $document;
6
7 public static function headers() {
8 // set headers
9 Response::header('Content-Type', 'application/xml');
10 }
11
12 private static function element($name, $value = null, $attributes = array()) {
13 $element = static::$document->createElement($name);
14
15 if(is_null($value) === false) {
16 $text = static::$document->createTextNode($value);
17 $element->appendChild($text);
18 }
19
20 foreach($attributes as $key => $val) {
21 $element->setAttribute($key, $val);
22 }
23
24 return $element;
25 }
26
27 public static function generate() {
28 // create a dom xml object
29 static::$document = new DOMDocument('1.0', 'UTF-8');
30
31 // create our rss feed
32 $rss = static::element('rss', null, array('version' => '2.0'));
33 static::$document->appendChild($rss);
34
35 // create channel
36 $channel = static::element('channel');
37 $rss->appendChild($channel);
38
39 // title
40 $title = static::element('title', Config::get('metadata.sitename'));
41 $channel->appendChild($title);
42
43 // link
44 $link = static::element('link', 'http://' . $_SERVER['HTTP_HOST']);
45 $channel->appendChild($link);
46
47 // description
48 $description = static::element('description', Config::get('metadata.description'));
49 $channel->appendChild($description);
50
51
52 // articles
53 $params = array('status' => 'published', 'sortby' => 'id', 'sortmode' => 'desc');
54
55 foreach(Posts::list_all($params) as $post) {
56 $item = static::element('item');
57 $channel->appendChild($item);
58
59 // title
60 $title = static::element('title', $post->title);
61 $item->appendChild($title);
62
63 // link
64 $url = 'http://' . $_SERVER['HTTP_HOST'] . Url::make(IoC::resolve('posts_page')->slug . '/' . $post->slug);
65 $link = static::element('link', $url);
66 $item->appendChild($link);
67
68 // description
69 $description = static::element('description', $post->description);
70 $item->appendChild($description);
71
72 // date
73 $date = static::element('pubDate', date(DATE_RSS, $post->created));
74 $item->appendChild($date);
75 }
76
77 // dump xml tree
78 Response::content(static::$document->saveXML());
79 }
80 }