Exemple #1
0
function add_to_cart($app, $db, $params)
{
    if (isset($params['id'], $params['attributes'])) {
        $pid = filter_var($params['id'], FILTER_VALIDATE_INT, array('min_range' => 1)) ? $params['id'] : NULL;
        if (empty($params['attributes']) || $params['attributes'] === '0') {
            // product don't have attributes
            $attributes = NULL;
        } elseif ($params['attributes'] === '1') {
            // product needs attributes
            $app->flash('info', 'You need to choose the attributes of your product before adding it to your Cart.');
            $category = \Data\ProductsRepository::get_category($db, $pid);
            $app->redirect($app->view()->url('/products/' . $category . '/' . $pid));
        } else {
            // actual attributes string
            $attributes = $params['attributes'];
        }
    }
    if (isset($pid, $params['action']) && $params['action'] == 'add') {
        $stmt = \Data\CartRepository::add_to_cart($db, $_SESSION['user_id'], $pid, 1, $attributes);
        $app->flash('info', 'Your cart have been updated. A new product have been added.');
        $app->redirect($app->view()->url('/shop/cart'));
    } elseif (isset($pid, $params['action']) && $params['action'] == 'remove') {
        $stmt = \Data\CartRepository::remove_from_cart($db, $_SESSION['user_id'], $pid, $attributes);
        $app->flash('info', 'Your cart have been updated. The selected product have been removed.');
        $app->redirect($app->view()->url('/shop/cart'));
    } elseif (isset($pid, $params['action'], $params['qty']) && $params['action'] == 'move') {
        $qty = filter_var($params['qty'], FILTER_VALIDATE_INT, array('min_range' => 1)) ? $params['qty'] : 1;
        $stmt = \Data\CartRepository::remove_from_cart($db, $_SESSION['user_id'], $pid, $attributes);
        $stmt = \Data\WishListRepository::add_to_wish_list($db, $_SESSION['user_id'], $pid, $qty, $attributes);
        $app->flash('info', 'Your cart have been updated. The product selected have been moved to your Wish List.');
        $app->redirect($app->view()->url('/shop/cart'));
    } elseif (isset($params['action']) && $params['action'] == 'clear') {
        $stmt = \Data\CartRepository::clear_cart($db, $_SESSION['user_id']);
        $app->flash('info', 'Your cart have been updated. Your cart is now empty.');
        $app->redirect($app->view()->url('/shop/cart'));
    } else {
        // show cart
        $app->view()->set_template('layouts/basic.php');
        $app->render('shop/cart.php', array('page_title' => 'Your Cart'));
        $app->stop();
    }
}
Exemple #2
0
    $flash = $app->view()->getData('flash');
    $errors = isset($flash['errors']) ? $flash['errors'] : array();
    $app->view()->set_template('layouts/basic.php');
    $app->render('session/login.php', array('page_title' => $app->view()->tr('pages.login'), 'errors' => $errors));
});
$app->post('/session/login', $require_ssl, function () use($app, $db) {
    include BASE_URI . DS . 'routes' . DS . 'validators' . DS . 'login.php';
    $data = $app->request()->post();
    $errors = validate($data);
    if ($errors) {
        $app->flash('error', $app->view()->tr('session.login.errors'));
        $app->redirect($app->view()->url_secure('/session/login'));
    }
    $user = \Data\UserRepository::get_user_by_email_and_password($db, $data['email'], $data['password']);
    if ($user) {
        \Data\CartRepository::clear_cart($db, $_SESSION['user_id']);
        // remove past items
        \Data\WishListRepository::clear_wish_list($db, $_SESSION['user_id']);
        // remove past items
        if ($user['type'] == 'admin') {
            session_regenerate_id(true);
            $_SESSION['admin'] = true;
        }
        $_SESSION['user_id'] = $user['id'];
        $_SESSION['username'] = $user['username'];
        $_SESSION['logged_in'] = true;
        $app->flash('info', 'Welcome to our store ' . $user['username'] . '. Enjoy!');
        $app->redirect($app->view()->url('/'));
    } else {
        $app->flash('error', $app->view()->tr('session.login.match.error'));
        $app->redirect($app->view()->url_secure('/session/login'));