PageRenderTime 62ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/classes/controller/FrontController.php

https://bitbucket.org/yhjohn/ayanapure.com
PHP | 1145 lines | 850 code | 132 blank | 163 comment | 197 complexity | b128ef7308ef0a597c71d460b62d9255 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0
  1. <?php
  2. /*
  3. * 2007-2012 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2012 PrestaShop SA
  23. * @version Release: $Revision: 7483 $
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. * International Registered Trademark & Property of PrestaShop SA
  26. */
  27. class FrontControllerCore extends Controller
  28. {
  29. public $errors = array();
  30. /**
  31. * @deprecated Deprecated shortcuts as of 1.5 - Use $context->var instead
  32. */
  33. protected static $smarty, $cookie, $link, $cart;
  34. public $iso;
  35. public $orderBy;
  36. public $orderWay;
  37. public $p;
  38. public $n;
  39. public $auth = false;
  40. public $guestAllowed = false;
  41. public $authRedirection = false;
  42. public $ssl = false;
  43. protected $restrictedCountry = false;
  44. protected $maintenance = false;
  45. public $display_column_left = true;
  46. public $display_column_right = true;
  47. public static $initialized = false;
  48. protected static $currentCustomerGroups;
  49. public $nb_items_per_page;
  50. public function __construct()
  51. {
  52. $this->controller_type = 'front';
  53. global $useSSL;
  54. parent::__construct();
  55. if (isset($useSSL))
  56. $this->ssl = $useSSL;
  57. else
  58. $useSSL = $this->ssl;
  59. }
  60. /**
  61. * @see Controller::checkAccess()
  62. *
  63. * @return boolean
  64. */
  65. public function checkAccess()
  66. {
  67. return true;
  68. }
  69. /**
  70. * @see Controller::viewAccess
  71. *
  72. * @return boolean
  73. */
  74. public function viewAccess()
  75. {
  76. return true;
  77. }
  78. public function init()
  79. {
  80. /*
  81. * Globals are DEPRECATED as of version 1.5.
  82. * Use the Context to access objects instead.
  83. * Example: $this->context->cart
  84. */
  85. global $useSSL, $cookie, $smarty, $cart, $iso, $defaultCountry, $protocol_link, $protocol_content, $link, $css_files, $js_files, $currency;
  86. if (self::$initialized)
  87. return;
  88. self::$initialized = true;
  89. parent::init();
  90. // If current URL use SSL, set it true (used a lot for module redirect)
  91. if (Tools::usingSecureMode())
  92. $useSSL = $this->ssl = true;
  93. // For compatibility with globals, DEPRECATED as of version 1.5
  94. $css_files = $this->css_files;
  95. $js_files = $this->js_files;
  96. if ($this->ssl && !Tools::usingSecureMode() && Configuration::get('PS_SSL_ENABLED'))
  97. {
  98. header('HTTP/1.1 301 Moved Permanently');
  99. header('Cache-Control: no-cache');
  100. header('Location: '.Tools::getShopDomainSsl(true).$_SERVER['REQUEST_URI']);
  101. exit();
  102. }
  103. elseif (Configuration::get('PS_SSL_ENABLED') && Tools::usingSecureMode() && !($this->ssl))
  104. {
  105. header('HTTP/1.1 301 Moved Permanently');
  106. header('Cache-Control: no-cache');
  107. header('Location: '.Tools::getShopDomain(true).$_SERVER['REQUEST_URI']);
  108. exit();
  109. }
  110. if ($this->ajax)
  111. {
  112. $this->display_header = false;
  113. $this->display_footer = false;
  114. }
  115. // if account created with the 2 steps register process, remove 'accoun_created' from cookie
  116. if (isset($this->context->cookie->account_created))
  117. {
  118. $this->context->smarty->assign('account_created', 1);
  119. unset($this->context->cookie->account_created);
  120. }
  121. ob_start();
  122. // Init cookie language
  123. // @TODO This method must be moved into switchLanguage
  124. Tools::setCookieLanguage($this->context->cookie);
  125. $currency = Tools::setCurrency($this->context->cookie);
  126. $protocol_link = (Configuration::get('PS_SSL_ENABLED') || Tools::usingSecureMode()) ? 'https://' : 'http://';
  127. $useSSL = ((isset($this->ssl) && $this->ssl && Configuration::get('PS_SSL_ENABLED')) || Tools::usingSecureMode()) ? true : false;
  128. $protocol_content = ($useSSL) ? 'https://' : 'http://';
  129. $link = new Link($protocol_link, $protocol_content);
  130. $this->context->link = $link;
  131. if ($id_cart = (int)$this->recoverCart())
  132. $this->context->cookie->id_cart = (int)$id_cart;
  133. if ($this->auth && !$this->context->customer->isLogged($this->guestAllowed))
  134. Tools::redirect('index.php?controller=authentication'.($this->authRedirection ? '&back='.$this->authRedirection : ''));
  135. /* Theme is missing */
  136. if (!is_dir(_PS_THEME_DIR_))
  137. die(sprintf(Tools::displayError('Current theme unavailable "%s". Please check your theme directory name and permissions.'), basename(rtrim(_PS_THEME_DIR_, '/\\'))));
  138. if (Configuration::get('PS_GEOLOCATION_ENABLED'))
  139. if (($newDefault = $this->geolocationManagement($this->context->country)) && Validate::isLoadedObject($newDefault))
  140. $this->context->country = $newDefault;
  141. if (isset($_GET['logout']) || ($this->context->customer->logged && Customer::isBanned($this->context->customer->id)))
  142. {
  143. $this->context->customer->logout();
  144. Tools::redirect(isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null);
  145. }
  146. elseif (isset($_GET['mylogout']))
  147. {
  148. $this->context->customer->mylogout();
  149. Tools::redirect(isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null);
  150. }
  151. /* Cart already exists */
  152. if ((int)$this->context->cookie->id_cart)
  153. {
  154. $cart = new Cart($this->context->cookie->id_cart);
  155. if ($cart->OrderExists())
  156. {
  157. unset($this->context->cookie->id_cart, $cart, $this->context->cookie->checkedTOS);
  158. $this->context->cookie->check_cgv = false;
  159. }
  160. /* Delete product of cart, if user can't make an order from his country */
  161. elseif (intval(Configuration::get('PS_GEOLOCATION_ENABLED')) &&
  162. !in_array(strtoupper($this->context->cookie->iso_code_country), explode(';', Configuration::get('PS_ALLOWED_COUNTRIES'))) &&
  163. $cart->nbProducts() && intval(Configuration::get('PS_GEOLOCATION_NA_BEHAVIOR')) != -1 &&
  164. !FrontController::isInWhitelistForGeolocation())
  165. unset($this->context->cookie->id_cart, $cart);
  166. // update cart values
  167. elseif ($this->context->cookie->id_customer != $cart->id_customer || $this->context->cookie->id_lang != $cart->id_lang || $currency->id != $cart->id_currency)
  168. {
  169. if ($this->context->cookie->id_customer)
  170. $cart->id_customer = (int)($this->context->cookie->id_customer);
  171. $cart->id_lang = (int)($this->context->cookie->id_lang);
  172. $cart->id_currency = (int)$currency->id;
  173. $cart->update();
  174. }
  175. /* Select an address if not set */
  176. if (isset($cart) && (!isset($cart->id_address_delivery) || $cart->id_address_delivery == 0 ||
  177. !isset($cart->id_address_invoice) || $cart->id_address_invoice == 0) && $this->context->cookie->id_customer)
  178. {
  179. $to_update = false;
  180. if (!isset($cart->id_address_delivery) || $cart->id_address_delivery == 0)
  181. {
  182. $to_update = true;
  183. $cart->id_address_delivery = (int)Address::getFirstCustomerAddressId($cart->id_customer);
  184. }
  185. if (!isset($cart->id_address_invoice) || $cart->id_address_invoice == 0)
  186. {
  187. $to_update = true;
  188. $cart->id_address_invoice = (int)Address::getFirstCustomerAddressId($cart->id_customer);
  189. }
  190. if ($to_update)
  191. $cart->update();
  192. }
  193. }
  194. if (!isset($cart) || !$cart->id)
  195. {
  196. $cart = new Cart();
  197. $cart->id_lang = (int)($this->context->cookie->id_lang);
  198. $cart->id_currency = (int)($this->context->cookie->id_currency);
  199. $cart->id_guest = (int)($this->context->cookie->id_guest);
  200. $cart->id_shop_group = (int)$this->context->shop->id_shop_group;
  201. $cart->id_shop = $this->context->shop->id;
  202. if ($this->context->cookie->id_customer)
  203. {
  204. $cart->id_customer = (int)($this->context->cookie->id_customer);
  205. $cart->id_address_delivery = (int)(Address::getFirstCustomerAddressId($cart->id_customer));
  206. $cart->id_address_invoice = $cart->id_address_delivery;
  207. }
  208. else
  209. {
  210. $cart->id_address_delivery = 0;
  211. $cart->id_address_invoice = 0;
  212. }
  213. // Needed if the merchant want to give a free product to every visitors
  214. $this->context->cart = $cart;
  215. CartRule::autoAddToCart($this->context);
  216. }
  217. $locale = strtolower(Configuration::get('PS_LOCALE_LANGUAGE')).'_'.strtoupper(Configuration::get('PS_LOCALE_COUNTRY').'.UTF-8');
  218. setlocale(LC_COLLATE, $locale);
  219. setlocale(LC_CTYPE, $locale);
  220. setlocale(LC_TIME, $locale);
  221. setlocale(LC_NUMERIC, 'en_US.UTF-8');
  222. /* get page name to display it in body id */
  223. // Are we in a payment module
  224. $module_name = '';
  225. if (Validate::isModuleName(Tools::getValue('module')))
  226. $module_name = Tools::getValue('module');
  227. if (!empty($this->page_name))
  228. $page_name = $this->page_name;
  229. elseif (!empty($this->php_self))
  230. $page_name = $this->php_self;
  231. elseif (Tools::getValue('fc') == 'module' && $module_name != '' && is_a($module_name, 'PaymentModule'))
  232. $page_name = 'module-payment-submit';
  233. // @retrocompatibility Are we in a module ?
  234. elseif (preg_match('#^'.preg_quote($this->context->shop->physical_uri, '#').'modules/([a-zA-Z0-9_-]+?)/(.*)$#', $_SERVER['REQUEST_URI'], $m))
  235. $page_name = 'module-'.$m[1].'-'.str_replace(array('.php', '/'), array('', '-'), $m[2]);
  236. else
  237. {
  238. $page_name = Dispatcher::getInstance()->getController();
  239. $page_name = (preg_match('/^[0-9]/', $page_name)) ? 'page_'.$page_name : $page_name;
  240. }
  241. $this->context->smarty->assign(Meta::getMetaTags($this->context->language->id, $page_name));
  242. $this->context->smarty->assign('request_uri', Tools::safeOutput(urldecode($_SERVER['REQUEST_URI'])));
  243. /* Breadcrumb */
  244. $navigationPipe = (Configuration::get('PS_NAVIGATION_PIPE') ? Configuration::get('PS_NAVIGATION_PIPE') : '>');
  245. $this->context->smarty->assign('navigationPipe', $navigationPipe);
  246. // Automatically redirect to the canonical URL if needed
  247. if (!empty($this->php_self) && !Tools::getValue('ajax'))
  248. $this->canonicalRedirection($this->context->link->getPageLink($this->php_self, $this->ssl, $this->context->language->id));
  249. Product::initPricesComputation();
  250. $display_tax_label = $this->context->country->display_tax_label;
  251. if ($cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')})
  252. {
  253. $infos = Address::getCountryAndState((int)($cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')}));
  254. $country = new Country((int)$infos['id_country']);
  255. if (Validate::isLoadedObject($country))
  256. $display_tax_label = $country->display_tax_label;
  257. }
  258. $languages = Language::getLanguages(true, $this->context->shop->id);
  259. $meta_language = array();
  260. foreach ($languages as $lang)
  261. $meta_language[] = $lang['iso_code'];
  262. $this->context->smarty->assign(array(
  263. // Usefull for layout.tpl
  264. 'mobile_device' => $this->context->getMobileDevice(),
  265. 'link' => $link,
  266. 'cart' => $cart,
  267. 'currency' => $currency,
  268. 'cookie' => $this->context->cookie,
  269. 'page_name' => $page_name,
  270. 'hide_left_column' => !$this->display_column_left,
  271. 'hide_right_column' => !$this->display_column_right,
  272. 'base_dir' => _PS_BASE_URL_.__PS_BASE_URI__,
  273. 'base_dir_ssl' => $protocol_link.Tools::getShopDomainSsl().__PS_BASE_URI__,
  274. 'content_dir' => $protocol_content.Tools::getHttpHost().__PS_BASE_URI__,
  275. 'base_uri' => $protocol_content.Tools::getHttpHost().__PS_BASE_URI__.(!Configuration::get('PS_REWRITING_SETTINGS') ? 'index.php' : ''),
  276. 'tpl_dir' => _PS_THEME_DIR_,
  277. 'modules_dir' => _MODULE_DIR_,
  278. 'mail_dir' => _MAIL_DIR_,
  279. 'lang_iso' => $this->context->language->iso_code,
  280. 'come_from' => Tools::getHttpHost(true, true).Tools::htmlentitiesUTF8(str_replace(array('\'', '\\'), '', urldecode($_SERVER['REQUEST_URI']))),
  281. 'cart_qties' => (int)$cart->nbProducts(),
  282. 'currencies' => Currency::getCurrencies(),
  283. 'languages' => $languages,
  284. 'meta_language' => implode('-', $meta_language),
  285. 'priceDisplay' => Product::getTaxCalculationMethod(),
  286. 'add_prod_display' => (int)Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),
  287. 'shop_name' => Configuration::get('PS_SHOP_NAME'),
  288. 'roundMode' => (int)Configuration::get('PS_PRICE_ROUND_MODE'),
  289. 'use_taxes' => (int)Configuration::get('PS_TAX'),
  290. 'display_tax_label' => (bool)$display_tax_label,
  291. 'vat_management' => (int)Configuration::get('VATNUMBER_MANAGEMENT'),
  292. 'opc' => (bool)Configuration::get('PS_ORDER_PROCESS_TYPE'),
  293. 'PS_CATALOG_MODE' => (bool)Configuration::get('PS_CATALOG_MODE') || !(bool)Group::getCurrent()->show_prices,
  294. 'b2b_enable' => (bool)Configuration::get('PS_B2B_ENABLE'),
  295. 'request' => $link->getPaginationLink(false, false, false, true)
  296. ));
  297. // Add the tpl files directory for mobile
  298. if ($this->context->getMobileDevice() != false)
  299. $this->context->smarty->assign(array(
  300. 'tpl_mobile_uri' => _PS_THEME_MOBILE_DIR_,
  301. ));
  302. // Deprecated
  303. $this->context->smarty->assign(array(
  304. 'id_currency_cookie' => (int)$currency->id,
  305. 'logged' => $this->context->customer->isLogged(),
  306. 'customerName' => ($this->context->customer->logged ? $this->context->cookie->customer_firstname.' '.$this->context->cookie->customer_lastname : false)
  307. ));
  308. $assign_array = array(
  309. 'img_ps_dir' => _PS_IMG_,
  310. 'img_cat_dir' => _THEME_CAT_DIR_,
  311. 'img_lang_dir' => _THEME_LANG_DIR_,
  312. 'img_prod_dir' => _THEME_PROD_DIR_,
  313. 'img_manu_dir' => _THEME_MANU_DIR_,
  314. 'img_sup_dir' => _THEME_SUP_DIR_,
  315. 'img_ship_dir' => _THEME_SHIP_DIR_,
  316. 'img_store_dir' => _THEME_STORE_DIR_,
  317. 'img_col_dir' => _THEME_COL_DIR_,
  318. 'img_dir' => _THEME_IMG_DIR_,
  319. 'css_dir' => _THEME_CSS_DIR_,
  320. 'js_dir' => _THEME_JS_DIR_,
  321. 'pic_dir' => _THEME_PROD_PIC_DIR_
  322. );
  323. // Add the images directory for mobile
  324. if ($this->context->getMobileDevice() != false)
  325. $assign_array['img_mobile_dir'] = _THEME_MOBILE_IMG_DIR_;
  326. // Add the CSS directory for mobile
  327. if ($this->context->getMobileDevice() != false)
  328. $assign_array['css_mobile_dir'] = _THEME_MOBILE_CSS_DIR_;
  329. foreach ($assign_array as $assign_key => $assign_value)
  330. if (substr($assign_value, 0, 1) == '/' || $protocol_content == 'https://')
  331. $this->context->smarty->assign($assign_key, $protocol_content.Tools::getMediaServer($assign_value).$assign_value);
  332. else
  333. $this->context->smarty->assign($assign_key, $assign_value);
  334. /*
  335. * These shortcuts are DEPRECATED as of version 1.5.
  336. * Use the Context to access objects instead.
  337. * Example: $this->context->cart
  338. */
  339. self::$cookie = $this->context->cookie;
  340. self::$cart = $cart;
  341. self::$smarty = $this->context->smarty;
  342. self::$link = $link;
  343. $defaultCountry = $this->context->country;
  344. $this->displayMaintenancePage();
  345. if ($this->restrictedCountry)
  346. $this->displayRestrictedCountryPage();
  347. //live edit
  348. if (Tools::isSubmit('live_edit') && ($ad = Tools::getValue('ad')) && Tools::getValue('liveToken') == Tools::getAdminToken('AdminModulesPositions'.(int)Tab::getIdFromClassName('AdminModulesPositions').(int)Tools::getValue('id_employee')))
  349. if (!is_dir(_PS_ROOT_DIR_.DIRECTORY_SEPARATOR.$ad))
  350. die(Tools::displayError());
  351. $this->iso = $iso;
  352. $this->setMedia();
  353. $this->context->cart = $cart;
  354. $this->context->currency = $currency;
  355. }
  356. public function postProcess()
  357. {
  358. /*// For retrocompatibility with versions before 1.5, preProcess support will be removed on next release
  359. if (method_exists(get_class($this), 'preProcess'))
  360. {
  361. $reflection = new ReflectionClass($this);
  362. if (!in_array($reflection->getMethod('preProcess')->class, array('FrontController', 'FrontControllerCore')))
  363. {
  364. Tools::displayAsDeprecated('Method preProcess() is deprecated in controllers, use method postProcess() instead');
  365. $this->preProcess();
  366. }
  367. }*/
  368. //$this->preProcess();
  369. }
  370. public function preProcess()
  371. {
  372. }
  373. public function initContent()
  374. {
  375. $this->process();
  376. if (!isset($this->context->cart))
  377. $this->context->cart = new Cart();
  378. if ($this->context->getMobileDevice() == false)
  379. {
  380. // These hooks aren't used for the mobile theme.
  381. // Needed hooks are called in the tpl files.
  382. if (!isset($this->context->cart))
  383. $this->context->cart = new Cart();
  384. $this->context->smarty->assign(array(
  385. 'HOOK_HEADER' => Hook::exec('displayHeader'),
  386. 'HOOK_TOP' => Hook::exec('displayTop'),
  387. 'HOOK_LEFT_COLUMN' => ($this->display_column_left ? Hook::exec('displayLeftColumn') : ''),
  388. 'HOOK_RIGHT_COLUMN' => ($this->display_column_right ? Hook::exec('displayRightColumn', array('cart' => $this->context->cart)) : ''),
  389. ));
  390. }
  391. else
  392. {
  393. $this->context->smarty->assign(array(
  394. 'HOOK_MOBILE_HEADER' => Hook::exec('displayMobileHeader'),
  395. ));
  396. }
  397. }
  398. /**
  399. * @deprecated 1.5.0
  400. */
  401. public function displayHeader($display = true)
  402. {
  403. // This method will be removed in 1.6
  404. Tools::displayAsDeprecated();
  405. $this->initHeader();
  406. $hook_header = Hook::exec('displayHeader');
  407. if ((Configuration::get('PS_CSS_THEME_CACHE') || Configuration::get('PS_JS_THEME_CACHE')) && is_writable(_PS_THEME_DIR_.'cache'))
  408. {
  409. // CSS compressor management
  410. if (Configuration::get('PS_CSS_THEME_CACHE'))
  411. $this->css_files = Media::cccCSS($this->css_files);
  412. //JS compressor management
  413. if (Configuration::get('PS_JS_THEME_CACHE'))
  414. $this->js_files = Media::cccJs($this->js_files);
  415. }
  416. // Call hook before assign of css_files and js_files in order to include correctly all css and javascript files
  417. $this->context->smarty->assign(array(
  418. 'HOOK_HEADER' => $hook_header,
  419. 'HOOK_TOP' => Hook::exec('displayTop'),
  420. 'HOOK_LEFT_COLUMN' => ($this->display_column_left ? Hook::exec('displayLeftColumn') : ''),
  421. 'HOOK_RIGHT_COLUMN' => ($this->display_column_right ? Hook::exec('displayRightColumn', array('cart' => $this->context->cart)) : ''),
  422. 'HOOK_FOOTER' => Hook::exec('displayFooter')
  423. ));
  424. $this->context->smarty->assign('css_files', $this->css_files);
  425. $this->context->smarty->assign('js_files', array_unique($this->js_files));
  426. $this->display_header = $display;
  427. $this->smartyOutputContent(_PS_THEME_DIR_.'header.tpl');
  428. }
  429. /**
  430. * @deprecated 1.5.0
  431. */
  432. public function displayFooter($display = true)
  433. {
  434. // This method will be removed in 1.6
  435. Tools::displayAsDeprecated();
  436. $this->smartyOutputContent(_PS_THEME_DIR_.'footer.tpl');
  437. }
  438. public function initCursedPage()
  439. {
  440. return $this->displayMaintenancePage();
  441. }
  442. public function process()
  443. {
  444. }
  445. public function redirect()
  446. {
  447. Tools::redirectLink($this->redirect_after);
  448. }
  449. /**
  450. * 1.4 retrocompatibility - will be removed in 1.6
  451. */
  452. public function displayContent()
  453. {
  454. }
  455. public function display()
  456. {
  457. Tools::safePostVars();
  458. // assign css_files and js_files at the very last time
  459. if ((Configuration::get('PS_CSS_THEME_CACHE') || Configuration::get('PS_JS_THEME_CACHE')) && is_writable(_PS_THEME_DIR_.'cache'))
  460. {
  461. // CSS compressor management
  462. if (Configuration::get('PS_CSS_THEME_CACHE'))
  463. $this->css_files = Media::cccCSS($this->css_files);
  464. //JS compressor management
  465. if (Configuration::get('PS_JS_THEME_CACHE'))
  466. $this->js_files = Media::cccJs($this->js_files);
  467. }
  468. $this->context->smarty->assign('css_files', $this->css_files);
  469. $this->context->smarty->assign('js_files', array_unique($this->js_files));
  470. $this->context->smarty->assign(array(
  471. 'errors' => $this->errors,
  472. 'display_header' => $this->display_header,
  473. 'display_footer' => $this->display_footer,
  474. ));
  475. // Don't use live edit if on mobile device
  476. if ($this->context->getMobileDevice() == false && Tools::isSubmit('live_edit'))
  477. $this->context->smarty->assign('live_edit', $this->getLiveEditFooter());
  478. $layout = $this->getLayout();
  479. if ($layout)
  480. {
  481. if ($this->template)
  482. $this->context->smarty->assign('template', $this->context->smarty->fetch($this->template));
  483. else // For retrocompatibility with 1.4 controller
  484. {
  485. ob_start();
  486. $this->displayContent();
  487. $template = ob_get_contents();
  488. ob_clean();
  489. $this->context->smarty->assign('template', $template);
  490. }
  491. $this->smartyOutputContent($layout);
  492. }
  493. else
  494. {
  495. // BEGIN - 1.4 retrocompatibility - will be removed in 1.6
  496. Tools::displayAsDeprecated('layout.tpl is missing in your theme directory');
  497. if ($this->display_header)
  498. $this->smartyOutputContent(_PS_THEME_DIR_.'header.tpl');
  499. if ($this->template)
  500. $this->smartyOutputContent($this->template);
  501. else // For retrocompatibility with 1.4 controller
  502. $this->displayContent();
  503. if ($this->display_footer)
  504. $this->smartyOutputContent(_PS_THEME_DIR_.'footer.tpl');
  505. // live edit
  506. if (Tools::isSubmit('live_edit') && ($ad = Tools::getValue('ad')) && Tools::getAdminToken('AdminModulesPositions'.(int)Tab::getIdFromClassName('AdminModulesPositions').(int)Tools::getValue('id_employee')))
  507. {
  508. $this->context->smarty->assign(array('ad' => $ad, 'live_edit' => true));
  509. $this->smartyOutputContent(_PS_ALL_THEMES_DIR_.'live_edit.tpl');
  510. }
  511. // END - 1.4 retrocompatibility - will be removed in 1.6
  512. }
  513. return true;
  514. }
  515. /* Display a maintenance page if shop is closed */
  516. protected function displayMaintenancePage()
  517. {
  518. if ($this->maintenance == true || (basename($_SERVER['PHP_SELF']) != 'disabled.php' && !(int)(Configuration::get('PS_SHOP_ENABLE'))))
  519. {
  520. $this->maintenance = true;
  521. if (!in_array(Tools::getRemoteAddr(), explode(',', Configuration::get('PS_MAINTENANCE_IP'))))
  522. {
  523. header('HTTP/1.1 503 temporarily overloaded');
  524. $this->context->smarty->assign(array(
  525. 'favicon_url' => _PS_IMG_.Configuration::get('PS_FAVICON'),
  526. 'logo_image_width' => Configuration::get('SHOP_LOGO_WIDTH'),
  527. 'logo_image_height' => Configuration::get('SHOP_LOGO_HEIGHT'),
  528. 'logo_url' => _PS_IMG_.Configuration::get('PS_LOGO').'?'.Configuration::get('PS_IMG_UPDATE_TIME')
  529. ));
  530. $template_dir = ($this->context->getMobileDevice() == true ? _PS_THEME_MOBILE_DIR_ : _PS_THEME_DIR_);
  531. $this->smartyOutputContent($template_dir.'maintenance.tpl');
  532. exit;
  533. }
  534. }
  535. }
  536. /* Display a specific page if the user country is not allowed */
  537. protected function displayRestrictedCountryPage()
  538. {
  539. header('HTTP/1.1 503 temporarily overloaded');
  540. $this->context->smarty->assign('favicon_url', _PS_IMG_.Configuration::get('PS_FAVICON'));
  541. $this->smartyOutputContent(_PS_THEME_DIR_.'restricted-country.tpl');
  542. exit;
  543. }
  544. protected function canonicalRedirection($canonical_url = '')
  545. {
  546. if (!$canonical_url || !Configuration::get('PS_CANONICAL_REDIRECT') || strtoupper($_SERVER['REQUEST_METHOD']) != 'GET')
  547. return;
  548. $match_url = (($this->ssl && Configuration::get('PS_SSL_ENABLED')) ? 'https://' : 'http://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
  549. $match_url = rawurldecode($match_url);
  550. if (!preg_match('/^'.Tools::pRegexp(rawurldecode($canonical_url), '/').'([&?].*)?$/', $match_url))
  551. {
  552. $params = array();
  553. $str_params = '';
  554. $url_details = parse_url($canonical_url);
  555. if (!empty($url_details['query']))
  556. {
  557. parse_str($url_details['query'], $query);
  558. foreach ($query as $key => $value)
  559. $params[Tools::safeOutput($key)] = Tools::safeOutput($value);
  560. }
  561. $excluded_key = array('isolang', 'id_lang', 'controller', 'fc', 'id_product', 'id_category', 'id_manufacturer', 'id_supplier', 'id_cms');
  562. foreach ($_GET as $key => $value)
  563. if (!in_array($key, $excluded_key) && Validate::isUrl($key) && Validate::isUrl($value))
  564. $params[Tools::safeOutput($key)] = Tools::safeOutput($value);
  565. $str_params = http_build_query($params, '', '&');
  566. if (!empty($str_params))
  567. $final_url = preg_replace('/^([^?]*)?.*$/', '$1', $canonical_url).'?'.$str_params;
  568. else
  569. $final_url = preg_replace('/^([^?]*)?.*$/', '$1', $canonical_url);
  570. if (defined('_PS_MODE_DEV_') && _PS_MODE_DEV_ && $_SERVER['REQUEST_URI'] != __PS_BASE_URI__)
  571. die('[Debug] This page has moved<br />Please use the following URL instead: <a href="'.$final_url.'">'.$final_url.'</a>');
  572. header('HTTP/1.0 301 Moved');
  573. header('Cache-Control: no-cache');
  574. Tools::redirectLink($final_url);
  575. }
  576. }
  577. protected function geolocationManagement($default_country)
  578. {
  579. if (!in_array($_SERVER['SERVER_NAME'], array('localhost', '127.0.0.1')))
  580. {
  581. /* Check if Maxmind Database exists */
  582. if (file_exists(_PS_GEOIP_DIR_.'GeoLiteCity.dat'))
  583. {
  584. if (!isset($this->context->cookie->iso_code_country) || (isset($this->context->cookie->iso_code_country) && !in_array(strtoupper($this->context->cookie->iso_code_country), explode(';', Configuration::get('PS_ALLOWED_COUNTRIES')))))
  585. {
  586. include_once(_PS_GEOIP_DIR_.'geoipcity.inc');
  587. $gi = geoip_open(realpath(_PS_GEOIP_DIR_.'GeoLiteCity.dat'), GEOIP_STANDARD);
  588. $record = geoip_record_by_addr($gi, Tools::getRemoteAddr());
  589. if (is_object($record))
  590. {
  591. if (!in_array(strtoupper($record->country_code), explode(';', Configuration::get('PS_ALLOWED_COUNTRIES'))) && !FrontController::isInWhitelistForGeolocation())
  592. {
  593. if (Configuration::get('PS_GEOLOCATION_BEHAVIOR') == _PS_GEOLOCATION_NO_CATALOG_)
  594. $this->restrictedCountry = true;
  595. elseif (Configuration::get('PS_GEOLOCATION_BEHAVIOR') == _PS_GEOLOCATION_NO_ORDER_)
  596. $this->context->smarty->assign(array(
  597. 'restricted_country_mode' => true,
  598. 'geolocation_country' => $record->country_name
  599. ));
  600. }
  601. else
  602. {
  603. $has_been_set = !isset($this->context->cookie->iso_code_country);
  604. $this->context->cookie->iso_code_country = strtoupper($record->country_code);
  605. }
  606. }
  607. }
  608. if (isset($this->context->cookie->iso_code_country) && ($id_country = Country::getByIso(strtoupper($this->context->cookie->iso_code_country))))
  609. {
  610. /* Update defaultCountry */
  611. if ($default_country->iso_code != $this->context->cookie->iso_code_country)
  612. $default_country = new Country($id_country);
  613. if (isset($has_been_set) && $has_been_set)
  614. $this->context->cookie->id_currency = (int)(Currency::getCurrencyInstance($default_country->id_currency ? (int)$default_country->id_currency : Configuration::get('PS_CURRENCY_DEFAULT'))->id);
  615. return $default_country;
  616. }
  617. elseif (Configuration::get('PS_GEOLOCATION_NA_BEHAVIOR') == _PS_GEOLOCATION_NO_CATALOG_ && !FrontController::isInWhitelistForGeolocation())
  618. $this->restrictedCountry = true;
  619. elseif (Configuration::get('PS_GEOLOCATION_NA_BEHAVIOR') == _PS_GEOLOCATION_NO_ORDER_ && !FrontController::isInWhitelistForGeolocation())
  620. $this->context->smarty->assign(array(
  621. 'restricted_country_mode' => true,
  622. 'geolocation_country' => 'Undefined'
  623. ));
  624. }
  625. /* If not exists we disabled the geolocation feature */
  626. else
  627. Configuration::updateValue('PS_GEOLOCATION_ENABLED', 0);
  628. }
  629. return false;
  630. }
  631. /**
  632. * Specific medias for mobile device.
  633. */
  634. public function setMobileMedia()
  635. {
  636. $this->addjquery();
  637. $this->addJS(_THEME_MOBILE_JS_DIR_.'jquery.mobile-1.1.1.min.js');
  638. $this->addJS(_THEME_MOBILE_JS_DIR_.'jqm-docs.js');
  639. $this->addJS(_PS_JS_DIR_.'tools.js');
  640. $this->addJS(_THEME_MOBILE_JS_DIR_.'global.js');
  641. $this->addjqueryPlugin('fancybox');
  642. $this->addCSS(_THEME_MOBILE_CSS_DIR_.'jquery.mobile-1.1.1.min.css', 'all');
  643. $this->addCSS(_THEME_MOBILE_CSS_DIR_.'jqm-docs.css', 'all');
  644. $this->addCSS(_THEME_MOBILE_CSS_DIR_.'global.css', 'all');
  645. }
  646. public function setMedia()
  647. {
  648. // if website is accessed by mobile device
  649. // @see FrontControllerCore::setMobileMedia()
  650. if ($this->context->getMobileDevice() != false)
  651. {
  652. $this->setMobileMedia();
  653. return true;
  654. }
  655. $this->addCSS(_THEME_CSS_DIR_.'global.css', 'all');
  656. $this->addjquery();
  657. $this->addjqueryPlugin('easing');
  658. $this->addJS(_PS_JS_DIR_.'tools.js');
  659. if (Tools::isSubmit('live_edit') && Tools::getValue('ad') && Tools::getAdminToken('AdminModulesPositions'.(int)Tab::getIdFromClassName('AdminModulesPositions').(int)Tools::getValue('id_employee')))
  660. {
  661. $this->addJqueryUI('ui.sortable');
  662. $this->addjqueryPlugin('fancybox');
  663. $this->addJS(_PS_JS_DIR_.'hookLiveEdit.js');
  664. $this->addCSS(_PS_CSS_DIR_.'jquery.fancybox-1.3.4.css', 'all'); // @TODO
  665. }
  666. if ($this->context->language->is_rtl)
  667. $this->addCSS(_THEME_CSS_DIR_.'rtl.css');
  668. // Execute Hook FrontController SetMedia
  669. Hook::exec('actionFrontControllerSetMedia', array());
  670. }
  671. public function initHeader()
  672. {
  673. // P3P Policies (http://www.w3.org/TR/2002/REC-P3P-20020416/#compact_policies)
  674. header('P3P: CP="IDC DSP COR CURa ADMa OUR IND PHY ONL COM STA"');
  675. /* Hooks are volontary out the initialize array (need those variables already assigned) */
  676. $this->context->smarty->assign(array(
  677. 'time' => time(),
  678. 'img_update_time' => Configuration::get('PS_IMG_UPDATE_TIME'),
  679. 'static_token' => Tools::getToken(false),
  680. 'token' => Tools::getToken(),
  681. 'logo_image_width' => Configuration::get('SHOP_LOGO_WIDTH'),
  682. 'logo_image_height' => Configuration::get('SHOP_LOGO_HEIGHT'),
  683. 'priceDisplayPrecision' => _PS_PRICE_DISPLAY_PRECISION_,
  684. 'content_only' => (int)Tools::getValue('content_only'),
  685. 'logo_url' => _PS_IMG_.Configuration::get('PS_LOGO').'?'.Configuration::get('PS_IMG_UPDATE_TIME'),
  686. 'favicon_url' => _PS_IMG_.Configuration::get('PS_FAVICON'),
  687. ));
  688. }
  689. public function initFooter()
  690. {
  691. $this->context->smarty->assign(array(
  692. 'HOOK_FOOTER' => Hook::exec('displayFooter'),
  693. 'conditions' => Configuration::get('PS_CONDITIONS'),
  694. 'id_cgv' => Configuration::get('PS_CONDITIONS_CMS_ID'),
  695. 'PS_SHOP_NAME' => Configuration::get('PS_SHOP_NAME'),
  696. 'PS_ALLOW_MOBILE_DEVICE' => isset($_SERVER['HTTP_USER_AGENT']) && (bool)Configuration::get('PS_ALLOW_MOBILE_DEVICE') && @filemtime(_PS_THEME_MOBILE_DIR_)
  697. ));
  698. }
  699. public function getLiveEditFooter()
  700. {
  701. if (Tools::isSubmit('live_edit')
  702. && ($ad = Tools::getValue('ad'))
  703. && Tools::getAdminToken('AdminModulesPositions'.(int)Tab::getIdFromClassName('AdminModulesPositions').(int)Tools::getValue('id_employee')))
  704. {
  705. $data = $this->context->smarty->createData();
  706. $data->assign(array(
  707. 'ad' => $ad,
  708. 'live_edit' => true,
  709. 'hook_list' => Hook::$executed_hooks,
  710. 'id_shop' => $this->context->shop->id
  711. ));
  712. return $this->context->smarty->createTemplate(_PS_ALL_THEMES_DIR_.'live_edit.tpl', $data)->fetch();
  713. }
  714. else
  715. return '';
  716. }
  717. public function productSort()
  718. {
  719. // $this->orderBy = Tools::getProductsOrder('by', Tools::getValue('orderby'));
  720. // $this->orderWay = Tools::getProductsOrder('way', Tools::getValue('orderway'));
  721. // 'orderbydefault' => Tools::getProductsOrder('by'),
  722. // 'orderwayposition' => Tools::getProductsOrder('way'), // Deprecated: orderwayposition
  723. // 'orderwaydefault' => Tools::getProductsOrder('way'),
  724. $stock_management = Configuration::get('PS_STOCK_MANAGEMENT') ? true : false; // no display quantity order if stock management disabled
  725. $order_by_values = array(0 => 'name', 1 => 'price', 2 => 'date_add', 3 => 'date_upd', 4 => 'position', 5 => 'manufacturer_name', 6 => 'quantity');
  726. $order_way_values = array(0 => 'asc', 1 => 'desc');
  727. $this->orderBy = Tools::strtolower(Tools::getValue('orderby', $order_by_values[(int)Configuration::get('PS_PRODUCTS_ORDER_BY')]));
  728. $this->orderWay = Tools::strtolower(Tools::getValue('orderway', $order_way_values[(int)Configuration::get('PS_PRODUCTS_ORDER_WAY')]));
  729. if (!in_array($this->orderBy, $order_by_values))
  730. $this->orderBy = $order_by_values[0];
  731. if (!in_array($this->orderWay, $order_way_values))
  732. $this->orderWay = $order_way_values[0];
  733. $this->context->smarty->assign(array(
  734. 'orderby' => $this->orderBy,
  735. 'orderway' => $this->orderWay,
  736. 'orderbydefault' => $order_by_values[(int)Configuration::get('PS_PRODUCTS_ORDER_BY')],
  737. 'orderwayposition' => $order_way_values[(int)Configuration::get('PS_PRODUCTS_ORDER_WAY')], // Deprecated: orderwayposition
  738. 'orderwaydefault' => $order_way_values[(int)Configuration::get('PS_PRODUCTS_ORDER_WAY')],
  739. 'stock_management' => (int)$stock_management));
  740. }
  741. public function pagination($nbProducts = 10)
  742. {
  743. if (!self::$initialized)
  744. $this->init();
  745. elseif (!$this->context)
  746. $this->context = Context::getContext();
  747. $nArray = (int)Configuration::get('PS_PRODUCTS_PER_PAGE') != 10 ? array((int)Configuration::get('PS_PRODUCTS_PER_PAGE'), 10, 20, 50) : array(10, 20, 50);
  748. // Clean duplicate values
  749. $nArray = array_unique($nArray);
  750. asort($nArray);
  751. $this->n = abs((int)(Tools::getValue('n', ((isset($this->context->cookie->nb_item_per_page) && $this->context->cookie->nb_item_per_page >= 10) ? $this->context->cookie->nb_item_per_page : (int)Configuration::get('PS_PRODUCTS_PER_PAGE')))));
  752. $this->p = abs((int)Tools::getValue('p', 1));
  753. if (!is_numeric(Tools::getValue('p', 1)) || Tools::getValue('p', 1) < 0)
  754. {
  755. $this->redirect_after = '404';
  756. $this->redirect();
  757. }
  758. $current_url = tools::htmlentitiesUTF8($_SERVER['REQUEST_URI']);
  759. //delete parameter page
  760. $current_url = preg_replace('/(\?)?(&amp;)?p=\d+/', '$1', $current_url);
  761. $range = 2; /* how many pages around page selected */
  762. if ($this->p < 0)
  763. $this->p = 0;
  764. if (isset($this->context->cookie->nb_item_per_page) && $this->n != $this->context->cookie->nb_item_per_page && in_array($this->n, $nArray))
  765. $this->context->cookie->nb_item_per_page = $this->n;
  766. if ($this->p > (($nbProducts / $this->n) + 1))
  767. {
  768. $this->redirect_after = preg_replace('/[&?]p=\d+/', '', $_SERVER['REQUEST_URI']);
  769. $this->redirect();
  770. }
  771. $pages_nb = ceil($nbProducts / (int)$this->n);
  772. $start = (int)($this->p - $range);
  773. if ($start < 1)
  774. $start = 1;
  775. $stop = (int)($this->p + $range);
  776. if ($stop > $pages_nb)
  777. $stop = (int)$pages_nb;
  778. $this->context->smarty->assign('nb_products', $nbProducts);
  779. $pagination_infos = array(
  780. 'products_per_page' => (int)Configuration::get('PS_PRODUCTS_PER_PAGE'),
  781. 'pages_nb' => $pages_nb,
  782. 'p' => $this->p,
  783. 'n' => $this->n,
  784. 'nArray' => $nArray,
  785. 'range' => $range,
  786. 'start' => $start,
  787. 'stop' => $stop,
  788. 'current_url' => $current_url
  789. );
  790. $this->context->smarty->assign($pagination_infos);
  791. }
  792. public static function getCurrentCustomerGroups()
  793. {
  794. if (!Group::isFeatureActive())
  795. return array();
  796. $context = Context::getContext();
  797. if (!isset($context->customer) || !$context->customer->id)
  798. return array();
  799. if (!is_array(self::$currentCustomerGroups))
  800. {
  801. self::$currentCustomerGroups = array();
  802. $result = Db::getInstance()->executeS('SELECT id_group FROM '._DB_PREFIX_.'customer_group WHERE id_customer = '.(int)$context->customer->id);
  803. foreach ($result as $row)
  804. self::$currentCustomerGroups[] = $row['id_group'];
  805. }
  806. return self::$currentCustomerGroups;
  807. }
  808. protected static function isInWhitelistForGeolocation()
  809. {
  810. $allowed = false;
  811. $userIp = Tools::getRemoteAddr();
  812. $ips = array();
  813. // retrocompatibility
  814. $ips_old = explode(';', Configuration::get('PS_GEOLOCATION_WHITELIST'));
  815. if (is_array($ips_old) && count($ips_old))
  816. foreach ($ips_old as $ip)
  817. $ips = array_merge($ips, explode("\n", $ip));
  818. if (is_array($ips) && count($ips))
  819. foreach ($ips as $ip)
  820. if (!empty($ip) && strpos($userIp, $ip) === 0)
  821. $allowed = true;
  822. return $allowed;
  823. }
  824. /**
  825. * Check if token is valid
  826. *
  827. * @since 1.5.0
  828. * @return bool
  829. */
  830. public function isTokenValid()
  831. {
  832. if (!Configuration::get('PS_TOKEN_ENABLE'))
  833. return true;
  834. return (strcasecmp(Tools::getToken(false), Tools::getValue('token')) == 0);
  835. }
  836. /**
  837. * Add one or several CSS for front, checking if css files are overriden in theme/css/modules/ directory
  838. *
  839. * @see Controller::addCSS()
  840. */
  841. public function addCSS($css_uri, $css_media_type = 'all')
  842. {
  843. if (!is_array($css_uri))
  844. $css_uri = array($css_uri => $css_media_type);
  845. $list_uri = array();
  846. foreach ($css_uri as $file => $media)
  847. {
  848. $different = 0;
  849. $override_path = str_replace(__PS_BASE_URI__.'modules/', _PS_ROOT_DIR_.'/themes/'._THEME_NAME_.'/css/modules/', $file, $different);
  850. if ($different && file_exists($override_path))
  851. $file = str_replace(__PS_BASE_URI__.'modules/', __PS_BASE_URI__.'themes/'._THEME_NAME_.'/css/modules/', $file, $different);
  852. $list_uri[$file] = $media;
  853. }
  854. return parent::addCSS($list_uri, $css_media_type);
  855. }
  856. /**
  857. * Add one or several JS files for front, checking if js files are overriden in theme/js/modules/ directory
  858. *
  859. * @see Controller::addJS()
  860. */
  861. public function addJS($js_uri)
  862. {
  863. if (!is_array($js_uri))
  864. $js_uri = array($js_uri);
  865. foreach ($js_uri as $key => &$file)
  866. {
  867. if (!preg_match('/^http(s?):\/\//i', $file))
  868. {
  869. $different = 0;
  870. $override_path = str_replace(__PS_BASE_URI__.'modules/', _PS_ROOT_DIR_.'/themes/'._THEME_NAME_.'/js/modules/', $file, $different);
  871. if ($different && file_exists($override_path))
  872. $file = str_replace(__PS_BASE_URI__.'modules/', __PS_BASE_URI__.'themes/'._THEME_NAME_.'/js/modules/', $file, $different);
  873. }
  874. }
  875. return parent::addJS($js_uri);
  876. }
  877. protected function recoverCart()
  878. {
  879. if (($id_cart = (int)Tools::getValue('recover_cart')) && Tools::getValue('token_cart') == md5(_COOKIE_KEY_.'recover_cart_'.$id_cart))
  880. {
  881. $cart = new Cart((int)$id_cart);
  882. if (Validate::isLoadedObject($cart))
  883. {
  884. $customer = new Customer((int)$cart->id_customer);
  885. if (Validate::isLoadedObject($customer))
  886. {
  887. $this->context->cookie->id_customer = (int)$customer->id;
  888. $this->context->cookie->customer_lastname = $customer->lastname;
  889. $this->context->cookie->customer_firstname = $customer->firstname;
  890. $this->context->cookie->logged = 1;
  891. $this->context->cookie->is_guest = $customer->isGuest();
  892. $this->context->cookie->passwd = $customer->passwd;
  893. $this->context->cookie->email = $customer->email;
  894. return $id_cart;
  895. }
  896. }
  897. }
  898. else
  899. return false;
  900. }
  901. /**
  902. * This is overrided to manage is behaviour
  903. * if a customer access to the site with mobile device.
  904. */
  905. public function setTemplate($default_template)
  906. {
  907. if ($this->context->getMobileDevice() != false)
  908. $this->setMobileTemplate($default_template);
  909. else
  910. {
  911. $template = $this->getOverrideTemplate();
  912. if ($template)
  913. parent::setTemplate($template);
  914. else
  915. parent::setTemplate($default_template);
  916. }
  917. }
  918. /**
  919. * Returns the template corresponding to the current page.
  920. * By default this method return false but could easily be overridden in a specific controller
  921. *
  922. * @since 1.5
  923. * @return bool
  924. */
  925. public function getOverrideTemplate()
  926. {
  927. return Hook::exec('DisplayOverrideTemplate', array('controller' => $this));
  928. }
  929. /**
  930. * Returns the layout corresponding to the current page by using the override system
  931. * Ex:
  932. * On the url: http://localhost/index.php?id_product=1&controller=product, this method will
  933. * check if the layout exists in the following files (in that order), and return the first found:
  934. * - /themes/default/override/layout-product-1.tpl
  935. * - /themes/default/override/layout-product.tpl
  936. * - /themes/default/layout.tpl
  937. *
  938. * @since 1.5
  939. * @return bool|string
  940. */
  941. public function getLayout()
  942. {
  943. $entity = Tools::getValue('controller');
  944. $id_item = (int)Tools::getValue('id_'.$entity);
  945. $layout_dir = _PS_THEME_DIR_;
  946. $layout_override_dir = _PS_THEME_OVERRIDE_DIR_;
  947. if ($this->context->getMobileDevice() != false)
  948. {
  949. $layout_dir = _PS_THEME_MOBILE_DIR_;
  950. $layout_override_dir = _PS_THEME_MOBILE_OVERRIDE_DIR_;
  951. }
  952. $layout = false;
  953. if ($entity)
  954. {
  955. if ($id_item > 0 && file_exists($layout_override_dir.'layout-'.$entity.'-'.$id_item.'.tpl'))
  956. $layout = $layout_override_dir.'layout-'.$entity.'-'.$id_item.'.tpl';
  957. elseif (file_exists($layout_override_dir.'layout-'.$entity.'.tpl'))
  958. $layout = $layout_override_dir.'layout-'.$entity.'.tpl';
  959. }
  960. if (!$layout && file_exists($layout_dir.'layout.tpl'))
  961. $layout = $layout_dir.'layout.tpl';
  962. return $layout;
  963. }
  964. /**
  965. * This checks if the template set is available for mobile themes,
  966. * otherwise the front template is choosen.
  967. */
  968. public function setMobileTemplate($template)
  969. {
  970. // Needed for site map
  971. $blockmanufacturer = Module::getInstanceByName('blockmanufacturer');
  972. $blocksupplier = Module::getInstanceByName('blocksupplier');
  973. $this->context->smarty->assign('categoriesTree', Category::getRootCategory()->recurseLiteCategTree(0));
  974. $this->context->smarty->assign('categoriescmsTree', CMSCategory::getRecurseCategory($this->context->language->id, 1, 1, 1));
  975. $this->context->smarty->assign('voucherAllowed', (int)CartRule::isFeatureActive());
  976. $this->context->smarty->assign('display_manufacturer_link', (bool)$blockmanufacturer->active);
  977. $this->context->smarty->assign('display_supplier_link', (bool)$blocksupplier->active);
  978. $this->context->smarty->assign('PS_DISPLAY_SUPPLIERS', Configuration::get('PS_DISPLAY_SUPPLIERS'));
  979. $this->context->smarty->assign('display_store', Configuration::get('PS_STORES_DISPLAY_SITEMAP'));
  980. $this->context->smarty->assign('conditions', Configuration::get('PS_CONDITIONS'));
  981. $this->context->smarty->assign('id_cgv', Configuration::get('PS_CONDITIONS_CMS_ID'));
  982. $this->context->smarty->assign('PS_SHOP_NAME', Configuration::get('PS_SHOP_NAME'));
  983. $mobile_template = '';
  984. $tpl_file = basename($template);
  985. $dirname = dirname($template).(substr(dirname($template), -1, 1) == '/' ? '' : '/');
  986. if ($dirname == _PS_THEME_DIR_)
  987. {
  988. if (file_exists(_PS_THEME_MOBILE_DIR_.$tpl_file))
  989. $template = _PS_THEME_MOBILE_DIR_.$tpl_file;
  990. }
  991. elseif ($dirname == _PS_THEME_MOBILE_DIR_)
  992. {
  993. if (!file_exists(_PS_THEME_MOBILE_DIR_.$tpl_file) && file_exists(_PS_THEME_DIR_.$tpl_file))
  994. $template = _PS_THEME_DIR_.$tpl_file;
  995. }
  996. $assign = array();
  997. $assign['tpl_file'] = basename($tpl_file, '.tpl');
  998. if (isset($this->php_self))
  999. $assign['controller_name'] = $this->php_self;
  1000. $this->context->smarty->assign($assign);
  1001. $this->template = $template;
  1002. }
  1003. }