|
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\grid\RefColumn; |
|
17
|
|
|
use hipanel\helpers\FontIcon; |
|
18
|
|
|
use hipanel\modules\hosting\models\DbSearch; |
|
19
|
|
|
use hipanel\modules\hosting\models\HdomainSearch; |
|
20
|
|
|
use hipanel\modules\hosting\models\Soft; |
|
21
|
|
|
use hipanel\modules\server\grid\ServerColumn; |
|
22
|
|
|
use hipanel\widgets\ArraySpoiler; |
|
23
|
|
|
use hipanel\widgets\State; |
|
24
|
|
|
use Yii; |
|
25
|
|
|
use yii\helpers\Html; |
|
26
|
|
|
|
|
27
|
|
|
class ServiceGridView extends \hipanel\grid\BoxedGridView |
|
28
|
|
|
{ |
|
29
|
|
|
public static function defaultColumns() |
|
30
|
|
|
{ |
|
31
|
|
|
return [ |
|
32
|
|
|
'service' => [ |
|
33
|
|
|
'class' => MainColumn::class, |
|
34
|
|
|
'attribute' => 'name', |
|
35
|
|
|
'filterAttribute' => 'service_like', |
|
36
|
|
|
], |
|
37
|
|
|
'server' => [ |
|
38
|
|
|
'class' => ServerColumn::class, |
|
39
|
|
|
], |
|
40
|
|
|
'object' => [ |
|
41
|
|
|
'format' => 'raw', |
|
42
|
|
|
'header' => Yii::t('hipanel/hosting', 'Object'), |
|
43
|
|
|
'value' => function ($model) { |
|
44
|
|
|
$html = $model->name . ' '; |
|
45
|
|
|
|
|
46
|
|
|
if ($model->soft_type === Soft::TYPE_WEB) { |
|
47
|
|
|
$url['ok'] = ['@hdomain', (new HdomainSearch)->formName() => ['server' => $model->server, 'service' => $model->name]]; |
|
|
|
|
|
|
48
|
|
|
$url['deleted'] = ['@hdomain', (new HdomainSearch)->formName() => ['server' => $model->server, 'service' => $model->name, 'state' => 'deleted']]; |
|
49
|
|
|
$type = function ($count) { |
|
50
|
|
|
return Yii::t('hipanel/hosting', '{0, plural, one{domain} other{domains}}', (int)$count); |
|
|
|
|
|
|
51
|
|
|
}; |
|
52
|
|
|
} elseif ($model->soft_type === Soft::TYPE_DB) { |
|
53
|
|
|
$url['ok'] = ['@db', (new DbSearch)->formName() => ['server' => $model->server, 'service' => $model->name]]; |
|
|
|
|
|
|
54
|
|
|
$url['deleted'] = ['@db', (new DbSearch)->formName() => ['server' => $model->server, 'service' => $model->name, 'state' => 'deleted']]; |
|
55
|
|
|
$type = function ($count) { |
|
56
|
|
|
return Yii::t('hipanel/hosting', '{0, plural, one{# DB} other{# DBs}}', (int)$count); |
|
|
|
|
|
|
57
|
|
|
}; |
|
58
|
|
|
} else { |
|
59
|
|
|
return $html; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
View Code Duplication |
if ($count = $model->objects_count['ok']) { |
|
|
|
|
|
|
63
|
|
|
$html .= Html::a( |
|
64
|
|
|
(int)$count . ' ' . FontIcon::i('fa-check') . ' ' . $type($count), |
|
65
|
|
|
$url['ok'], |
|
66
|
|
|
['class' => 'btn btn-success btn-xs'] |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
$html .= ' '; |
|
70
|
|
View Code Duplication |
if (($count = $model->objects_count['deleted']) > 0) { |
|
|
|
|
|
|
71
|
|
|
$html .= Html::a( |
|
72
|
|
|
(int)$count . ' ' . FontIcon::i('fa-trash') . ' ' . $type($count), |
|
73
|
|
|
$url['deleted'], |
|
74
|
|
|
['class' => 'btn btn-xs btn-warning'] |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $html; |
|
79
|
|
|
}, |
|
80
|
|
|
], |
|
81
|
|
|
'ip' => [ |
|
82
|
|
|
'format' => 'raw', |
|
83
|
|
|
'label' => Yii::t('hipanel/hosting', 'IP'), |
|
84
|
|
|
'value' => function ($model) { |
|
85
|
|
|
return ArraySpoiler::widget(['data' => array_unique(array_merge((array)$model->ip, (array)$model->ips))]); |
|
86
|
|
|
}, |
|
87
|
|
|
], |
|
88
|
|
|
'bin' => [ |
|
89
|
|
|
'format' => 'html', |
|
90
|
|
|
'value' => function ($model) { |
|
91
|
|
|
return $model->bin ? Html::tag('code', $model->bin) : ''; |
|
92
|
|
|
} |
|
93
|
|
|
], |
|
94
|
|
|
'etc' => [ |
|
95
|
|
|
'format' => 'html', |
|
96
|
|
|
'value' => function ($model) { |
|
97
|
|
|
return $model->etc ? Html::tag('code', $model->etc) : ''; |
|
98
|
|
|
} |
|
99
|
|
|
], |
|
100
|
|
|
'soft' => [ |
|
101
|
|
|
'value' => function ($model) { |
|
102
|
|
|
return $model->soft; |
|
103
|
|
|
} |
|
104
|
|
|
], |
|
105
|
|
|
'state' => [ |
|
106
|
|
|
'class' => RefColumn::class, |
|
107
|
|
|
'i18nDictionary' => 'hipanel/hosting', |
|
108
|
|
|
'format' => 'raw', |
|
109
|
|
|
'value' => function ($model) { |
|
110
|
|
|
return State::widget(compact('model')); |
|
111
|
|
|
}, |
|
112
|
|
|
'gtype' => 'state,service', |
|
113
|
|
|
], |
|
114
|
|
|
'actions' => [ |
|
115
|
|
|
'class' => ActionColumn::class, |
|
116
|
|
|
'template' => '{view}', |
|
117
|
|
|
'header' => Yii::t('hipanel/hosting', 'Actions'), |
|
118
|
|
|
], |
|
119
|
|
|
]; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.