Menu::scenarios()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace toir427\admin\models\searchs;
4
5
use Yii;
6
use yii\base\Model;
7
use yii\data\ActiveDataProvider;
8
use toir427\admin\models\Menu as MenuModel;
9
10
/**
11
 * Menu represents the model behind the search form about [[\toir427\admin\models\Menu]].
12
 * 
13
 * @author Misbahul D Munir <[email protected]>
14
 * @since 1.0
15
 */
16
class Menu extends MenuModel
17
{
18
19
    /**
20
     * @inheritdoc
21
     */
22
    public function rules()
23
    {
24
        return [
25
            [['id', 'parent', 'order'], 'integer'],
26
            [['name', 'route', 'parent_name'], 'safe'],
27
        ];
28
    }
29
30
    /**
31
     * @inheritdoc
32
     */
33
    public function scenarios()
34
    {
35
        // bypass scenarios() implementation in the parent class
36
        return Model::scenarios();
37
    }
38
39
    /**
40
     * Searching menu
41
     * @param  array $params
42
     * @return \yii\data\ActiveDataProvider
43
     */
44
    public function search($params)
45
    {
46
        $query = MenuModel::find()
47
            ->from(MenuModel::tableName() . ' t')
48
            ->joinWith(['menuParent' => function ($q) {
49
            $q->from(MenuModel::tableName() . ' parent');
50
        }]);
51
52
        $dataProvider = new ActiveDataProvider([
53
            'query' => $query
54
        ]);
55
56
        $sort = $dataProvider->getSort();
57
        $sort->attributes['menuParent.name'] = [
58
            'asc' => ['parent.name' => SORT_ASC],
59
            'desc' => ['parent.name' => SORT_DESC],
60
            'label' => 'parent',
61
        ];
62
        $sort->attributes['order'] = [
63
            'asc' => ['parent.order' => SORT_ASC, 't.order' => SORT_ASC],
64
            'desc' => ['parent.order' => SORT_DESC, 't.order' => SORT_DESC],
65
            'label' => 'order',
66
        ];
67
        $sort->defaultOrder = ['menuParent.name' => SORT_ASC];
68
69
        if (!($this->load($params) && $this->validate())) {
70
            return $dataProvider;
71
        }
72
73
        $query->andFilterWhere([
74
            't.id' => $this->id,
75
            't.parent' => $this->parent,
76
        ]);
77
78
        $query->andFilterWhere(['like', 'lower(t.name)', strtolower($this->name)])
79
            ->andFilterWhere(['like', 't.route', $this->route])
80
            ->andFilterWhere(['like', 'lower(parent.name)', strtolower($this->parent_name)]);
81
82
        return $dataProvider;
83
    }
84
}
85