|
1
|
|
|
<?php |
|
2
|
|
|
namespace App\Http\Api\Frontend\Controller; |
|
3
|
|
|
|
|
4
|
|
|
use App\Http\Api\Form\ArticleDislikeForm; |
|
5
|
|
|
use App\Http\Api\Form\ArticleLikeForm; |
|
6
|
|
|
use App\Http\Api\Model\Article; |
|
7
|
|
|
use App\Model\StatusInterface; |
|
8
|
|
|
use yii\base\DynamicModel; |
|
9
|
|
|
|
|
10
|
|
|
class ArticleController extends ActiveController |
|
11
|
|
|
{ |
|
12
|
|
|
public $modelClass = Article::class; |
|
13
|
|
|
|
|
14
|
|
|
public function behaviors() |
|
15
|
|
|
{ |
|
16
|
|
|
$behaviors = parent::behaviors(); |
|
17
|
|
|
|
|
18
|
|
|
$behaviors['authenticator']['optional'] = ['options', 'index']; |
|
19
|
|
|
|
|
20
|
|
|
return $behaviors; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function getQuery($action) |
|
24
|
|
|
{ |
|
25
|
|
|
return parent::getQuery($action) |
|
26
|
|
|
->alias('a') |
|
27
|
|
|
->orderBy([ |
|
28
|
|
|
'a.release_time' => SORT_DESC |
|
29
|
|
|
]) |
|
30
|
|
|
->andWhere([ |
|
31
|
|
|
'a.is_deleted' => 0, |
|
32
|
|
|
'a.status' => StatusInterface::STATUS_ACTIVE, |
|
33
|
|
|
]) |
|
34
|
|
|
->andWhere(['<=', 'a.release_time', time()]); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function searchModel() |
|
38
|
|
|
{ |
|
39
|
|
|
return (new DynamicModel(['title' => '', 'author' => '', 'summary' => ''])) |
|
40
|
|
|
->addRule(['title', 'author', 'summary'], 'string'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
protected function applyFilter($query, $model, $filter) |
|
44
|
|
|
{ |
|
45
|
|
|
foreach (['title', 'author', 'summary'] as $name) { |
|
46
|
|
|
if (!empty($model->$name)) { |
|
47
|
|
|
$query->andWhere(['LIKE', 'a.' . $name, $model->$name]); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Likes an article. |
|
54
|
|
|
*/ |
|
55
|
|
|
public function actionLike($id) |
|
56
|
|
|
{ |
|
57
|
|
|
$model = new ArticleLikeForm(); |
|
58
|
|
|
$model->load(['id' => $id], ''); |
|
59
|
|
|
return $model->handle(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Dislikes an article. |
|
64
|
|
|
*/ |
|
65
|
|
|
public function actionDislike($id) |
|
66
|
|
|
{ |
|
67
|
|
|
$model = new ArticleDislikeForm(); |
|
68
|
|
|
$model->load(['id' => $id], ''); |
|
69
|
|
|
return $model->handle(); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|