BasketController.php 3.06 KB
<?php
    
    namespace frontend\controllers;
    
    use common\models\Basket;
    use yii\web\Response;
    
    class BasketController extends \yii\web\Controller
    {
        
        public $enableCsrfValidation = false;
        
        public function actionMain()
        {
            $response = \Yii::$app->response;
            $response->format = Response::FORMAT_JSON;
            /**
             * @var Basket $basket
             */
            $basket = \Yii::$app->basket;
            $result = [
                'basket' => $basket->getData(),
                'modal'  => $this->getModal($basket),
                'cart'   => $this->getCart($basket),
            ];
            return $result;
        }
        
        public function actionAdd(int $product_variant_id, int $count)
        {
            $response = \Yii::$app->response;
            $response->format = Response::FORMAT_JSON;
            /**
             * @var Basket $basket
             */
            $basket = \Yii::$app->basket;
            $basket->add($product_variant_id, $count);
            $result = [
                'basket' => $basket->getData(),
                'modal'  => $this->getModal($basket),
                'cart'   => $this->getCart($basket),
            ];
            return $result;
        }
        
        public function actionSet(int $product_variant_id, int $count)
        {
            $response = \Yii::$app->response;
            $response->format = Response::FORMAT_JSON;
            /**
             * @var Basket $basket
             */
            $basket = \Yii::$app->basket;
            $basket->set($product_variant_id, $count);
            $result = [
                'basket' => $basket->getData(),
                'modal'  => $this->getModal($basket),
                'cart'   => $this->getCart($basket),
            ];
            return $result;
        }
        
        public function actionTest()
        {
            /**
             * @var Basket $basket
             */
            $basket = \Yii::$app->basket;
            $modal = $this->getModal($basket);
            return $modal;
        }
        
        /**
         * @var $basket \common\models\Basket
         * @return string modal_items
         */
        public function getModal($basket): string
        {
            $output = '';
            $data = $basket->getData();
            $models = $basket->findModels(array_keys($data));
            if(!empty( $models )) {
                $output = $this->renderPartial('modal_items', [
                    'models' => $models,
                    'basket' => $basket,
                ]);
            }
            return $output;
        }
        
        /**
         * @param Basket $basket
         *
         * @return string
         */
        public function getCart($basket): string
        {
            $count = $basket->getCount();
            $sum = $basket->getSum();
            $output = $this->renderPartial('cart', [
                'count' => $count,
                'sum'   => $sum,
            ]);
            return $output;
        }
    }