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\User; |
12
|
|
|
use Yii; |
13
|
|
|
use yii\data\Pagination; |
14
|
|
|
use yii\web\Controller; |
15
|
|
|
use yii\web\NotFoundHttpException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class UserController |
19
|
|
|
* |
20
|
|
|
* @author Agiel K. Saputra <[email protected]> |
21
|
|
|
* @since 0.1.0 |
22
|
|
|
*/ |
23
|
|
|
class UserController extends Controller |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Displays a single User model. |
27
|
|
|
* |
28
|
|
|
* @param null $id User ID |
29
|
|
|
* @param string|null $user Username |
30
|
|
|
* @return mixed |
31
|
|
|
* @throws \yii\web\NotFoundHttpException |
32
|
|
|
*/ |
33
|
|
View Code Duplication |
public function actionView($id = null, $user = null) |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$render = '/user/view'; |
36
|
|
|
|
37
|
|
|
if ($id) { |
38
|
|
|
$model = $this->findModel($id); |
39
|
|
|
} elseif ($user) { |
|
|
|
|
40
|
|
|
$model = $this->findModelByUsername($user); |
41
|
|
|
} else { |
42
|
|
|
throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.')); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$query = $model->getPosts() |
|
|
|
|
46
|
|
|
->andWhere(['status' => 'publish']) |
47
|
|
|
->andWhere(['<=', 'date', date('Y-m-d H:i:s')]) |
48
|
|
|
->orderBy(['date' => 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 . '/user/view-' . $model->username . '.php')) { |
59
|
|
|
$render = 'view-' . $model->username . '.php'; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $this->render($render, [ |
63
|
|
|
'user' => $model, |
64
|
|
|
'posts' => $posts, |
65
|
|
|
'pages' => isset($pages) ? $pages : null, |
66
|
|
|
]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.')); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Finds the User model based on its primary key value. |
74
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
75
|
|
|
* |
76
|
|
|
* @param int $id User ID |
77
|
|
|
* @return User the loaded model |
78
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
protected function findModel($id) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
if (($model = User::findOne($id)) !== null) { |
83
|
|
|
return $model; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.')); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Finds the User model based on its primary key value. |
91
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
92
|
|
|
* |
93
|
|
|
* @param string $username Username |
94
|
|
|
* @return User the loaded model |
95
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
96
|
|
|
*/ |
97
|
|
View Code Duplication |
protected function findModelByUsername($username) |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
if (($model = User::findOne(['username' => $username])) !== null) { |
100
|
|
|
return $model; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.')); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
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.