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; |
13
|
|
|
use frontend\models\ContactForm; |
14
|
|
|
use Yii; |
15
|
|
|
use yii\data\Pagination; |
16
|
|
|
use yii\filters\AccessControl; |
17
|
|
|
use yii\filters\VerbFilter; |
18
|
|
|
use yii\web\Controller; |
19
|
|
|
use yii\web\ForbiddenHttpException; |
20
|
|
|
use yii\web\NotFoundHttpException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class SiteController |
24
|
|
|
* |
25
|
|
|
* @author Agiel K. Saputra <[email protected]> |
26
|
|
|
* @since 0.1.0 |
27
|
|
|
*/ |
28
|
|
|
class SiteController extends Controller |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @inheritdoc |
32
|
|
|
*/ |
33
|
|
View Code Duplication |
public function behaviors() |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
return [ |
36
|
|
|
'access' => [ |
37
|
|
|
'class' => AccessControl::className(), |
38
|
|
|
'only' => ['logout'], |
39
|
|
|
'rules' => [ |
40
|
|
|
[ |
41
|
|
|
'actions' => ['logout'], |
42
|
|
|
'allow' => true, |
43
|
|
|
'roles' => ['@'], |
44
|
|
|
], |
45
|
|
|
], |
46
|
|
|
], |
47
|
|
|
'verbs' => [ |
48
|
|
|
'class' => VerbFilter::className(), |
49
|
|
|
'actions' => [ |
50
|
|
|
'logout' => ['post'], |
51
|
|
|
], |
52
|
|
|
], |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @inheritdoc |
58
|
|
|
*/ |
59
|
|
|
public function actions() |
60
|
|
|
{ |
61
|
|
|
return [ |
62
|
|
|
'error' => [ |
63
|
|
|
'class' => 'yii\web\ErrorAction', |
64
|
|
|
], |
65
|
|
|
'captcha' => [ |
66
|
|
|
'class' => 'yii\captcha\CaptchaAction', |
67
|
|
|
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, |
68
|
|
|
], |
69
|
|
|
]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Render home page of the site. |
74
|
|
|
* |
75
|
|
|
* @throws \yii\web\NotFoundHttpException |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
public function actionIndex() |
79
|
|
|
{ |
80
|
|
|
/* @var $post \common\models\Post */ |
81
|
|
|
|
82
|
|
|
$query = Post::find() |
83
|
|
|
->from(['t' => Post::tableName()]) |
84
|
|
|
->andWhere(['status' => 'publish']) |
85
|
|
|
->andWhere(['<=', 'date', date('Y-m-d H:i:s')]) |
86
|
|
|
->orderBy(['t.date' => SORT_DESC]); |
87
|
|
|
|
88
|
|
|
if (Option::get('show_on_front') == 'page' && $frontPage = Option::get('front_page')) { |
89
|
|
|
$render = '/post/view'; |
90
|
|
|
$comment = new PostComment(); |
91
|
|
|
$query = $query->andWhere(['id' => $frontPage]); |
92
|
|
|
if ($post = $query->one()) { |
93
|
|
|
if (is_file($this->view->theme->basePath . '/post/view-' . $post->postType->slug . '.php')) { |
94
|
|
|
$render = '/post/view-' . $post->postType->slug; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->render($render, [ |
98
|
|
|
'post' => $post, |
99
|
|
|
'comment' => $comment, |
100
|
|
|
]); |
101
|
|
|
} |
102
|
|
|
throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.')); |
103
|
|
|
} else { |
104
|
|
|
if (Option::get('front_post_type') !== 'all') { |
105
|
|
|
$query->innerJoinWith(['postType'])->andWhere(['name' => Option::get('front_post_type')]); |
106
|
|
|
} |
107
|
|
|
$countQuery = clone $query; |
108
|
|
|
$pages = new Pagination([ |
109
|
|
|
'totalCount' => $countQuery->count(), |
110
|
|
|
'pageSize' => Option::get('posts_per_page'), |
111
|
|
|
]); |
112
|
|
|
$query->offset($pages->offset)->limit($pages->limit); |
113
|
|
|
if ($posts = $query->all()) { |
114
|
|
|
return $this->render('index', [ |
115
|
|
|
'posts' => $posts, |
116
|
|
|
'pages' => isset($pages) ? $pages : null, |
117
|
|
|
]); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.')); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return \yii\web\Response |
126
|
|
|
*/ |
127
|
|
|
public function actionLogout() |
128
|
|
|
{ |
129
|
|
|
Yii::$app->user->logout(); |
130
|
|
|
|
131
|
|
|
return $this->goHome(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return string|\yii\web\Response |
136
|
|
|
*/ |
137
|
|
|
public function actionContact() |
138
|
|
|
{ |
139
|
|
|
$model = new ContactForm(); |
140
|
|
|
|
141
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->validate()) { |
142
|
|
|
if ($model->sendEmail(Option::get('admin_email'))) { |
143
|
|
|
Yii::$app->session->setFlash( |
144
|
|
|
'success', |
145
|
|
|
'Thank you for contacting us. We will respond to you as soon as possible.' |
146
|
|
|
); |
147
|
|
|
} else { |
148
|
|
|
Yii::$app->session->setFlash('error', 'There was an error sending email.'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $this->refresh(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $this->render('contact', [ |
155
|
|
|
'model' => $model, |
156
|
|
|
]); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Search post by title and content |
161
|
|
|
* |
162
|
|
|
* @param string $s Keyword to search posts. |
163
|
|
|
* @return string |
164
|
|
|
* @throws \yii\web\NotFoundHttpException |
165
|
|
|
*/ |
166
|
|
|
public function actionSearch($s) |
167
|
|
|
{ |
168
|
|
|
$query = Post::find() |
169
|
|
|
->orWhere(['like', 'title', $s]) |
170
|
|
|
->orWhere(['like', 'content', $s]) |
171
|
|
|
->andWhere(['status' => 'publish']) |
172
|
|
|
->andWhere(['<=', 'date', date('Y-m-d H:i:s')]) |
173
|
|
|
->orderBy(['date' => SORT_DESC]); |
174
|
|
|
$countQuery = clone $query; |
175
|
|
|
$pages = new Pagination([ |
176
|
|
|
'totalCount' => $countQuery->count(), |
177
|
|
|
'pageSize' => Option::get('posts_per_page'), |
178
|
|
|
]); |
179
|
|
|
$query->offset($pages->offset)->limit($pages->limit); |
180
|
|
|
$posts = $query->all(); |
181
|
|
|
|
182
|
|
|
if ($posts) { |
|
|
|
|
183
|
|
|
return $this->render('/site/search', [ |
184
|
|
|
'posts' => $posts, |
185
|
|
|
'pages' => $pages, |
186
|
|
|
's' => $s, |
187
|
|
|
]); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.')); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @throws \yii\web\ForbiddenHttpException |
195
|
|
|
*/ |
196
|
|
|
public function actionForbidden() |
197
|
|
|
{ |
198
|
|
|
throw new ForbiddenHttpException(Yii::t('writesdown', 'You are not allowed to perform this action.')); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @throws \yii\web\NotFoundHttpException |
203
|
|
|
*/ |
204
|
|
|
public function actionNotFound() |
205
|
|
|
{ |
206
|
|
|
throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.')); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
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.