Skip to content

Commit

Permalink
Added add-to-cart-dedicated, added Product and Purchase cart models (
Browse files Browse the repository at this point in the history
…#99)

* Added `add-to-cart-dedicated`, added Product and Purchase cart models

* Added new attributes to Osimage model

* Added ability added to cart dedicated server

* minor
  • Loading branch information
tafid authored and hiqsol committed Jun 14, 2019
1 parent 27dc7b4 commit 6de725f
Show file tree
Hide file tree
Showing 12 changed files with 437 additions and 11 deletions.
30 changes: 30 additions & 0 deletions src/cart/ConfigCalculation.php
@@ -0,0 +1,30 @@
<?php

namespace hipanel\modules\server\cart;

use hipanel\base\ModelTrait;
use hipanel\modules\finance\cart\Calculation;


class ConfigCalculation extends Calculation
{
use ModelTrait;

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

$this->object = 'serverConfig';
$this->type = 'purchase';
}

/** {@inheritdoc} */
public function rules()
{
return array_merge(parent::rules(), [
[['object_id'], 'integer'],
[['location'], 'string'],
]);
}
}
183 changes: 183 additions & 0 deletions src/cart/ServerOrderDedicatedProduct.php
@@ -0,0 +1,183 @@
<?php
/**
* Server module for HiPanel
*
* @link https://github.com/hiqdev/hipanel-module-server
* @package hipanel-module-server
* @license BSD-3-Clause
* @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
*/

namespace hipanel\modules\server\cart;

use hipanel\modules\server\models\Config;
use hipanel\modules\server\models\Osimage;
use hipanel\modules\server\models\Package;
use hipanel\modules\server\widgets\cart\OrderPositionDescriptionWidget;
use Yii;
use yii\helpers\ArrayHelper;

class ServerOrderDedicatedProduct extends AbstractServerProduct
{
/** {@inheritdoc} */
protected $_purchaseModel = ServerOrderDedicatedPurchase::class;

/** @var Package */
protected $_model;

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

/**
* @var Osimage the selected OS image detailed information
*/
protected $_image;

/**
* @var string
*/
public $location;

/**
* @var integer
*/
public $object_id;

/**
* @var integer
*/
public $tariff_id;

/**
* @var string
*/
public $label;

/**
* @var string link to any kind of social network
*/
public $administration;

/**
* @var string osimage name. Is used to load [[_image]] on product initialisation
*/
public $osimage;

/**
* @var string software package name
*/
public $softpack;

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

return $result;
}

/** {@inheritdoc} */
private function ensureRelatedData()
{
$this->_model = Config::findOne($this->object_id);
$this->_image = Osimage::find()->where(['osimage' => $this->osimage, 'type' => 'dedicated'])->one();
$this->name = $this->_model->name;
$this->description = $this->_model->descr;
}

/** {@inheritdoc} */
public function getId()
{
if ($this->_id === null) {
$this->_id = hash('crc32b', implode('_', ['server', 'order', 'dedicated', $this->_model->id]));
}

return $this->_id;
}

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

/** {@inheritdoc} */
public function getPurchaseModel($options = [])
{
$this->ensureRelatedData();

$options = array_merge([
'osimage' => $this->osimage,
'object_id' => $this->object_id,
'label' => $this->label,
'administration' => $this->administration,
'softpack' => $this->softpack,
'tariff_id' => $this->tariff_id,
], $options);

return parent::getPurchaseModel($options);
}

/** {@inheritdoc} */
public function rules()
{
return array_merge(parent::rules(), [
[['object_id', 'tariff_id'], 'integer'],
[['administration', 'osimage', 'label', 'location'], 'string'],
[['tariff_id', 'object_id', 'osimage', 'location'], 'required'],
]);
}

/** {@inheritdoc} */
public function attributeLabels()
{
return ArrayHelper::merge(parent::attributeLabels(), [
'object_id' => Yii::t('hipanel:server:order', 'Server config'),
'label' => Yii::t('hipanel:server:order', 'Label'),
]);
}

/** {@inheritdoc} */
public function attributeHints()
{
return ArrayHelper::merge(parent::attributeHints(), [
'label' => Yii::t('hipanel:server:order', 'How are you going to use the server?'),
'administration' => Yii::t('hipanel:server:order', 'Any social network link. Will be used in case of emergency contact'),
]);
}

/** {@inheritdoc} */
public function renderDescription()
{
return OrderPositionDescriptionWidget::widget(['position' => $this]);
}

/**
* @return Osimage
*/
public function getImage()
{
return $this->_image;
}

