1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Server module for HiPanel |
5
|
|
|
* |
6
|
|
|
* @link https://github.com/hiqdev/hipanel-module-server |
7
|
|
|
* @package hipanel-module-server |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
* @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace hipanel\modules\server\grid; |
13
|
|
|
|
14
|
|
|
use hipanel\grid\ActionColumn; |
15
|
|
|
use hipanel\grid\MainColumn; |
16
|
|
|
use hipanel\grid\RefColumn; |
17
|
|
|
use hipanel\grid\XEditableColumn; |
18
|
|
|
use hipanel\helpers\Url; |
19
|
|
|
use hipanel\modules\server\widgets\DiscountFormatter; |
20
|
|
|
use hipanel\modules\server\widgets\Expires; |
21
|
|
|
use hipanel\modules\server\widgets\OSFormatter; |
22
|
|
|
use hipanel\modules\server\widgets\State; |
23
|
|
|
use hipanel\modules\hosting\controllers\AccountController; |
24
|
|
|
use hipanel\modules\hosting\controllers\IpController; |
25
|
|
|
use hipanel\widgets\ArraySpoiler; |
26
|
|
|
use hipanel\widgets\Label; |
27
|
|
|
use Yii; |
28
|
|
|
use yii\helpers\ArrayHelper; |
29
|
|
|
use yii\helpers\Html; |
30
|
|
|
|
31
|
|
|
class ServerGridView extends \hipanel\grid\BoxedGridView |
32
|
|
|
{ |
33
|
|
|
public $controllerUrl = '@server'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
public static $osImages; |
39
|
|
|
|
40
|
|
|
public static function setOsImages($osImages) |
41
|
|
|
{ |
42
|
|
|
static::$osImages = $osImages; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public static function formatTariff($model) |
46
|
|
|
{ |
47
|
|
|
if (Yii::$app->user->can('manage')) { |
48
|
|
|
|
49
|
|
|
if ($model->parent_tariff) { |
50
|
|
|
$html[] = Html::tag('abbr', $model->parent_tariff, ['title' => $model->tariff, 'data-toggle' => 'tooltip']); |
|
|
|
|
51
|
|
|
} else { |
52
|
|
|
$html[] = $model->tariff; |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$html[] = Html::a('<i class="fa fa-external-link"></i>', ['@tariff/view', 'id' => $model->tariff_id]); |
56
|
|
|
|
57
|
|
|
return implode(' ', $html); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return !empty($model->parent_tariff) ? $model->parent_tariff : $model->tariff; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public static function defaultColumns() |
64
|
|
|
{ |
65
|
|
|
$osImages = self::$osImages; |
66
|
|
|
|
67
|
|
|
return [ |
|
|
|
|
68
|
|
|
'server' => [ |
69
|
|
|
'class' => MainColumn::class, |
70
|
|
|
'attribute' => 'name', |
71
|
|
|
'filterAttribute' => 'name_like', |
72
|
|
|
'note' => Yii::$app->user->can('support') ? 'label' : 'note', |
73
|
|
|
'noteOptions' => [ |
74
|
|
|
'url' => Yii::$app->user->can('support') ? Url::to('set-label') : Url::to('set-note'), |
75
|
|
|
], |
76
|
|
|
'badges' => function ($model) { |
77
|
|
|
$badges = ''; |
78
|
|
|
if (Yii::$app->user->can('support')) { |
79
|
|
|
if ($model->wizzarded) { |
80
|
|
|
$badges .= Label::widget(['label' => 'W', 'tag' => 'sup', 'color' => 'success']); |
81
|
|
|
} |
82
|
|
|
/*if ($model->state === 'disabled') { |
|
|
|
|
83
|
|
|
$badges .= ' ' . Label::widget(['label' => 'Panel OFF', 'tag' => 'sup', 'color' => 'danger', 'type' => 'text']); |
84
|
|
|
}*/ |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $badges; |
88
|
|
|
}, |
89
|
|
|
], |
90
|
|
|
'dc' => [ |
91
|
|
|
'attribute' => 'dc', |
92
|
|
|
'filter' => false, |
93
|
|
|
], |
94
|
|
|
'state' => [ |
95
|
|
|
'class' => RefColumn::class, |
96
|
|
|
'i18nDictionary' => 'hipanel:server', |
97
|
|
|
'format' => 'raw', |
98
|
|
|
'gtype' => 'state,device', |
99
|
|
|
'value' => function ($model) { |
100
|
|
|
$html = State::widget(compact('model')); |
101
|
|
|
if ($model->status_time) { |
102
|
|
|
$html .= ' '. Html::tag('nobr', Yii::t('hipanel:server', 'since {date}', ['date' => Yii::$app->formatter->asDate($model->status_time)])); |
103
|
|
|
} |
104
|
|
|
return $html; |
105
|
|
|
}, |
106
|
|
|
], |
107
|
|
|
'panel' => [ |
108
|
|
|
'attribute' => 'panel', |
109
|
|
|
'format' => 'text', |
110
|
|
|
'contentOptions' => ['class' => 'text-uppercase'], |
111
|
|
|
'value' => function ($model) { |
112
|
|
|
return $model->panel ? Yii::t('hipanel:server:panel', $model->panel) : Yii::t('hipanel:server:panel', 'No control panel'); |
113
|
|
|
}, |
114
|
|
|
], |
115
|
|
|
'os' => [ |
116
|
|
|
'attribute' => 'os', |
117
|
|
|
'format' => 'raw', |
118
|
|
|
'value' => function ($model) use ($osImages) { |
119
|
|
|
return OSFormatter::widget([ |
120
|
|
|
'osimages' => $osImages, |
121
|
|
|
'imageName' => $model->osimage, |
122
|
|
|
]); |
123
|
|
|
}, |
124
|
|
|
], |
125
|
|
|
'os_and_panel' => [ |
126
|
|
|
'attribute' => 'os', |
127
|
|
|
'format' => 'raw', |
128
|
|
|
'value' => function ($model) use ($osImages) { |
129
|
|
|
$html = OSFormatter::widget([ |
130
|
|
|
'osimages' => $osImages, |
131
|
|
|
'imageName' => $model->osimage, |
132
|
|
|
]); |
133
|
|
|
$html .= ' ' . $model->panel ?: ''; |
134
|
|
|
return $html; |
135
|
|
|
}, |
136
|
|
|
], |
137
|
|
|
'discount' => [ |
138
|
|
|
'attribute' => 'discount', |
139
|
|
|
'label' => Yii::t('hipanel:server', 'Discount'), |
140
|
|
|
'format' => 'raw', |
141
|
|
|
'headerOptions' => ['style' => 'width: 1em'], |
142
|
|
View Code Duplication |
'value' => function ($model) { |
|
|
|
|
143
|
|
|
return DiscountFormatter::widget([ |
144
|
|
|
'current' => $model->discounts['fee']['current'], |
145
|
|
|
'next' => $model->discounts['fee']['next'], |
146
|
|
|
]); |
147
|
|
|
}, |
148
|
|
|
], |
149
|
|
|
'expires' => [ |
150
|
|
|
'filter' => false, |
151
|
|
|
'format' => 'raw', |
152
|
|
|
'headerOptions' => ['style' => 'width: 1em'], |
153
|
|
|
'value' => function ($model) { |
154
|
|
|
return Expires::widget(compact('model')); |
155
|
|
|
}, |
156
|
|
|
], |
157
|
|
|
'tariff' => [ |
158
|
|
|
'format' => 'raw', |
159
|
|
|
'filterAttribute' => 'tariff_like', |
160
|
|
|
'value' => function ($model) { |
161
|
|
|
return self::formatTariff($model); |
162
|
|
|
}, |
163
|
|
|
], |
164
|
|
|
'tariff_and_discount' => [ |
165
|
|
|
'attribute' => 'tariff', |
166
|
|
|
'filterAttribute' => 'tariff_like', |
167
|
|
|
'format' => 'raw', |
168
|
|
View Code Duplication |
'value' => function ($model) { |
|
|
|
|
169
|
|
|
return self::formatTariff($model) . ' ' . DiscountFormatter::widget([ |
170
|
|
|
'current' => $model->discounts['fee']['current'], |
171
|
|
|
'next' => $model->discounts['fee']['next'], |
172
|
|
|
]); |
173
|
|
|
}, |
174
|
|
|
], |
175
|
|
|
'ip' => [ |
176
|
|
|
'filter' => false, |
177
|
|
|
], |
178
|
|
|
'mac' => [ |
179
|
|
|
'filter' => false, |
180
|
|
|
], |
181
|
|
|
'ips' => [ |
182
|
|
|
'format' => 'raw', |
183
|
|
|
'attribute' => 'ips', |
184
|
|
|
'filter' => false, |
185
|
|
|
'value' => function ($model) { |
186
|
|
|
return ArraySpoiler::widget([ |
187
|
|
|
'data' => ArrayHelper::getColumn($model->ips, 'ip'), |
188
|
|
|
'delimiter' => '<br />', |
189
|
|
|
'visibleCount' => 3, |
190
|
|
|
'button' => ['popoverOptions' => ['html' => true]], |
191
|
|
|
]); |
192
|
|
|
}, |
193
|
|
|
], |
194
|
|
|
'sale_time' => [ |
195
|
|
|
'attribute' => 'sale_time', |
196
|
|
|
'format' => 'datetime', |
197
|
|
|
], |
198
|
|
|
'note' => [ |
199
|
|
|
'class' => XEditableColumn::class, |
200
|
|
|
'pluginOptions' => [ |
201
|
|
|
'url' => Url::to('set-note'), |
202
|
|
|
], |
203
|
|
|
'widgetOptions' => [ |
204
|
|
|
'linkOptions' => [ |
205
|
|
|
'data-type' => 'textarea', |
206
|
|
|
], |
207
|
|
|
], |
208
|
|
|
], |
209
|
|
|
'label' => [ |
210
|
|
|
'class' => XEditableColumn::class, |
211
|
|
|
'visible' => Yii::$app->user->can('support'), |
212
|
|
|
'pluginOptions' => [ |
213
|
|
|
'url' => Url::to('set-label'), |
214
|
|
|
], |
215
|
|
|
'widgetOptions' => [ |
216
|
|
|
'linkOptions' => [ |
217
|
|
|
'data-type' => 'textarea', |
218
|
|
|
], |
219
|
|
|
], |
220
|
|
|
], |
221
|
|
|
'type' => [ |
222
|
|
|
'format' => 'html', |
223
|
|
|
'filter' => false, |
224
|
|
|
'value' => function ($model) { |
225
|
|
|
return $model->type_label; |
226
|
|
|
}, |
227
|
|
|
], |
228
|
|
|
'rack' => [ |
229
|
|
|
'format' => 'html', |
230
|
|
|
'filter' => false, |
231
|
|
|
'value' => function ($model) { |
232
|
|
|
return $model->switches['rack']['switch']; |
233
|
|
|
}, |
234
|
|
|
], |
235
|
|
|
'net' => [ |
236
|
|
|
'format' => 'html', |
237
|
|
|
'filter' => false, |
238
|
|
|
'value' => function ($model) { |
239
|
|
|
return static::renderSwitchPort($model->switches['net']); |
240
|
|
|
}, |
241
|
|
|
], |
242
|
|
|
'kvm' => [ |
243
|
|
|
'format' => 'html', |
244
|
|
|
'filter' => false, |
245
|
|
|
'value' => function ($model) { |
246
|
|
|
return static::renderSwitchPort($model->switches['kvm']); |
247
|
|
|
}, |
248
|
|
|
], |
249
|
|
|
'pdu' => [ |
250
|
|
|
'format' => 'html', |
251
|
|
|
'filter' => false, |
252
|
|
|
'value' => function ($model) { |
253
|
|
|
return static::renderSwitchPort($model->switches['pdu']); |
254
|
|
|
}, |
255
|
|
|
], |
256
|
|
|
'ipmi' => [ |
257
|
|
|
'format' => 'raw', |
258
|
|
|
'filter' => false, |
259
|
|
|
'value' => function ($model) { |
260
|
|
|
$ipmi = $model->switches['ipmi']['device_ip']; |
261
|
|
|
$link = $ipmi ? Html::a($ipmi, "http://$ipmi/", ['target' => '_blank']) . ' ' : ''; |
262
|
|
|
return $link . static::renderSwitchPort($model->switches['ipmi']); |
263
|
|
|
}, |
264
|
|
|
], |
265
|
|
|
'nums' => [ |
266
|
|
|
'label' => '', |
267
|
|
|
'format' => 'raw', |
268
|
|
|
'value' => function ($model) { |
269
|
|
|
$ips_num = $model->ips_num; |
270
|
|
|
$ips = $ips_num ? Html::a("$ips_num ips", IpController::getSearchUrl(['server' => $model->name])) : 'no ips'; |
271
|
|
|
$act_acs_num = $model->acs_num - $model->del_acs_num; |
272
|
|
|
$del_acs_num = $model->del_acs_num; |
273
|
|
|
$acs_num = $act_acs_num . ($del_acs_num ? "+$del_acs_num" : ''); |
274
|
|
|
$acs = $acs_num ? Html::a("$acs_num acc", AccountController::getSearchUrl(['server' => $model->name])) : 'no acc'; |
275
|
|
|
return Html::tag('nobr', $ips) . ' ' . Html::tag('nobr', $acs); |
276
|
|
|
}, |
277
|
|
|
], |
278
|
|
|
'actions' => [ |
279
|
|
|
'class' => ActionColumn::class, |
280
|
|
|
'template' => '{view} {rrd} {switch-graph}', |
281
|
|
|
'buttons' => [ |
282
|
|
|
'switch-graph' => function ($url, $model) { |
283
|
|
|
return Html::a('<i class="fa fa-fw fa-area-chart"></i>' . Yii::t('hipanel:server', 'Switch graphs'), ['@switch-graph/view', 'id' => $model->id]); |
284
|
|
|
}, |
285
|
|
|
'rrd' => function ($url, $model) { |
286
|
|
|
return Html::a('<i class="fa fa-fw fa-signal"></i>' . Yii::t('hipanel:server', 'Resources usage graphs'), ['@rrd/view', 'id' => $model->id]); |
287
|
|
|
}, |
288
|
|
|
], |
289
|
|
|
], |
290
|
|
|
]; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
public static function renderSwitchPort($data) |
294
|
|
|
{ |
295
|
|
|
$label = $data['switch_label']; |
296
|
|
|
$inn = $data['switch_inn']; |
297
|
|
|
$name = $data['switch']; |
298
|
|
|
$port = $data['port']; |
299
|
|
|
|
300
|
|
|
$inn = $inn ? "($inn)" : ''; |
301
|
|
|
$main = $port ? "$name:$port" : $name; |
302
|
|
|
$main = $main ? "<b>$main</b>" : ''; |
303
|
|
|
|
304
|
|
|
return "$inn $main $label"; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
public static function defaultRepresentations() |
308
|
|
|
{ |
309
|
|
|
return [ |
310
|
|
|
'common' => [ |
311
|
|
|
'label' => Yii::t('hipanel', 'common'), |
312
|
|
|
'columns' => [ |
313
|
|
|
'checkbox', |
314
|
|
|
'server', 'client_id', 'seller_id', |
315
|
|
|
'ips', 'state', 'expires', |
316
|
|
|
'tariff_and_discount', |
317
|
|
|
], |
318
|
|
|
], |
319
|
|
|
'manager' => Yii::$app->user->can('support') ? [ |
320
|
|
|
'label' => Yii::t('hipanel:server', 'manager'), |
321
|
|
|
'columns' => [ |
322
|
|
|
'checkbox', 'client_id', |
323
|
|
|
'rack', 'server', 'tariff', |
324
|
|
|
'hwsummary', 'nums', 'actions', |
325
|
|
|
], |
326
|
|
|
] : null, |
327
|
|
|
'admin' => Yii::$app->user->can('support') ? [ |
328
|
|
|
'label' => Yii::t('hipanel:server', 'admin'), |
329
|
|
|
'columns' => [ |
330
|
|
|
'checkbox', 'dc', 'server', 'type', |
331
|
|
|
'net', 'kvm', 'ipmi', 'pdu', 'ip', 'mac', |
332
|
|
|
], |
333
|
|
|
] : null, |
334
|
|
|
]; |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
|
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.