public function product()
 {
     $this->setTemplate('store_product.tpl');
     $product_id = 0;
     if (isset($_GET['id']) && is_numeric($_GET['id'])) {
         $product_id = $_GET['id'];
     }
     if (isset($_GET['product_name'])) {
         $name_arr = explode('-', $_GET['product_name']);
         $product_id = $name_arr[sizeof($name_arr) - 1];
     }
     $_GET['product_id'] = $product_id;
     if ($this->isXMLHttpRequest()) {
         header('Content-type: application/json');
         $c = Model::load('ProductColour');
         $c->id = $_GET['colour_id'];
         $c->load();
         $item = array();
         $item['image'] = $c->image;
         $item['option_id'] = $c->property_option_id;
         echo json_encode($item);
         exit;
     } else {
         // default event from store.php
         $p = Model::load('ProductItem');
         $c = Model::load('CategoryItem');
         $v = Model::load('ProductVariant');
         $o = Model::load('ProductVariantPropertyOption');
         $c->id = 0;
         $p->id = $_GET['product_id'];
         $v->id = 0;
         $o->id = 0;
         if (isset($_POST['add'])) {
             $options = array();
             if (isset($_POST['property'])) {
                 foreach ($_POST['property'] as $option) {
                     array_push($options, $option);
                 }
             }
             if (sizeof($options) > 0) {
                 $variant_id = $v->findVariant($options, $p->id);
             } else {
                 $sql = ' WHERE product_id = ' . $p->id . ' LIMIT 0, 1';
                 $variant = $v->getAllCustom(Model::getTable('ProductVariant'), $sql);
                 if (sizeof($variant) > 0) {
                     $variant_id = $variant[0]['id'];
                 }
             }
             if (is_numeric($variant_id) && $variant_id > 0) {
                 $sc = new ShoppingCart();
                 $sc->add($variant_id, 1);
                 $this->redirect('store/cart');
             }
         }
         $l = new ProductsLayout($c, $p, $v, $o, $this);
         $this->assign('breadcrumb', $l->getBreadCrumb());
         $this->assign('buttons', $l->getButtons());
         $this->assign('p_nav', $l->getPNav());
         if ($_GET['product_id'] != 0) {
             $c = Model::load('ProductColour');
             $colours = $c->getColoursIndexed($_GET['product_id']);
             if (sizeof($colours) > 0) {
                 $this->assign('colours', $colours);
             }
             if (defined('ELIB_USE_PRODUCT_BRANDS') && ELIB_USE_PRODUCT_BRANDS == true) {
                 $b = Model::load('BrandItem');
                 $b->id = $p->brand_id;
                 $b->load();
                 $this->assign('brand', $b->name);
             }
             $this->assign('product_view', 1);
             //$variant_count = $this->assignVariantsTableData($p, $v);
             //$this->getPropertiesAndOptions($p->id, (sizeof($colours) > 0));
             $this->getPropertiesAndOptions($p, sizeof($colours) > 0);
         }
         $_GET['category_id'] = -1;
         $this->assign('category_id', $_GET['category_id']);
         $this->assign('product_id', $_GET['product_id']);
         $this->assign('option_id', 0);
         if ($_GET['product_id'] != 0) {
             $p = $l->getProduct();
             if (sizeof($colours) > 0) {
                 $p->image = $c->getFirstColourImage($_GET['product_id']);
             }
             $this->assign('product', $p);
             $qty = array();
             $i = 0;
             for ($i; $i < 31; $i++) {
                 $qty[$i] = $i;
             }
             $this->assign('qty', $qty);
         }
     }
     // seo
     $c = Model::load('CategoryItem');
     $c->id = $p->category_id;
     $c->load();
     $this->assign('price', $p->getPrice());
     $custom_title = '';
     $custom_keywords = '';
     $custom_description = '';
     if (isset($b)) {
         $custom_title = $b->name . ' ';
         $custom_keywords = $b->name . ' ';
         $custom_description = $b->name . ' ';
     }
     $custom_title .= $p->name . ' - ' . $c->name . ' at Brighton BMX Co';
     $custom_keywords .= $p->name . ' ' . $c->name;
     $custom_description .= $p->name . ' in ' . $c->name;
     $this->assign('custom_title', $custom_title);
     $this->assign('custom_keywords', strtolower($custom_keywords));
     $this->assign('custom_description', $custom_description . ' - ' . strip_tags($p->description));
 }
 public function cart()
 {
     $this->setTemplate('cart.tpl');
     $c = new ShoppingCart();
     if (isset($_POST['update'])) {
         foreach ($_POST['qty'] as $v => $qty) {
             if (is_numeric($qty) && $qty > 0) {
                 $c->update($v, $qty);
             } elseif (is_numeric($qty) && $qty == 0) {
                 $c->remove($v);
                 // vendor locking
                 if ($c->isEmpty()) {
                     Session::clear('vendor_lock');
                 }
             }
         }
         $this->redirect('store/cart');
     } elseif (isset($_POST['checkout'])) {
         $this->redirect('store/checkout');
     }
     $items = $c->loadFromCart($this);
     if (sizeof($items) > 0) {
         $ids = array();
         foreach ($items as $item) {
             array_push($ids, $item['id']);
         }
         $v = Model::load('ProductVariant');
         $cat_ids = $v->getCategories($ids);
         $cat = Model::load('CategoryItem');
         $calc = new ShippingCalculator($c->calcTotal($items), $cat_ids, $cat, sizeof($items), false);
         $shipping = $calc->getFee();
         $shipping = 0;
         $this->assign('shipping', $shipping);
         $this->assign('total', $c->calcTotal($items) + $shipping);
         $this->assign('items', $items);
     }
     if (isset($this->vendor_lock)) {
         $this->assign('vendor_id', $this->vendor_lock);
     }
     $this->assign('last_cat', Session::get('last_cat'));
 }
 public function default_event()
 {
     $c = new ShoppingCart();
     if ($c->isEmpty()) {
         $this->redirect('paypal/cancel');
     }
     $items = $c->loadFromCart($this);
     $co = new Checkout($items, $this);
     $invoice_no = $co->getInvoiceNo();
     $o = Model::load('OrderItem');
     $o->id = $invoice_no;
     $o->load();
     $products = array();
     //$product[0]['alias'] = 'Some product';
     //$product[0]['price'] = 1.99;
     //$product[0]['code'] = 23;
     $interface = 'http://' . WEB_ROOT . PUBLIC_DIR . '/paypal/';
     $message = '';
     $p = new PaypalClass();
     $p->ipn_log = false;
     $p->add_field('cmd', '_cart');
     $p->add_field('upload', '1');
     // address
     $p->add_field('first_name', $o->first_name);
     $p->add_field('last_name', $o->last_name);
     $p->add_field('address1', $o->address1);
     $p->add_field('address2', $o->address2);
     $p->add_field('city', $o->city);
     $p->add_field('state', $o->state);
     $p->add_field('zip', $o->zip);
     $p->add_field('country', $o->country);
     $p->add_field('address_override', 1);
     $p->add_field('no_shipping', 1);
     // shipping
     $ids = array();
     foreach ($items as $item) {
         array_push($ids, $item['id']);
     }
     $v = Model::load('ProductVariant');
     $cat_ids = $v->getCategories($ids);
     $cat = Model::load('CategoryItem');
     if ($o->country != 'GB') {
         $intl = true;
     } else {
         $intl = false;
     }
     $calc = new ShippingCalculator($c->calcTotal($items), $cat_ids, $cat, sizeof($items), $intl);
     $shipping = $calc->getFee();
     $shipping = $this->getShipping();
     $p->add_field('shipping_1', $shipping);
     $o->shipping = $shipping;
     $o->save(Model::getTable('OrderItem'), array(), 1);
     $i = 1;
     foreach ($items as $index => $item) {
         $p->add_field('item_name_' . $i, $item['name']);
         $p->add_field('amount_' . $i, $item['price']);
         $p->add_field('item_number_' . $i, $this->getItemNumber($item['id']));
         $p->add_field('quantity_' . $i, $item['qty']);
         $o = explode(', ', $item['options']);
         $pr = explode(', ', $item['properties']);
         foreach ($o as $index => $item) {
             $p->add_field('os' . $index . '_' . $i, $o[$index]);
             $p->add_field('on' . $index . '_' . $i, $pr[$index]);
         }
         $i++;
     }
     //    $p->add_field('image_url', 'http://'.WEB_ROOT.PUBLIC_DIR.'/img/pier.png');
     $p->add_field('invoice', $this->getInvoiceNumber($invoice_no));
     $p->add_field('no_shipping', 1);
     $p->add_field('currency_code', 'GBP');
     $p->add_field('business', '*****@*****.**');
     //$p->add_field('business', '*****@*****.**');
     //$p->add_field('business', $this->getBusiness());
     $p->add_field('return', $interface . 'success');
     $p->add_field('notify_url', $interface . 'ipn');
     $p->add_field('cancel_return', $interface . 'cancel');
     $p->paypal_url = $this->getPayPalURL();
     $c->emptyCart();
     $this->presenter->assign('paypal_url', $p->paypal_url);
     $this->presenter->assign('fields', $p->fields);
     //$p->dump_fields();
 }