protected function serializationMap()
{
$parent = parent::serializationMap();
$parent['object_id'] = $this->object_id;
$parent['osimage'] = $this->osimage;
$parent['label'] = $this->label;
$parent['administration'] = $this->administration;
$parent['softpack'] = $this->softpack;
$parent['tariff_id'] = $this->tariff_id;
$parent['location'] = $this->location;
$parent['_image'] = $this->_image;

return $parent;
}
}
71 changes: 71 additions & 0 deletions src/cart/ServerOrderDedicatedPurchase.php
@@ -0,0 +1,71 @@
<?php
/**
* Server module for HiPanel
*
* @link https://github.com/hiqdev/hipanel-module-server
* @package hipanel-module-server
* @license BSD-3-Clause
* @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
*/

namespace hipanel\modules\server\cart;

use hipanel\base\ModelTrait;
use hipanel\modules\finance\cart\PendingPurchaseException;
use hipanel\widgets\Box;
use Yii;

/**
* Class ServerOrderPurchase.
*/
class ServerOrderDedicatedPurchase extends AbstractServerPurchase
{
use ModelTrait;

/** {@inheritdoc} */
public static function operation()
{
return 'Buy';
}

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

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

public function rules()
{
return array_merge(parent::rules(), [
[['osimage', 'config_id', 'tariff_id'], 'required'],
[['osimage', 'administration', 'softpack', 'label', 'location'], 'string'],
[['tariff_id', 'object_id'], 'integer'],
]);
}

public function execute()
{
if (parent::execute()) {
$remark = Box::widget([
'options' => ['class' => 'box-solid box-warning'],
'body' => Yii::t('hipanel:server:order', 'You will receive an email with server access information right after setup'),
]);

Yii::$app->getView()->params['remarks'][__CLASS__] = $remark;

if (is_array($this->_result) && isset($this->_result['_action_pending'])) {
throw new PendingPurchaseException(Yii::t('hipanel:server:order', 'Server setup will be performed as soon as manager confirms your account verification. Pleas wait.'), $this);
}

return true;
}

return false;
}

public function renderNotes()
{
}
}
22 changes: 22 additions & 0 deletions src/controllers/OrderController.php
Expand Up @@ -13,8 +13,11 @@
use hipanel\base\CrudController;
use hipanel\filters\EasyAccessControl;
use hipanel\modules\finance\models\Tariff;
use hipanel\modules\server\cart\ServerOrderDedicatedProduct;
use hipanel\modules\server\cart\ServerOrderProduct;
use hipanel\modules\server\helpers\ServerHelper;
use hipanel\modules\server\models\Config;
use hipanel\modules\server\models\Osimage;
use hipanel\modules\server\Module;
use hiqdev\yii2\cart\actions\AddToCartAction;
use Yii;
Expand Down Expand Up @@ -46,6 +49,7 @@ public function behaviors()
'class' => EasyAccessControl::class,
'actions' => [
'add-to-cart' => 'server.pay',
'add-to-cart-dedicated' => 'server.pay',
'index' => 'server.pay',
'xen-ssd' => 'server.pay',
'open-vz' => 'server.pay',
Expand All @@ -63,6 +67,11 @@ public function actions()
'productClass' => ServerOrderProduct::class,
'redirectToCart' => true,
],
'add-to-cart-dedicated' => [
'class' => AddToCartAction::class,
'productClass' => ServerOrderDedicatedProduct::class,
'redirectToCart' => true,
],
];
}

Expand Down Expand Up @@ -112,6 +121,19 @@ public function actionOpenVz()
***/
}

public function actionDedicated()
{
$this->layout = '@hipanel/server/order/yii/views/layouts/advancedhosting';
$params = Yii::$app->user->isGuest ? ['seller' => Yii::$app->params['user.seller']] : [
'seller' => Yii::$app->user->identity->seller,
'seller_id' => Yii::$app->user->identity->seller_id,
];
$configs = Config::find()->addAction('get-available')->where($params)->withPrices()->all();
$osimages = Osimage::find()->where(['type' => 'dedicated'])->all();

return $this->render('dedicated', compact('configs', 'osimages'));
}

public function actionTariffsDetails()
{
return $this->render('tariffs_details');
Expand Down
21 changes: 21 additions & 0 deletions src/models/Config.php
Expand Up @@ -12,6 +12,7 @@

use hipanel\base\Model;
use hipanel\base\ModelTrait;
use hipanel\modules\server\models\query\ConfigQuery;
use Yii;

class Config extends Model
Expand All @@ -28,6 +29,10 @@ class Config extends Model

const SCENARIO_DISABLE = 'disable';

const LOCATION_NL = 'nl';

const LOCATION_US = 'us';

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -94,4 +99,20 @@ public function attributeLabels()
'sort_order' => Yii::t('hipanel:server:config', 'Sort order'),
]);
}

public function getPrices()
{
return $this->hasMany(ConfigPrice::class, ['config_id' => 'id'])->indexBy('location');
}

/**
* {@inheritdoc}
* @return ConfigQuery
*/
public static function find($options = [])
{
return new ConfigQuery(get_called_class(), [
'options' => $options,
]);
}
}

0 comments on commit 6de725f

Please sign in to comment.