|
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\widgets\ArraySpoiler; |
|
24
|
|
|
use Yii; |
|
25
|
|
|
use yii\helpers\ArrayHelper; |
|
26
|
|
|
use yii\helpers\Html; |
|
27
|
|
|
|
|
28
|
|
|
class ServerGridView extends \hipanel\grid\BoxedGridView |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
public static $osImages; |
|
34
|
|
|
|
|
35
|
|
|
public static function setOsImages($osImages) |
|
36
|
|
|
{ |
|
37
|
|
|
static::$osImages = $osImages; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public static function formatTariff($model) |
|
41
|
|
|
{ |
|
42
|
|
|
if (Yii::$app->user->can('support')) { |
|
43
|
|
|
if ($model->parent_tariff && $model->parent_tariff !== $model->tariff) { |
|
44
|
|
|
return Html::tag('abbr', $model->parent_tariff, ['title' => $model->tariff, 'data-toggle' => 'tooltip']); |
|
45
|
|
|
} else { |
|
46
|
|
|
return $model->tariff; |
|
47
|
|
|
} |
|
48
|
|
|
} else { |
|
49
|
|
|
return !empty($model->parent_tariff) ? $model->parent_tariff : $model->tariff; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public static function defaultColumns() |
|
54
|
|
|
{ |
|
55
|
|
|
$osImages = self::$osImages; |
|
56
|
|
|
|
|
57
|
|
|
return [ |
|
|
|
|
|
|
58
|
|
|
'server' => [ |
|
59
|
|
|
'class' => MainColumn::class, |
|
60
|
|
|
'attribute' => 'name', |
|
61
|
|
|
'filterAttribute' => 'name_like', |
|
62
|
|
|
'note' => Yii::$app->user->can('support') ? 'label' : 'note', |
|
63
|
|
|
'noteOptions' => [ |
|
64
|
|
|
'url' => Yii::$app->user->can('support') ? Url::to('set-label') : Url::to('set-note'), |
|
65
|
|
|
], |
|
66
|
|
|
], |
|
67
|
|
|
'dc' => [ |
|
68
|
|
|
'attribute' => 'dc', |
|
69
|
|
|
'filter' => false, |
|
70
|
|
|
], |
|
71
|
|
|
'state' => [ |
|
72
|
|
|
'class' => RefColumn::class, |
|
73
|
|
|
'i18nDictionary' => 'hipanel/server', |
|
74
|
|
|
'format' => 'raw', |
|
75
|
|
|
'gtype' => 'state,device', |
|
76
|
|
|
'value' => function ($model) { |
|
77
|
|
|
$html = State::widget(compact('model')); |
|
78
|
|
|
if ($model->status_time) { |
|
79
|
|
|
$html .= ' '. Html::tag('nobr', Yii::t('hipanel/server', 'since {date}', ['date' => Yii::$app->formatter->asDate($model->status_time)])); |
|
80
|
|
|
} |
|
81
|
|
|
return $html; |
|
82
|
|
|
}, |
|
83
|
|
|
], |
|
84
|
|
|
'panel' => [ |
|
85
|
|
|
'attribute' => 'panel', |
|
86
|
|
|
'format' => 'text', |
|
87
|
|
|
'contentOptions' => ['class' => 'text-uppercase'], |
|
88
|
|
|
'value' => function ($model) { |
|
89
|
|
|
return $model->panel ? Yii::t('hipanel/server/panel', $model->panel) : Yii::t('hipanel/server/panel', 'No control panel'); |
|
90
|
|
|
}, |
|
91
|
|
|
], |
|
92
|
|
|
'os' => [ |
|
93
|
|
|
'attribute' => 'os', |
|
94
|
|
|
'format' => 'raw', |
|
95
|
|
|
'value' => function ($model) use ($osImages) { |
|
96
|
|
|
return OSFormatter::widget([ |
|
97
|
|
|
'osimages' => $osImages, |
|
98
|
|
|
'imageName' => $model->osimage, |
|
99
|
|
|
]); |
|
100
|
|
|
}, |
|
101
|
|
|
], |
|
102
|
|
|
'os_and_panel' => [ |
|
103
|
|
|
'attribute' => 'os', |
|
104
|
|
|
'format' => 'raw', |
|
105
|
|
|
'value' => function ($model) use ($osImages) { |
|
106
|
|
|
$html = OSFormatter::widget([ |
|
107
|
|
|
'osimages' => $osImages, |
|
108
|
|
|
'imageName' => $model->osimage, |
|
109
|
|
|
]); |
|
110
|
|
|
$html .= ' ' . $model->panel ?: ''; |
|
111
|
|
|
return $html; |
|
112
|
|
|
}, |
|
113
|
|
|
], |
|
114
|
|
|
'tariff_and_discount' => [ |
|
115
|
|
|
'attribute' => 'tariff', |
|
116
|
|
|
'format' => 'raw', |
|
117
|
|
View Code Duplication |
'value' => function ($model) { |
|
|
|
|
|
|
118
|
|
|
return self::formatTariff($model) . ' ' . DiscountFormatter::widget([ |
|
119
|
|
|
'current' => $model->discounts['fee']['current'], |
|
120
|
|
|
'next' => $model->discounts['fee']['next'], |
|
121
|
|
|
]); |
|
122
|
|
|
}, |
|
123
|
|
|
], |
|
124
|
|
|
'discount' => [ |
|
125
|
|
|
'attribute' => 'discount', |
|
126
|
|
|
'label' => Yii::t('hipanel/server', 'Discount'), |
|
127
|
|
|
'format' => 'raw', |
|
128
|
|
|
'headerOptions' => ['style' => 'width: 1em'], |
|
129
|
|
View Code Duplication |
'value' => function ($model) { |
|
|
|
|
|
|
130
|
|
|
return DiscountFormatter::widget([ |
|
131
|
|
|
'current' => $model->discounts['fee']['current'], |
|
132
|
|
|
'next' => $model->discounts['fee']['next'], |
|
133
|
|
|
]); |
|
134
|
|
|
}, |
|
135
|
|
|
], |
|
136
|
|
|
'actions' => [ |
|
137
|
|
|
'class' => ActionColumn::class, |
|
138
|
|
|
'template' => '{view}', |
|
139
|
|
|
'header' => Yii::t('hipanel', 'Actions'), |
|
140
|
|
|
], |
|
141
|
|
|
'expires' => [ |
|
142
|
|
|
'filter' => false, |
|
143
|
|
|
'format' => 'raw', |
|
144
|
|
|
'headerOptions' => ['style' => 'width: 1em'], |
|
145
|
|
|
'value' => function ($model) { |
|
146
|
|
|
return Expires::widget(compact('model')); |
|
147
|
|
|
}, |
|
148
|
|
|
], |
|
149
|
|
|
'tariff' => [ |
|
150
|
|
|
'format' => 'raw', |
|
151
|
|
|
'attribute' => 'tariff', |
|
152
|
|
|
'value' => function ($model) { |
|
153
|
|
|
return self::formatTariff($model); |
|
154
|
|
|
}, |
|
155
|
|
|
], |
|
156
|
|
|
'tariff_note' => [ |
|
157
|
|
|
'attribute' => 'tariff_note', |
|
158
|
|
|
'value' => function ($model) { |
|
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
}, |
|
161
|
|
|
], |
|
162
|
|
|
'ip' => [ |
|
163
|
|
|
'filter' => false, |
|
164
|
|
|
], |
|
165
|
|
|
'mac' => [ |
|
166
|
|
|
'filter' => false, |
|
167
|
|
|
], |
|
168
|
|
|
'ips' => [ |
|
169
|
|
|
'format' => 'raw', |
|
170
|
|
|
'attribute' => 'ips', |
|
171
|
|
|
'filter' => false, |
|
172
|
|
|
'value' => function ($model) { |
|
173
|
|
|
return ArraySpoiler::widget([ |
|
174
|
|
|
'data' => ArrayHelper::getColumn($model->ips, 'ip'), |
|
175
|
|
|
'delimiter' => '<br />', |
|
176
|
|
|
'visibleCount' => 3, |
|
177
|
|
|
'button' => ['popoverOptions' => ['html' => true]], |
|
178
|
|
|
]); |
|
179
|
|
|
}, |
|
180
|
|
|
], |
|
181
|
|
|
'sale_time' => [ |
|
182
|
|
|
'attribute' => 'sale_time', |
|
183
|
|
|
'format' => 'date', |
|
184
|
|
|
], |
|
185
|
|
|
'note' => [ |
|
186
|
|
|
'class' => XEditableColumn::class, |
|
187
|
|
|
'pluginOptions' => [ |
|
188
|
|
|
'url' => Url::to('set-note'), |
|
189
|
|
|
], |
|
190
|
|
|
'widgetOptions' => [ |
|
191
|
|
|
'linkOptions' => [ |
|
192
|
|
|
'data-type' => 'textarea', |
|
193
|
|
|
], |
|
194
|
|
|
], |
|
195
|
|
|
], |
|
196
|
|
|
'label' => [ |
|
197
|
|
|
'class' => XEditableColumn::class, |
|
198
|
|
|
'visible' => Yii::$app->user->can('support'), |
|
199
|
|
|
'pluginOptions' => [ |
|
200
|
|
|
'url' => Url::to('set-label'), |
|
201
|
|
|
], |
|
202
|
|
|
'widgetOptions' => [ |
|
203
|
|
|
'linkOptions' => [ |
|
204
|
|
|
'data-type' => 'textarea', |
|
205
|
|
|
], |
|
206
|
|
|
], |
|
207
|
|
|
], |
|
208
|
|
|
'type' => [ |
|
209
|
|
|
'format' => 'html', |
|
210
|
|
|
'filter' => false, |
|
211
|
|
|
'value' => function ($model) { |
|
212
|
|
|
return $model->type_label; |
|
213
|
|
|
}, |
|
214
|
|
|
], |
|
215
|
|
|
'rack' => [ |
|
216
|
|
|
'format' => 'html', |
|
217
|
|
|
'filterAttribute' => 'rack_like', |
|
218
|
|
|
'value' => function ($model) { |
|
219
|
|
|
return $model->switches['rack']['switch']; |
|
220
|
|
|
}, |
|
221
|
|
|
], |
|
222
|
|
|
'net' => [ |
|
223
|
|
|
'format' => 'html', |
|
224
|
|
|
'filter' => false, |
|
225
|
|
|
'value' => function ($model) { |
|
226
|
|
|
return static::renderSwitchPort($model->switches['net']); |
|
227
|
|
|
}, |
|
228
|
|
|
], |
|
229
|
|
|
'kvm' => [ |
|
230
|
|
|
'format' => 'html', |
|
231
|
|
|
'filter' => false, |
|
232
|
|
|
'value' => function ($model) { |
|
233
|
|
|
return static::renderSwitchPort($model->switches['kvm']); |
|
234
|
|
|
}, |
|
235
|
|
|
], |
|
236
|
|
|
'pdu' => [ |
|
237
|
|
|
'format' => 'html', |
|
238
|
|
|
'filter' => false, |
|
239
|
|
|
'value' => function ($model) { |
|
240
|
|
|
return static::renderSwitchPort($model->switches['pdu']); |
|
241
|
|
|
}, |
|
242
|
|
|
], |
|
243
|
|
|
'ipmi' => [ |
|
244
|
|
|
'format' => 'raw', |
|
245
|
|
|
'filter' => false, |
|
246
|
|
|
'value' => function ($model) { |
|
247
|
|
|
$ipmi = $model->switches['ipmi']['device_ip']; |
|
248
|
|
|
$link = $ipmi ? Html::a($ipmi, "http://$ipmi/", ['target' => '_blank']) . ' ' : ''; |
|
249
|
|
|
return $link . static::renderSwitchPort($model->switches['ipmi']); |
|
250
|
|
|
}, |
|
251
|
|
|
], |
|
252
|
|
|
]; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
public static function renderSwitchPort($data) |
|
256
|
|
|
{ |
|
257
|
|
|
$label = $data['switch_label']; |
|
258
|
|
|
$inn = $data['switch_inn']; |
|
259
|
|
|
$name = $data['switch']; |
|
260
|
|
|
$port = $data['port']; |
|
261
|
|
|
|
|
262
|
|
|
$inn = $inn ? "($inn)" : ''; |
|
263
|
|
|
$main = $port ? "$name:$port" : $name; |
|
264
|
|
|
$main = $main ? "<b>$main</b>" : ''; |
|
265
|
|
|
|
|
266
|
|
|
return "$inn $main $label"; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
public static function defaultRepresentations() |
|
270
|
|
|
{ |
|
271
|
|
|
return [ |
|
272
|
|
|
'common' => [ |
|
273
|
|
|
'label' => Yii::t('hipanel', 'common'), |
|
274
|
|
|
'columns' => [ |
|
275
|
|
|
'checkbox', |
|
276
|
|
|
'server', 'client_id', 'seller_id', |
|
277
|
|
|
'ips', 'state', 'expires', |
|
278
|
|
|
'tariff_and_discount', |
|
279
|
|
|
], |
|
280
|
|
|
], |
|
281
|
|
|
'manager' => Yii::$app->user->can('support') ? [ |
|
282
|
|
|
'label' => Yii::t('hipanel/server', 'manager'), |
|
283
|
|
|
'columns' => [ |
|
284
|
|
|
'checkbox', 'client_id', |
|
285
|
|
|
'rack', 'dc', 'server', 'tariff', 'hwsummary', |
|
286
|
|
|
], |
|
287
|
|
|
] : null, |
|
288
|
|
|
'admin' => Yii::$app->user->can('support') ? [ |
|
289
|
|
|
'label' => Yii::t('hipanel/server', 'admin'), |
|
290
|
|
|
'columns' => [ |
|
291
|
|
|
'checkbox', 'dc', 'server', 'type', |
|
292
|
|
|
'net', 'kvm', 'ipmi', 'pdu', 'ip', 'mac', |
|
293
|
|
|
], |
|
294
|
|
|
] : null, |
|
295
|
|
|
]; |
|
296
|
|
|
} |
|
297
|
|
|
} |
|
298
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.