Blame view

frontend/controllers/BlogController.php 4.5 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
8
9
      use yii\web\Controller;
      use artbox\weblog\models\Article;
      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
43
44
45
46
47
48
49
50
51
52
          public function actionIndex()
          {
      
              $query = Article::find()
                              ->where(
                                  [
                                      'status' => true,
                                  ]
                              )
                              ->orderBy("sort");
      
              return $this->prepareProviderAndRender($query);
          }
          
30a01994   Timur Kastemirov   blog
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
          public function actionArticle($id)
          {
              
              $model = $this->findModel($id);
              
              return $this->render(
                  'view',
                  [
                      'article' => $model,
                  ]
              );
              
          }
      
          protected function findModel($id)
          {
30a01994   Timur Kastemirov   blog
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
              $model = Article::find()
                              ->where(
                                  [
                                      'id' => $id
                                  ]
                              )
                              ->with("lang")
                              ->one();
              
              if ( $model !== NULL) {
                  return $model;
              }
              else {
                  throw new NotFoundHttpException('The requested page does not exist.');
              }
          }
          
1729300e   Timur Kastemirov   blog categories &...
86
87
88
          public function actionSearch()
          {
              
2309b955   Timur Kastemirov   blog categories &...
89
              if( \Yii::$app->request->isPost ){
1729300e   Timur Kastemirov   blog categories &...
90
  
2309b955   Timur Kastemirov   blog categories &...
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
                  $req = \Yii::$app->request;
                  if (!empty($req->post("title"))){
                      $title = Html::encode($req->post("title"));
                      $query = Article::find()
                                      ->joinWith("lang")
                                      ->where(
                                          [
                                              'status' => true,
                                          ]
                                      )
                                      ->andWhere(
                                          [
                                              "like", "lower(title)", $title
                                          ]
                                      )
                                      ->orderBy("sort");
      
1729300e   Timur Kastemirov   blog categories &...
108
                      return $this->prepareProviderAndRender($query);
2309b955   Timur Kastemirov   blog categories &...
109
110
111
112
113
114
115
                  }
              }
              
              return $this->redirect(Url::toRoute(['blog/index']));
              
          }
          
1729300e   Timur Kastemirov   blog categories &...
116
          public function actionCategory($id) {
2309b955   Timur Kastemirov   blog categories &...
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
              
              $query = Article::find()
                              ->joinWith("categories.lang")
                              ->where(
                                  [
                                      'blog_article.status' => true,
                                  ]
                              )
                              ->andWhere(
                                  [
                                      "blog_category.id" => $id,
                                      "blog_category.status" => true,
                                  ]
                              )
                              ->orderBy("sort");
      
1729300e   Timur Kastemirov   blog categories &...
133
134
              return $this->prepareProviderAndRender($query);
          }
2309b955   Timur Kastemirov   blog categories &...
135
      
1729300e   Timur Kastemirov   blog categories &...
136
137
138
139
140
141
142
143
144
145
146
147
          public function actionTag($id){
              $query = Article::find()
                              ->joinWith("tags.lang")
                              ->where(
                                  [
                                      'blog_article.status' => true,
                                      'blog_tag.id' => $id,
                                  ]
                              )
                              ->orderBy('sort');
              
              return $this->prepareProviderAndRender($query);
2309b955   Timur Kastemirov   blog categories &...
148
149
          }
          
30a01994   Timur Kastemirov   blog
150
      }