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\grid; |
12
|
|
|
|
13
|
|
|
use hipanel\grid\BoxedGridView; |
14
|
|
|
use hipanel\grid\MainColumn; |
15
|
|
|
use hipanel\grid\XEditableColumn; |
16
|
|
|
use hipanel\helpers\FontIcon; |
17
|
|
|
use hipanel\helpers\Url; |
18
|
|
|
use hipanel\modules\hosting\menus\IpActionsMenu; |
19
|
|
|
use hipanel\modules\hosting\models\HdomainSearch; |
20
|
|
|
use hipanel\modules\hosting\models\Ip; |
21
|
|
|
use hipanel\modules\hosting\widgets\ip\ApplyPtrChange; |
22
|
|
|
use hipanel\modules\hosting\widgets\ip\IpTag; |
23
|
|
|
use hipanel\widgets\ArraySpoiler; |
24
|
|
|
use hipanel\widgets\XEditable; |
25
|
|
|
use hiqdev\yii2\menus\grid\MenuColumn; |
26
|
|
|
use Yii; |
27
|
|
|
use yii\base\InvalidParamException; |
28
|
|
|
use yii\helpers\Html; |
29
|
|
|
|
30
|
|
|
class IpGridView extends BoxedGridView |
31
|
|
|
{ |
32
|
|
|
public $controllerUrl = '@ip'; |
33
|
|
|
|
34
|
|
|
public $ipTags = []; |
35
|
|
|
|
36
|
|
|
public function columns() |
37
|
|
|
{ |
38
|
|
|
return array_merge(parent::columns(), [ |
39
|
|
|
'ip' => [ |
40
|
|
|
'class' => MainColumn::class, |
41
|
|
|
'filterAttribute' => 'ip_like', |
42
|
|
|
], |
43
|
|
|
'note' => [ |
44
|
|
|
'class' => XEditableColumn::class, |
45
|
|
|
'pluginOptions' => [ |
46
|
|
|
'url' => Url::to('set-note'), |
47
|
|
|
], |
48
|
|
|
'widgetOptions' => [ |
49
|
|
|
'linkOptions' => [ |
50
|
|
|
'data-type' => 'textarea', |
51
|
|
|
], |
52
|
|
|
], |
53
|
|
|
'visible' => Yii::$app->user->can('admin'), |
54
|
|
|
], |
55
|
|
|
'tags' => [ |
56
|
|
|
'format' => 'raw', |
57
|
|
|
'attribute' => 'tag', |
58
|
|
|
'header' => Yii::t('hipanel:hosting', 'Tags'), |
59
|
|
|
'visible' => Yii::$app->user->can('admin'), |
60
|
|
|
'filter' => function ($column, $model) { |
61
|
|
|
return Html::activeDropDownList($model, 'tag_in', array_merge(['' => Yii::t('hipanel', '---')], $this->ipTags), ['class' => 'form-control']); |
62
|
|
|
}, |
63
|
|
|
'value' => function ($model) { |
64
|
|
|
$labels = []; |
65
|
|
|
foreach ($model->tags as $tag) { |
66
|
|
|
$labels[] = IpTag::widget(['tag' => $tag]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return implode(' ', $labels); |
70
|
|
|
}, |
71
|
|
|
], |
72
|
|
|
'counters' => [ |
73
|
|
|
'format' => 'html', |
74
|
|
|
'header' => Yii::t('hipanel:hosting', 'Counters'), |
75
|
|
|
'value' => function ($model) { |
76
|
|
|
$html = ''; |
77
|
|
|
foreach ($model->objects_count as $count) { |
78
|
|
|
if ($count['type'] === 'hdomain') { |
79
|
|
|
$url['ok'] = ['@hdomain', (new HdomainSearch())->formName() => ['ip_like' => $model->ip]]; |
|
|
|
|
80
|
|
|
$url['deleted'] = ['@hdomain', (new HdomainSearch())->formName() => ['ip_like' => $model->ip, 'state' => 'deleted']]; |
|
|
|
|
81
|
|
|
$type = function ($count) { |
82
|
|
|
return Yii::t('hipanel:hosting', '{0, plural, one{domain} other{domains}}', (int) $count); |
|
|
|
|
83
|
|
|
}; |
84
|
|
|
} else { |
85
|
|
|
throw new InvalidParamException('The object type is not supported', $model); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if ($count['ok']) { |
89
|
|
|
$html .= Html::a( |
90
|
|
|
(int) $count['ok'] . ' ' . FontIcon::i('fa-check') . ' ' . $type($count['ok']), |
91
|
|
|
$url['ok'], |
92
|
|
|
['class' => 'btn btn-success btn-xs'] |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
$html .= ' '; |
96
|
|
|
if ($count['deleted'] > 0) { |
97
|
|
|
$html .= Html::a( |
98
|
|
|
(int) $count['deleted'] . ' ' . FontIcon::i('fa-trash') . ' ' . $type($count['deleted']), |
99
|
|
|
$url['deleted'], |
100
|
|
|
['class' => 'btn btn-xs btn-warning'] |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $html; |
106
|
|
|
}, |
107
|
|
|
], |
108
|
|
|
'links' => [ |
109
|
|
|
'format' => 'html', |
110
|
|
|
'value' => function ($model) { |
111
|
|
|
$items = []; |
112
|
|
|
foreach ($model->links as $link) { |
113
|
|
|
$item = Html::a($link->device, ['@server/view', 'id' => $link->device_id]); |
114
|
|
|
if ($link->service_id) { |
115
|
|
|
$item .= ' ' . FontIcon::i('fa-long-arrow-right'); |
116
|
|
|
$item .= ' ' . Html::a($link->service ?: $link->soft, ['@service/view', 'id' => $link->service_id]); |
117
|
|
|
} |
118
|
|
|
$items[] = $item; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return ArraySpoiler::widget(['data' => $items, 'visibleCount' => 3]); |
122
|
|
|
}, |
123
|
|
|
], |
124
|
|
|
'services' => [ |
125
|
|
|
'attribute' => 'links', |
126
|
|
|
'format' => 'html', |
127
|
|
|
'label' => Yii::t('hipanel:server', 'Services'), |
128
|
|
|
'value' => function ($model) { |
129
|
|
|
return ArraySpoiler::widget([ |
130
|
|
|
'data' => $model->links, |
131
|
|
|
'formatter' => function ($link) { |
132
|
|
|
if (Yii::$app->user->can('support') && Yii::getAlias('@service', false)) { |
133
|
|
|
return Html::a($link->service, ['@service/view', 'id' => $link->service_id]); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $link->service; |
137
|
|
|
}, |
138
|
|
|
]); |
139
|
|
|
}, |
140
|
|
|
], |
141
|
|
|
'actions' => [ |
142
|
|
|
'class' => MenuColumn::class, |
143
|
|
|
'menuClass' => IpActionsMenu::class, |
144
|
|
|
], |
145
|
|
|
'ptr' => [ |
146
|
|
|
'options' => [ |
147
|
|
|
'style' => 'width: 40%', |
148
|
|
|
], |
149
|
|
|
'format' => 'raw', |
150
|
|
|
'value' => static function (Ip $model): string { |
151
|
|
|
if ($model->canSetPtr()) { |
152
|
|
|
return XEditable::widget([ |
153
|
|
|
'model' => $model, |
154
|
|
|
'attribute' => 'ptr', |
155
|
|
|
'pluginOptions' => [ |
156
|
|
|
'url' => Url::to('@ip/set-ptr'), |
157
|
|
|
], |
158
|
|
|
]); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return ApplyPtrChange::widget(compact('model')); |
162
|
|
|
}, |
163
|
|
|
], |
164
|
|
|
]); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.