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