Skip to content

Commit 85df0a4

Browse files
committed
init
0 parents  commit 85df0a4

File tree

4 files changed

+265
-0
lines changed

4 files changed

+265
-0
lines changed

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2013 Aleksandr Zelenin
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

RssView.php

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
3+
namespace Zelenin\yii\extensions\Rss;
4+
5+
use Yii;
6+
use yii\base\InvalidConfigException;
7+
use yii\helpers\ArrayHelper;
8+
use yii\widgets\BaseListView;
9+
use Zelenin\Feed;
10+
11+
class RssView extends BaseListView
12+
{
13+
/** @var Feed $feed */
14+
public $feed;
15+
/** @var array $channel */
16+
public $channel;
17+
/** @var array $items */
18+
public $items;
19+
/** @var array $requiredChannelElements */
20+
public $requiredChannelElements = ['title', 'link', 'description'];
21+
/** @var array $requiredItemElements */
22+
public $requiredItemElements = ['title', 'description', 'link', 'pubDate'];
23+
24+
/** @var array $channelAttributes */
25+
private $channelAttributes = [];
26+
/** @var array $itemAttributes */
27+
private $itemAttributes = [];
28+
29+
/**
30+
* @throws InvalidConfigException
31+
*/
32+
public function init()
33+
{
34+
if ($this->dataProvider === null) {
35+
throw new InvalidConfigException('The "dataProvider" property must be set');
36+
}
37+
$this->channelAttributes = $this->getAttributes($this->channel);
38+
foreach ($this->requiredChannelElements as $channelElement) {
39+
if (!in_array($channelElement, $this->channelAttributes)) {
40+
throw new InvalidConfigException('Required channel attribute "' . $channelElement . '" must be set');
41+
}
42+
}
43+
$this->itemAttributes = $this->getAttributes($this->items);
44+
foreach ($this->requiredItemElements as $itemElement) {
45+
if (!in_array($itemElement, $this->itemAttributes)) {
46+
throw new InvalidConfigException('Required item attribute "' . $itemElement . '" must be set');
47+
}
48+
}
49+
$this->feed = new Feed;
50+
}
51+
52+
/**
53+
* @return string|Feed
54+
*/
55+
public function run()
56+
{
57+
$this->renderChannel();
58+
if ($this->dataProvider->getCount() > 0) {
59+
$this->renderItems();
60+
}
61+
return $this->feed;
62+
}
63+
64+
/**
65+
* @return Feed
66+
*/
67+
public function getFeed()
68+
{
69+
return $this->feed;
70+
}
71+
72+
public function renderChannel()
73+
{
74+
$this->getFeed()->addChannel();
75+
foreach ($this->channel as $element => $value) {
76+
if (is_string($value)) {
77+
$this->getFeed()->addChannelElement($element, $value);
78+
} else {
79+
$result = call_user_func($value, $this);
80+
if (is_string($result)) {
81+
$this->getFeed()->addChannelElement($element, $result);
82+
}
83+
}
84+
}
85+
}
86+
87+
public function renderItems()
88+
{
89+
$models = $this->dataProvider->getModels();
90+
foreach ($models as $model) {
91+
$this->renderItem($model);
92+
}
93+
}
94+
95+
/**
96+
* @param $model
97+
*/
98+
public function renderItem($model)
99+
{
100+
$this->getFeed()->addItem();
101+
foreach ($this->items as $element => $value) {
102+
if (is_string($value)) {
103+
if (is_string($element)) {
104+
$this->getFeed()->addItemElement($element, $value);
105+
} else {
106+
$this->getFeed()->addItemElement($value, ArrayHelper::getValue($model, $value));
107+
}
108+
} else {
109+
$result = call_user_func($value, $model, $this);
110+
if (is_string($result)) {
111+
$this->getFeed()->addItemElement($element, $result);
112+
}
113+
}
114+
}
115+
}
116+
117+
/**
118+
* @param $configArray
119+
* @return array
120+
* @throws InvalidConfigException
121+
*/
122+
private function getAttributes($configArray)
123+
{
124+
$attributes = [];
125+
foreach ($configArray as $key => $value) {
126+
if (is_string($key)) {
127+
$attributes[] = $key;
128+
} else {
129+
if (is_string($value)) {
130+
$attributes[] = $value;
131+
} else {
132+
throw new InvalidConfigException('Wrong configured attribute');
133+
}
134+
}
135+
}
136+
return $attributes;
137+
}
138+
}

