Skip to content

Commit

Permalink
Added server renew operation
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverFire committed Mar 18, 2016
1 parent 0dfa016 commit f74e93e
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 1 deletion.
63 changes: 63 additions & 0 deletions src/cart/AbstractServerProduct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace hipanel\modules\server\cart;

use DateTime;
use hipanel\modules\server\models\Server;
use hipanel\modules\finance\cart\AbstractCartPosition;
use Yii;

abstract class AbstractServerProduct extends AbstractCartPosition
{
/**
* @var Server
*/
protected $_model;

/**
* @var string the operation name
*/
protected $_operation;

/** {@inheritdoc} */
protected $_calculationModel = Calculation::class;

/** {@inheritdoc} */
public function getIcon()
{
return '<i class="fa fa-server"></i>';
}

public function rules()
{
return [
[['name'], 'required'],
];
}

/** {@inheritdoc} */
public function getQuantityOptions()
{
$result = [];
foreach ([1, 3, 6, 12] as $n) {
$date = (new DateTime($this->_model->expires))->add(new \DateInterval("P{$n}M"));

$result[$n] = Yii::t('hipanel/server', '{n, plural, one{# month} other{# months}} till {date}', [
'n' => $n,
'date' => Yii::$app->formatter->asDate($date)
]);
}

return $result;
}

/** {@inheritdoc} */
public function getCalculationModel($options = [])
{
return parent::getCalculationModel(array_merge([
'type' => $this->_operation,
'server' => $this->name,
'expires' => $this->_model->expires,
], $options));
}
}
35 changes: 35 additions & 0 deletions src/cart/AbstractServerPurchase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace hipanel\modules\server\cart;

/**
* Abstract class AbstractServerPurchase.
*/
abstract class AbstractServerPurchase extends \hipanel\modules\finance\cart\AbstractPurchase
{
use \hipanel\base\ModelTrait;

/** {@inheritdoc} */
public static function type()
{
return 'server';
}

/** {@inheritdoc} */
public function init()
{
parent::init();

$this->server = $this->position->name;
$this->amount = $this->position->getQuantity();
}

/** {@inheritdoc} */
public function rules()
{
return array_merge(parent::rules(), [
[['server', 'expires'], 'safe'],
[['amount'], 'number'],
]);
}
}
27 changes: 27 additions & 0 deletions src/cart/Calculation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace hipanel\modules\server\cart;

class Calculation extends \hipanel\modules\finance\models\Calculation
{
use \hipanel\base\ModelTrait;

/** {@inheritdoc} */
public function init()
{
parent::init();

$this->client = $this->position->getModel()->client;
$this->seller = $this->position->getModel()->seller;
$this->object = 'server';
}

/** {@inheritdoc} */
public function rules()
{
return array_merge(parent::rules(), [
[['server', 'expires'], 'safe'],
[['id'], 'integer'],
]);
}
}
78 changes: 78 additions & 0 deletions src/cart/ServerRenewProduct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/*
* Domain plugin for HiPanel
*
* @link https://github.com/hiqdev/hipanel-module-domain
* @package hipanel-module-domain
* @license BSD-3-Clause
* @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
*/

namespace hipanel\modules\server\cart;

use hipanel\modules\server\models\Server;
use Yii;

class ServerRenewProduct extends AbstractServerProduct
{
/** {@inheritdoc} */
protected $_purchaseModel = 'hipanel\modules\server\cart\ServerRenewPurchase';

/** {@inheritdoc} */
protected $_operation = 'renew';

/** {@inheritdoc} */
public static function primaryKey()
{
return ['model_id'];
}

/** {@inheritdoc} */
public function load($data, $formName = null)
{
$result = parent::load($data, '');
if ($result) {
$this->loadRelatedData();
}

return $result;
}

/** {@inheritdoc} */
private function loadRelatedData()
{
$this->_model = Server::findOne(['id' => $this->model_id]);
$this->name = $this->_model->name;
$this->description = Yii::t('hipanel/server', 'Renewal');
}

/** {@inheritdoc} */
public function getId()
{
return hash('crc32b', implode('_', ['server', 'renew', $this->_model->id]));
}

/** {@inheritdoc} */
public function getCalculationModel($options = [])
{
return parent::getCalculationModel(array_merge([
'id' => $this->model_id,
], $options));
}

/** {@inheritdoc} */
public function getPurchaseModel($options = [])
{
$this->loadRelatedData(); // To get fresh domain expiration date
return parent::getPurchaseModel(array_merge(['expires' => $this->_model->expires], $options));
}

/** {@inheritdoc} */
public function rules()
{
return array_merge(parent::rules(), [
[['model_id'], 'integer'],
]);
}
}
24 changes: 24 additions & 0 deletions src/cart/ServerRenewPurchase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace hipanel\modules\server\cart;

class ServerRenewPurchase extends AbstractServerPurchase
{
/** {@inheritdoc} */
public static function operation()
{
return 'Renew';
}

/**
* @var string domain expiration datetime
*/
public $expires;

public function rules()
{
return array_merge(parent::rules(), [
[['expires'], 'required'],
]);
}
}
8 changes: 7 additions & 1 deletion src/controllers/ServerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace hipanel\modules\server\controllers;

use hipanel\actions\Action;
use hipanel\actions\IndexAction;
use hipanel\actions\ProxyAction;
use hipanel\actions\RedirectAction;
Expand All @@ -19,14 +20,15 @@
use hipanel\base\CrudController;
use hipanel\models\Ref;
use hipanel\modules\finance\models\Tariff;
use hipanel\modules\server\cart\ServerRenewProduct;
use hipanel\modules\server\helpers\ServerHelper;
use hipanel\modules\server\models\Osimage;
use hipanel\modules\server\models\Server;
use hipanel\modules\server\models\ServerUseSearch;
use hiqdev\yii2\cart\actions\AddToCartAction;
use Yii;
use yii\base\Event;
use yii\helpers\ArrayHelper;
use yii\helpers\VarDumper;
use yii\web\NotFoundHttpException;

class ServerController extends CrudController
Expand Down Expand Up @@ -255,6 +257,10 @@ public function actions()
'class' => RedirectAction::class,
'url' => Yii::$app->params['orgUrl'],
],
'add-to-cart-renewal' => [
'class' => AddToCartAction::class,
'productClass' => ServerRenewProduct::class,
],
];
}

Expand Down
4 changes: 4 additions & 0 deletions src/messages/ru/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,8 @@
'Services' => 'Сервисы',
'Manage IP addresses' => 'Управлять IP адресами',
'Resources usage charts' => 'Графики потребления ресурсов',
'Renewal' => 'Продление',
'Renew server' => 'Продлить сервер',
'{n, plural, one{# month} other{# months}} till {date}' => '{n, plural, one{# месяц} few{# месяца} other{# месяцев}} до {date}',
'Server detailed information' => 'Подробная информация о сервере',
];
3 changes: 3 additions & 0 deletions src/views/server/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
<?php Pjax::begin(['enablePushState' => false]) ?>
<div class="profile-usermenu">
<ul class="nav">
<li>
<?= Html::a('<i class="fa fa-forward"></i>' . Yii::t('hipanel/server', 'Renew server'), ['add-to-cart-renewal', 'model_id' => $model->id], ['data-pjax' => 0]); ?>
</li>
<?php if ($model->isPwChangeSupported()) { ?>
<li>
<?= $this->render('_reset-password', compact(['model'])) ?>
Expand Down

0 comments on commit f74e93e

Please sign in to comment.