public function modify($cart)
 {
     if (!empty($_POST)) {
         $rules = ['id' => FILTER_VALIDATE_INT, 'quantity' => FILTER_VALIDATE_INT];
         $sanitize = filter_input_array(INPUT_POST, $rules);
         $product = new Product();
         $products = $product->find($sanitize['id']);
         $p = new \Cart\Product();
         $p->setName($products->title);
         $p->setPrice($products->price);
         $cart->restore($p, $sanitize['quantity']);
         header('Location: /cart');
     }
 }
 public function store()
 {
     if (empty($_SESSION)) {
         session_start();
     }
     empty($_SESSION['old']) ?: ($_SESSION['old'] = []);
     empty($_SESSION['error']) ?: ($_SESSION['error'] = []);
     $rules = ['email' => FILTER_VALIDATE_EMAIL, 'number' => ['filter' => FILTER_CALLBACK, 'options' => function ($nb) {
         if (preg_match('/[0-9]{16}/', $nb)) {
             return (int) $nb;
         }
         return false;
     }], 'address' => FILTER_SANITIZE_STRING];
     $sanitize = filter_input_array(INPUT_POST, $rules);
     var_dump($sanitize);
     $error = false;
     if (!$sanitize['email']) {
         $error = true;
         $_SESSION['error']['email'] = "Email Invalid";
     }
     if (!$sanitize['number']) {
         $error = true;
         $_SESSION['error']['number'] = "Blue Card number Invalid";
     }
     if (!$sanitize['address']) {
         $error = true;
         $_SESSION['error']['address'] = "You must give your address";
     }
     if ($error) {
         $_SESSION['old']['email'] = $sanitize['email'];
         $_SESSION['old']['address'] = $sanitize['address'];
         $this->redirect(url('cart'));
     }
     try {
         \Connect::$pdo->beginTransaction();
         $history = new History();
         $customer = new Customer();
         $customer->create(['email' => $sanitize['email'], 'number' => $sanitize['number'], 'addess' => $sanitize['address']]);
         $customerId = \Connect::$pdo->LastInsertID;
         $storage = $this->cart->all();
         $products = [];
         foreach ($storage as $id => $total) {
             $p = new Product();
             $stmt = $p->find($id);
             $history->create(['product_id' => $id, 'price' => (double) $stmt->price, 'total' => $total, 'quantity' => $total / $stmt->price, 'commandet_at' => date('Y-m-d h:i:s')]);
             $this->cart->reset();
             $this->redirect(url());
         }
         \Connect::$pdo->commit();
     } catch (\PDOException $e) {
         \Connect::$pdo->rollback();
     }
 }
 /**
  * @return array
  * @description the name of product is a primary key of product command
  */
 private function storage()
 {
     $storage = $this->cart->all();
     $products = [];
     foreach ($storage as $name => $total) {
         $pr = new Product();
         $p = $pr->find($name);
         // $name is id
         $title = $p->title;
         $products[$title]['price'] = (int) $p->price;
         $products[$title]['total'] = (double) $total;
         $products[$title]['quantity'] = (int) ($total / $p->price);
         $products[$title]['product_id'] = (int) $p->id;
     }
     return $products;
 }
Example #4
0
 /**
  * Return all products
  *
  * @return array
  */
 public function actionIndex()
 {
     return ProductModel::find();
 }
 public function store()
 {
     if (!checked_token($_POST['_token'])) {
         $this->redirect(url('cart'));
     }
     //if(empty($_SESSION)) session_start();
     if (!empty($_SESSION['old'])) {
         $_SESSION['old'] = [];
     }
     if (!empty($_SESSION['error'])) {
         $_SESSION['error'] = [];
     }
     $rules = ['email' => FILTER_VALIDATE_EMAIL, 'number' => ['filter' => FILTER_CALLBACK, 'options' => function ($nb) {
         if (preg_match('/[0-9]{16}/', $nb)) {
             return $nb;
         }
         return false;
     }], 'adresse' => FILTER_SANITIZE_STRING];
     $sanitize = filter_input_array(INPUT_POST, $rules);
     //var_dump($sanitize);
     $error = false;
     if (!$sanitize['email']) {
         $error = true;
         $_SESSION['error']['email'] = 'your email is invalid';
     }
     if (!$sanitize['number']) {
         $error = true;
         $_SESSION['error']['number'] = 'your blue card number is invalid';
     }
     if (!empty($sanitize['adresse'])) {
         $error = true;
         $_SESSION['error']['adresse'] = 'you must give your address';
     }
     if ($error) {
         $_SESSION['old']['email'] = $sanitize['email'];
         $_SESSION['old']['adresse'] = $sanitize['adresse'];
         $this->redirect(url('cart'));
     }
     //transactionnelle PDO
     try {
         \Connect::$pdo->beginTransaction();
         $history = new History();
         $customer = new Customer();
         $customer->create(['email' => $sanitize['email'], 'number' => $sanitize['number'], 'adresse' => $sanitize['adresse']]);
         $customer_id = \Connect::$pdo->lastInsertId();
         $storage = $this->cart->all();
         foreach ($storage as $id => $total) {
             $p = new Product();
             // product du Model pas du Cart
             $stmt = $p->find($id);
             $history->create(['product_id' => $id, 'customer_id' => $customer_id, 'price' => (double) $stmt->price, 'total' => $total, 'quantity' => $total / $stmt->price, 'commanded_at' => date('Y-m-d h:i:s')]);
         }
         \Connect::$pdo->commit();
         $this->cart->reset();
         $this->redirect(url());
     } catch (\PDOException $e) {
         \Connect::$pdo->rollBack();
     }
 }