CategoriesblogController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace app\controllers;
  3. use app\models\BlogPostsSearch;
  4. use Yii;
  5. use app\models\Categoriesblog;
  6. use app\models\CategoriesblogSearch;
  7. use yii\web\Controller;
  8. use yii\web\NotFoundHttpException;
  9. use yii\filters\VerbFilter;
  10. use yii\filters\AccessControl;
  11. use yii\data\ActiveDataProvider;
  12. use app\models\Posts;
  13. use app\models\BlogPosts;
  14. use app\models\Pages;
  15. /**
  16. */
  17. class CategoriesblogController extends Controller
  18. {
  19. public function behaviors()
  20. {
  21. return [
  22. 'verbs' => [
  23. 'class' => VerbFilter::className(),
  24. 'actions' => [
  25. 'delete' => ['POST'],
  26. ],
  27. ],
  28. 'access' =>[
  29. 'class' => AccessControl::className(),
  30. 'rules' => [
  31. [
  32. 'allow' => true,
  33. 'actions' => ['create', 'update', 'delete', 'index', 'category', 'view', 'blog'],
  34. 'roles' => ['@'],
  35. ],
  36. [
  37. 'allow' => true,
  38. 'actions' => ['index', 'category', 'view', 'blog'],
  39. 'roles' => ['?'],
  40. ],
  41. ],
  42. ],
  43. ];
  44. }
  45. public function actionIndex()
  46. {
  47. $searchModel = new CategoriesblogSearch();
  48. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  49. return $this->render('index', [
  50. 'searchModel' => $searchModel,
  51. 'dataProvider' => $dataProvider,
  52. ]);
  53. }
  54. public function actionView($id)
  55. {
  56. return $this->render('view', [
  57. 'model' => $this->findModel($id),
  58. ]);
  59. }
  60. public function actionPage( $slug )
  61. {
  62. $this->layout = '@app/views/layouts/public';
  63. if(false != ($model = Pages::findOne(['slug' => $slug]))){
  64. return $this->render('page', [
  65. 'model' => $model,
  66. ]);
  67. }else{
  68. throw new NotFoundHttpException(Yii::t('app','Post not found'));
  69. }
  70. }
  71. public function actionCreate()
  72. {
  73. $model = new Categoriesblog();
  74. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  75. return $this->redirect(['view', 'id' => $model->id]);
  76. } else {
  77. return $this->render('create', [
  78. 'model' => $model,
  79. ]);
  80. }
  81. }
  82. public function actionBlog(){
  83. $dataProvider = new ActiveDataProvider([
  84. 'query' => Posts::find(),
  85. 'pagination' => [
  86. 'defaultPageSize' => 5,
  87. 'forcePageParam' => false,
  88. 'pageSizeParam' => false,
  89. ],
  90. 'sort' => [
  91. 'defaultOrder'=>[
  92. 'id' => SORT_DESC,
  93. ],
  94. ],
  95. ]);
  96. return $this->render('blog', [
  97. 'dataProvider' => $dataProvider,
  98. ]);
  99. }
  100. public function actionCategory($slug, $lang = null)
  101. {
  102. $this->layout = '@app/views/layouts/public';
  103. $query = Categoriesblog::findOne(['slug' => $slug]);
  104. //метатеги для категорий
  105. $title = $query->title;
  106. $key = $query->key_words;
  107. $desc = $query->description;
  108. //метатеги для категорий lang = ru
  109. $title_ru = $query->title_ru;
  110. $key_ru = $query->key_words_ru;
  111. $desc_ru = $query->description_ru;
  112. if($lang == 'ru'){
  113. $this->view->title = $title_ru;
  114. $this->view->registerMetaTag(['name' => 'keywords', 'content' => $key_ru]);
  115. $this->view->registerMetaTag(['name' => 'description', 'content' => $desc_ru]);
  116. }else {
  117. $this->view->title = $title;
  118. $this->view->registerMetaTag(['name' => 'keywords', 'content' => $key]);
  119. $this->view->registerMetaTag(['name' => 'description', 'content' => $desc]);
  120. }
  121. //-----
  122. $searchModel = new BlogPostsSearch();
  123. $searchModel->categorySlug = $slug;
  124. $dataProvider = $searchModel->search(Yii::$app->request->queryParams, '');
  125. //if($lang === null) unset($lang);
  126. return $this->render('blog',[
  127. 'searchModel'=>$searchModel,
  128. 'dataProvider' => $dataProvider,
  129. 'lang' => $lang,
  130. ]);
  131. }
  132. public function actionUpdate($id)
  133. {
  134. $model = $this->findModel($id);
  135. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  136. return $this->redirect(['view', 'id' => $model->id]);
  137. } else {
  138. return $this->render('update', [
  139. 'model' => $model,
  140. ]);
  141. }
  142. }
  143. public function actionDelete($id)
  144. {
  145. $this->findModel($id)->delete();
  146. return $this->redirect(['index']);
  147. }
  148. protected function findModel($id)
  149. {
  150. if (($model = Categoriesblog::findOne($id)) !== null) {
  151. return $model;
  152. } else {
  153. throw new NotFoundHttpException('The requested page does not exist.');
  154. }
  155. }
  156. }