Completed
Push — master ( 94fb91...3924c7 )
by Andrii
13:41
created

ServerActionsMenu::getSettingsItems()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 48
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 48
ccs 0
cts 31
cp 0
rs 9.36
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * Server module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-server
6
 * @package   hipanel-module-server
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\server\menus;
12
13
use hipanel\widgets\AjaxModalWithTemplatedButton;
14
use Yii;
15
use yii\bootstrap\Modal;
16
use yii\helpers\Html;
17
18
class ServerActionsMenu extends \hiqdev\yii2\menus\Menu
19
{
20
    public $model;
21
22
    public function items(): array
23
    {
24
        $user = Yii::$app->user;
25
26
        $items = [
27
            'view' => [
28
                'label' => Yii::t('hipanel', 'View'),
29
                'icon' => 'fa-info',
30
                'url' => ['@server/view', 'id' => $this->model->id],
31
                'linkOptions' => [
32
                    'data-pjax' => 0,
33
                ],
34
            ],
35
            'update' => [
36
                'label' => Yii::t('hipanel', 'Update'),
37
                'icon' => 'fa-pencil',
38
                'url' => ['@server/update', 'id' => $this->model->id],
39
                'linkOptions' => [
40
                    'data-pjax' => 0,
41
                ],
42
                'visible' => Yii::$app->user->can('server.update'),
0 ignored issues
show
Bug introduced by
The method can() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
                'visible' => Yii::$app->user->/** @scrutinizer ignore-call */ can('server.update'),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
            ],
44
            [
45
                'label' => Yii::t('hipanel:server', 'Renew server'),
46
                'icon' => 'fa-forward',
47
                'url' => ['add-to-cart-renewal', 'model_id' => $this->model->id],
48
                'linkOptions' => [
49
                    'data-pjax' => 0,
50
                ],
51
                'visible' => !empty($this->model->expires) && Yii::$app->user->can('server.pay'),
52
            ],
53
            'assign-hubs' => [
54
                'label' => Yii::t('hipanel:server', 'Assign hubs'),
55
                'icon' => 'fa-exchange',
56
                'url' => ['@server/assign-hubs', 'id' => $this->model->id],
57
                'visible' => Yii::$app->user->can('server.update'),
58
                'linkOptions' => [
59
                    'data-pjax' => 0,
60
                ],
61
            ],
62
            [
63
                'label' => Yii::t('hipanel:server', 'Performance graphs'),
64
                'icon' => 'fa-signal',
65
                'url' => ['@rrd/view', 'id' => $this->model->id],
66
                'linkOptions' => [
67
                    'data-pjax' => 0,
68
                ],
69
            ],
70
            [
71
                'label' => Yii::t('hipanel:server', 'Switch graphs'),
72
                'icon' => 'fa-area-chart',
73
                'url' => ['@switch-graph/view', 'id' => $this->model->id],
74
                'linkOptions' => [
75
                    'data-pjax' => 0,
76
                ],
77
            ],
78
            [
79
                'label' => Yii::t('hipanel:server', 'Server IPs'),
80
                'icon' => 'fa-location-arrow',
81
                'url' => ['@ip/index', 'IpSearch' => ['server_in' => $this->model->name]],
82
                'linkOptions' => [
83
                    'data-pjax' => 0,
84
                ],
85
                'visible' => $user->can('ip.read') && Yii::getAlias('@ip', false),
0 ignored issues
show
Bug introduced by
The method can() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
                'visible' => $user->/** @scrutinizer ignore-call */ can('ip.read') && Yii::getAlias('@ip', false),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
            ],
87
            [
88
                'label' => Yii::t('hipanel:server', 'Server Accounts'),
89
                'icon' => 'fa-user',
90
                'url' => ['@account/index', 'AccountSearch' => ['server' => $this->model->name]],
91
                'linkOptions' => [
92
                    'data-pjax' => 0,
93
                ],
94
                'visible' => $user->can('account.read') && Yii::getAlias('@account', false),
95
            ],
96
        ];
97
98
        array_splice($items, 9, 0, $this->getSettingsItems());
99
100
        return $items;
101
    }
102
103
    private function getSettingsItems(): array
104
    {
105
        $items = [];
106
107
        foreach ([
108
                     'hardware-settings' => [
109
                         'label' => Yii::t('hipanel:server', 'Hardware properties'),
110
                         'url' => ['hardware-settings', 'id' => $this->model->id],
111
                         'size' => Modal::SIZE_LARGE,
112
                     ],
113
                     'software-settings' => [
114
                         'label' => Yii::t('hipanel:server', 'Software properties'),
115
                         'url' => ['software-settings', 'id' => $this->model->id],
116
                     ],
117
                     'monitoring-settings' => [
118
                         'label' => Yii::t('hipanel:server', 'Monitoring properties'),
119
                         'url' => ['monitoring-settings', 'id' => $this->model->id],
120
                     ],
121
                     'mail-settings' => [
122
                         'label' => Yii::t('hipanel:server', 'Mail settings'),
123
                         'url' => ['mail-settings', 'id' => $this->model->id],
124
                         'size' => Modal::SIZE_SMALL,
125
                     ],
126
                 ] as $key => $item) {
127
            array_push($items, [
128
                'label' => AjaxModalWithTemplatedButton::widget([
129
                    'ajaxModalOptions' => [
130
                        'id' => "{$key}-modal",
131
                        'bulkPage' => true,
132
                        'header' => Html::tag('h4', $item['label'], ['class' => 'modal-title']),
133
                        'scenario' => 'default',
134
                        'actionUrl' => $item['url'],
135
                        'size' => $item['size'],
136
                        'handleSubmit' => $item['url'],
137
                        'toggleButton' => [
138
                            'tag' => 'a',
139
                            'label' => Html::tag('i', null, ['class' => 'fa fa-fw fa-cogs']) . $item['label'],
140
                            'style' => 'cursor: pointer;',
141
                        ],
142
                    ],
143
                    'toggleButtonTemplate' => '{toggleButton}',
144
                ]),
145
                'encode' => false,
146
                'visible' => Yii::$app->user->can('server.manage-settings'),
147
            ]);
148
        }
149
150
        return $items;
151
    }
152
}
153