DefaultController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace rss\controllers;
  3. use Yii;
  4. use yii\web\Controller;
  5. use yii\web\Response;
  6. use yii\helpers\ArrayHelper;
  7. use yii\helpers\Url;
  8. /**
  9. * DefaultController implements actions
  10. */
  11. class DefaultController extends Controller
  12. {
  13. public $defaultAction = 'rss';
  14. public function actionIndex()
  15. {
  16. return $this->redirect("rss");
  17. }
  18. /**
  19. * Displays the Yandex turbo-pages for frontend.
  20. *
  21. * @return string
  22. */
  23. public function actionTurbo() {
  24. $module = $this->module;
  25. if ($module->cacheExpire !== 0 && ($cache = Yii::$app->getCache()) ) {
  26. $data = $cache->getOrSet(md5('yandex-turbo'), function () use ($module) {
  27. return [
  28. 'items' => $module->getTurboItems(),
  29. 'builded_at' => date('r')
  30. ];
  31. }, intval($module->cacheExpire));
  32. } else {
  33. $data = [
  34. 'items' => $module->getTurboItems(),
  35. 'builded_at' => date('r')
  36. ];
  37. }
  38. $channel = [];
  39. if (is_array($module->channelOptions))
  40. $channel = $module->channelOptions;
  41. if (!isset($channel['title']))
  42. $channel['title'] = Yii::$app->name;
  43. if (!isset($channel['link']))
  44. $channel['link'] = Url::base(true);
  45. if (!isset($channel['language']))
  46. $channel['language'] = Yii::$app->language;
  47. Yii::$app->response->format = Response::FORMAT_XML;
  48. Yii::$app->getResponse()->getHeaders()->set('Content-Type', 'text/xml; charset=UTF-8');
  49. return $this->renderPartial('turbo', [
  50. 'channel' => $channel,
  51. 'items' => $data['items']
  52. ]);
  53. }
  54. public function actionDzen() {
  55. $topic = \Yii::$app->request->get("topic",0);
  56. $module = $this->module;
  57. if ($module->cacheExpire !== 0 && ($cache = Yii::$app->getCache()) ) {
  58. $data = $cache->getOrSet(md5('yandex-dzen'.$topic), function () use ($module, $topic) {
  59. return [
  60. 'items' => ($topic)?$module->getTopicsItems($topic):$module->getTurboItems(),
  61. 'builded_at' => date('r')
  62. ];
  63. }, intval($module->cacheExpire));
  64. } else {
  65. $data = [
  66. 'items' => ($topic)?$module->getTopicsItems($topic):$module->getTurboItems(),
  67. 'builded_at' => date('r')
  68. ];
  69. }
  70. $channel = [];
  71. if (is_array($module->channelOptions))
  72. $channel = $module->channelOptions;
  73. if (!isset($channel['title']))
  74. $channel['title'] = Yii::$app->name;
  75. if (!isset($channel['link']))
  76. $channel['link'] = Url::base(true);
  77. if (!isset($channel['language']))
  78. $channel['language'] = Yii::$app->language;
  79. Yii::$app->response->format = Response::FORMAT_XML;
  80. Yii::$app->getResponse()->getHeaders()->set('Content-Type', 'text/xml; charset=UTF-8');
  81. $channel['clink'] = $channel['link'];
  82. if( $topic ){
  83. $mtopic = new \app\models\base\NewsTopic();
  84. $ptopic = $mtopic->GetById($topic);
  85. $channel['title'] = $ptopic->meta_title;
  86. $channel['clink'] = $ptopic->geturl(true);
  87. return $this->renderPartial('dzentopic', [
  88. 'channel' => $channel,
  89. 'items' => $data['items']
  90. ]);
  91. }
  92. return $this->renderPartial('dzen', [
  93. 'channel' => $channel,
  94. 'items' => $data['items']
  95. ]);
  96. }
  97. public function actionRia() {
  98. $module = $this->module;
  99. if ($module->cacheExpire !== 0 && ($cache = Yii::$app->getCache()) ) {
  100. $data = $cache->getOrSet(md5('yandex-ria'), function () use ($module) {
  101. return [
  102. 'items' => $module->getTurboItems(),
  103. 'builded_at' => date('r')
  104. ];
  105. }, intval($module->cacheExpire));
  106. } else {
  107. $data = [
  108. 'items' => $module->getTurboItems(),
  109. 'builded_at' => date('r')
  110. ];
  111. }
  112. $channel = [];
  113. if (is_array($module->channelOptions))
  114. $channel = $module->channelOptions;
  115. if (!isset($channel['title']))
  116. $channel['title'] = Yii::$app->name;
  117. if (!isset($channel['link']))
  118. $channel['link'] = Url::base(true);
  119. if (!isset($channel['language']))
  120. $channel['language'] = Yii::$app->language;
  121. Yii::$app->response->format = Response::FORMAT_XML;
  122. Yii::$app->getResponse()->getHeaders()->set('Content-Type', 'text/xml; charset=UTF-8');
  123. return $this->renderPartial('ria', [
  124. 'channel' => $channel,
  125. 'items' => $data['items']
  126. ]);
  127. }
  128. public function actionRss() {
  129. $module = $this->module;
  130. if ($module->cacheExpire !== 0 && ($cache = Yii::$app->getCache() )) {
  131. $data = $cache->getOrSet(md5('rss'), function () use ($module) {
  132. return [
  133. 'items' => $module->getTurboItems(),
  134. 'builded_at' => date('r')
  135. ];
  136. }, intval($module->cacheExpire));
  137. } else {
  138. $data = [
  139. 'items' => $module->getTurboItems(),
  140. 'builded_at' => date('r')
  141. ];
  142. }
  143. $channel = [];
  144. if (is_array($module->channelOptions))
  145. $channel = $module->channelOptions;
  146. if (!isset($channel['title']))
  147. $channel['title'] = Yii::$app->name;
  148. if (!isset($channel['link']))
  149. $channel['link'] = Url::base(true);
  150. if (!isset($channel['language']))
  151. $channel['language'] = Yii::$app->language;
  152. Yii::$app->response->format = Response::FORMAT_XML;
  153. Yii::$app->getResponse()->getHeaders()->set('Content-Type', 'text/xml; charset=UTF-8');
  154. return $this->renderPartial('rss', [
  155. 'channel' => $channel,
  156. 'items' => $data['items']
  157. ]);
  158. }
  159. }