NewsController.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace app\controllers;
  3. use app\models\base\NewsTopic;
  4. use app\models\News;
  5. use yii\web\Controller;
  6. use yii\web\NotFoundHttpException;
  7. class NewsController extends Controller
  8. {
  9. public function actionIndex($topic=NULL)
  10. {
  11. $topic = NewsTopic::findOne(['url'=>$topic]);
  12. if(!$topic instanceof NewsTopic){
  13. $topic = new \stdClass();
  14. $topic->id = NULL;
  15. }
  16. $news_query = \app\models\News::find()->joinWith('topics t')->andFilterWhere(['t.id'=>$topic->id]);
  17. $dateRange = \Yii::$app->request->get('daterange',NULL);
  18. if(!is_null($dateRange) && $dateRange!=""){
  19. $dateRange = explode(" — ",$dateRange);
  20. $dateRange[0] = date("Y-m-d 00:00:01",strtotime($dateRange[0]));
  21. $dateRange[1] = date("Y-m-d 23:59:59",strtotime($dateRange[1]));
  22. $news_query->andWhere(["BETWEEN","dt_pub",$dateRange[0],$dateRange[1]]);
  23. }
  24. return $this->render("index",["topic"=>$topic,"daterange"=>$dateRange,"news_query"=>$news_query]);
  25. }
  26. /**
  27. * @param int $id
  28. * @return string
  29. */
  30. public function actionView(int $id):string {
  31. $model = $this->findModel($id);
  32. if(!$model instanceof News) throw new NotFoundHttpException("Новость не найдена");
  33. return $this->render('view',['model'=>$model]);
  34. }
  35. public function actionStoryslugView($storyslug):string
  36. {
  37. $model = News::find()->andWhere(['alias'=>$storyslug])->one();
  38. if(!$model instanceof News) throw new NotFoundHttpException("Новость не найдена");
  39. return $this->render('view',['model'=>$model]);
  40. }
  41. private function findModel($id)
  42. {
  43. return News::find()->andWhere(['id'=>$id])->one();
  44. }
  45. }