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\Term; |
12
|
|
|
use Yii; |
13
|
|
|
use yii\data\Pagination; |
14
|
|
|
use yii\web\Controller; |
15
|
|
|
use yii\web\NotFoundHttpException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class TermController |
19
|
|
|
* |
20
|
|
|
* @author Agiel K. Saputra <[email protected]> |
21
|
|
|
* @since 0.1.0 |
22
|
|
|
*/ |
23
|
|
|
class TermController extends Controller |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Displays a single Term model. |
27
|
|
|
* |
28
|
|
|
* @param integer $id Term ID |
29
|
|
|
* @param null $slug Term slug |
30
|
|
|
* @throws \yii\web\NotFoundHttpException |
31
|
|
|
* @return mixed |
32
|
|
|
*/ |
33
|
|
|
public function actionView($id = null, $slug = null) |
34
|
|
|
{ |
35
|
|
|
$render = 'view'; |
36
|
|
|
|
37
|
|
|
if ($id) { |
|
|
|
|
38
|
|
|
$model = $this->findModel($id); |
39
|
|
|
} elseif ($slug) { |
40
|
|
|
$model = $this->findModelBySlug($slug); |
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
|
|
|
$posts = $query->offset($pages->offset) |
55
|
|
|
->limit($pages->limit) |
56
|
|
|
->all(); |
57
|
|
|
|
58
|
|
|
if (is_file($this->view->theme->basePath . '/term/view-' . $model->taxonomy->name . '.php')) { |
59
|
|
|
$render = 'view-' . $model->taxonomy->name; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $this->render($render, [ |
63
|
|
|
'posts' => $posts, |
64
|
|
|
'pages' => $pages, |
65
|
|
|
'term' => $model, |
66
|
|
|
]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Finds the Post model based on its primary key value. |
71
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
72
|
|
|
* |
73
|
|
|
* @param integer $id Term ID |
74
|
|
|
* @return Term the loaded model |
75
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
76
|
|
|
*/ |
77
|
|
|
protected function findModel($id) |
78
|
|
|
{ |
79
|
|
|
$model = Term::findOne(['id' => $id]); |
80
|
|
|
|
81
|
|
|
if ($model) { |
82
|
|
|
return $model; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.')); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Finds the Post model based on its primary key value. |
90
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
91
|
|
|
* |
92
|
|
|
* @param string $slug Term Slug |
93
|
|
|
* @throws \yii\web\NotFoundHttpException |
94
|
|
|
* @internal param string $postslug |
95
|
|
|
* @return Term the loaded model |
96
|
|
|
*/ |
97
|
|
|
protected function findModelBySlug($slug) |
98
|
|
|
{ |
99
|
|
|
$model = Term::findOne(['slug' => $slug]); |
100
|
|
|
|
101
|
|
|
if ($model) { |
102
|
|
|
return $model; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.')); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: