Issues (213)

src/grid/ConfigGridView.php (1 issue)

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::a($config->{$region . '_tariff'}, ['@plan/view', 'id' => $config->{$region . '_tariff_id'}]);
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
            $columns["{$region}_servers"] = [
52
                'format' => 'raw',
53
                'value' => function (Config $config) use ($region): string {
54
                    $servers = explode(',', $config->{$region . '_servers'});
55
                    $server_ids = explode(',', $config->{$region . '_server_ids'});
56
57
                    $links = [];
58
                    foreach (array_combine($server_ids, $servers) as $id => $server) {
59
                        $links[] = Html::a(trim($server), ['@server/view', 'id' => trim($id)]);
60
                    }
61
62
                    return implode(', ', array_unique($links));
63
                },
64
            ];
65
        }
66
67
        return $columns;
68
    }
69
70
    public function columns()
71
    {
72
        return array_merge(parent::columns(), $this->getTariffsWithOldPriceColumns(), [
73
            'actions' => [
74
                'class' => MenuColumn::class,
75
                'menuClass' => ConfigActionsMenu::class,
76
            ],
77
            'name' => [
78
                'class' => MainColumn::class,
79
                'label' => Yii::t('hipanel', 'Name'),
80
                'filterOptions' => ['class' => 'narrow-filter'],
81
                'format' => 'html',
82
                'value' => function ($model) {
83
                    return Html::a($model->name, ['@config/view', 'id' => $model->id]) .
84
                        '</br>' . Html::tag('span', $model->label);
85
                },
86
            ],
87
            'config' => [
88
                'class' => MainColumn::class,
89
                'format' => 'html',
90
                'value' => function ($model) {
91
                    $value = '';
92
                    foreach (['cpu', 'ram', 'hdd', 'ssd'] as $item) {
93
                        if (empty($model->$item)) {
94
                            continue;
95
                        }
96
                        $value .= '<nobr>' . Html::tag('span', strtoupper($item) . ': ') .
97
                            Html::tag('span', $model->$item) . '</nobr></br>';
98
                    }
99
100
                    return $value;
101
                },
102
            ],
103
            'profiles' => [
104
                'format' => 'raw',
105
                'filterOptions' => ['class' => 'narrow-filter'],
106
                'attribute' => 'profiles',
107
                'filter' => ConfigProfileCombo::widget([
108
                    'attribute' => 'profiles_like',
109
                    'model' => $this->filterModel ?? new ConfigSearch(),
110
                    'formElementSelector' => 'td',
111
                ]),
112
                'enableSorting' => false,
113
                'value' => function (Config $config): string {
114
                    $colors = ['bg-teal', 'bg-green', 'bg-yellow', 'bg-purple', 'bg-aqua', 'bg-red'];
115
                    return Html::tag('ul', implode('<br>', array_map(function ($profile) use (&$colors) {
116
                        return Html::tag('li', $profile, ['class' => 'badge ' . array_pop($colors)]);
117
                    }, array_map('trim', explode(',', $config->profiles)))), ['class' => 'list-unstyled']);
0 ignored issues
show
Bug Best Practice introduced by Andrey Klochok
The property profiles does not exist on hipanel\modules\server\models\Config. Since you implemented __get, consider adding a @property annotation.
Loading history...
118
                },
119
            ],
120
            'tariffs' => [
121
                'class' => MainColumn::class,
122
                'format' => 'html',
123
                'value' => function ($model) {
124
                    return Html::tag('span', 'NL:') .
125
                        Html::a($model->nl_tariff, ['@plan/view', 'id' => $model->nl_tariff_id]) . '</br>' .
126
                        Html::tag('span', 'US:') .
127
                        Html::a($model->us_tariff, ['@plan/view', 'id' => $model->us_tariff_id]);
128
                },
129
            ],
130
            'servers' => [
131
                'format' => 'raw',
132
                'value' => function (Config $model) {
133
                    $servers = explode(',', $model->servers);
134
                    $server_ids = explode(',', $model->server_ids);
135
136
                    $links = [];
137
                    foreach (array_combine($server_ids, $servers) as $id => $server) {
138
                        $links[] = Html::a(trim($server), ['@server/view', 'id' => trim($id)]);
139
                    }
140
141
                    return implode(', ', array_unique($links));
142
                },
143
            ],
144
            'cc_servers' => [
145
                'label' => Yii::t('hipanel', 'Servers'),
146
                'format' => 'html',
147
                'value' => function ($model) {
148
                    return Html::tag('span', 'NL:') . $model->nl_servers . '<br/>' .
149
                        Html::tag('span', 'US:') . $model->us_servers;
150
                },
151
            ],
152
            'state' => [
153
                'label' => Yii::t('hipanel', 'State'),
154
                'format' => 'raw',
155
                'class' => RefColumn::class,
156
                'filterOptions' => ['class' => 'narrow-filter'],
157
                'i18nDictionary' => 'hipanel',
158
                'gtype' => 'state,config',
159
            ],
160
        ]);
161
    }
162
}
163