Exemple #1
0
 public function sell()
 {
     if (!isset($_SESSION['userId'])) {
         header('Location: /php_project/application/public/');
         exit;
     }
     $order_id = $this->input->get(0);
     $product_id = $this->input->get(1);
     $productDb = new \Models\Product();
     $orderDb = new \Models\Order();
     $order = $orderDb->get('order_id = ' . $order_id)[0];
     $product = $productDb->get('product_id=' . $product_id)[0];
     if (!is_numeric($product_id) || !is_numeric($order_id) || !$product || !$order) {
         header('Location: /php_project/application/public/');
         exit;
     }
     $orderDb->update('order', array('order_id' => $order_id, 'status' => 'deleted'));
     if ($this->user == null) {
         $this->getUser();
     }
     $price = $product['price'];
     if ($product['promotion_id'] != null) {
         $promoDb = new \Models\Promotion();
         $discount = $promoDb->get('promotion_id = ' . $product['promotion_id'])[0]['discount'];
         if ($discount > 0) {
             $price = $price - $price * $discount / 100;
         }
     }
     $this->userDb->update('user', array('user_id' => $_SESSION['userId'], 'cash' => $this->user['cash'] + $price));
     $sellProduct['product_id'] = $product_id;
     $sellProduct['quantity'] = $product['quantity'] + 1;
     $productDb->update('product', $sellProduct);
     header('Location: /php_project/application/public/user/profile');
 }
Exemple #2
0
 public function edit()
 {
     if (!isset($_SESSION['userId']) && $_SESSION['editor'] != true && $_SESSION['admin'] != true) {
         header('Location: /php_project/application/public/');
         exit;
     }
     $promotion_id = $this->input->get(0);
     $promotionDb = new \Models\Promotion();
     $promotion = $promotionDb->get('promotion_id = ' . $promotion_id)[0];
     if (isset($_POST['promotion_name']) || isset($_POST['discount'])) {
         $cleaner = new \Framework\Common();
         $name = $cleaner->normalize($_POST['promotion_name'], 'trim|xss|string');
         $discount = $cleaner->normalize($_POST['discount'], 'trim|xss|float');
         if ($name == $promotion['promotoin_name'] && $discount == $promotion['discount']) {
             header('Location: /php_project/application/public/editor/index');
             exit;
         }
         $updatePromotion = array();
         $updatePromotion['promotion_name'] = $name;
         $updatePromotion['discount'] = $discount;
         $updatePromotion['promotion_id'] = $promotion_id;
         $updatePromotion['user_id'] = $_SESSION['userId'];
         $promotionDb->update('promotion', $updatePromotion);
         header('Location: /php_project/application/public/editor/index');
         exit;
     }
     if (!is_numeric($promotion_id) || !$promotion) {
         header('Location: /php_project/application/public/');
         exit;
     }
     $this->view->appendToLayout('body', 'editPromotion');
     $this->view->display('layouts.default', $promotion);
 }
Exemple #3
0
 public function promo()
 {
     if (!isset($_SESSION['userId']) && $_SESSION['editor'] != true && $_SESSION['admin'] != true) {
         header('Location: /php_project/application/public/');
         exit;
     }
     $promoDb = new \Models\Promotion();
     $product_id = $this->input->get(0);
     $productDb = new \Models\Product();
     $product = $productDb->get('product_id=' . $product_id)[0];
     if (isset($_POST['name'])) {
         $updateProduct = array();
         $promoName = $_POST['name'];
         $promotion = $promoDb->get('promotion_name = "' . $promoName . '"')[0];
         if ($product['promotion_id'] == null) {
             $updateProduct['promotion_id'] = $promotion['promotion_id'];
             $updateProduct['product_id'] = $product['product_id'];
             $productDb->update('product', $updateProduct);
             header('Location: /php_project/application/public/editor/index');
             exit;
         } else {
             $oldPromo = $promoDb->get('promotion_id = ' . $product['promotion_id'])[0];
             if ($oldPromo['discount'] >= $promotion['discount']) {
                 header('Location: /php_project/application/public/editor/index');
                 exit;
             } else {
                 $updateProduct['promotion_id'] = $promotion['promotion_id'];
                 $updateProduct['product_id'] = $product['product_id'];
                 $productDb->update('product', $updateProduct);
                 header('Location: /php_project/application/public/editor/index');
                 exit;
             }
         }
     }
     if (!is_numeric($product_id) || !$product) {
         header('Location: /php_project/application/public/');
         exit;
     }
     $promos = $promoDb->find();
     $this->view->appendToLayout('body', 'addPromoProduct');
     $this->view->display('layouts.default', $promos);
 }
Exemple #4
0
 public function remove()
 {
     if (!isset($_SESSION['userId']) && $_SESSION['admin'] != true) {
         header('Location: /php_project/application/public/');
         exit;
     }
     $promotionDb = new \Models\Promotion();
     $promotion_id = $this->input->get(0);
     $promotion = $promotionDb->get('promotion_id=' . $promotion_id)[0];
     if (!is_numeric($promotion_id) || !$promotion) {
         header('Location: /php_project/application/public/');
         exit;
     }
     $updatePromotion = array();
     $updatePromotion['promotion_id'] = $promotion_id;
     $updatePromotion['deleted'] = true;
     $promotionDb->update('promotion', $updatePromotion);
     header('Location: /php_project/application/public/editor/index');
     exit;
 }