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\forms; |
12
|
|
|
|
13
|
|
|
use hipanel\helpers\StringHelper; |
14
|
|
|
use hipanel\modules\server\models\Server; |
15
|
|
|
use hipanel\modules\server\validators\MacValidator; |
16
|
|
|
use yii\helpers\ArrayHelper; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class ServerForm represent create/update form. |
20
|
|
|
*/ |
21
|
|
|
class ServerForm extends Server |
22
|
|
|
{ |
23
|
|
|
use \hipanel\base\ModelTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
public static function tableName() |
29
|
|
|
{ |
30
|
|
|
return 'server'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Create ServerForm model from Server model. |
35
|
|
|
* |
36
|
|
|
* @param Server $server |
37
|
|
|
* @return ServerForm |
38
|
|
|
*/ |
39
|
|
|
public static function fromServer(Server $server): ServerForm |
40
|
|
|
{ |
41
|
|
|
return new self(array_merge($server->getAttributes(), ['server' => $server->name, 'scenario' => 'update'], [ |
42
|
|
|
'ips' => implode(',', ArrayHelper::getColumn($server->ips, 'ip')), |
|
|
|
|
43
|
|
|
])); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
1 |
|
public function rules() |
50
|
|
|
{ |
51
|
1 |
|
return array_merge(parent::rules(), [ |
52
|
|
|
// Create/update servers |
53
|
1 |
|
[['server', 'type', 'state'], 'required', 'on' => ['create', 'update']], |
54
|
|
|
[['server', 'dc'], 'unique', 'on' => ['create']], |
55
|
|
|
[ |
56
|
1 |
|
['server'], 'unique', 'on' => ['update'], 'when' => function ($model) { |
57
|
|
|
if ($model->isAttributeChanged('server')) { |
58
|
|
|
return self::findOne($model->id)->name !== $model->server; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return false; |
62
|
1 |
|
}, |
63
|
|
|
], |
64
|
|
|
[ |
65
|
1 |
|
['dc'], 'unique', 'on' => ['update'], 'when' => function ($model) { |
66
|
|
|
if ($model->isAttributeChanged('dc')) { |
67
|
|
|
return self::findOne($model->id)->dc !== $model->dc; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return false; |
71
|
1 |
|
}, |
72
|
|
|
], |
73
|
|
|
[['name', 'dc', 'label', 'order_no', 'hwsummary'], 'string', 'on' => ['create', 'update']], |
74
|
|
|
[['mac'], MacValidator::class, 'on' => ['create', 'update']], |
75
|
|
|
[ |
76
|
1 |
|
['ips'], 'filter', |
77
|
1 |
|
'filter' => function ($value) { |
78
|
|
|
return (mb_strlen($value) > 0) ? StringHelper::mexplode($value) : ''; |
79
|
1 |
|
}, |
80
|
|
|
'on' => ['create', 'update'], |
81
|
|
|
], |
82
|
|
|
[['ips'], 'each', 'rule' => ['ip'], 'on' => ['create', 'update']], |
83
|
|
|
]); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|