Issues (213)

src/grid/BindingColumn.php (2 issues)

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\helpers\StringHelper;
14
use hipanel\modules\server\models\Binding;
15
use Yii;
16
use yii\grid\DataColumn;
17
use yii\helpers\Html;
18
19
class BindingColumn extends DataColumn
20
{
21
    public $format = 'raw';
22
23
    public $filter = false;
24
25
    public function init()
26
    {
27
        parent::init();
28
        $this->visible = Yii::$app->user->can('hub.read');
29
        $this->label = $this->getLabel();
30
    }
31
32
    public function getDataCellValue($model, $key, $index)
33
    {
34
        if (StringHelper::startsWith($this->attribute, 'ipmi', false) && ($ipmi = $model->getBinding($this->attribute)) !== null) {
35
            $link = Html::a($ipmi->device_ip, "http://$ipmi->device_ip/", ['target' => '_blank']) . ' ';
36
37
            return $link . $this->renderSwitchPort($ipmi);
38
        }
39
40
        return $this->renderSwitchPort($model->bindings[$this->attribute]);
41
    }
42
43
    /**
44
     * @param Binding|null $binding
45
     * @return string
46
     */
47
    protected function renderSwitchPort(?Binding $binding): string
48
    {
49
        if ($binding === null) {
50
            return '';
51
        }
52
53
        $label = $binding->switch_label;
0 ignored issues
show
Bug Best Practice introduced by tofid
The property switch_label does not exist on hipanel\modules\server\models\Binding. Since you implemented __get, consider adding a @property annotation.
Loading history...
54
        $inn = $binding->switch_inn;
0 ignored issues
show
Bug Best Practice introduced by tofid
The property switch_inn does not exist on hipanel\modules\server\models\Binding. Since you implemented __get, consider adding a @property annotation.
Loading history...
55
        $inn = $inn ? "($inn) " : '';
56
        $main = $binding->switch . ($binding->port ? ':' . $binding->port : '');
57
58
        return "$inn<b>$main</b> $label";
59
    }
60
61
    /**
62
     * @return string
63
     */
64
    private function getLabel(): string
65
    {
66
        $defaultLabel = $this->getHeaderCellLabel();
67
        $addColon = preg_replace('/(\d+)/', ':${1}', $defaultLabel);
68
        $replaceName = preg_replace(['/Net/', '/Pdu/'], ['Switch', 'APC'], $addColon);
69
70
        return (string) $replaceName;
71
    }
72
}
73