コード例 #1
0
 /**
  * Url: /yandex|vk|tw|fb/rss.xml
  * @param $kind
  * @return string
  * @throws NotFoundHttpException
  */
 public function actionSocialRss($kind)
 {
     $kind = 'is_' . $kind . '_rss';
     $post = new Post();
     if (!$post->hasAttribute($kind)) {
         throw new NotFoundHttpException('Страница не найдена.');
     }
     Yii::$app->response->format = Response::FORMAT_RAW;
     $headers = Yii::$app->response->headers;
     $headers->add('Content-Type', 'text/xml; charset=utf-8');
     $posts = Post::find()->where(['is_public' => 1, $kind => 1])->orderBy(['created_at' => SORT_DESC])->limit(50)->all();
     $items = [];
     foreach ($posts as $post) {
         $content = $post->getShortContent(500, 700);
         $fulltext = strip_tags($post->content, '<p><a><br>');
         $authorName = isset($post->user) ? $post->user->getDisplayName() : false;
         $image = $post->getAsset(Asset::THUMBNAIL_CONTENT);
         $enclosureUrl = file_exists($image->getFilePath()) ? $image->getFileUrl() : false;
         $enclosureType = $enclosureUrl ? image_type_to_mime_type(exif_imagetype($image->getFilePath())) : false;
         $item = ['title' => htmlspecialchars($post->title), 'link' => $post->url, 'description' => htmlspecialchars($content), 'fulltext' => htmlspecialchars($fulltext), 'pubDate' => date('r', strtotime($post->created_at)), 'authorName' => $authorName, 'enclosureUrl' => $enclosureUrl, 'enclosureType' => $enclosureType];
         $items[] = (object) $item;
     }
     $title = 'Динамомания: Новости';
     $description = 'Лента последних новостей';
     if ($kind == 'is_yandex_rss') {
         $view = '@frontend/views/site/yandex_rss';
     } else {
         $view = '@frontend/views/site/rss';
     }
     return $this->renderPartial($view, compact('title', 'description', 'items'));
 }