Exemplo n.º 1
0
 public function show($id)
 {
     $productModel = new ProductsModel();
     try {
         $product = $productModel->getProductById($id);
     } catch (\Exception $e) {
         header("Location: " . __MAIN_URL__ . __HOME_DIRECTORY__);
         exit;
     }
     $model["product"] = $product;
     if (isset($_POST['addToCardButton'])) {
         $quantity = (int) $_POST['quantity'];
         $productId = (int) $_POST['productId'];
         $productName = $_POST['productName'];
         $errors = [];
         if ($quantity <= 0) {
             $errors[] = "Invalid quantity";
         }
         if (count($errors) == 0) {
             try {
                 $productModel->addToCard($productId, $quantity, $productName);
             } catch (\Exception $e) {
                 View::$viewBag['errors'][] = $e->getMessage();
             }
         } else {
             View::$viewBag['errors'] = $errors;
         }
     }
     if (isset($_POST['reviewButton'])) {
         if (!isset($_SESSION['is_logged'])) {
             header("Location: " . __MAIN_URL__ . __HOME_DIRECTORY__);
             exit;
         }
         $review = $_POST['review'];
         $productId = (int) $_POST['productId'];
         $productModel->addReview($productId, $review, $_SESSION['id']);
     }
     $reviews = $productModel->getProductReviews($id);
     $model['reviews'] = $reviews;
     $categoriesModel = new CategoriesModel();
     $categories = $categoriesModel->getAllCategories();
     $model["categories"] = $categories;
     return new View($model);
 }