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