Media::rules()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 22
rs 9.2
cc 1
eloc 15
nc 1
nop 0
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 common\models\search;
9
10
use common\models\Media as MediaModel;
11
use Yii;
12
use yii\base\Model;
13
use yii\data\ActiveDataProvider;
14
use yii\helpers\ArrayHelper;
15
16
/**
17
 * Media represents the model behind the search form about `common\models\Media`.
18
 *
19
 * @author Agiel K. Saputra <[email protected]>
20
 * @since 0.1.0
21
 */
22
class Media extends MediaModel
23
{
24
    /**
25
     * @inheritdoc
26
     */
27
    public function rules()
28
    {
29
        return [
30
            [['id', 'author', 'post_id', 'comment_count'], 'integer'],
31
            [
32
                [
33
                    'title',
34
                    'excerpt',
35
                    'content',
36
                    'password',
37
                    'date',
38
                    'modified',
39
                    'slug',
40
                    'mime_type',
41
                    'comment_status',
42
                    'username',
43
                    'post_title',
44
                ],
45
                'safe',
46
            ],
47
        ];
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function scenarios()
54
    {
55
        // bypass scenarios() implementation in the parent class
56
        return Model::scenarios();
57
    }
58
59
    /**
60
     * Creates data provider instance with search query applied
61
     *
62
     * @param array $params
63
     * @return ActiveDataProvider
64
     */
65
    public function search($params)
66
    {
67
        $query = MediaModel::find();
68
        $query->innerJoinWith([
69
            'mediaAuthor' => function ($query) {
70
                /* @var $query \yii\db\ActiveQuery */
71
                return $query->from(['user' => User::tableName()]);
72
            },
73
        ])->from(['media' => $this->tableName()]);
74
        $query->leftJoin(['post' => Post::tableName()], 'media.post_id = post.id');
75
76
        $dataProvider = new ActiveDataProvider([
77
            'query' => $query,
78
        ]);
79
80
        $dataProvider->setSort([
81
            'attributes' => ArrayHelper::merge($dataProvider->sort->attributes, [
82
                'username' => [
83
                    'asc' => ['username' => SORT_ASC],
84
                    'desc' => ['username' => SORT_DESC],
85
                    'label' => 'Author',
86
                    'value' => 'username',
87
                ],
88
            ]),
89
            'defaultOrder' => ['id' => SORT_DESC],
90
        ]);
91
92
        $this->load($params);
93
94
        if (!$this->validate()) {
95
            return $dataProvider;
96
        }
97
98
        $query->andFilterWhere([
99
            'media.id' => $this->id,
100
            'media.author' => $this->author,
101
            'post_id' => $this->post_id,
102
            'media.comment_count' => $this->comment_count,
103
        ]);
104
105
        $query->andFilterWhere(['like', 'media.title', $this->title])
106
            ->andFilterWhere(['like', 'media.excerpt', $this->excerpt])
107
            ->andFilterWhere(['like', 'media.content', $this->content])
108
            ->andFilterWhere(['like', 'media.password', $this->password])
109
            ->andFilterWhere(['like', 'media.slug', $this->slug])
110
            ->andFilterWhere(['like', 'mime_type', $this->mime_type])
111
            ->andFilterWhere(['like', 'media.comment_status', $this->comment_status])
112
            ->andFilterWhere(['like', 'media.date', $this->date])
113
            ->andFilterWhere(['like', 'media.modified', $this->modified])
114
            ->andFilterWhere(['like', 'post.title', $this->post_title])
115
            ->andFilterWhere(['like', 'user.username', $this->username]);
116
117
        return $dataProvider;
118
    }
119
}
120