BasketController.php 3 KB
<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use app\models\Mod;
use app\models\Order;
use app\models\OrdersProducts;
use yii\web\HttpException;

class BasketController extends Controller
{

    public function actionIndex(){
            $modelMod = new Mod;
            $modelOrder = new Order;

            if(!empty($_GET['deleteID'])){
                $modelMod->deleteBasketMod($_GET['deleteID']);
                return Yii::$app->response->redirect(['basket/index']);
            }
            
            if(isset($_POST['update'])){
                foreach ($_POST['Mod'] as $index=>$row) {
                  $modelMod->updateBasket($row);  
                }
            }elseif(isset($_POST['Mod'])){
                $body = '';
                foreach ($_POST['Mod'] as $index=>$row) {
                  $body .= $row['product_name'].' '.$row['name'].' Кол:'.$row['count'].' Цена:'.$row['sum_cost'];
                  $body .= "\n\r";
                }
                $body .= "\n\r";
  
                    if ($modelOrder->load(Yii::$app->request->post()) && $modelOrder->save() && $modelOrder->contact('info@bubochka.in.ua',$body)) {
                        foreach ($_POST['Mod'] as $index=>$row) {
                          $modelOrdersProducts = new OrdersProducts;
                          unset($row['id']);
                          $data['OrdersProducts'] = $row;
                          $data['OrdersProducts']['order_id'] = $modelOrder->id;
                          $modelOrdersProducts->load($data);
                          $modelOrdersProducts->save();
                        }
                        $modelMod->clearBasket();
                        return Yii::$app->response->redirect(['basket/success']);      
                    }
                }            
            
            $basket_mods = $modelMod->getBasketMods();
           
            return $this->render('index', [
                    'modelMod'=>$modelMod,
                    'basket_mods'=>$basket_mods,
                    'modelOrder'=>$modelOrder,
            ]);        
    }
    
    public function actionInfo()
    {
            $modelMod = new Mod;
            $info = $modelMod->rowBasket();
            return $this->renderAjax('ajax_info', [
                    'info'=>$info,
            ]);
    } 
    
    public function actionAdd(){
    $modelMod = new Mod;
            if(isset($_POST['mod_id'],$_POST['count']) && $_POST['mod_id']>0 && $_POST['count']>0){
        $modelMod->addBasket($_POST['mod_id'],$_POST['count']);
    }
            
            Yii::$app->end();
    }
    
    
    public function actionSuccess(){
        return $this->render('success');
    }
        
    public function actionUpdate(){
        $modelMod = new Mod;
        
        foreach ($_POST['Mod'] as $index=>$row) {
            $modelMod->updateBasket($row);
        }
        
        return $this->actionInfo();
    }
    
}