getConfig('forceAction')) { case 'view': if (count($this->_restParams) != 1) { $this->_doc->display404(); } $this->_getDataObj()->setPrimaryKeyByAlias($this->_restParams[0]); $this->_action = 'view'; if (null !== ($action = Qs_Request::getPostValue('action'))) { if (in_array($action, $this->_actions)) { $this->_action = $action; } } Qs_Navigation::disable(); break; case 'list': // break omitted intentionally default: if (!empty($this->_restParams)) { if (!$this->_getDataObj()->setCategoryByAlias($this->_restParams)) { $this->_doc->display404(); } } $this->_action = 'list'; break; } return $this; } protected function _hasSubCategories() { $categoryId = $this->_getDataObj()->getCategoryId(); $categoryObj = new App_ECommerce_Product_Category_Obj(); $tree = $categoryObj->getCategoryTree(); $node = $categoryObj->getNode($tree, $categoryId); return ($node['productsCount'] || $node['subcategoriesProductsCount']); } protected function _getOrderFormOptions() { $options = [ 'method' => 'get', 'id' => strtolower($this->getApplicationName()) . '-order-form', 'class' => 'sorter_form clearfix', ]; return $options; } protected function _getOrderForm() { if (null === $this->_orderForm) { $this->_orderForm = $this->_getFormInstance('order', $this->_getOrderFormOptions()); } return $this->_orderForm; } protected function _getList() { parent::_getList(); $orderForm = $this->_getOrderForm(); if ($orderForm->isSubmitted() && $orderForm->validate()) { $orderValues = $orderForm->getValues(); $this->_list->setOrderId((int) $orderValues['orderId']); $this->_list->addUrlVariables(array_keys($orderValues)); } return $this->_list; } protected function _getListOptions() { $options = parent::_getListOptions(); $options['productBaseUrl'] = static::getViewUrl(); return $options; } protected function _addListItem() { $this->_initListRedirection(); $item = $this->_getListItem(); $this->_initListPageNumber($item); if (empty($item['list']) && !$this->_hasSubCategories()) { $this->_do404(); } if (!empty($item['list'])) { $this->_addFormItem($this->_getOrderForm()); $this->_initCartForms($item['list']); } $this->_addLinksItem(); $this->_addItem($item); return $this; } protected function _doList() { if (!empty($this->_restParams)) { if (!$this->_isCategoryVisible($this->_getDataObj()->getCategoryId())) { $this->_do404(); } parent::_doList(); } return $this; } protected function _initCartForms(array &$list) { foreach ($list as &$item) { if ('n' == $item['soldOut']) { $form = $this->_getAddToCartForm($item, ['listForm' => true]); $item['form'] = ($form) ? $form->render() : false; } } return $this; } protected function _isItemAccessible(array $item) { return parent::_isItemAccessible($item) && $this->_isProductVisible($item); } protected function _prepareViewItem(array &$item) { $item['imageContainer'] = $this->getConfig('imageContainer'); $item['tabsContainer'] = $this->getConfig('tabsContainer'); if (false !== ($cartForm = $this->_getAddToCartForm())) { if ($this->_getDataObj()->getData('soldOut') != 'y') { $item['cartForm'] = $cartForm->render(); } } $this->_prepareReviewsItem($item); $item['tabs'] = []; if ($item['description']) { $item['tabs'][] = [ 'id' => 'overview', 'title' => 'Overview', 'tpl' => $this->getTemplate('tab-overview.tpl'), ]; } if ($item['review']) { $item['tabs'][] = [ 'id' => 'reviews', 'title' => 'Reviews', 'tpl' => $this->getTemplate('tab-reviews.tpl')]; } return parent::_prepareViewItem($item); } protected function _initDetailsPage(array $item) { $this->_setProductCategory(); $this->_doc->addCriticalStylesheet('css/thirdpart/royalslider.css'); $this->_doc->addCriticalStylesheet('css/thirdpart/royalslider-default.css'); $this->_doc->addScript('js/royalslider/jquery.royalslider.js'); $this->_doc->addScript('js/royalslider/jquery.rs.thumbnails.js'); $this->_doc->addScript('js/royalslider/jquery.rs.global-caption.js'); $this->_doc->addNonCriticalStylesheet('css/thirdpart/fancybox/jquery.fancybox.css'); $this->_doc->addScript('js/fancybox/jquery.fancybox.js'); $this->_doc->addScript('js/app/ECommerce/product/view.js'); $options = [ 'imageContainer' => $this->getConfig('imageContainer'), 'tabsContainer' => $this->getConfig('tabsContainer'), 'image' => $this->getConfig('image')->toArray(), 'gallery' => $this->_getGallery4Script($item['additionalImages']), ]; $this->_doc->addInitObject('app.ecommerce.product.View', [$options]); return parent::_initDetailsPage($item); } protected function _getGallery4Script(array $list) { $result = []; $config = $this->getConfig('image'); foreach ($list as $item) { $result[] = [ 'href' => Qs_ImageFs::get($item['image'], $config->width, $config->height, $config->resizeMethod), 'title' => $item['title'], ]; } return $result; } protected function _setProductCategory() { $productCategories = $this->_getDataObj()->getData('categories'); $changedCategory = App_ECommerce_Product_Category_View::getCategoryId(); $productChangedCategory = App_ECommerce_Product_Category_View::getCategoryId($this->_restAlias); if (!in_array($changedCategory, $productCategories) && empty($productChangedCategory)) { $changedCategory = current($productCategories); } elseif (!in_array($changedCategory, $productCategories)) { $changedCategory = $productChangedCategory; } if ($productChangedCategory != $changedCategory) { $productChangedCategory = $changedCategory; } App_ECommerce_Product_Category_View::saveCategoryId($productChangedCategory, $this->_restAlias); return $this; } /** * Return add to cart form * @param array|null $product * @param array $formOptions * @return App_ECommerce_Cart_Form_AddItem|bool */ protected function _getAddToCartForm(array $product = null, array $formOptions = []) { if (null === $product) { $product = $this->_getDataObj()->getData(); } if ('y' == Qs_Array::get($product, 'soldOut')) { return false; } if (!array_key_exists($product['id'], $this->_cartForm)) { static $cartItem; if (null === $cartItem) { $cartItem = Qs_SiteMap::findFirst(null, ['type' => 'ECommerce_Cart_'], ['type' => 'default']); } if (!empty($cartItem)) { static $formAction; if (null === $formAction) { $formAction = constant('BASE_URL') . '/' . Qs_SiteMap::findFirst( null, ['type' => 'ECommerce_Product_'], ['forceAction' => 'view'], 'fullAlias' ); } $cartFormOptions = [ 'method' => 'post', 'action' => $formAction . '/' . $product['alias'], 'attribs' => [ 'id' => 'cart-form-' . $product['id'], 'class' => 'cart_form', ], 'defaults' => [ 'productId' => $product['id'], 'quantity' => 1, ], ]; $cartFormOptions = array_merge($cartFormOptions, $formOptions); $form = new App_ECommerce_Cart_Form_AddItem($cartFormOptions); $form->prependId(); $form->setDefaults(); $form->setScriptOption( 'onSuccessCallback', ['fn' => 'app.ecommerce.product.AddToCart.onSuccessCallback', 'args' => $form->getId()] ); $this->_cartForm[$product['id']] = $form; } else { $this->_cartForm[$product['id']] = false; } } return $this->_cartForm[$product['id']]; } protected function _doAddItemAjax() { $form = $this->_getAddToCartForm(); $result = $form->validateAjax(); if ($result['isValid'] === true) { $data = $form->getValues(); $result['success'] = $this->_addToCart($data); if ($result['success'] !== false) { $result['success']['quantity'] = $this->_getCart()->getItemsCount(); $result['success']['message'] .= '
View Shopping Cart >'; $this->_postAddItem(); } } $this->_displayJson($result); } /** * Add item to cart action */ protected function _doAddItem() { $form = $this->_getAddToCartForm(); if ($form->validate()) { $backUrl = Qs_SiteMap::findFirst(null, ['type' => 'ECommerce_Cart_'], ['type' => 'default'], 'url'); $this->_setBackUrl($backUrl); $data = $form->getValues(); $data['applyShipping'] = 'y'; $result = $this->_addToCart($data); if (false === $result) { throw new Qs_Exception('Error while add item'); } else { $this->{'_setBack' . ucfirst($result['type'])}($result['message']); } $this->_postAddItem(); $this->_doBack(); } $this->_doView(); } protected function _postAddItem() { $this->_log(); return $this; } protected function _prepareReviewsItem(array &$item) { $productId = $this->_getDataObj()->getPrimaryKey(); if (!$productId) { return $this; } $view = new ReviewView(); $view->setDoc($this->_doc); $view->setProductId($productId); $item['review'] = [ 'list' => $view->getListItem(), 'form' => $view->getNewFormItem(), ]; return $this; } }