_loadConfigMessage(self::MSG_NOT_FOUND_REDIRECT);
return $this;
}
protected function _initAction()
{
switch ($this->getConfig('forceAction')) {
case 'view':
if (count($this->_restParams) != 1) {
$this->trigger404();
}
$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 trigger404()
{
$url = Qs_SiteMap::findFirst(null, ['type' => 'ECommerce_Product_Category_'], ['type' => ''], 'url');
$this->_setBackUrl($url);
$this->_setBackMessage(self::MSG_NOT_FOUND_REDIRECT);
$this->_doBack();
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 = array(
'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->_addLinksItem();
$this->_addItem($item);
$this->_setGtmValues($item);
return $this;
}
protected function _setGtmValues(array $item)
{
$this->_doc->addGtmValues(Qs_Gtm::adGetCategoryPage($item['list']));
$categoryTitle = $this->_getDataObj()->getCategoryTitle();
$listTitle = '"' . $categoryTitle . '" Category List';
$pageNumber = (int) $this->_getList()->getPageNumber();
$ipp = (int) $this->_getList()->getIpp();
$startPosition = --$pageNumber * $ipp;
$this->_doc->addGtmValues(Qs_Gtm::eeGetProductImpressions(
$item['list'],
$categoryTitle,
$listTitle,
$startPosition
));
if ($this->_doc->isGtmEnabled()) {
$products = $item['list'];
$products = array_combine(Qs_Array::fetchCol($products, 'id'), $products);
$options = [
'products' => Qs_Gtm::eeGetProducts($products, Qs_Gtm::DATASET_CLICK, [], true),
'actionField' => ['list' => $listTitle]
];
$this->_doc->addInitObject('lib.Gtm', [$options]);
}
return $this;
}
protected function _doList()
{
if (!empty($this->_restParams)) {
if (!$this->_isCategoryVisible($this->_getDataObj()->getCategoryId())) {
$this->_do404();
}
parent::_doList();
}
return $this;
}
/**
* @param int $categoryId
* @return bool
*/
protected function _isCategoryVisible($categoryId)
{
if (null === $this->_categoryObj) {
$this->_categoryObj = new App_ECommerce_Product_Category_Obj();
}
return (bool) $this->_categoryObj->getNode($this->_categoryObj->getCategoryTree(), $categoryId);
}
/**
* @param array|null $product
* @return bool
*/
protected function _isProductVisible($product)
{
if (!empty($product)) {
foreach ($product['categories'] as $categoryId) {
if ($this->_isCategoryVisible($categoryId)) {
return true;
}
}
}
return false;
}
protected function _isItemAccessible(array $item)
{
return parent::_isItemAccessible($item) && $this->_isProductVisible($item);
}
protected function _prepareViewItem(array &$item)
{
$item['imageContainer'] = $this->getConfig('imageContainer');
if (false !== ($cartForm = $this->_getAddToCartForm())) {
if ($this->_getDataObj()->getData('soldOut') != 'y') {
$item['cartForm'] = $cartForm->render();
}
}
return parent::_prepareViewItem($item);
}
protected function _addViewItem()
{
$item = $this->getData();
if (!$this->_isItemAccessible($item)) {
$this->_doc->display404();
}
$this->_prepareViewItem($item);
$this->_addItem($item);
$this->_initDetailsPage($item);
$this->_doc->addGtmValues(Qs_Gtm::adGetProductView($item));
$this->_doc->addGtmValues(Qs_Gtm::eeGetProductDetailImpressions($item));
return $this;
}
protected function _initDetailsPage(array $item)
{
$this->_setProductCategory();
$this->_doc->addStyleSheet('css/thirdpart/royalslider.css');
$this->_doc->addStyleSheet('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.fullscreen.js');
$this->_doc->addScript('js/royalslider/jquery.rs.global-caption.js');
$this->_doc->addScript('js/app/ECommerce/product/view.js');
$options = array(
'imageContainer' => $this->getConfig('imageContainer'),
'image' => $this->getConfig('image')->toArray()
);
$this->_doc->addInitObject('App_Product_View', array($options));
return parent::_initDetailsPage($item);
}
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
*
* @return App_ECommerce_Cart_Form_AddItem|bool
*/
protected function _getAddToCartForm()
{
if (null === $this->_cartForm) {
$cartItem = Qs_SiteMap::findFirst(null, array('type' => 'ECommerce_Cart_'), array('type' => 'default'));
$this->_cartForm = false;
if (!empty($cartItem)) {
$cartFormOptions = array(
'method' => 'post',
'action' => $this->url(),
'attribs' => array(
'id' => 'cart-form',
'class' => 'cart_form',
),
'defaults' => array(
'productId' => $this->_getDataObj()->getPrimaryKey(),
'quantity' => 1
)
);
$this->_cartForm = new App_ECommerce_Cart_Form_AddItem($cartFormOptions);
$this->_cartForm->setDefaults();
$this->_cartForm->setScriptOption(
'onSuccessCallback',
array('fn' => 'App_Product_View_AddItem.onSuccessCallback', 'args' => $this->_cartForm->getId())
);
}
}
return $this->_cartForm;
}
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, array('type' => 'ECommerce_Cart_'), array('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;
}
public function writeXmlSitemap(App_Sitemap_Xml_Writer $xml)
{
$categoryView = new App_ECommerce_Product_Category_View();
$menu = $categoryView->getMenu();
if ('view' == $this->getConfig('forceAction')) {
$enabledCategories = [];
Qs_Array::treeWalk($menu, function ($category) use (&$enabledCategories) {
$enabledCategories[] = $category['id'];
});
$this->_getDataObj()->addFilter(['categoryId' => $enabledCategories]);
return $this->writeDefaultXmlSitemap($xml);
}
$loop = function ($menu) use ($xml, &$loop) {
foreach ($menu as $item) {
$xml->writeUrl($item['url']);
if (!empty($item['sub'])) {
$loop($item['sub']);
}
}
};
$loop($menu);
return $this;
}
}