-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 85df0a4
Showing
4 changed files
with
265 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2013 Aleksandr Zelenin | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
<?php | ||
|
||
namespace Zelenin\yii\extensions\Rss; | ||
|
||
use Yii; | ||
use yii\base\InvalidConfigException; | ||
use yii\helpers\ArrayHelper; | ||
use yii\widgets\BaseListView; | ||
use Zelenin\Feed; | ||
|
||
class RssView extends BaseListView | ||
{ | ||
/** @var Feed $feed */ | ||
public $feed; | ||
/** @var array $channel */ | ||
public $channel; | ||
/** @var array $items */ | ||
public $items; | ||
/** @var array $requiredChannelElements */ | ||
public $requiredChannelElements = ['title', 'link', 'description']; | ||
/** @var array $requiredItemElements */ | ||
public $requiredItemElements = ['title', 'description', 'link', 'pubDate']; | ||
|
||
/** @var array $channelAttributes */ | ||
private $channelAttributes = []; | ||
/** @var array $itemAttributes */ | ||
private $itemAttributes = []; | ||
|
||
/** | ||
* @throws InvalidConfigException | ||
*/ | ||
public function init() | ||
{ | ||
if ($this->dataProvider === null) { | ||
throw new InvalidConfigException('The "dataProvider" property must be set'); | ||
} | ||
$this->channelAttributes = $this->getAttributes($this->channel); | ||
foreach ($this->requiredChannelElements as $channelElement) { | ||
if (!in_array($channelElement, $this->channelAttributes)) { | ||
throw new InvalidConfigException('Required channel attribute "' . $channelElement . '" must be set'); | ||
} | ||
} | ||
$this->itemAttributes = $this->getAttributes($this->items); | ||
foreach ($this->requiredItemElements as $itemElement) { | ||
if (!in_array($itemElement, $this->itemAttributes)) { | ||
throw new InvalidConfigException('Required item attribute "' . $itemElement . '" must be set'); | ||
} | ||
} | ||
$this->feed = new Feed; | ||
} | ||
|
||
/** | ||
* @return string|Feed | ||
*/ | ||
public function run() | ||
{ | ||
$this->renderChannel(); | ||
if ($this->dataProvider->getCount() > 0) { | ||
$this->renderItems(); | ||
} | ||
return $this->feed; | ||
} | ||
|
||
/** | ||
* @return Feed | ||
*/ | ||
public function getFeed() | ||
{ | ||
return $this->feed; | ||
} | ||
|
||
public function renderChannel() | ||
{ | ||
$this->getFeed()->addChannel(); | ||
foreach ($this->channel as $element => $value) { | ||
if (is_string($value)) { | ||
$this->getFeed()->addChannelElement($element, $value); | ||
} else { | ||
$result = call_user_func($value, $this); | ||
if (is_string($result)) { | ||
$this->getFeed()->addChannelElement($element, $result); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public function renderItems() | ||
{ | ||
$models = $this->dataProvider->getModels(); | ||
foreach ($models as $model) { | ||
$this->renderItem($model); | ||
} | ||
} | ||
|
||
/** | ||
* @param $model | ||
*/ | ||
public function renderItem($model) | ||
{ | ||
$this->getFeed()->addItem(); | ||
foreach ($this->items as $element => $value) { | ||
if (is_string($value)) { | ||
if (is_string($element)) { | ||
$this->getFeed()->addItemElement($element, $value); | ||
} else { | ||
$this->getFeed()->addItemElement($value, ArrayHelper::getValue($model, $value)); | ||
} | ||
} else { | ||
$result = call_user_func($value, $model, $this); | ||
if (is_string($result)) { | ||
$this->getFeed()->addItemElement($element, $result); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param $configArray | ||
* @return array | ||
* @throws InvalidConfigException | ||
*/ | ||
private function getAttributes($configArray) | ||
{ | ||
$attributes = []; | ||
foreach ($configArray as $key => $value) { | ||
if (is_string($key)) { | ||
$attributes[] = $key; | ||
} else { | ||
if (is_string($value)) { | ||
$attributes[] = $value; | ||
} else { | ||
throw new InvalidConfigException('Wrong configured attribute'); | ||
} | ||
} | ||
} | ||
return $attributes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "zelenin/yii2-rss", | ||
"description": "Yii2 RSS extension adds RSS-feed to your site", | ||
"version": "0.0.1", | ||
"type": "yii2-extension", | ||
"keywords": ["yii2", "rss", "feed"], | ||
"homepage": "https://github.com/zelenin/yii2-rss", | ||
"time": "2014-07-05", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Aleksandr Zelenin", | ||
"email": "[email protected]", | ||
"homepage": "http://zelenin.me", | ||
"role": "Developer" | ||
} | ||
], | ||
"support": { | ||
"issues": "https://github.com/zelenin/yii2-rss/issues", | ||
"source": "https://github.com/zelenin/yii2-rss" | ||
}, | ||
"require": { | ||
"php": ">=5.4.0", | ||
"yiisoft/yii2": "*", | ||
"zelenin/rss-generator": "2.0.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Zelenin\\yii\\extensions\\Rss\\": "" | ||
} | ||
}, | ||
"minimum-stability": "stable" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Yii2 RSS extension | ||
|
||
[Yii2](http://www.yiiframework.com) RSS extension adds RSS-feed to your site | ||
|
||
## Installation | ||
|
||
### Composer | ||
|
||
The preferred way to install this extension is through [Composer](http://getcomposer.org/). | ||
|
||
Either run | ||
|
||
``` | ||
php composer.phar require zelenin/yii2-rss "dev-master" | ||
``` | ||
|
||
or add | ||
|
||
``` | ||
"zelenin/yii2-rss": "dev-master" | ||
``` | ||
|
||
to the require section of your ```composer.json``` | ||
|
||
## Usage | ||
|
||
Add action to your controller: | ||
|
||
```php | ||
public function actionRss() | ||
{ | ||
$dataProvider = new ActiveDataProvider([ | ||
'query' => Post::find()->with(['user']), | ||
'pagination' => [ | ||
'pageSize' => 10 | ||
], | ||
]); | ||
echo \Zelenin\yii\extensions\Rss\RssView::widget([ | ||
'dataProvider' => $dataProvider, | ||
'channel' => [ | ||
'title' => Yii::$app->name, | ||
'link' => Url::toRoute('/', true), | ||
'description' => 'Posts ', | ||
'language' => Yii::$app->language | ||
], | ||
'items' => [ | ||
'title' => function ($model, $widget) { | ||
return $model->name; | ||
}, | ||
'description' => function ($model, $widget) { | ||
return StringHelper::truncateWords($model->content, 50); | ||
}, | ||
'link' => function ($model, $widget) { | ||
return Url::toRoute(['post/view', 'id' => $model->id], true); | ||
}, | ||
'author' => function ($model, $widget) { | ||
return $model->user->email . ' (' . $model->user->username . ')'; | ||
}, | ||
'guid' => function ($model, $widget) { | ||
$date = \DateTime::createFromFormat('Y-m-d H:i:s', $model->updated_at); | ||
return Url::toRoute(['post/view', 'id' => $model->id], true) . ' ' . $date->format(DATE_RSS); | ||
}, | ||
'pubDate' => function ($model, $widget) { | ||
$date = \DateTime::createFromFormat('Y-m-d H:i:s', $model->updated_at); | ||
return $date->format(DATE_RSS); | ||
} | ||
] | ||
]); | ||
} | ||
``` | ||
|
||
## Author | ||
|
||
[Aleksandr Zelenin](https://github.com/zelenin/), e-mail: [[email protected]](mailto:[email protected]) |