-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.php
97 lines (72 loc) · 2.34 KB
/
cart.php
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
93
94
95
96
97
<?
require_once('includes/utils.php');
__autoload('CartDAO');
__autoload('PromoDAO');
class cartController extends BaseController {
// 0: guest
// 1: admin
// 2: mod
public function getPermissionDefine() {
return array(
'index' => array(0, 1, 2)
);
}
public function isAllowCountStats() {
return TRUE;
}
public function index() {
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
$this->handlePost();
}
$productDAO = new ProductDAO( DataSource::getInstance() );
$categoryDAO = new CatDAO( DataSource::getInstance() );
$brandDAO = new BrandDAO( DataSource::getInstance() );
$categories_list = $categoryDAO->findByAll_Navigation();
$brands_list = $brandDAO->findByAll();
$cart = getCart();
$cart_details = array();
foreach ($cart->listItem as $key => $value) {
$cart_details[$key] = $productDAO->findById($key);
}
$promoDAO = new PromoDAO( DataSource::getInstance() );
$this->registry->template->promo_seo_url_newest = $promoDAO->findNewestSeoUrl();
$this->registry->template->categories_list = $categories_list;
$this->registry->template->brands_list = $brands_list;
$this->registry->template->cart = $cart;
$this->registry->template->cart_details = $cart_details;
$this->registry->template->is_cart_active = TRUE;
$this->registry->template->tile_title = 'Giỏ Hàng';
$this->registry->template->body_class = 'cart-template';
$this->registry->template->tile_content = 'cart.php';
$this->registry->template->tile_footer = 'footer.php';
$this->registry->template->show('layout/user.php');
}
private function handlePost() {
if ( isset($_POST['update_cart']) ) {
// update the cart
$cart = getCart();
foreach ($cart->listItem as $key => $value) {
$quantity = (int)$_POST['updates_' . $key];
$cart->removeItem($key);
if ($quantity > 0) {
$cart->addItem($key, $quantity);
}
}
// checkout
if ($_POST['update_cart'] == '2') {
if ( $cart->checkout() ) {
$cartDAO = new CartDAO( DataSource::getInstance() );
$cart_id = $cartDAO->insert( $cart->listItem );
$this->registry->template->checkout = $cart_id;
} else { // FAILED
$this->registry->template->checkout = -1;
}
}
} else { // add to cart
$id = (int)$_POST['id'];
if ($id <= 0) return;
getCart()->addItem($id, 1);
}
}
}
?>