public function login() { if (isset($_POST['register'])) { $this->redirect('users', 'register'); exit; } if (isset($_POST['login'])) { $username = $_POST['username']; $passwordHash = md5($_POST['password']); $info = UserRepository::create()->loginCheck($username, $passwordHash); if ($info) { $_SESSION['userId'] = $info['id']; $_SESSION['username'] = $info['username']; $_SESSION['email'] = $info['email']; $_SESSION['roleId'] = $info['roleId']; $_SESSION['cash'] = $info['cash']; $_SESSION['userCart'] = CartRepository::create()->getUserCard($info['id']); if ($info['roleId'] == 1) { $this->redirect('home', 'userHome'); } if ($info['roleId'] == 2) { $this->redirect('home', 'editorHome'); } $this->redirect('home', 'editorHome'); } echo 'Invalid details'; } }
public function manage() { $_SESSION['userCart'] = CartRepository::create()->getUserCard($_SESSION['userId']); $this->checkToken(); if (isset($_POST['checkout'])) { CartRepository::create()->checkout(); $this->redirect('cart', 'manage'); } if (isset($_POST['empty'])) { CartRepository::create()->emptyCart(); $this->redirect('cart', 'manage'); } if (isset($_POST['remove'])) { CartRepository::create()->remove(); $this->redirect('cart', 'manage'); } }
public function buy() { $this->checkToken(); $product = $this->productRepository->getProduct($this->parameters[0]); $_SESSION['product'] = $product; $cart = CartRepository::create()->getUserCard($_SESSION['userId']); if (isset($_POST['buy'])) { $quantity = floatval($_POST['quantity']); $price = floatval($product['price']); if ($quantity < 1) { echo 'Invalid quantity!'; die; } if ($quantity > $product['quantity']) { echo 'Do not have enough available quantities!'; die; } if ($quantity * floatval($product['price']) + floatval($cart['value']) > $_SESSION['cash']) { echo 'You will not have enough money to checkout the cart remove some products!'; die; } $order = new Order(intval($product['id']), intval($cart[0]), $quantity, $price); $this->productRepository->buy($order); $this->redirect('products', 'category'); } }