Blame view

frontend/controllers/BlogController.php 4.63 KB
30a01994   Timur Kastemirov   blog
1
2
3
4
  <?php
      namespace frontend\controllers;
  
      use yii\data\ActiveDataProvider;
2309b955   Timur Kastemirov   blog categories &...
5
6
      use yii\helpers\Html;
      use yii\helpers\Url;
30a01994   Timur Kastemirov   blog
7
      use yii\web\Controller;
ec7e1832   Kateryna_Sarnytska   image_id moved to...
8
      use common\models\Article;
30a01994   Timur Kastemirov   blog
9
      use yii\web\NotFoundHttpException;
1729300e   Timur Kastemirov   blog categories &...
10
      use yii\db\ActiveQuery;
30a01994   Timur Kastemirov   blog
11
12
13
14
15
16
17
18
19
  
      /**
       * User: timur
       * Date: 26.01.18
       * Time: 8:46
       */
      
      class BlogController extends Controller
      {
2309b955   Timur Kastemirov   blog categories &...
20
      
1729300e   Timur Kastemirov   blog categories &...
21
          protected function prepareProviderAndRender(ActiveQuery $query): string {
30a01994   Timur Kastemirov   blog
22
23
              $dataProvider = new ActiveDataProvider(
                  [
2309b955   Timur Kastemirov   blog categories &...
24
                      'query' => $query,
30a01994   Timur Kastemirov   blog
25
26
27
28
29
                      'pagination' => [
                          'pageSize' => 5,
                      ],
                  ]
              );
1729300e   Timur Kastemirov   blog categories &...
30
          
30a01994   Timur Kastemirov   blog
31
32
33
34
35
36
37
38
              return $this->render(
                  'index',
                  [
                      'dataProvider' => $dataProvider,
                  ]
              );
          }
          
1729300e   Timur Kastemirov   blog categories &...
39
40
41
42
          public function actionIndex()
          {
      
              $query = Article::find()
ec7e1832   Kateryna_Sarnytska   image_id moved to...
43
                              ->with('lang.image')
1729300e   Timur Kastemirov   blog categories &...
44
45
46
47
48
                              ->where(
                                  [
                                      'status' => true,
                                  ]
                              )
be160418   Alexey Boroda   blog index order ...
49
                              ->orderBy(["created_at" => SORT_DESC]);
1729300e   Timur Kastemirov   blog categories &...
50
51
52
53
      
              return $this->prepareProviderAndRender($query);
          }
          
30a01994   Timur Kastemirov   blog
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
          public function actionArticle($id)
          {
              
              $model = $this->findModel($id);
              
              return $this->render(
                  'view',
                  [
                      'article' => $model,
                  ]
              );
              
          }
      
          protected function findModel($id)
          {
30a01994   Timur Kastemirov   blog
70
              $model = Article::find()
ec7e1832   Kateryna_Sarnytska   image_id moved to...
71
72
73
                  ->with('lang.image')
  
                  ->where(
30a01994   Timur Kastemirov   blog
74
75
76
77
                                  [
                                      'id' => $id
                                  ]
                              )
ec7e1832   Kateryna_Sarnytska   image_id moved to...
78
                              ->with("lang.image")
30a01994   Timur Kastemirov   blog
79
80
81
82
83
84
85
86
87
88
                              ->one();
              
              if ( $model !== NULL) {
                  return $model;
              }
              else {
                  throw new NotFoundHttpException('The requested page does not exist.');
              }
          }
          
1729300e   Timur Kastemirov   blog categories &...
89
90
91
          public function actionSearch()
          {
              
2309b955   Timur Kastemirov   blog categories &...
92
              if( \Yii::$app->request->isPost ){
1729300e   Timur Kastemirov   blog categories &...
93
  
2309b955   Timur Kastemirov   blog categories &...
94
95
96
97
                  $req = \Yii::$app->request;
                  if (!empty($req->post("title"))){
                      $title = Html::encode($req->post("title"));
                      $query = Article::find()
ec7e1832   Kateryna_Sarnytska   image_id moved to...
98
                                      ->joinWith("lang.image")
2309b955   Timur Kastemirov   blog categories &...
99
100
101
102
103
104
105
106
107
108
109
110
                                      ->where(
                                          [
                                              'status' => true,
                                          ]
                                      )
                                      ->andWhere(
                                          [
                                              "like", "lower(title)", $title
                                          ]
                                      )
                                      ->orderBy("sort");
      
1729300e   Timur Kastemirov   blog categories &...
111
                      return $this->prepareProviderAndRender($query);
2309b955   Timur Kastemirov   blog categories &...
112
113
114
115
116
117
118
                  }
              }
              
              return $this->redirect(Url::toRoute(['blog/index']));
              
          }
          
1729300e   Timur Kastemirov   blog categories &...
119
          public function actionCategory($id) {
2309b955   Timur Kastemirov   blog categories &...
120
121
              
              $query = Article::find()
ec7e1832   Kateryna_Sarnytska   image_id moved to...
122
                              ->joinWith(["categories.lang", 'lang.image'])
2309b955   Timur Kastemirov   blog categories &...
123
124
125
126
127
128
129
130
131
132
133
134
135
                              ->where(
                                  [
                                      'blog_article.status' => true,
                                  ]
                              )
                              ->andWhere(
                                  [
                                      "blog_category.id" => $id,
                                      "blog_category.status" => true,
                                  ]
                              )
                              ->orderBy("sort");
      
1729300e   Timur Kastemirov   blog categories &...
136
137
              return $this->prepareProviderAndRender($query);
          }
2309b955   Timur Kastemirov   blog categories &...
138
      
1729300e   Timur Kastemirov   blog categories &...
139
140
          public function actionTag($id){
              $query = Article::find()
ec7e1832   Kateryna_Sarnytska   image_id moved to...
141
                              ->joinWith(["tags.lang", 'lang.image'])
1729300e   Timur Kastemirov   blog categories &...
142
143
144
145
146
147
148
149
150
                              ->where(
                                  [
                                      'blog_article.status' => true,
                                      'blog_tag.id' => $id,
                                  ]
                              )
                              ->orderBy('sort');
              
              return $this->prepareProviderAndRender($query);
2309b955   Timur Kastemirov   blog categories &...
151
152
          }
          
be160418   Alexey Boroda   blog index order ...
153
      }