Completed
Push — master ( 9b9020...8f5910 )
by Razon
02:07
created

ArticleController::behaviors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace App\Http\Api\Frontend\Controller;
3
4
use App\Http\Api\Model\Article;
5
use yii\base\DynamicModel;
6
use App\Model\StatusInterface;
7
8
class ArticleController extends ActiveController
9
{
10
    public $modelClass = Article::class;
11
12
    public function behaviors()
13
    {
14
        $behaviors = parent::behaviors();
15
16
        $behaviors['authenticator']['optional'] = ['options', 'index'];
17
18
        return $behaviors;
19
    }
20
    
21
    public function getQuery($action)
22
    {
23
        return parent::getQuery($action)
24
            ->alias('a')
25
            ->orderBy([
26
                'a.release_time' => SORT_DESC
27
            ])
28
            ->andWhere([
29
                'a.is_deleted' => 0,
30
                'a.status' => StatusInterface::STATUS_ACTIVE,
31
            ])
32
            ->andWhere(['<=', 'a.release_time', time()]);
33
    }
34
35
    public function searchModel()
36
    {
37
        return (new DynamicModel(['title' => '', 'author' => '', 'summary' => '']))
38
            ->addRule(['title', 'author', 'summary'], 'string');
39
    }
40
41
    protected function applyFilter($query, $model, $filter)
42
    {
43
        foreach (['title', 'author', 'summary'] as $name) {
44
            if (!empty($model->$name)) {
45
                $query->andWhere(['LIKE', 'a.' . $name, $model->$name]);
46
            }
47
        }
48
    }
49
}
50