public function getCategory($id) { $result['categories'] = $this->category->getCategories(); $result['title'] = 'Shop'; $result['currentCategory'] = $id; $result['isEditor'] = Auth::isUserInRole(array('editor', 'admin')); $result['isAdmin'] = Auth::isUserInRole(array('admin')); if ($result['isEditor']) { $result['products'] = $this->product->getProductsForCategoryWitnUnavailable($id); } else { $result['products'] = $this->product->getProductsForCategory($id); } $all_promotion = $this->promotion->getHighestActivePromotion(); foreach ($result['products'] as $k => $p) { $productPromotion = max($all_promotion['discount'], $p['discount'], $p['category_discount']); if (is_numeric($productPromotion)) { $result['products'][$k]['promotion_price'] = $p['price'] - $p['price'] * ($productPromotion / 100); } } View::make('index', $result); if (Auth::isAuth()) { View::appendTemplateToLayout('topBar', 'top_bar/user'); } else { View::appendTemplateToLayout('topBar', 'top_bar/guest'); } View::appendTemplateToLayout('header', 'includes/header')->appendTemplateToLayout('footer', 'includes/footer')->appendTemplateToLayout('catMenu', 'side_bar/category_menu')->render(); }
public function getAdd() { $result['title'] = 'Shop'; $result['action'] = '/promotion/add'; $result['submit'] = 'add'; $categories = $this->category->getCategories(); $result['categories'][] = array('text' => 'No category', 'options' => array('value' => 0)); foreach ($categories as $c) { $currentCategory = array(); $currentCategory['text'] = $c['name']; $currentCategory['options'] = array('value' => $c['id']); $result['categories'][] = $currentCategory; } $products = $this->product->getProducts(); $result['products'][] = array('text' => 'No product', 'options' => array('value' => 0)); foreach ($products as $c) { $currentProduct = array(); $currentProduct['text'] = $c['name']; $currentProduct['options'] = array('value' => $c['id']); $result['products'][] = $currentProduct; } View::make('promotion.add', $result); if (Auth::isAuth()) { View::appendTemplateToLayout('topBar', 'top_bar/user'); } else { View::appendTemplateToLayout('topBar', 'top_bar/guest'); } View::appendTemplateToLayout('header', 'includes/header')->appendTemplateToLayout('footer', 'includes/footer')->render(); }
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'); } }
/** * The function update product attribute */ public function updateProduct($attributes, models\Category $category) { $product = new models\Product(); $product->setCode($attributes['Code']); $product->setArticle($attributes['Article']); $product->setName($attributes['Name']); $product->setBasicMeasure($attributes['Basic_measure']); $product->setMeasure($attributes['Measure']); $product->setCost($attributes['Cost']); $product->setCurrency($attributes['Currency']); $product->setIdCodeCategory($category); $this->entityManager->merge($product); $this->entityManager->flush(); }
public function sellProduct($id, $quantity, $upid) { $this->user->startTran(); if ($this->user->changeProductQuantity(Auth::getUserId(), $id, $quantity, $upid) !== 1) { Session::setError('not enough products'); $this->user->rollback(); Redirect::back(); } $userProduct = $this->user->getProduct(Auth::getUserId(), $id, $upid); if ($userProduct['quantity'] < 1) { if ($this->user->deleteProduct(Auth::getUserId(), $id, $upid) !== 1) { Session::setError('something went wrong'); $this->user->rollback(); Redirect::back(); } } $soldProducts = $this->product->getProduct($id); if ($this->product->addQuantity($soldProducts['id'], $quantity) !== 1) { Session::setError('something went wrong'); $this->user->rollback(); Redirect::back(); } if ($this->user->addCash(Auth::getUserId(), $soldProducts['price'] * $quantity) !== 1) { Session::setError('something went wrong'); $this->user->rollback(); Redirect::back(); } $this->user->commit(); Session::setMessage('You sold ' . $quantity . ' of ' . $userProduct['name']); Redirect::to('/user/' . Auth::getUserId() . '/products'); }
public function delete($id) { if ($this->product->delete($id) !== 1) { Session::setError('can not delete this product'); Redirect::back(); } Session::setMessage('done'); Redirect::to(''); }
public function edit(Product $product) { $query = "Update products Set name = ?, categoryId = ?, price = ?, quantity = ?, editorId = ?\n WHERE products.id = ?"; $params = [$product->getName(), $product->getCategoryId(), $product->getPrice(), $product->getQuantity(), $product->getEditorId(), $product->getId()]; $this->db->query($query, $params); $result = $this->db->row(); return $result; }
public function hackable_create() { $args = $this->request->args; $token = $_COOKIE['user_token']; $user = User::retrieve_by_token(Validate::token($token)); $username = $user->username; $total = Validate::udouble($args['total']); $products = $args['products']; foreach ($products as $id => $quantity) { Product::decrease_quantity(Validate::uint($id), Validate::uint($quantity)); } $order = Order::create($username, $total); $this->response->set_header(Lib\Response::HTTP_CREATED); $this->response->set('order', $order); }
public function create() { $user = $this->session->get_user(); if (!$user->is_admin()) { throw new Lib\Exceptions\UnauthorizedException(); } $args = $this->request->args; $name = Validate::plaintext($args['name']); $price = Validate::udouble($args['price']); $quantity = Validate::uint($args['quantity']); $image_url = Validate::image_url($args['image_url']); $product = Product::create($name, $price, $quantity, $image_url); $this->response->set_header(Lib\Response::HTTP_CREATED); $this->response->set('product', $product); }
private function getProductsFromCart($cart) { $all_promotion = $this->promotion->getHighestActivePromotion(); $productsFromCart = array(); foreach ($cart as $id => $q) { if ($currentProduct = $this->product->getProduct($id)) { $productPromotion = max($all_promotion['discount'], $currentProduct['discount'], $currentProduct['category_discount']); if (is_numeric($productPromotion)) { $currentProduct['price'] = $currentProduct['price'] - $currentProduct['price'] * ($productPromotion / 100); } $currentProduct['cart_quantity'] = $q['quantity']; $productsFromCart[] = $currentProduct; } } return $productsFromCart; }
public function removeFromCart() { $cart = \Lib\Session::get('cart'); $db = Register::get('db'); $product_id = $this->http->get('product_id'); $product_quantity = $this->http->get('product_quantity'); if ($product_quantity < 1) { Location::To(URL . 'cart/show'); } $product = new Product($product_id); if (!empty($cart)) { foreach ($cart as $p) { if ($p['product_id'] == $product_id) { $orders = new Order($p['order_id']); $quantity = $orders->getQuantity(); if ($quantity >= $product_quantity) { $orders->setQuantity($quantity - $product_quantity); $p_q = $product->getQuantity(); $product->setQuantity($p_q + $product_quantity); $product->changeProductSold(-$product_quantity); $db->query('START TRANSACTION'); if ($product->writeData(true) && $orders->writeData(true)) { $db->query('COMMIT'); } else { $db->query('ROLLBACK'); } if ($orders->getQuantity() == 0) { $orders->delete(); } } } } } $this->ActNumberOfProducts(); Location::To(URL . 'cart/show'); }
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(); } }
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(); } }
public function addToSlider() { $http = new Http(); $return = array(); $id = $http->post('id_new_product'); if (!empty($id)) { $slider_m = new Slider(); $slider_m->setProductId($id); $slider_m->writeData(); $product = new Product($id); $return['id'] = $id; $return['name'] = $product->getName(); $return['img'] = $product->getImage(); $return['price'] = $product->getPrice(); } echo json_encode($return); }
/** * @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; }
public function actionView($id) { return ProductModel::findOne(['id' => $id]); }
public function deleteProduct($id) { unlink(Product::showImageProduct($id)); $sql = 'DELETE FROM products WHERE product_id = :id'; $this->products = parent::connect()->prepare($sql); $data = array('id' => $id); try { $this->products->execute($data); } catch (PDOException $e) { die($e->getMessage()); } }
public function delete($id) { Product::deleteProduct($id); parent::redirectTo('indexProduct'); }
public function update($id) { Order::updateOrder($id); Product::updateStockProduct($id); parent::redirectTo('indexOrder'); }
public function updateProduct(Product $product) { $result = $this->db->prepare("\n\t\t\tUPDATE product\n\t\t\tSET name = ?,\n\t\t\t\tquantity = ?,\n\t\t\t\tdescription = ?,\n\t\t\t\tcategory_id = ?\n\t\t\tWHERE id = ?\n\t\t"); $result->execute([$product->getName(), $product->getQuantity(), $product->getDescription(), $product->getCategoryId(), $product->getId()]); }