Dictionary::search()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 26
rs 9.7666
c 0
b 0
f 0
cc 3
nc 4
nop 0
1
<?php
2
/**
3
 * User: execut
4
 * Date: 25.07.16
5
 * Time: 14:55
6
 */
7
8
namespace execut\import\models;
9
10
11
use execut\yii\base\Model;
12
use yii\data\ActiveDataProvider;
13
use yii\db\ActiveQuery;
14
15
class Dictionary extends Model
16
{
17
    public $type = null;
18
    public $name = null;
19
20
    public function rules()
21
    {
22
        return [
23
            [['type', 'name'], 'safe'],
24
        ];
25
    }
26
27
    public function attributes()
28
    {
29
        return [
30
            'name',
31
            'type',
32
        ];
33
    }
34
    
35
    public static function primaryKey() {
36
        return ['id'];
37
    }
38
39
    public function search() {
40
        $types = SettingsSheet::getDictionaries();
41
        $type = explode('.', $this->type)[0];
42
        /**
43
         * @var ActiveQuery $query
44
         */
45
        if (isset($types[$type])) {
46
            $query = $types[$type];
47
        } else {
48
            $query = current($types);
49
            $query->where('0=1');
50
        }
51
52
        if ($this->name) {
53
            $query->andWhere([
54
                'ILIKE',
55
                'name',
56
                $this->name
57
            ]);
58
        }
59
60
        $dp = new ActiveDataProvider([
61
            'query' => $query,
62
        ]);
63
64
        return $dp;
65
    }
66
67
    public function formName()
68
    {
69
        return '';
70
    }
71
}