HdomainIpCombo   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 2
c 0
b 0
f 0
lcom 0
cbo 3
dl 0
loc 94
ccs 0
cts 60
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getPluginOptions() 0 52 1
A getFilter() 0 11 1
1
<?php
2
/**
3
 * Hosting Plugin for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-hosting
6
 * @package   hipanel-module-hosting
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\hosting\widgets\ip;
12
13
use hiqdev\combo\Combo;
14
use yii\helpers\ArrayHelper;
15
use yii\web\JsExpression;
16
17
/**
18
 * Class HdomainIpCombo.
19
 */
20
class HdomainIpCombo extends Combo
21
{
22
    /** {@inheritdoc} */
23
    public $type = 'hosting/hdomain-ip';
24
25
    /** {@inheritdoc} */
26
    public $name = 'hdomain-ip';
27
28
    /** {@inheritdoc} */
29
    public $url = '/hosting/ip/index';
30
31
    /** {@inheritdoc} */
32
    public $_return = [
33
        'id',
34
        'expanded_ips',
35
        'links',
36
    ];
37
38
    public $_rename = [
39
        'text' => 'ip',
40
    ];
41
42
    /** {@inheritdoc} */
43
    public $_filter = [
44
        'client' => 'client/client',
45
        'server' => 'server/server',
46
    ];
47
48
    public function getPluginOptions($options = [])
49
    {
50
        return parent::getPluginOptions(ArrayHelper::merge([
51
            'activeWhen'     => [
52
                'hosting/account',
53
            ],
54
            'select2Options' => [
55
                'ajax' => [
56
                    'processResults' => new JsExpression('
57
                        function (data) {
58
                            var ret = [];
59
                            var used_ips = {};
60
                            $.each(data, function (i, v) {
61
                                $.each(v.expanded_ips, function (ip, ipval) {
62
                                    if (used_ips[ip]) return;
63
64
                                    var row = {id: ip};
65
                                    used_ips[ip] = true
66
                                    if (v.links) {
67
                                        $.each(v.links, function(k,link) {
68
                                            row.text = ip;
69
                                            row.service = link.service;
70
                                        });
71
                                    } else {
72
                                        row.text = ip;
73
                                    }
74
                                    ret.push(row);
75
                                });
76
                            });
77
78
                            return {results: ret};
79
                        }
80
                '),
81
                ],
82
                'templateResult' => new JsExpression("
83
                    function (data) {
84
                        if (data.loading || !data.service) {
85
                            return data.text;
86
                        }
87
88
                        return data.service + ': ' + data.text;
89
                    }
90
                "),
91
                'templateSelection' => new JsExpression("
92
                    function (row) {
93
                        if (!row.service) return row.text;
94
                        return row.service + ': ' + row.text;
95
                    }
96
                "),
97
            ],
98
        ], $options));
99
    }
100
101
    /** {@inheritdoc} */
102
    public function getFilter()
103
    {
104
        return ArrayHelper::merge(parent::getFilter(), [
105
            'soft_type'             => ['format' => 'web'],
106
            'state_in'              => ['format' => ['ok']],
107
            'not_tags'              => ['format' => 'aux'],
108
            'with_links'            => ['format' => '1'],
109
            'show_only_device_link' => ['format' => '1'],
110
            'with_expanded_ips'     => ['format' => '1'],
111
        ]);
112
    }
113
}
114