Skip to content

Commit

Permalink
basically fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
hiqsol committed Nov 12, 2015
1 parent 859bc0c commit 3dc05b9
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 83 deletions.
2 changes: 0 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
}
],
"require": {
"hiqdev/hipanel-core": "*",
"hiqdev/hipanel-module-client": "*",
"omnilight/yii2-shopping-cart": "*"
},
"autoload": {
Expand Down
50 changes: 48 additions & 2 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,54 @@
* @copyright Copyright (c) 2015, HiQDev (https://hiqdev.com/)
*/

namespace hipanel\modules\cart;
namespace hiqdev\yii2\cart;

class Module extends \hipanel\base\Module
use Yii;

/**
* Cart Module.
*
* Example application configuration:
*
* ```php
* 'modules' => [
* 'cart' => [
* 'class' => 'hiqdev\cart\Module',
* ],
* ],
* ```
*/
class Module extends \yii\base\Module
{
public function init()
{
parent::init();
if (!$this->has('cart')) {
$this->set('cart', [
'class' => 'yz\shoppingcart\ShoppingCart',
]);
}
}

public static $name = 'cart';

/**
* Finds cart module.
* TODO think of how to find NOT by name
*/
public static function getInstance()
{
return Yii::$app->getModule(static::$name);
}

public function getCart()
{
return $this->get('cart');
}

public function buildUrl($route = null)
{
return '/' . $this->id . '/' . ($route ?: 'cart/index');
}

}
23 changes: 0 additions & 23 deletions src/Plugin.php

This file was deleted.

53 changes: 53 additions & 0 deletions src/actions/AddToCartAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace hiqdev\yii2\cart\actions;

use hiqdev\hiart\Collection;
use hiqdev\yii2\cart\Module;
use Yii;

class AddToCartAction extends \yii\base\Action
{
public $productClass;

public $bulkLoad = false;

public function getModule()
{
return Module::getInstance();
}

public function run()
{
$data = null;
$cart = $this->getModule()->getCart();
$request = Yii::$app->request;
$collection = new Collection([
'model' => new $this->productClass
]);
// $data = $request->isPost ? $request->post() : $request->get();

if (!$this->bulkLoad) {
$data = [$request->post() ?: $request->get()];
}

if ($collection->load($data) && $collection->validate()) {
foreach ($collection->models as $position) {
if (!$cart->hasPosition($position->getId())) {
$cart->put($position);
Yii::$app->session->addFlash('success', Yii::t('app', 'Item is added to cart'));
} else {
Yii::$app->session->addFlash('warning', Yii::t('app', 'Item already exists in the cart'));
}
}
} else {
Yii::$app->session->addFlash('warning', Yii::t('app', 'Item does not exists'));
}

if ($request->isAjax) {
Yii::$app->end();
} else {
return $this->controller->redirect($request->referrer);
}
}
}
42 changes: 16 additions & 26 deletions src/controllers/CartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,66 +9,56 @@
* @copyright Copyright (c) 2015, HiQDev (https://hiqdev.com/)
*/

namespace hipanel\modules\cart\controllers;
use hipanel\modules\domain\models\Domain;
use hipanel\modules\domain\models\DomainProduct;
namespace hiqdev\yii2\cart\controllers;

use Yii;
use yii\data\ArrayDataProvider;
use yii\web\NotFoundHttpException;
use yz\shoppingcart\ShoppingCart;
use hiqdev\yii2\cart\Module;

/**
* Cart controller.
*/
class CartController extends \hipanel\base\Controller
class CartController extends \yii\web\Controller
{
public function getCart()
{
return Module::getInstance()->getCart();
}

public function actionIndex()
{
$dataProvider = new ArrayDataProvider([
'allModels' => Yii::$app->cart->getPositions(),
'allModels' => $this->getCart()->getPositions(),
'pagination' => [
'pageSize' => 10,
'pageSize' => 25,
],
]);
return $this->render('index', [
'cart' => $this->getCart(),
'dataProvider' => $dataProvider,
]);
}

public function actionRemove($id)
{
Yii::$app->cart->removeById($id);
$this->getCart()->removeById($id);

if (Yii::$app->request->isAjax)
Yii::$app->end();

return $this->redirect(['index']);
}

public function actionAdd()
{
$cart = Yii::$app->cart;
$cart->removeAll();

$model = new DomainProduct([
'model' => Domain::find()->where(['login' => 'rubbertire'])->one()
]);
if ($model) {
$cart->put($model, 1);
return $this->redirect(['index']);
}
throw new NotFoundHttpException();
}

public function actionUpdateQuantity()
{
$request = Yii::$app->request;
$id = $request->post('id');
$quantity = $request->post('quantity');
if ($id && $quantity) {
$position = Yii::$app->cart->getPositionById($id);
$position = $this->getCart()->getPositionById($id);
if ($position) {
Yii::$app->cart->update($position, $quantity);
$this->getCart()->update($position, $quantity);
$this->redirect('index');
}
}
Expand All @@ -78,7 +68,7 @@ public function actionUpdateQuantity()

public function actionClear()
{
Yii::$app->cart->removeAll();
$this->getCart()->removeAll();

if (Yii::$app->request->isAjax)
Yii::$app->end();
Expand Down
4 changes: 2 additions & 2 deletions src/grid/QuantityColumn.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace hipanel\modules\cart\grid;
namespace hiqdev\yii2\cart\grid;

use hiqdev\xeditable\grid\XEditableColumn;
use hiqdev\xeditable\widgets\XEditable;
Expand All @@ -13,4 +13,4 @@ public function getDataCellValue($model, $key, $index)
{
return $key;
}
}
}
6 changes: 3 additions & 3 deletions src/views/cart/index.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use hipanel\modules\cart\grid\QuantityColumn;
use hipanel\modules\cart\widgets\QuantityCell;
use hiqdev\yii2\cart\grid\QuantityColumn;
use hiqdev\yii2\cart\widgets\QuantityCell;
use yii\grid\ActionColumn;
use yii\grid\GridView;
use yii\helpers\Html;
Expand Down Expand Up @@ -98,7 +98,7 @@
</tr>
<tr>
<th><?= Yii::t('app', 'Total') ?>:</th>
<td><?= Yii::$app->cart->getCost(true); ?></td>
<td><?= $cart->getCost(true) ?></td>
</tr>
</tbody>
</table>
Expand Down
12 changes: 10 additions & 2 deletions src/widgets/PanelTopCart.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
<?php

namespace hipanel\modules\cart\widgets;
namespace hiqdev\yii2\cart\widgets;

use Yii;
use hiqdev\yii2\cart\Module;

class PanelTopCart extends \yii\base\Widget
{
public function run()
{
return $this->render('PanelTopCart_view', [
'view' => $this->getView(),
'widget' => $this,
'view' => $this->getView(),
'cart' => $this->getModule()->getCart(),
]);
}

public function getModule()
{
return Module::getInstance();
}
}
6 changes: 3 additions & 3 deletions src/widgets/QuantityCell.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace hipanel\modules\cart\widgets;
namespace hiqdev\yii2\cart\widgets;

use Yii;

Expand All @@ -23,9 +23,9 @@ public function init()
public function run()
{
return $this->render('QuantityCell_view', [
'view' => $this->getView(),
'view' => $this->getView(),
'model' => $this->model,
'type' => $this->type ?: self::MODE_SELECT,
'type' => $this->type ?: self::MODE_SELECT,
]);
}

Expand Down
23 changes: 11 additions & 12 deletions src/widgets/views/PanelTopCart_view.php
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
<?php

use \Yii;
use yii\helpers\Html;

?>
<!-- Notifications: style can be found in dropdown.less -->
<li class="dropdown notifications-menu">
<a class="dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-shopping-cart fa-lg"></i>
<span class="label label-warning"><?= Yii::$app->cart->getCount(); ?></span>
<a class="dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-shopping-cart fa-lg"></i>
<span class="label label-warning"><?= $cart->count ?></span>
</a>
<ul class="dropdown-menu">
<li class="header">
<div class="row">
<div class="col-md-6 text-bold"><?= Yii::t('app', 'Total:') ?></div>
<!-- /.col-md-6 -->
<div class="col-md-6 text-right"><?= Yii::$app->cart->getCost(); ?></div>
<div class="col-md-6 text-right"><?= $cart->cost ?></div>
<!-- /.col-md-6 -->
</div>
<!-- /.row -->
</li>
<li>
<!-- inner menu: contains the actual data -->
<ul class="menu">
<?php if (Yii::$app->cart->getCount() > 0) : ?>
<?php foreach (Yii::$app->cart->getPositions() as $position) : ?>
<?php if ($cart->count > 0) : ?>
<?php foreach ($cart->positions as $position) : ?>
<li>
<a>
<?= $position->icon ?> <?= $position->name; ?> | <?= $position->description ?>
<?= $position->icon ?> <?= $position->name ?> | <?= $position->description ?>
</a>
</li>
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach ?>
<?php endif ?>
</ul>
</li>
<li class="footer"><?= Html::a(Yii::t('app', 'View Cart'), '/cart/cart/index'); ?></li>
<li class="footer"><?= Html::a(Yii::t('app', 'View Cart'), $widget->module->buildUrl()) ?></li>
</ul>
</li>
</li>
15 changes: 7 additions & 8 deletions src/widgets/views/QuantityCell_view.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
<?php

use hipanel\modules\cart\widgets\QuantityCell;
use hiqdev\yii2\cart\widgets\QuantityCell;
use yii\helpers\Html;

?>

<?= Html::beginForm('update-quantity', 'post', ['id' => $model->id]) ?>

<?= Html::hiddenInput('id', $model->id) ?>

<?php if ($type == QuantityCell::MODE_SELECT) : ?>
<?= Html::dropDownList('quantity', $model->quantity, $model->getQuantityOptions(), ['class' => 'form-control quantity-field']) ?>
<?php else : ?>
<?= Html::input('number', 'quantity', $model->quantity, ['class' => 'form-control quantity-field', 'min' => 1, 'max' => 99, 'step' => 1]) ?>
<?php endif; ?>
<?= Html::hiddenInput('id', $model->id) ?>
<?php if ($type == QuantityCell::MODE_SELECT) : ?>
<?= Html::dropDownList('quantity', $model->quantity, $model->getQuantityOptions(), ['class' => 'form-control quantity-field']) ?>
<?php else : ?>
<?= Html::input('number', 'quantity', $model->quantity, ['class' => 'form-control quantity-field', 'min' => 1, 'max' => 99, 'step' => 1]) ?>
<?php endif; ?>

<?= Html::endForm() ?>

0 comments on commit 3dc05b9

Please sign in to comment.