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::className(), |
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
|
|
|
'state' => [ |
68
|
|
|
'class' => RefColumn::className(), |
69
|
|
|
'format' => 'raw', |
70
|
|
|
'gtype' => 'state,device', |
71
|
|
|
'value' => function ($model) { |
72
|
|
|
$html = State::widget(compact('model')); |
73
|
|
|
if ($model->status_time) { |
74
|
|
|
$html .= ' '. Html::tag('nobr', Yii::t('hipanel/server', 'since {date}', ['date' => Yii::$app->formatter->asDate($model->status_time)])); |
75
|
|
|
} |
76
|
|
|
return $html; |
77
|
|
|
}, |
78
|
|
|
], |
79
|
|
|
'panel' => [ |
80
|
|
|
'attribute' => 'panel', |
81
|
|
|
'format' => 'text', |
82
|
|
|
'contentOptions' => ['class' => 'text-uppercase'], |
83
|
|
|
'value' => function ($model) { |
84
|
|
|
return $model->panel ? Yii::t('hipanel/server/panel', $model->panel) : Yii::t('hipanel/server/panel', 'No control panel'); |
85
|
|
|
}, |
86
|
|
|
], |
87
|
|
|
'os' => [ |
88
|
|
|
'attribute' => 'os', |
89
|
|
|
'format' => 'raw', |
90
|
|
|
'value' => function ($model) use ($osImages) { |
91
|
|
|
return OSFormatter::widget([ |
92
|
|
|
'osimages' => $osImages, |
93
|
|
|
'imageName' => $model->osimage, |
94
|
|
|
]); |
95
|
|
|
}, |
96
|
|
|
], |
97
|
|
|
'os_and_panel' => [ |
98
|
|
|
'attribute' => 'os', |
99
|
|
|
'format' => 'raw', |
100
|
|
|
'value' => function ($model) use ($osImages) { |
101
|
|
|
$html = OSFormatter::widget([ |
102
|
|
|
'osimages' => $osImages, |
103
|
|
|
'imageName' => $model->osimage, |
104
|
|
|
]); |
105
|
|
|
$html .= ' ' . $model->panel ?: ''; |
106
|
|
|
return $html; |
107
|
|
|
}, |
108
|
|
|
], |
109
|
|
|
'tariff_and_discount' => [ |
110
|
|
|
'attribute' => 'tariff', |
111
|
|
|
'format' => 'raw', |
112
|
|
View Code Duplication |
'value' => function ($model) { |
|
|
|
|
113
|
|
|
return self::formatTariff($model) . ' ' . DiscountFormatter::widget([ |
114
|
|
|
'current' => $model->discounts['fee']['current'], |
115
|
|
|
'next' => $model->discounts['fee']['next'], |
116
|
|
|
]); |
117
|
|
|
}, |
118
|
|
|
], |
119
|
|
|
'discount' => [ |
120
|
|
|
'attribute' => 'discount', |
121
|
|
|
'label' => Yii::t('hipanel/server', 'Discount'), |
122
|
|
|
'format' => 'raw', |
123
|
|
|
'headerOptions' => ['style' => 'width: 1em'], |
124
|
|
View Code Duplication |
'value' => function ($model) { |
|
|
|
|
125
|
|
|
return DiscountFormatter::widget([ |
126
|
|
|
'current' => $model->discounts['fee']['current'], |
127
|
|
|
'next' => $model->discounts['fee']['next'], |
128
|
|
|
]); |
129
|
|
|
}, |
130
|
|
|
], |
131
|
|
|
'actions' => [ |
132
|
|
|
'class' => ActionColumn::className(), |
133
|
|
|
'template' => '{view}', |
134
|
|
|
'header' => Yii::t('hipanel', 'Actions'), |
135
|
|
|
], |
136
|
|
|
'expires' => [ |
137
|
|
|
'filter' => false, |
138
|
|
|
'format' => 'raw', |
139
|
|
|
'headerOptions' => ['style' => 'width: 1em'], |
140
|
|
|
'value' => function ($model) { |
141
|
|
|
return Expires::widget(compact('model')); |
142
|
|
|
}, |
143
|
|
|
], |
144
|
|
|
'tariff' => [ |
145
|
|
|
'format' => 'raw', |
146
|
|
|
'attribute' => 'tariff', |
147
|
|
|
'value' => function ($model) { |
148
|
|
|
return self::formatTariff($model); |
149
|
|
|
}, |
150
|
|
|
], |
151
|
|
|
'tariff_note' => [ |
152
|
|
|
'attribute' => 'tariff_note', |
153
|
|
|
'value' => function ($model) { |
|
|
|
|
154
|
|
|
|
155
|
|
|
}, |
156
|
|
|
], |
157
|
|
|
'ips' => [ |
158
|
|
|
'format' => 'raw', |
159
|
|
|
'attribute' => 'ips', |
160
|
|
|
'label' => Yii::t('hipanel/server', 'IP addresses'), |
161
|
|
|
'value' => function ($model) { |
162
|
|
|
return ArraySpoiler::widget([ |
163
|
|
|
'data' => ArrayHelper::getColumn($model->ips, 'ip'), |
164
|
|
|
'delimiter' => '<br />', |
165
|
|
|
'visibleCount' => 3, |
166
|
|
|
'button' => ['popoverOptions' => ['html' => true]], |
167
|
|
|
]); |
168
|
|
|
}, |
169
|
|
|
], |
170
|
|
|
'sale_time' => [ |
171
|
|
|
'attribute' => 'sale_time', |
172
|
|
|
'format' => 'date', |
173
|
|
|
], |
174
|
|
|
'note' => [ |
175
|
|
|
'class' => XEditableColumn::class, |
176
|
|
|
'pluginOptions' => [ |
177
|
|
|
'url' => Url::to('set-note'), |
178
|
|
|
], |
179
|
|
|
'widgetOptions' => [ |
180
|
|
|
'linkOptions' => [ |
181
|
|
|
'data-type' => 'textarea', |
182
|
|
|
], |
183
|
|
|
], |
184
|
|
|
], |
185
|
|
|
'label' => [ |
186
|
|
|
'class' => XEditableColumn::class, |
187
|
|
|
'visible' => Yii::$app->user->can('support'), |
188
|
|
|
'pluginOptions' => [ |
189
|
|
|
'url' => Url::to('set-label'), |
190
|
|
|
], |
191
|
|
|
'widgetOptions' => [ |
192
|
|
|
'linkOptions' => [ |
193
|
|
|
'data-type' => 'textarea', |
194
|
|
|
], |
195
|
|
|
], |
196
|
|
|
], |
197
|
|
|
'rack' => [ |
198
|
|
|
'format' => 'html', |
199
|
|
|
'filterAttribute' => 'rack_like', |
200
|
|
|
'value' => function ($model) { |
201
|
|
|
return $model->switches['rack']['switch']; |
202
|
|
|
}, |
203
|
|
|
], |
204
|
|
|
]; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public static function defaultRepresentations() |
208
|
|
|
{ |
209
|
|
|
return [ |
210
|
|
|
'common' => [ |
211
|
|
|
'label' => Yii::t('hipanel', 'common'), |
212
|
|
|
'columns' => [ |
213
|
|
|
'checkbox', |
214
|
|
|
'server', 'client_id', 'seller_id', |
215
|
|
|
'ips', 'state', 'expires', |
216
|
|
|
'tariff_and_discount', |
217
|
|
|
], |
218
|
|
|
], |
219
|
|
|
'manager' => Yii::$app->user->can('support') ? [ |
220
|
|
|
'label' => Yii::t('hipanel/server', 'manager'), |
221
|
|
|
'columns' => [ |
222
|
|
|
'checkbox', 'client_id', |
223
|
|
|
'rack', 'dc', 'server', 'tariff', 'hwsummary', |
224
|
|
|
], |
225
|
|
|
] : null, |
226
|
|
|
]; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
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_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.