Completed
Push — master ( 04057c...846ed7 )
by Dmitry
05:05
created

TemplatePlanCombo::getPluginOptions()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 28
ccs 0
cts 25
cp 0
rs 8.8571
c 1
b 0
f 0
cc 1
eloc 9
nc 1
nop 1
crap 2
1
<?php
2
3
namespace hipanel\modules\finance\widgets\combo;
4
5
use hiqdev\combo\Combo;
6
use yii\base\InvalidConfigException;
7
use yii\web\JsExpression;
8
9
/**
10
 * Class TemplatePlanCombo
11
 *
12
 * @author Dmytro Naumenko <[email protected]>
13
 */
14
class TemplatePlanCombo extends Combo
15
{
16
    /** {@inheritdoc} */
17
    public $type = 'plan/template';
18
19
    /** {@inheritdoc} */
20
    public $name = 'name';
21
22
    /** {@inheritdoc} */
23
    public $url = '/finance/plan/templates';
24
25
    /** {@inheritdoc} */
26
    public $_return = ['id'];
27
28
    /** {@inheritdoc} */
29
    public $_rename = ['text' => 'name'];
30
31
    public $_primaryFilter = 'name_ilike';
32
33
    /**
34
     * @var int ID of plan that will be used in this templates suggestion
35
     */
36
    public $plan_id;
37
    /**
38
     * @var string name of combo that contains target object
39
     */
40
    public $object_input_type;
41
42
    public function init()
43
    {
44
        parent::init();
45
46
        if (empty($this->plan_id)) {
47
            throw new InvalidConfigException('Property "plan_id" must be set');
48
        }
49
        if (empty($this->object_input_type)) {
50
            throw new InvalidConfigException('Property "object_input_type" must be set');
51
        }
52
53
        $this->_pluginOptions['type'] = 'get';
54
        $this->_filter['plan_id'] = ['format' => $this->plan_id];
55
        $this->_filter['object_id'] = [
56
            'field' => $this->object_input_type,
57
            'format' => 'id'
58
        ];
59
    }
60
61
    public function getPluginOptions($options = [])
62
    {
63
        return parent::getPluginOptions([
64
            'activeWhen' => $this->object_input_type,
65
            'select2Options' => [
66
                'ajax' => [
67
                    'type' => 'get',
68
                ],
69
                'templateResult' => new JsExpression("function (data) {
70
                    if (data.loading) {
71
                      return data.text;
72
                    }
73
74
                    return data.name + '<br><small>' +  data.reason + '</small>';
75
                }"),
76
                'templateSelection' => new JsExpression("function (data) {
77
                    if (!data.id.length) {
78
                        return data.text;
79
                    }
80
                    
81
                    return data.name + ' &mdash; <small>' +  data.reason + '</small>';
82
                }"),
83
                'escapeMarkup' => new JsExpression('function (markup) {
84
                    return markup; // Allows HTML
85
                }'),
86
            ],
87
        ]);
88
    }
89
}
90