composer.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "zelenin/yii2-rss",
3+
"description": "Yii2 RSS extension adds RSS-feed to your site",
4+
"version": "0.0.1",
5+
"type": "yii2-extension",
6+
"keywords": ["yii2", "rss", "feed"],
7+
"homepage": "https://github.com/zelenin/yii2-rss",
8+
"time": "2014-07-05",
9+
"license": "MIT",
10+
"authors": [
11+
{
12+
"name": "Aleksandr Zelenin",
13+
"email": "[email protected]",
14+
"homepage": "http://zelenin.me",
15+
"role": "Developer"
16+
}
17+
],
18+
"support": {
19+
"issues": "https://github.com/zelenin/yii2-rss/issues",
20+
"source": "https://github.com/zelenin/yii2-rss"
21+
},
22+
"require": {
23+
"php": ">=5.4.0",
24+
"yiisoft/yii2": "*",
25+
"zelenin/rss-generator": "2.0.0"
26+
},
27+
"autoload": {
28+
"psr-4": {
29+
"Zelenin\\yii\\extensions\\Rss\\": ""
30+
}
31+
},
32+
"minimum-stability": "stable"
33+
}

readme.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Yii2 RSS extension
2+
3+
[Yii2](http://www.yiiframework.com) RSS extension adds RSS-feed to your site
4+
5+
## Installation
6+
7+
### Composer
8+
9+
The preferred way to install this extension is through [Composer](http://getcomposer.org/).
10+
11+
Either run
12+
13+
```
14+
php composer.phar require zelenin/yii2-rss "dev-master"
15+
```
16+
17+
or add
18+
19+
```
20+
"zelenin/yii2-rss": "dev-master"
21+
```
22+
23+
to the require section of your ```composer.json```
24+
25+
## Usage
26+
27+
Add action to your controller:
28+
29+
```php
30+
public function actionRss()
31+
{
32+
$dataProvider = new ActiveDataProvider([
33+
'query' => Post::find()->with(['user']),
34+
'pagination' => [
35+
'pageSize' => 10
36+
],
37+
]);
38+
echo \Zelenin\yii\extensions\Rss\RssView::widget([
39+
'dataProvider' => $dataProvider,
40+
'channel' => [
41+
'title' => Yii::$app->name,
42+
'link' => Url::toRoute('/', true),
43+
'description' => 'Posts ',
44+
'language' => Yii::$app->language
45+
],
46+
'items' => [
47+
'title' => function ($model, $widget) {
48+
return $model->name;
49+
},
50+
'description' => function ($model, $widget) {
51+
return StringHelper::truncateWords($model->content, 50);
52+
},
53+
'link' => function ($model, $widget) {
54+
return Url::toRoute(['post/view', 'id' => $model->id], true);
55+
},
56+
'author' => function ($model, $widget) {
57+
return $model->user->email . ' (' . $model->user->username . ')';
58+
},
59+
'guid' => function ($model, $widget) {
60+
$date = \DateTime::createFromFormat('Y-m-d H:i:s', $model->updated_at);
61+
return Url::toRoute(['post/view', 'id' => $model->id], true) . ' ' . $date->format(DATE_RSS);
62+
},
63+
'pubDate' => function ($model, $widget) {
64+
$date = \DateTime::createFromFormat('Y-m-d H:i:s', $model->updated_at);
65+
return $date->format(DATE_RSS);
66+
}
67+
]
68+
]);
69+
}
70+
```
71+
72+
## Author
73+
74+
[Aleksandr Zelenin](https://github.com/zelenin/), e-mail: [[email protected]](mailto:[email protected])

0 commit comments

Comments
 (0)