diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..aded701 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/RssView.php b/RssView.php new file mode 100644 index 0000000..dc9c9a1 --- /dev/null +++ b/RssView.php @@ -0,0 +1,138 @@ +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; + } +} diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..ee463f1 --- /dev/null +++ b/composer.json @@ -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": "aleksandr@zelenin.me", + "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" +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..96383b6 --- /dev/null +++ b/readme.md @@ -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: [aleksandr@zelenin.me](mailto:aleksandr@zelenin.me)