1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.writesdown.com/ |
4
|
|
|
* @copyright Copyright (c) 2015 WritesDown |
5
|
|
|
* @license http://www.writesdown.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace frontend\controllers; |
9
|
|
|
|
10
|
|
|
use common\models\Option; |
11
|
|
|
use common\models\Post; |
12
|
|
|
use common\models\PostComment as Comment; |
13
|
|
|
use common\models\PostType; |
14
|
|
|
use Yii; |
15
|
|
|
use yii\data\Pagination; |
16
|
|
|
use yii\web\Controller; |
17
|
|
|
use yii\web\NotFoundHttpException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class PostController |
21
|
|
|
* |
22
|
|
|
* @author Agiel K. Saputra <[email protected]> |
23
|
|
|
* @since 0.1.0 |
24
|
|
|
*/ |
25
|
|
|
class PostController extends Controller |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @param int|null $id Post type ID |
29
|
|
|
* @param string|null $slug Post type slug. |
30
|
|
|
* @return string |
31
|
|
|
* @throws \yii\web\NotFoundHttpException |
32
|
|
|
*/ |
33
|
|
View Code Duplication |
public function actionIndex($id = null, $slug = null) |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$render = 'index'; |
36
|
|
|
|
37
|
|
|
if ($id) { |
|
|
|
|
38
|
|
|
$postType = $this->findPostType($id); |
39
|
|
|
} elseif ($slug) { |
|
|
|
|
40
|
|
|
$postType = $this->findPostTypeBySlug($slug); |
41
|
|
|
} else { |
42
|
|
|
throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.')); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$query = $postType->getPosts() |
|
|
|
|
46
|
|
|
->andWhere(['status' => 'publish']) |
47
|
|
|
->andWhere(['<=', 'date', date('Y-m-d H:i:s')]) |
48
|
|
|
->orderBy(['id' => SORT_DESC]); |
49
|
|
|
$countQuery = clone $query; |
50
|
|
|
$pages = new Pagination([ |
51
|
|
|
'totalCount' => $countQuery->count(), |
52
|
|
|
'pageSize' => Option::get('posts_per_page'), |
53
|
|
|
]); |
54
|
|
|
$query->offset($pages->offset)->limit($pages->limit); |
55
|
|
|
$posts = $query->all(); |
56
|
|
|
|
57
|
|
|
if ($posts) { |
58
|
|
|
if (is_file($this->view->theme->basePath . '/post/index-' . $postType->name . '.php')) { |
59
|
|
|
$render = 'index-' . $postType->name . '.php'; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $this->render($render, [ |
63
|
|
|
'postType' => $postType, |
64
|
|
|
'posts' => $posts, |
65
|
|
|
'pages' => $pages, |
66
|
|
|
]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.')); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Displays a single Post model. |
74
|
|
|
* |
75
|
|
|
* @param null $slug Post slug |
76
|
|
|
* @param integer $id Post ID |
77
|
|
|
* @throws \yii\web\NotFoundHttpException |
78
|
|
|
* @return mixed |
79
|
|
|
*/ |
80
|
|
|
public function actionView($id = null, $slug = null) |
81
|
|
|
{ |
82
|
|
|
$render = 'view'; |
83
|
|
|
$comment = new Comment(); |
84
|
|
|
|
85
|
|
|
if ($id) { |
|
|
|
|
86
|
|
|
$model = $this->findModel($id); |
87
|
|
|
} elseif ($slug) { |
88
|
|
|
$model = $this->findModelBySlug($slug); |
89
|
|
|
} else { |
90
|
|
|
throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.')); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
View Code Duplication |
if ($comment->load(Yii::$app->request->post()) && $comment->save()) { |
|
|
|
|
94
|
|
|
if (!$comment->parent) { |
95
|
|
|
$model->comment_count++; |
96
|
|
|
} |
97
|
|
|
if ($model->save()) { |
98
|
|
|
$this->refresh(); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
View Code Duplication |
if ($model->password && $model->password !== Yii::$app->request->post('password')) { |
|
|
|
|
103
|
|
|
return $this->render('protected', ['post' => $model]); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (is_file($this->view->theme->basePath . '/post/view-' . $model->postType->name . '.php')) { |
107
|
|
|
$render = 'view-' . $model->postType->name . '.php'; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $this->render($render, [ |
111
|
|
|
'post' => $model, |
112
|
|
|
'comment' => $comment, |
113
|
|
|
]); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Finds the Post model based on its primary key value. |
118
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
119
|
|
|
* |
120
|
|
|
* @param integer $id |
121
|
|
|
* @return Post the loaded model |
122
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
123
|
|
|
*/ |
124
|
|
View Code Duplication |
protected function findModel($id) |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
$model = Post::find() |
|
|
|
|
127
|
|
|
->andWhere(['id' => $id, 'status' => 'publish']) |
128
|
|
|
->andWhere(['<=', 'date', date('Y-m-d H:i:s')]) |
129
|
|
|
->one(); |
130
|
|
|
|
131
|
|
|
if ($model) { |
132
|
|
|
return $model; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.')); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Finds the Post model based on its primary key value. |
141
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
142
|
|
|
* |
143
|
|
|
* @param string $slug |
144
|
|
|
* @return Post the loaded model |
145
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
146
|
|
|
*/ |
147
|
|
View Code Duplication |
protected function findModelBySlug($slug) |
|
|
|
|
148
|
|
|
{ |
149
|
|
|
$model = Post::find() |
|
|
|
|
150
|
|
|
->andWhere(['slug' => $slug, 'status' => 'publish']) |
151
|
|
|
->andWhere(['<=', 'date', date('Y-m-d H:i:s')]) |
152
|
|
|
->one(); |
153
|
|
|
|
154
|
|
|
if ($model) { |
155
|
|
|
return $model; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.')); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Finds the Post model based on its primary key value. |
163
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
164
|
|
|
* |
165
|
|
|
* @param $id |
166
|
|
|
* @throws \yii\web\NotFoundHttpException |
167
|
|
|
* @return PostType the loaded model |
168
|
|
|
*/ |
169
|
|
|
protected function findPostType($id) |
170
|
|
|
{ |
171
|
|
|
$model = PostType::findOne($id); |
172
|
|
|
|
173
|
|
|
if ($model) { |
174
|
|
|
return $model; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.')); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Finds the Post model based on its primary key value. |
182
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
183
|
|
|
* |
184
|
|
|
* @param string $slug Post type slug |
185
|
|
|
* @throws \yii\web\NotFoundHttpException |
186
|
|
|
* @return PostType the loaded model |
187
|
|
|
*/ |
188
|
|
|
protected function findPostTypeBySlug($slug) |
189
|
|
|
{ |
190
|
|
|
$model = PostType::findOne(['slug' => $slug]); |
191
|
|
|
|
192
|
|
|
if ($model) { |
193
|
|
|
return $model; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.')); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.