AuthItem::attributeLabels()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 9
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\ArrayDataProvider;
8
use toir427\admin\components\Configs;
9
use yii\rbac\Item;
10
11
/**
12
 * AuthItemSearch represents the model behind the search form about AuthItem.
13
 * 
14
 * @author Misbahul D Munir <[email protected]>
15
 * @since 1.0
16
 */
17
class AuthItem extends Model
18
{
19
    const TYPE_ROUTE = 101;
20
21
    public $name;
22
    public $type;
23
    public $description;
24
    public $ruleName;
25
    public $data;
26
27
    /**
28
     * @inheritdoc
29
     */
30
    public function rules()
31
    {
32
        return [
33
            [['name', 'ruleName', 'description'], 'safe'],
34
            [['type'], 'integer'],
35
        ];
36
    }
37
38
    /**
39
     * @inheritdoc
40
     */
41
    public function attributeLabels()
42
    {
43
        return [
44
            'name' => Yii::t('rbac-admin', 'Name'),
45
            'item_name' => Yii::t('rbac-admin', 'Name'),
46
            'type' => Yii::t('rbac-admin', 'Type'),
47
            'description' => Yii::t('rbac-admin', 'Description'),
48
            'ruleName' => Yii::t('rbac-admin', 'Rule Name'),
49
            'data' => Yii::t('rbac-admin', 'Data'),
50
        ];
51
    }
52
53
    /**
54
     * Search authitem
55
     * @param array $params
56
     * @return \yii\data\ActiveDataProvider|\yii\data\ArrayDataProvider
57
     */
58
    public function search($params)
59
    {
60
        /* @var \yii\rbac\Manager $authManager */
61
        $authManager = Configs::authManager();
62
        if ($this->type == Item::TYPE_ROLE) {
63
            $items = $authManager->getRoles();
64
        } else {
65
            $items = array_filter($authManager->getPermissions(), function($item) {
66
                return $this->type == Item::TYPE_PERMISSION xor strncmp($item->name, '/', 1) === 0;
67
            });
68
        }
69
        $this->load($params);
70
        if ($this->validate()) {
71
72
            $search = mb_strtolower(trim($this->name));
73
            $desc = mb_strtolower(trim($this->description));
74
            $ruleName = $this->ruleName;
75
            foreach ($items as $name => $item) {
76
                $f = (empty($search) || mb_strpos(mb_strtolower($item->name), $search) !== false) &&
77
                    (empty($desc) || mb_strpos(mb_strtolower($item->description), $desc) !== false) &&
78
                    (empty($ruleName) || $item->ruleName == $ruleName);
79
                if (!$f) {
80
                    unset($items[$name]);
81
                }
82
            }
83
        }
84
85
        return new ArrayDataProvider([
86
            'allModels' => $items,
87
        ]);
88
    }
89
}
90