BasketController.php
3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?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();
}
}