c237629a
Anastasia
first commit
|
1
2
3
4
5
6
7
8
|
<?php
namespace frontend\controllers;
use common\models\blog\Article;
use common\models\blog\Category;
use common\models\blog\Tag;
use yii\data\ActiveDataProvider;
|
68e72651
Anastasia
- blog
|
9
10
|
use yii\db\ActiveQuery;
use yii\helpers\ArrayHelper;
|
c237629a
Anastasia
first commit
|
11
12
13
14
15
16
17
18
19
20
|
use yii\web\Controller;
use yii\web\NotFoundHttpException;
/**
* Class BlogController
*
* @package frontend\controllers
*/
class BlogController extends Controller
{
|
68e72651
Anastasia
- blog
|
21
|
public function actionIndex()
|
c237629a
Anastasia
first commit
|
22
|
{
|
c237629a
Anastasia
first commit
|
23
24
25
26
27
28
29
30
31
32
|
$categories = Category::find()
->with(
[
'language',
]
)
->where(['status' => true])
->orderBy([ 'sort' => SORT_ASC ])
->all();
|
df88ecc9
Anastasia
- index package
|
33
|
$data = ArrayHelper::map($categories, 'language.alias.value', 'language.title');
|
c237629a
Anastasia
first commit
|
34
35
36
37
38
39
40
41
42
43
44
45
46
|
$dataProvider = new ActiveDataProvider(
[
'query' => Article::find()
->orderBy(
[
'created_at' => SORT_DESC,
]
)
->with(
[
'categories.language',
]
|
68e72651
Anastasia
- blog
|
47
48
49
|
)->with(['comments' => function (ActiveQuery $query){
$query->andWhere(['status' => true]);
}])
|
c237629a
Anastasia
first commit
|
50
51
|
->joinWith('language')
->where([ 'blog_article.status' => true ])
|
c237629a
Anastasia
first commit
|
52
53
|
->distinct(),
'pagination' => [
|
68e72651
Anastasia
- blog
|
54
|
'pageSize' => 6,
|
c237629a
Anastasia
first commit
|
55
56
57
58
59
60
61
|
],
]
);
return $this->render(
'index',
[
|
68e72651
Anastasia
- blog
|
62
|
'categories' => $data,
|
c237629a
Anastasia
first commit
|
63
64
65
66
67
|
'dataProvider' => $dataProvider,
]
);
}
|
68e72651
Anastasia
- blog
|
68
|
public function actionView($id)
|
c237629a
Anastasia
first commit
|
69
|
{
|
68e72651
Anastasia
- blog
|
70
71
72
73
74
75
76
77
78
|
$model = $this->findModel($id);
$model->views +=1;
$model->save();
$tags = Tag::find()
->with([ 'language' ])
->orderBy([ 'sort' => SORT_ASC ])
->all();
|
c237629a
Anastasia
first commit
|
79
|
return $this->render(
|
68e72651
Anastasia
- blog
|
80
81
82
83
84
|
'view',
[
'tags' => $tags,
'model' => $model,
]
|
c237629a
Anastasia
first commit
|
85
86
87
88
89
|
);
}
public function actionCategory($id)
{
|
df88ecc9
Anastasia
- index package
|
90
91
92
93
94
95
96
97
98
99
|
$categories = Category::find()
->with(
[
'language',
]
)
->where(['status' => true])
->orderBy([ 'sort' => SORT_ASC ])
->all();
$data = ArrayHelper::map($categories, 'language.alias.value', 'language.title');
|
c237629a
Anastasia
first commit
|
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
/**
* @var Category $model
*/
$model = Category::find()
->where(
[
'id' => $id,
]
)
->with(
[
'articles',
]
)
->where(['status' => true])
->orderBy([ 'sort' => SORT_ASC ])
->one();
$dataProvider = new ActiveDataProvider(
[
'query' => $model->getArticles()
->with(
[
'language',
'categories.language',
]
)
->where(['blog_article.status' => true])
->orderBy(
[
'created_at' => SORT_DESC,
]
),
'pagination' => [
'pageSize' => 3,
],
]
);
return $this->render(
'category',
[
|
c237629a
Anastasia
first commit
|
143
144
|
'dataProvider' => $dataProvider,
'model' => $model,
|
df88ecc9
Anastasia
- index package
|
145
|
'categories' => $data
|
c237629a
Anastasia
first commit
|
146
147
148
149
150
151
|
]
);
}
public function actionTag($id)
{
|
df88ecc9
Anastasia
- index package
|
152
153
154
155
156
157
158
159
160
161
|
$categories = Category::find()
->with(
[
'language',
]
)
->where(['status' => true])
->orderBy([ 'sort' => SORT_ASC ])
->all();
$data = ArrayHelper::map($categories, 'language.alias.value', 'language.title');
|
c237629a
Anastasia
first commit
|
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
/**
* @var Category $model
*/
$model = Tag::find()
->where(
[
'id' => $id,
]
)
->with(
[
'articles',
]
)
->one();
$dataProvider = new ActiveDataProvider(
[
'query' => $model->getArticles()
->with(
[
'language',
'categories.language',
]
)
->where(['blog_article.status' => true])
->orderBy(
[
'created_at' => SORT_DESC,
]
),
'pagination' => [
'pageSize' => 3,
],
]
);
return $this->render(
'tag',
[
|
c237629a
Anastasia
first commit
|
203
204
|
'dataProvider' => $dataProvider,
'model' => $model,
|
df88ecc9
Anastasia
- index package
|
205
|
'categories' => $data
|
c237629a
Anastasia
first commit
|
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
]
);
}
/**
* @param $id
*
* @return Article
* @throws \yii\web\NotFoundHttpException
*/
protected function findModel($id)
{
/**
* @var Article | null $model
*/
$model = Article::find()
->where([ 'id' => $id ])
->with(
[
'language',
'categories.language',
'tags.language',
]
)
->andWhere([ 'status' => true ])
->one();
if (empty($model)) {
throw new NotFoundHttpException(\Yii::t('app', 'Article not found'));
} else {
return $model;
}
}
}
|