PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/Context.php

https://github.com/netplayer/PrestaShop
PHP | 237 lines | 118 code | 30 blank | 89 comment | 24 complexity | 6dc70f5b84ec5c1a37f36b1a4009825f MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /*
  3. * 2007-2014 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-2014 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. /**
  27. * @since 1.5.0
  28. */
  29. class ContextCore
  30. {
  31. /**
  32. * @var Context
  33. */
  34. protected static $instance;
  35. /**
  36. * @var Cart
  37. */
  38. public $cart;
  39. /**
  40. * @var Customer
  41. */
  42. public $customer;
  43. /**
  44. * @var Cookie
  45. */
  46. public $cookie;
  47. /**
  48. * @var Link
  49. */
  50. public $link;
  51. /**
  52. * @var Country
  53. */
  54. public $country;
  55. /**
  56. * @var Employee
  57. */
  58. public $employee;
  59. /**
  60. * @var Controller
  61. */
  62. public $controller;
  63. /**
  64. * @var string
  65. */
  66. public $override_controller_name_for_translations;
  67. /**
  68. * @var Language
  69. */
  70. public $language;
  71. /**
  72. * @var Currency
  73. */
  74. public $currency;
  75. /**
  76. * @var AdminTab
  77. */
  78. public $tab;
  79. /**
  80. * @var Shop
  81. */
  82. public $shop;
  83. /**
  84. * @var Theme
  85. */
  86. public $theme;
  87. /**
  88. * @var Smarty
  89. */
  90. public $smarty;
  91. /**
  92. * @ var Mobile Detect
  93. */
  94. public $mobile_detect;
  95. /**
  96. * @var boolean|string mobile device of the customer
  97. */
  98. protected $mobile_device = null;
  99. const DEVICE_COMPUTER = 1;
  100. const DEVICE_TABLET = 2;
  101. const DEVICE_MOBILE = 4;
  102. public function getMobileDetect()
  103. {
  104. if ($this->mobile_detect === null)
  105. {
  106. require_once(_PS_TOOL_DIR_.'mobile_Detect/Mobile_Detect.php');
  107. $this->mobile_detect = new Mobile_Detect();
  108. }
  109. return $this->mobile_detect;
  110. }
  111. public function getMobileDevice()
  112. {
  113. if ($this->mobile_device === null)
  114. {
  115. $this->mobile_device = false;
  116. if ($this->checkMobileContext())
  117. {
  118. if (isset(Context::getContext()->cookie->no_mobile) && Context::getContext()->cookie->no_mobile == false AND (int)Configuration::get('PS_ALLOW_MOBILE_DEVICE') != 0)
  119. $this->mobile_device = true;
  120. else
  121. {
  122. $mobile_detect = $this->getMobileDetect();
  123. switch ((int)Configuration::get('PS_ALLOW_MOBILE_DEVICE'))
  124. {
  125. case 1: // Only for mobile device
  126. if ($mobile_detect->isMobile() && !$mobile_detect->isTablet())
  127. $this->mobile_device = true;
  128. break;
  129. case 2: // Only for touchpads
  130. if ($mobile_detect->isTablet() && !$mobile_detect->isMobile())
  131. $this->mobile_device = true;
  132. break;
  133. case 3: // For touchpad or mobile devices
  134. if ($mobile_detect->isMobile() || $mobile_detect->isTablet())
  135. $this->mobile_device = true;
  136. break;
  137. }
  138. }
  139. }
  140. }
  141. return $this->mobile_device;
  142. }
  143. public function getDevice()
  144. {
  145. static $device = null;
  146. if ($device === null)
  147. {
  148. $mobile_detect = $this->getMobileDetect();
  149. if ($mobile_detect->isTablet())
  150. $device = Context::DEVICE_TABLET;
  151. elseif ($mobile_detect->isMobile())
  152. $device = Context::DEVICE_MOBILE;
  153. else
  154. $device = Context::DEVICE_COMPUTER;
  155. }
  156. return $device;
  157. }
  158. protected function checkMobileContext()
  159. {
  160. // Check mobile context
  161. if (Tools::isSubmit('no_mobile_theme'))
  162. {
  163. Context::getContext()->cookie->no_mobile = true;
  164. if (Context::getContext()->cookie->id_guest)
  165. {
  166. $guest = new Guest(Context::getContext()->cookie->id_guest);
  167. $guest->mobile_theme = false;
  168. $guest->update();
  169. }
  170. }
  171. elseif (Tools::isSubmit('mobile_theme_ok'))
  172. {
  173. Context::getContext()->cookie->no_mobile = false;
  174. if (Context::getContext()->cookie->id_guest)
  175. {
  176. $guest = new Guest(Context::getContext()->cookie->id_guest);
  177. $guest->mobile_theme = true;
  178. $guest->update();
  179. }
  180. }
  181. return isset($_SERVER['HTTP_USER_AGENT'])
  182. && isset(Context::getContext()->cookie)
  183. && (bool)Configuration::get('PS_ALLOW_MOBILE_DEVICE')
  184. && @filemtime(_PS_THEME_MOBILE_DIR_)
  185. && !Context::getContext()->cookie->no_mobile;
  186. }
  187. /**
  188. * Get a singleton context
  189. *
  190. * @return Context
  191. */
  192. public static function getContext()
  193. {
  194. if (!isset(self::$instance))
  195. self::$instance = new Context();
  196. return self::$instance;
  197. }
  198. /**
  199. * Clone current context
  200. *
  201. * @return Context
  202. */
  203. public function cloneContext()
  204. {
  205. return clone($this);
  206. }
  207. }