1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Server module for HiPanel |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/hipanel-module-server |
6
|
|
|
* @package hipanel-module-server |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hipanel\modules\server\grid; |
12
|
|
|
|
13
|
|
|
use hipanel\grid\BoxedGridView; |
14
|
|
|
use hipanel\grid\MainColumn; |
15
|
|
|
use hipanel\grid\RefColumn; |
16
|
|
|
use hipanel\modules\server\menus\ConfigActionsMenu; |
17
|
|
|
use hipanel\modules\server\models\Config; |
18
|
|
|
use hipanel\modules\server\models\ConfigSearch; |
19
|
|
|
use hipanel\modules\server\widgets\combo\ConfigProfileCombo; |
20
|
|
|
use hiqdev\yii2\menus\grid\MenuColumn; |
21
|
|
|
use Yii; |
22
|
|
|
use yii\helpers\Html; |
23
|
|
|
|
24
|
|
|
class ConfigGridView extends BoxedGridView |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
|
|
private function getTariffsWithOldPriceColumns(): array |
30
|
|
|
{ |
31
|
|
|
$columns = []; |
32
|
|
|
foreach (['nl' => 'EUR', 'us' => 'USD'] as $region => $currency) { |
33
|
|
|
$columns["{$region}_tariff"] = [ |
34
|
|
|
'format' => 'raw', |
35
|
|
|
'value' => function (Config $config) use ($region, $currency): string { |
36
|
|
|
$tariff = Html::tag('span', $config->{$region . '_tariff'}); |
37
|
|
|
$oldPrice = Html::tag( |
38
|
|
|
'span', |
39
|
|
|
Yii::t('hipanel:server:config', 'Old price: {price}', [ |
40
|
|
|
'price' => Yii::$app->formatter->asCurrency($config->{$region . '_old_price'}, $currency) |
41
|
|
|
]), |
42
|
|
|
['class' => 'badge'] |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
return Html::tag('span', $tariff . $oldPrice, [ |
46
|
|
|
'style' => 'display: flex; flex-direction: row; justify-content: space-between; flex-wrap: wrap;' |
47
|
|
|
]); |
48
|
|
|
}, |
49
|
|
|
]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $columns; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function columns() |
56
|
|
|
{ |
57
|
|
|
return array_merge(parent::columns(), $this->getTariffsWithOldPriceColumns(), [ |
58
|
|
|
'actions' => [ |
59
|
|
|
'class' => MenuColumn::class, |
60
|
|
|
'menuClass' => ConfigActionsMenu::class, |
61
|
|
|
], |
62
|
|
|
'name' => [ |
63
|
|
|
'class' => MainColumn::class, |
64
|
|
|
'label' => Yii::t('hipanel', 'Name'), |
65
|
|
|
'filterOptions' => ['class' => 'narrow-filter'], |
66
|
|
|
'format' => 'html', |
67
|
|
|
'value' => function ($model) { |
68
|
|
|
return Html::a($model->name, ['@config/view', 'id' => $model->id]) . |
69
|
|
|
'</br>' . Html::tag('span', $model->label); |
70
|
|
|
}, |
71
|
|
|
], |
72
|
|
|
'config' => [ |
73
|
|
|
'class' => MainColumn::class, |
74
|
|
|
'format' => 'html', |
75
|
|
|
'value' => function ($model) { |
76
|
|
|
$value = ''; |
77
|
|
|
foreach (['cpu', 'ram', 'hdd', 'ssd'] as $item) { |
78
|
|
|
if (empty($model->$item)) { |
79
|
|
|
continue; |
80
|
|
|
} |
81
|
|
|
$value .= '<nobr>' . Html::tag('span', strtoupper($item) . ': ') . |
82
|
|
|
Html::tag('span', $model->$item) . '</nobr></br>'; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $value; |
86
|
|
|
}, |
87
|
|
|
], |
88
|
|
|
'profiles' => [ |
89
|
|
|
'format' => 'raw', |
90
|
|
|
'filterOptions' => ['class' => 'narrow-filter'], |
91
|
|
|
'attribute' => 'profiles', |
92
|
|
|
'filter' => ConfigProfileCombo::widget([ |
93
|
|
|
'attribute' => 'profiles_like', |
94
|
|
|
'model' => $this->filterModel ?? new ConfigSearch(), |
95
|
|
|
'formElementSelector' => 'td', |
96
|
|
|
]), |
97
|
|
|
'enableSorting' => false, |
98
|
|
|
'value' => function (Config $config): string { |
99
|
|
|
$colors = ['bg-teal', 'bg-green', 'bg-yellow', 'bg-purple', 'bg-aqua', 'bg-red']; |
100
|
|
|
return Html::tag('ul', implode('<br>', array_map(function ($profile) use (&$colors) { |
101
|
|
|
return Html::tag('li', $profile, ['class' => 'badge ' . array_pop($colors)]); |
102
|
|
|
}, array_map('trim', explode(',', $config->profiles)))), ['class' => 'list-unstyled']); |
|
|
|
|
103
|
|
|
}, |
104
|
|
|
], |
105
|
|
|
'tariffs' => [ |
106
|
|
|
'class' => MainColumn::class, |
107
|
|
|
'format' => 'html', |
108
|
|
|
'value' => function ($model) { |
109
|
|
|
return Html::tag('span', 'NL:') . |
110
|
|
|
Html::a($model->nl_tariff, ['@tariff/view', 'id' => $model->nl_tariff_id]) . '</br>' . |
111
|
|
|
Html::tag('span', 'US:') . |
112
|
|
|
Html::a($model->us_tariff, ['@tariff/view', 'id' => $model->us_tariff_id]); |
113
|
|
|
}, |
114
|
|
|
], |
115
|
|
|
'servers' => [ |
116
|
|
|
'value' => function (Config $model) { |
117
|
|
|
return implode(', ', array_unique(explode(', ', $model->servers))); |
|
|
|
|
118
|
|
|
}, |
119
|
|
|
], |
120
|
|
|
'cc_servers' => [ |
121
|
|
|
'label' => Yii::t('hipanel', 'Servers'), |
122
|
|
|
'format' => 'html', |
123
|
|
|
'value' => function ($model) { |
124
|
|
|
return Html::tag('span', 'NL:') . $model->nl_servers . '<br/>' . |
125
|
|
|
Html::tag('span', 'US:') . $model->us_servers; |
126
|
|
|
}, |
127
|
|
|
], |
128
|
|
|
'state' => [ |
129
|
|
|
'label' => Yii::t('hipanel', 'State'), |
130
|
|
|
'format' => 'raw', |
131
|
|
|
'class' => RefColumn::class, |
132
|
|
|
'filterOptions' => ['class' => 'narrow-filter'], |
133
|
|
|
'i18nDictionary' => 'hipanel', |
134
|
|
|
'gtype' => 'state,config', |
135
|
|
|
], |
136
|
|
|
]); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|