-
Notifications
You must be signed in to change notification settings - Fork 1
/
Feedreader.php
89 lines (72 loc) · 2.14 KB
/
Feedreader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/**
* @copyright Copyright © Klaus Mergen, 2014
* @package yii2-widgets
* @version 1.1.0
*/
namespace kmergen\feed;
use Zend\Feed\Reader\Reader;
/**
* Feedreader widget provides an RSS and Atom Reader.
*
* @author Klaus Mergen <[email protected]>
* @since 1.0
*/
class Feedreader extends \yii\base\Widget
{
/**
* @var string the feed Url.
*/
public $feedUrl;
/**
* @var string the template file. This is the view that is rendered by the widget to show the feeds.
*/
public $templateFile = 'default';
/**
* @var string the css class for the feed. If null it will be set in [[init()]]
*/
public $wrapperClass;
public function init()
{
//At the moment we can only use http protocol
$this->feedUrl = str_replace('https', 'http', $this->feedUrl);
if ($this->wrapperClass === null) {
$this->wrapperClass = 'feed-' . $this->templateFile;
}
}
public function run()
{
$feed = Reader::import($this->feedUrl);
$data = array(
'title' => $feed->getTitle(),
'link' => $feed->getLink(),
'dateModified' => $feed->getDateModified(),
'description' => $feed->getDescription(),
'language' => $feed->getLanguage(),
'entries' => array(),
);
foreach ($feed as $entry) {
$edata = array(
'title' => $entry->getTitle(),
'description' => $entry->getDescription(),
'dateModified' => $entry->getDateModified(),
'authors' => $entry->getAuthors(),
'link' => $entry->getLink(),
'content' => $entry->getContent(),
);
$data['entries'][] = $edata;
}
echo $this->render('default', ['data' => $data]);
// $this->registerClientScript();
}
/**
* Register CSS and Script.
*/
protected function registerClientScript()
{
//Now we create the js for the counter
$js = <<<JS
JS;
$this->view->registerJs($js);
}
}