1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Hosting Plugin for HiPanel |
5
|
|
|
* |
6
|
|
|
* @link https://github.com/hiqdev/hipanel-module-hosting |
7
|
|
|
* @package hipanel-module-hosting |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
* @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace hipanel\modules\hosting\grid; |
13
|
|
|
|
14
|
|
|
use hipanel\grid\ActionColumn; |
15
|
|
|
use hipanel\grid\MainColumn; |
16
|
|
|
use hipanel\helpers\FontIcon; |
17
|
|
|
use hipanel\helpers\Url; |
18
|
|
|
use hipanel\modules\hosting\models\HdomainSearch; |
19
|
|
|
use hipanel\modules\hosting\widgets\ip\IpTag; |
20
|
|
|
use hipanel\widgets\ArraySpoiler; |
21
|
|
|
use hipanel\widgets\XEditable; |
22
|
|
|
use Yii; |
23
|
|
|
use yii\base\InvalidParamException; |
24
|
|
|
use yii\helpers\Html; |
25
|
|
|
|
26
|
|
|
class IpGridView extends \hipanel\grid\BoxedGridView |
27
|
|
|
{ |
28
|
|
|
public $controllerUrl = '@ip'; |
29
|
|
|
|
30
|
|
|
public static $ipTags = []; |
31
|
|
|
|
32
|
|
|
public static function setIpTags($ipTags) |
33
|
|
|
{ |
34
|
|
|
static::$ipTags = $ipTags; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public static function defaultColumns() |
38
|
|
|
{ |
39
|
|
|
return [ |
|
|
|
|
40
|
|
|
'ip' => [ |
41
|
|
|
'class' => MainColumn::class, |
42
|
|
|
'filterAttribute' => 'ip_like', |
43
|
|
|
], |
44
|
|
|
'tags' => [ |
45
|
|
|
'format' => 'raw', |
46
|
|
|
'attribute' => 'tag', |
47
|
|
|
'header' => Yii::t('hipanel/hosting', 'Tags'), |
48
|
|
|
'filter' => function ($column, $model) { |
49
|
|
|
return Html::activeDropDownList($model, 'tag_in', array_merge(['' => Yii::t('app', '---')], static::$ipTags), ['class' => 'form-control']); |
50
|
|
|
}, |
51
|
|
|
'value' => function ($model) { |
52
|
|
|
$labels = []; |
53
|
|
|
foreach ($model->tags as $tag) { |
54
|
|
|
$labels[] = IpTag::widget(['tag' => $tag]); |
55
|
|
|
} |
56
|
|
|
return implode(' ', $labels); |
57
|
|
|
} |
58
|
|
|
], |
59
|
|
|
'counters' => [ |
60
|
|
|
'format' => 'html', |
61
|
|
|
'header' => Yii::t('hipanel/hosting', 'Counters'), |
62
|
|
|
'value' => function ($model) { |
63
|
|
|
$html = ''; |
64
|
|
|
foreach ($model->objects_count as $count) { |
65
|
|
|
if ($count['type'] === 'hdomain') { |
66
|
|
|
$url['ok'] = ['@hdomain', (new HdomainSearch)->formName() => ['ip_like' => $model->ip]]; |
|
|
|
|
67
|
|
|
$url['deleted'] = ['@hdomain', (new HdomainSearch)->formName() => ['ip_like' => $model->ip, 'state' => 'deleted']]; |
|
|
|
|
68
|
|
|
$type = function ($count) { |
69
|
|
|
return Yii::t('hipanel/hosting', '{0, plural, one{domain} other{domains}}', (int)$count); |
|
|
|
|
70
|
|
|
}; |
71
|
|
|
} else { |
72
|
|
|
throw new InvalidParamException('The object type is not supported', $model); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
View Code Duplication |
if ($count['ok']) { |
|
|
|
|
76
|
|
|
$html .= Html::a( |
77
|
|
|
(int)$count['ok'] . ' ' . FontIcon::i('fa-check') . ' ' . $type($count['ok']), |
78
|
|
|
$url['ok'], |
79
|
|
|
['class' => 'btn btn-success btn-xs'] |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
$html .= ' '; |
83
|
|
View Code Duplication |
if ($count['deleted'] > 0) { |
|
|
|
|
84
|
|
|
$html .= Html::a( |
85
|
|
|
(int)$count['deleted'] . ' ' . FontIcon::i('fa-trash') . ' ' . $type($count['deleted']), |
86
|
|
|
$url['deleted'], |
87
|
|
|
['class' => 'btn btn-xs btn-warning'] |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $html; |
93
|
|
|
} |
94
|
|
|
], |
95
|
|
|
'links' => [ |
96
|
|
|
'format' => 'html', |
97
|
|
|
'value' => function ($model) { |
98
|
|
|
$items = []; |
99
|
|
|
foreach ($model->links as $link) { |
100
|
|
|
$item = Html::a($link->device, ['@server/view', 'id' => $link->device_id]); |
101
|
|
|
if ($link->service_id) { |
102
|
|
|
$item .= ' ' . FontIcon::i('fa-long-arrow-right'); |
103
|
|
|
$item .= ' ' . Html::a($link->service ?: $link->soft, ['@service/update', 'id' => $link->service_id]); |
104
|
|
|
} |
105
|
|
|
$items[] = $item; |
106
|
|
|
} |
107
|
|
|
return ArraySpoiler::widget(['data' => $items, 'visibleCount' => 3]); |
108
|
|
|
} |
109
|
|
|
], |
110
|
|
|
'services' => [ |
111
|
|
|
'attribute' => 'links', |
112
|
|
|
'format' => 'html', |
113
|
|
|
'label' => Yii::t('hipanel/server', 'Services'), |
114
|
|
|
'value' => function ($model) { |
115
|
|
|
return ArraySpoiler::widget([ |
116
|
|
|
'data' => $model->links, |
117
|
|
|
'formatter' => function ($link) { |
118
|
|
|
if (Yii::$app->user->can('support') && Yii::getAlias('@service', false)) { |
119
|
|
|
return Html::a($link->service, ['@service/view', 'id' => $link->service_id]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $link->service; |
123
|
|
|
}, |
124
|
|
|
]); |
125
|
|
|
}, |
126
|
|
|
], |
127
|
|
|
'actions' => [ |
128
|
|
|
'class' => ActionColumn::class, |
129
|
|
|
'template' => '{view} {expand} {update} {delete}', |
130
|
|
|
'buttons' => [ |
131
|
|
|
'expand' => function ($url, $model) { |
132
|
|
|
$options = array_merge([ |
133
|
|
|
'title' => Yii::t('hipanel/hosting', 'Expand'), |
134
|
|
|
'aria-label' => Yii::t('hipanel/hosting', 'Expand'), |
135
|
|
|
'data-pjax' => '0', |
136
|
|
|
'data-id' => $model->id, |
137
|
|
|
'class' => 'btn-expand-ip', |
138
|
|
|
]); |
139
|
|
|
|
140
|
|
|
return Html::a(FontIcon::i('fa-th') . Yii::t('hipanel/hosting', 'Expand'), $url, $options); |
141
|
|
|
}, |
142
|
|
|
], |
143
|
|
|
], |
144
|
|
|
'ptr' => [ |
145
|
|
|
'options' => [ |
146
|
|
|
'style' => 'width: 40%', |
147
|
|
|
], |
148
|
|
|
'format' => 'raw', |
149
|
|
|
'value' => function ($model) { |
150
|
|
|
if ($model->canSetPtr()) { |
151
|
|
|
return XEditable::widget([ |
152
|
|
|
'model' => $model, |
153
|
|
|
'attribute' => 'ptr', |
154
|
|
|
'pluginOptions' => [ |
155
|
|
|
'url' => Url::to('@ip/set-ptr') |
156
|
|
|
], |
157
|
|
|
]); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return null; |
161
|
|
|
} |
162
|
|
|
], |
163
|
|
|
]; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
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.