1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Finance module for HiPanel |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/hipanel-module-finance |
6
|
|
|
* @package hipanel-module-finance |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hipanel\modules\finance\models; |
12
|
|
|
|
13
|
|
|
use hipanel\base\ModelTrait; |
14
|
|
|
use hipanel\modules\finance\models\decorators\server\AbstractServerResourceDecorator; |
15
|
|
|
use hipanel\modules\finance\models\decorators\server\ServerResourceDecoratorFactory; |
16
|
|
|
use Yii; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class DomainResource. |
20
|
|
|
*/ |
21
|
|
|
class ServerResource extends Resource |
22
|
|
|
{ |
23
|
|
|
use ModelTrait; |
24
|
|
|
|
25
|
|
|
public static function tableName() |
26
|
|
|
{ |
27
|
|
|
return 'resource'; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
const MODEL_TYPE_CPU = 'cpu'; |
31
|
|
|
const MODEL_TYPE_RAM = 'ram'; |
32
|
|
|
const MODEL_TYPE_HDD = 'hdd'; |
33
|
|
|
const MODEL_TYPE_CHASSIS = 'chassis'; |
34
|
|
|
|
35
|
|
|
const TYPE_ISP5 = 'isp5'; |
36
|
|
|
const TYPE_ISP = 'isp'; |
37
|
|
|
const TYPE_SUPPORT_TIME = 'support_time'; |
38
|
|
|
const TYPE_IP_NUMBER = 'ip_num'; |
39
|
|
|
const TYPE_SERVER_TRAF_MAX = 'server_traf_max'; |
40
|
|
|
const TYPE_SERVER_TRAF95_MAX = 'server_traf95_max'; |
41
|
|
|
const TYPE_BACKUP_DU = 'backup_du'; |
42
|
|
|
|
43
|
|
|
public function rules() |
44
|
|
|
{ |
45
|
|
|
$rules = parent::rules(); |
46
|
|
|
$rules[] = [['model_type', 'partno', 'r_object_id'], 'safe']; |
47
|
|
|
$rules['create-required'] = [ |
48
|
|
|
['object_id'], |
49
|
|
|
'required', |
50
|
|
|
'on' => ['create', 'update'], |
51
|
|
|
'when' => function ($model) { |
52
|
|
|
return $model->isHardwareTypeCorrect(); |
53
|
|
|
}, |
54
|
|
|
]; |
55
|
|
|
unset($rules['create-required-price']); |
56
|
|
|
|
57
|
|
|
return $rules; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
|
public function getHardwareTypes() |
64
|
|
|
{ |
65
|
|
|
return [ |
66
|
|
|
static::MODEL_TYPE_CHASSIS => Yii::t('hipanel:finance:tariff', 'Chassis'), |
67
|
|
|
static::MODEL_TYPE_CPU => Yii::t('hipanel:finance:tariff', 'CPU'), |
68
|
|
|
static::MODEL_TYPE_RAM => Yii::t('hipanel:finance:tariff', 'RAM'), |
69
|
|
|
static::MODEL_TYPE_HDD => Yii::t('hipanel:finance:tariff', 'HDD'), |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function isHardwareTypeCorrect() |
74
|
|
|
{ |
75
|
|
|
return isset($this->getHardwareTypes()[$this->model_type]); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getTypes() |
79
|
|
|
{ |
80
|
|
|
return [ |
81
|
|
|
static::TYPE_ISP5 => Yii::t('hipanel:finance:tariff', 'ISP Manager 5'), |
82
|
|
|
static::TYPE_ISP => Yii::t('hipanel:finance:tariff', 'ISP Manager'), |
83
|
|
|
static::TYPE_SUPPORT_TIME => Yii::t('hipanel:finance:tariff', 'Support time'), |
84
|
|
|
static::TYPE_IP_NUMBER => Yii::t('hipanel:finance:tariff', 'IP addresses count'), |
85
|
|
|
static::TYPE_SERVER_TRAF_MAX => Yii::t('hipanel:finance:tariff', 'Server traffic'), |
86
|
|
|
static::TYPE_SERVER_TRAF95_MAX => Yii::t('hipanel:finance:tariff', '95 percentile traffic'), |
87
|
|
|
static::TYPE_BACKUP_DU => Yii::t('hipanel:finance:tariff', 'Backup disk usage'), |
88
|
|
|
]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return AbstractServerResourceDecorator |
93
|
|
|
*/ |
94
|
|
|
public function decorator() |
95
|
|
|
{ |
96
|
|
|
if (empty($this->decorator)) { |
97
|
|
|
$this->decorator = ServerResourceDecoratorFactory::createFromResource($this); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this->decorator; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function realObjectId() |
104
|
|
|
{ |
105
|
|
|
if (!$this->isPeriodic()) { |
106
|
|
|
return $this->object_id; |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (!$this->tariff->is_personal && isset($this->r_object_id)) { |
|
|
|
|
110
|
|
|
return $this->r_object_id; |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $this->object_id; |
|
|
|
|
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.