|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Server module for HiPanel |
|
5
|
|
|
* |
|
6
|
|
|
* @link https://github.com/hiqdev/hipanel-module-server |
|
7
|
|
|
* @package hipanel-module-server |
|
8
|
|
|
* @license BSD-3-Clause |
|
9
|
|
|
* @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace hipanel\modules\server\cart; |
|
13
|
|
|
|
|
14
|
|
|
use DateTime; |
|
15
|
|
|
use hipanel\modules\server\models\Server; |
|
16
|
|
|
use Yii; |
|
17
|
|
|
use yii\helpers\Html; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class ServerRenewProduct. |
|
21
|
|
|
*/ |
|
22
|
|
|
class ServerRenewProduct extends AbstractServerProduct |
|
23
|
|
|
{ |
|
24
|
|
|
/** {@inheritdoc} */ |
|
25
|
|
|
protected $_purchaseModel = 'hipanel\modules\server\cart\ServerRenewPurchase'; |
|
26
|
|
|
|
|
27
|
|
|
/** {@inheritdoc} */ |
|
28
|
|
|
protected $_calculationModel = RenewCalculation::class; |
|
29
|
|
|
|
|
30
|
|
|
/** {@inheritdoc} */ |
|
31
|
|
|
public static function primaryKey() |
|
32
|
|
|
{ |
|
33
|
|
|
return ['model_id']; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** {@inheritdoc} */ |
|
37
|
|
|
public function load($data, $formName = null) |
|
38
|
|
|
{ |
|
39
|
|
|
if ($result = parent::load($data, '')) { |
|
40
|
|
|
$this->ensureRelatedData(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $result; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** {@inheritdoc} */ |
|
47
|
|
|
private function ensureRelatedData() |
|
48
|
|
|
{ |
|
49
|
|
|
$this->_model = Server::findOne(['id' => $this->model_id]); |
|
|
|
|
|
|
50
|
|
|
$this->name = $this->_model->name; |
|
|
|
|
|
|
51
|
|
|
$this->description = Yii::t('hipanel/server', 'Renewal'); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** {@inheritdoc} */ |
|
55
|
|
|
public function getId() |
|
56
|
|
|
{ |
|
57
|
|
|
return hash('crc32b', implode('_', ['server', 'renew', $this->_model->id])); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** {@inheritdoc} */ |
|
61
|
|
|
public function getCalculationModel($options = []) |
|
62
|
|
|
{ |
|
63
|
|
|
return parent::getCalculationModel(array_merge([ |
|
64
|
|
|
'id' => $this->model_id, |
|
|
|
|
|
|
65
|
|
|
'type' => 'renew', |
|
66
|
|
|
'server' => $this->name, |
|
|
|
|
|
|
67
|
|
|
'expires' => $this->_model->expires, |
|
|
|
|
|
|
68
|
|
|
], $options)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** {@inheritdoc} */ |
|
72
|
|
View Code Duplication |
public function getQuantityOptions() |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
$result = []; |
|
75
|
|
|
foreach ([1, 3, 6, 12] as $n) { |
|
76
|
|
|
$date = (new DateTime($this->_model->expires))->add(new \DateInterval("P{$n}M")); |
|
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
$result[$n] = Yii::t('hipanel/server', '{n, plural, one{# month} other{# months}} till {date}', [ |
|
79
|
|
|
'n' => $n, |
|
80
|
|
|
'date' => Yii::$app->formatter->asDate($date), |
|
81
|
|
|
]); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $result; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** {@inheritdoc} */ |
|
88
|
|
|
public function getPurchaseModel($options = []) |
|
89
|
|
|
{ |
|
90
|
|
|
$this->ensureRelatedData(); // To get fresh domain expiration date |
|
91
|
|
|
return parent::getPurchaseModel(array_merge(['expires' => $this->_model->expires], $options)); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** {@inheritdoc} */ |
|
95
|
|
|
public function rules() |
|
96
|
|
|
{ |
|
97
|
|
|
return array_merge(parent::rules(), [ |
|
98
|
|
|
[['model_id'], 'integer'], |
|
99
|
|
|
[['name'], 'required'], |
|
100
|
|
|
]); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function renderDescription() |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->getIcon() . ' ' . Html::a($this->getName(), ['@server/view', 'id' => $this->model_id]) . ' ' . Html::tag('span', $this->getDescription(), ['class' => 'text-muted']); |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
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@propertyannotation 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.