public function index()
 {
     require_once '../app/models/Cart.php';
     $model = new Cart();
     if (Sessions_helper::logged_in()) {
         $cart = $model->generateCartFromDb();
     } else {
         if (isset($_SESSION['cart'])) {
             $cart = $model->generateCartFromSession($_SESSION['cart']);
         } else {
             $cart = [];
         }
     }
     // Commented since I've added this in the Product model - don't think it's needed here anymore.
     // Will remove later
     #$amountInCart = count($cart);
     #for ($i=0; $i < $amountInCart; $i++) {
     #	$catagory = ucfirst($cart[$i]['product_catagory']);
     #	require_once('../app/models/' . $catagory . '.php');
     #	$model = new $catagory;
     #	$cart[$i] = $model->splitListsToArray($cart[$i]);
     #}
     $view = new View('cart/index');
     $view->pass_data('cart', $cart);
     $view->set_title('Basket');
     $view->load_page();
 }
 public function index($name = '')
 {
     require_once '../app/models/Product.php';
     $featuredProductsIds = Product::findRandomProductIds('18');
     $view = new View('home/index');
     $view->pass_data('featuredProductsIds', $featuredProductsIds);
     $view->load_page();
 }
 public function orders()
 {
     require_once '../app/models/Purchase.php';
     $model = new Purchase();
     $purchaseList = $model->generatePurchaseArray();
     $view = new View('purchases/index');
     $view->set_title('Your Orders');
     $view->pass_data('purchaseList', $purchaseList);
     $view->load_page();
 }
 public function show($wishListId)
 {
     if (!Wish_list::listBelongsToUser($wishListId)) {
         $this->redirect_to('wish_lists');
         break;
     }
     #TODO add a method of properly sorting wish lists by the date each product was added.
     $wishList = Wish_list::findById($wishListId);
     $listOfWishLists = Wish_list::findByUserId($_SESSION['user_id']);
     if (isset($wishList['listInfo'])) {
         $listInfo = $wishList['listInfo'];
         unset($wishList['listInfo']);
     } else {
         $listInfo = ['wish_list_id' => $wishList[0]['wish_list_id'], 'fk_wish_list_user' => $wishList[0]['fk_wish_list_user'], 'wish_list_name' => $wishList[0]['wish_list_name']];
     }
     $view = new View('wish_lists/show');
     $view->set_title('Wish List - ' . $listInfo['wish_list_name']);
     $view->pass_data('listInfo', $listInfo);
     $view->pass_data('wishList', $wishList);
     $view->pass_data('listOfWishLists', $listOfWishLists);
     $view->load_page();
 }
 public function newUser()
 {
     $user = new User();
     if (isset($_POST['first_name'])) {
         $user->assignProperties($_POST);
         if ($user->saveToDb('INSERT INTO', 'users', $user->properties)) {
             Sessions_helper::login();
             if (array_key_exists('redirect', $_POST)) {
                 $this->redirect_to($_POST['redirect']);
             } else {
                 $this->redirect_to('home/index');
             }
         }
     }
     $view = new View('layouts/register_login', ['header' => false, 'footer' => false]);
     $view->set_title('Create account');
     $view->pass_data('user', $user);
     $view->load_page();
 }
 public function login($error = false)
 {
     if (isset($_POST['email']) && isset($_POST['password']) && $_POST['email'] != '' && $_POST['password'] != '') {
         if (Sessions_helper::login()) {
             if (array_key_exists('redirect', $_POST)) {
                 $this->redirect_to($_POST['redirect']);
             } else {
                 $this->redirect_to('home/index');
             }
         } else {
             $_POST['email'] = '';
             $_POST['password'] = '';
             $this->login(true);
         }
     }
     $view = new View('layouts/register_login', ['header' => false, 'footer' => false]);
     $view->set_title('Login');
     $view->pass_data('loginError', $error);
     $view->load_page();
 }
 public function item($id = '')
 {
     require_once '../app/models/Product.php';
     $product = Product::findByProductVersionId($id);
     if ($product) {
         $catagory = ucfirst($product['product_catagory']);
         require_once '../app/models/' . $catagory . '.php';
         $model = new $catagory();
         $product = $model->splitListsToArray($product);
         $productVersions = Product::findProductVersions($product['base_product_id']);
         require_once '../app/models/Wish_list.php';
         $view = new View('products/item');
         $view->set_title($product['product_name']);
         session_start();
         if (isset($_SESSION['user_id'])) {
             $wishLists = Wish_list::findByUserId($_SESSION['user_id']);
             $view->pass_data('wishLists', $wishLists);
         }
         $view->pass_data('product', $product);
         $view->pass_data('productVersions', $productVersions);
     } else {
         $view = new View('products/not_found');
         $view->set_title('Product not found');
     }
     $view->load_page();
 }
 public function submit()
 {
     require_once '../app/models/Purchase.php';
     $purchase = new Purchase();
     $purchase->assignProperties($_SESSION['checkout']['properties']);
     $purchaseId = $purchase->savePreparedStatementToDb('purchase', $purchase->properties);
     $products = [];
     foreach ($_SESSION['checkout']['cart'] as $key => $value) {
         if (is_array($value)) {
             array_push($products, ['fkPurchaseProductVersionProductVersion' => $key, 'quantityInPurchase' => $value['cart_quantity'], 'priceAtPurchase' => $value['product_price']]);
         }
     }
     $purchase->addToJoinTable($products, 'purchase_product_version');
     require_once '../app/models/Cart.php';
     $cart = new Cart($_SESSION['checkout']['properties']['fk_purchase_user']);
     $cart->emptyCart();
     $view = new View('checkout/submit');
     $view->set_title('Order Complete');
     $view->pass_data('deliveryDue', $_SESSION['checkout']['properties']['deliveryDue']);
     $view->load_page();
     unset($_SESSION['checkout']);
 }