public function setLastVisitedCategory() { $cache_id = 'pspagebuilder::setLastVisitedCategory'; $context = Context::getContext(); if (!Cache::isStored($cache_id)) { if (method_exists($context->controller, 'getCategory') && ($category = $context->controller->getCategory())) { $context->cookie->last_visited_category = $category->id; } elseif (method_exists($context->controller, 'getProduct') && ($product = $context->controller->getProduct())) { if (!isset($context->cookie->last_visited_category) || !Product::idIsOnCategoryId($product->id, array(array('id_category' => $context->cookie->last_visited_category))) || !Category::inShopStatic($context->cookie->last_visited_category, $context->shop)) { $context->cookie->last_visited_category = (int) $product->id_category_default; } } Cache::store($cache_id, $context->cookie->last_visited_category); } return Cache::retrieve($cache_id); }
/** * Assign template vars related to category */ protected function assignCategory() { // Assign category to the template if ($this->category !== false && Validate::isLoadedObject($this->category) && $this->category->inShop() && $this->category->isAssociatedToShop()) { $path = Tools::getPath($this->category->id, $this->product->name, true); } elseif (Category::inShopStatic($this->product->id_category_default, $this->context->shop)) { $this->category = new Category((int) $this->product->id_category_default, (int) $this->context->language->id); if (Validate::isLoadedObject($this->category) && $this->category->active && $this->category->isAssociatedToShop()) { $path = Tools::getPath((int) $this->product->id_category_default, $this->product->name); } } if (!isset($path) || !$path) { $path = Tools::getPath((int) $this->context->shop->id_category, $this->product->name); } $sub_categories = array(); if (Validate::isLoadedObject($this->category)) { $sub_categories = $this->category->getSubCategories($this->context->language->id, true); // various assignements before Hook::exec $this->context->smarty->assign(array('path' => $path, 'category' => $this->category, 'subCategories' => $sub_categories, 'id_category_current' => (int) $this->category->id, 'id_category_parent' => (int) $this->category->id_parent, 'return_category_name' => Tools::safeOutput($this->category->getFieldByLang('name')), 'categories' => Category::getHomeCategories($this->context->language->id, true, (int) $this->context->shop->id))); } $this->context->smarty->assign(array('HOOK_PRODUCT_FOOTER' => Hook::exec('displayFooterProduct', array('product' => $this->product, 'category' => $this->category)))); }
public function hookLeftColumn($params) { if (!$this->isCached('blockcategories.tpl', $this->getCacheId())) { // Get all groups for this customer and concatenate them as a string: "1,2,3..." $groups = implode(', ', Customer::getGroupsStatic((int) $this->context->customer->id)); $maxdepth = Configuration::get('BLOCK_CATEG_MAX_DEPTH'); if (!($result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' SELECT DISTINCT c.id_parent, c.id_category, cl.name, cl.description, cl.link_rewrite FROM `' . _DB_PREFIX_ . 'category` c INNER JOIN `' . _DB_PREFIX_ . 'category_lang` cl ON (c.`id_category` = cl.`id_category` AND cl.`id_lang` = ' . (int) $this->context->language->id . Shop::addSqlRestrictionOnLang('cl') . ') INNER JOIN `' . _DB_PREFIX_ . 'category_shop` cs ON (cs.`id_category` = c.`id_category` AND cs.`id_shop` = ' . (int) $this->context->shop->id . ') WHERE (c.`active` = 1 OR c.`id_category` = ' . (int) Configuration::get('PS_HOME_CATEGORY') . ') AND c.`id_category` != ' . (int) Configuration::get('PS_ROOT_CATEGORY') . ' ' . ((int) $maxdepth != 0 ? ' AND `level_depth` <= ' . (int) $maxdepth : '') . ' AND c.id_category IN (SELECT id_category FROM `' . _DB_PREFIX_ . 'category_group` WHERE `id_group` IN (' . pSQL($groups) . ')) ORDER BY `level_depth` ASC, ' . (Configuration::get('BLOCK_CATEG_SORT') ? 'cl.`name`' : 'cs.`position`') . ' ' . (Configuration::get('BLOCK_CATEG_SORT_WAY') ? 'DESC' : 'ASC')))) { return; } $resultParents = array(); $resultIds = array(); $isDhtml = Configuration::get('BLOCK_CATEG_DHTML') == 1 ? true : false; foreach ($result as &$row) { $resultParents[$row['id_parent']][] =& $row; $resultIds[$row['id_category']] =& $row; } $blockCategTree = $this->getTree($resultParents, $resultIds, Configuration::get('BLOCK_CATEG_MAX_DEPTH')); unset($resultParents, $resultIds); $this->smarty->assign('blockCategTree', $blockCategTree); if (file_exists(_PS_THEME_DIR_ . 'modules/blockcategories/blockcategories.tpl')) { $this->smarty->assign('branche_tpl_path', _PS_THEME_DIR_ . 'modules/blockcategories/category-tree-branch.tpl'); } else { $this->smarty->assign('branche_tpl_path', _PS_MODULE_DIR_ . 'blockcategories/category-tree-branch.tpl'); } $this->smarty->assign('isDhtml', $isDhtml); } $id_category = (int) Tools::getValue('id_category'); $id_product = (int) Tools::getValue('id_product'); if (Tools::isSubmit('id_category')) { $this->context->cookie->last_visited_category = (int) $id_category; $this->smarty->assign('currentCategoryId', $this->context->cookie->last_visited_category); } if (Tools::isSubmit('id_product')) { if (!isset($this->context->cookie->last_visited_category) || !Product::idIsOnCategoryId($id_product, array('0' => array('id_category' => $this->context->cookie->last_visited_category))) || !Category::inShopStatic($this->context->cookie->last_visited_category, $this->context->shop)) { $product = new Product((int) $id_product); if (isset($product) && Validate::isLoadedObject($product)) { $this->context->cookie->last_visited_category = (int) $product->id_category_default; } } $this->smarty->assign('currentCategoryId', (int) $this->context->cookie->last_visited_category); } $display = $this->display(__FILE__, 'blockcategories.tpl', $this->getCacheId()); return $display; }
/** * Assign template vars related to category. */ protected function assignCategory() { // Assign category to the template if (($this->category === false || !Validate::isLoadedObject($this->category) || !$this->category->inShop() || !$this->category->isAssociatedToShop()) && Category::inShopStatic($this->product->id_category_default, $this->context->shop)) { $this->category = new Category((int) $this->product->id_category_default, (int) $this->context->language->id); } $sub_categories = array(); if (Validate::isLoadedObject($this->category)) { $sub_categories = $this->category->getSubCategories($this->context->language->id, true); // various assignements before Hook::exec $this->context->smarty->assign(array('category' => $this->category, 'subCategories' => $sub_categories, 'id_category_current' => (int) $this->category->id, 'id_category_parent' => (int) $this->category->id_parent, 'return_category_name' => Tools::safeOutput($this->category->getFieldByLang('name')), 'categories' => Category::getHomeCategories($this->context->language->id, true, (int) $this->context->shop->id))); } }
public function setLastVisitedCategory() { if (method_exists($this->context->controller, 'getCategory') && ($category = $this->context->controller->getCategory())) { $this->context->cookie->last_visited_category = $category->id; } elseif (method_exists($this->context->controller, 'getProduct') && ($product = $this->context->controller->getProduct())) { if (!isset($this->context->cookie->last_visited_category) || !Product::idIsOnCategoryId($product->id, array(array('id_category' => $this->context->cookie->last_visited_category))) || !Category::inShopStatic($this->context->cookie->last_visited_category, $this->context->shop)) { $this->context->cookie->last_visited_category = (int) $product->id_category_default; } } }
/** * Initialize product controller * @see FrontController::init() */ public function init() { if (Tools::getValue('product_rewrite')) { $rewrite_url = Tools::getValue('product_rewrite'); $id_product = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' SELECT `id_product` FROM `' . _DB_PREFIX_ . 'product_lang` WHERE `link_rewrite` = \'' . $rewrite_url . '\''); if ($id_product > 0) { $_GET['id_product'] = $id_product; $_GET['noredirect'] = 1; } else { $redirect_url = str_replace('.html', '', $_SERVER['REQUEST_URI']); $redirect_type = Configuration::get('PS_CANONICAL_REDIRECT') == 2 ? '301' : '302'; header('HTTP/1.0 ' . $redirect_type . ' Moved'); header('Cache-Control: no-cache'); Tools::redirectLink('http://' . $_SERVER['HTTP_HOST'] . $redirect_url); Tools::display404Error(); die; } } parent::init(); if ($id_product = (int) Tools::getValue('id_product')) { $this->product = new Product($id_product, true, $this->context->language->id, $this->context->shop->id); } if (!Validate::isLoadedObject($this->product)) { header('HTTP/1.1 404 Not Found'); header('Status: 404 Not Found'); $this->errors[] = Tools::displayError('Product not found'); } else { $this->canonicalRedirection(); /* * If the product is associated to the shop * and is active or not active but preview mode (need token + file_exists) * allow showing the product * In all the others cases => 404 "Product is no longer available" */ if (!$this->product->isAssociatedToShop() || !$this->product->active) { if (Tools::getValue('adtoken') == Tools::getAdminToken('AdminProducts' . (int) Tab::getIdFromClassName('AdminProducts') . (int) Tools::getValue('id_employee')) && $this->product->isAssociatedToShop()) { // If the product is not active, it's the admin preview mode $this->context->smarty->assign('adminActionDisplay', true); } else { $this->context->smarty->assign('adminActionDisplay', false); if ($this->product->id_product_redirected == $this->product->id) { $this->product->redirect_type = '404'; } switch ($this->product->redirect_type) { case '301': header('HTTP/1.1 301 Moved Permanently'); header('Location: ' . $this->context->link->getProductLink($this->product->id_product_redirected)); break; case '302': header('HTTP/1.1 302 Moved Temporarily'); header('Cache-Control: no-cache'); header('Location: ' . $this->context->link->getProductLink($this->product->id_product_redirected)); break; case '404': default: header('HTTP/1.1 404 Not Found'); header('Status: 404 Not Found'); $this->errors[] = Tools::displayError('This product is no longer available.'); break; } } } elseif (!$this->product->checkAccess(isset($this->context->customer) ? $this->context->customer->id : 0)) { header('HTTP/1.1 403 Forbidden'); header('Status: 403 Forbidden'); $this->errors[] = Tools::displayError('You do not have access to this product.'); } else { // Load category $id_category = false; if (isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] == Tools::secureReferrer($_SERVER['HTTP_REFERER']) && preg_match('~^.*(?<!\\/content)\\/([0-9]+)\\-(.*[^\\.])|(.*)id_(category|product)=([0-9]+)(.*)$~', $_SERVER['HTTP_REFERER'], $regs)) { // If the previous page was a category and is a parent category of the product use this category as parent category $id_object = false; if (isset($regs[1]) && is_numeric($regs[1])) { $id_object = (int) $regs[1]; } elseif (isset($regs[5]) && is_numeric($regs[5])) { $id_object = (int) $regs[5]; } if ($id_object) { $referers = array($_SERVER['HTTP_REFERER'], urldecode($_SERVER['HTTP_REFERER'])); if (in_array($this->context->link->getCategoryLink($id_object), $referers)) { $id_category = (int) $id_object; } elseif (isset($this->context->cookie->last_visited_category) && (int) $this->context->cookie->last_visited_category && in_array($this->context->link->getProductLink($id_object), $referers)) { $id_category = (int) $this->context->cookie->last_visited_category; } } } if (!$id_category || !Category::inShopStatic($id_category, $this->context->shop) || !Product::idIsOnCategoryId((int) $this->product->id, array('0' => array('id_category' => $id_category)))) { $id_category = (int) $this->product->id_category_default; } if (Category::inDecor($id_category) && !$this->ajax && strpos($_SERVER['SERVER_NAME'], 'decor') <= -1) { $final_url = $this->context->link->getProductLink($this->product); $redirect_type = Configuration::get('PS_CANONICAL_REDIRECT') == 2 ? '301' : '302'; header('HTTP/1.0 ' . $redirect_type . ' Moved'); header('Cache-Control: no-cache'); Tools::redirectLink($final_url); } $this->category = new Category((int) $id_category, (int) $this->context->cookie->id_lang); if (isset($this->context->cookie) && isset($this->category->id_category) && !(Module::isInstalled('blockcategories') && Module::isEnabled('blockcategories'))) { $this->context->cookie->last_visited_category = (int) $this->category->id_category; } } } }