Example #1
0
 /**
  * Display product
  *
  * @param		$pId
  * @return     	void
  */
 public function displayTask()
 {
     $pId = $this->warehouse->productExists(Request::getVar('product', ''));
     if (!$pId) {
         App::abort(404, Lang::txt('COM_STOREFRONT_PRODUCT_NOT_FOUND'));
     }
     $this->view->pId = $pId;
     $this->view->css();
     $this->view->js('product_display.js');
     // A flag whether the item is available for purchase (for any reason, used by the auditors)
     $productAvailable = true;
     $pageMessages = array();
     // Get the cart
     require_once PATH_CORE . DS . 'components' . DS . 'com_cart' . DS . 'models' . DS . 'CurrentCart.php';
     $cart = new CartModelCurrentCart();
     // POST add to cart request
     $addToCartRequest = Request::getVar('addToCart', false, 'post');
     $options = Request::getVar('og', false, 'post');
     $qty = Request::getInt('qty', 1, 'post');
     if ($addToCartRequest) {
         // Initialize errors array
         $errors = array();
         // Check if passed options/productID map to a SKU
         try {
             $sku = $this->warehouse->mapSku($pId, $options);
             $cart->add($sku, $qty);
         } catch (Exception $e) {
             $errors[] = $e->getMessage();
             $pageMessages[] = array($e->getMessage(), 'error');
         }
         if (!empty($errors)) {
             $this->view->setError($errors);
         } else {
             // prevent resubmitting by refresh
             // If not an ajax call, redirect to cart
             $redirect_url = Route::url('index.php?option=' . 'com_cart');
             App::redirect($redirect_url);
         }
     }
     // Get the product info
     $product = $this->warehouse->getProductInfo($pId);
     $this->view->product = $product;
     // Run the auditor
     require_once PATH_CORE . DS . 'components' . DS . 'com_cart' . DS . 'helpers' . DS . 'Audit.php';
     $auditor = Audit::getAuditor($product, $cart->getCartInfo()->crtId);
     $auditorResponse = $auditor->audit();
     //print_r($auditor); die;
     if (!empty($auditorResponse) && $auditorResponse->status != 'ok') {
         if ($auditorResponse->status == 'error') {
             // Product is not available for purchase
             $productAvailable = false;
             foreach ($auditorResponse->notices as $notice) {
                 $pageMessages[] = array($notice, 'warning');
             }
         }
     }
     // Get option groups with options and SKUs
     $data = $this->warehouse->getProductOptions($pId);
     if ($data) {
         //throw new Exception(Lang::txt('COM_STOREFRONT_PRODUCT_ERROR'), 404);
         $this->view->options = $data->options;
     }
     //print_r($data); die;
     // Find a price range for the product
     $priceRange = array('high' => 0, 'low' => false);
     /*
     	Find if there is a need to display a product quantity dropdown on the initial view load. It will be only displayed for single SKU that allows multiple items.
     	For multiple SKUs it will be generated by JS (no drop-down for non-JS users, sorry)
     */
     $qtyDropDownMaxVal = 0;
     $inStock = true;
     if (!$data || !count($data->skus)) {
         $inStock = false;
     }
     $this->view->inStock = $inStock;
     if ($data && count($data->skus) == 1) {
         // Set the max value for the dropdown QTY
         // TODO: add it to the SKU table to set on the per SKU level
         $qtyDropDownMaxValLimit = 20;
         // Get the first and the only value
         $sku = array_shift(array_values($data->skus));
         // If no inventory tracking, there is no limit on how many can be purchased
         $qtyDropDownMaxVal = $qtyDropDownMaxValLimit;
         if ($sku['info']->sTrackInventory) {
             $qtyDropDownMaxVal = $sku['info']->sInventory;
         }
         if ($qtyDropDownMaxVal < 1) {
             $qtyDropDownMaxVal = 1;
         } elseif ($qtyDropDownMaxVal > $qtyDropDownMaxValLimit) {
             $qtyDropDownMaxVal = $qtyDropDownMaxValLimit;
         }
         // If the SKU doesn't allow multiple items, set the dropdown to 1
         if (!$sku['info']->sAllowMultiple) {
             $qtyDropDownMaxVal = 1;
         }
     }
     $this->view->qtyDropDown = $qtyDropDownMaxVal;
     if ($data) {
         foreach ($data->skus as $sId => $info) {
             $info = $info['info'];
             if ($info->sPrice > $priceRange['high']) {
                 $priceRange['high'] = $info->sPrice;
             }
             if (!$priceRange['low'] || $priceRange['low'] > $info->sPrice) {
                 $priceRange['low'] = $info->sPrice;
             }
         }
     }
     $this->view->price = $priceRange;
     // Add custom page JS
     if ($data && (count($data->options) > 1 || count($data->skus) > 1)) {
         $js = $this->getDisplayJs($data->options, $data->skus);
         Document::addScriptDeclaration($js);
     }
     // Get images (if any), gets all images from /site/storefront/products/$pId
     $allowedImgExt = array('jpg', 'gif', 'png');
     $productImg = array();
     $imgWebPath = DS . 'site' . DS . 'storefront' . DS . 'products' . DS . $pId;
     $imgPath = PATH_APP . $imgWebPath;
     if (file_exists($imgPath)) {
         $files = scandir($imgPath);
         foreach ($files as $file) {
             if (in_array(pathinfo($file, PATHINFO_EXTENSION), $allowedImgExt)) {
                 if (substr($file, 0, 7) == 'default') {
                     // Let the default image to be the first one
                     array_unshift($productImg, $imgWebPath . DS . $file);
                 } else {
                     $productImg[] = $imgWebPath . DS . $file;
                 }
             }
         }
     } else {
         $productImg[] = DS . 'site' . DS . 'storefront' . DS . 'products' . DS . 'noimage.png';
     }
     $this->view->productImg = $productImg;
     $this->view->productAvailable = $productAvailable;
     //build pathway
     $this->_buildPathway($product->pName);
     // Set notifications
     $this->view->notifications = $pageMessages;
     $this->view->display();
 }