PageRenderTime 82ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/core/Controllers/FrontendController.php

https://bitbucket.org/icoa/bladecommerce-core
PHP | 443 lines | 349 code | 66 blank | 28 comment | 73 complexity | 0feea39a209dc70d096960f17c2f557b MD5 | raw file
  1. <?php
  2. /*
  3. * This is a N3m3s7s creation
  4. * @author Fabio Politi <f.politi@icoa.it at icoa.it>
  5. * @created 12-giu-2013 15.08.42
  6. */
  7. class FrontendController extends BaseController
  8. {
  9. protected $themeFolder = 'frontend';
  10. protected $theme;
  11. protected $view = '';
  12. protected $cookie = null;
  13. protected $segments = null;
  14. protected $scope_id = null;
  15. protected $asset_min = false;
  16. protected $asset_versioning = false;
  17. protected $redirect_url;
  18. /** @var Illuminate\Http\Response */
  19. protected $response;
  20. function __construct($repository = null)
  21. {
  22. $this->mobile();
  23. Session::set('theme', $this->themeFolder);
  24. $layout = 'default';
  25. if (Request::ajax()) {
  26. $layout = 'ajax';
  27. }
  28. $this->theme = Theme::theme($this->themeFolder)->layout($layout);
  29. \Config::set('theme::assetUrl', Theme::getConfig('assetUrl'));
  30. $this->asset_min = Theme::getConfig('assetUseMin');
  31. if (Input::get('assetUseMin') == 'false') {
  32. $this->asset_min = false;
  33. }
  34. $this->asset_versioning = Theme::getConfig('assetVersioning');
  35. \FrontTpl::setTheme($this->theme);
  36. \FrontTpl::setController($this);
  37. $this->_assets();
  38. $this->handleCampaign();
  39. $this->handleB2B();
  40. $this->handleCustomerGroup();
  41. parent::__construct();
  42. }
  43. function setSegments($segments)
  44. {
  45. $this->segments = $segments;
  46. }
  47. function setScopeId($scope_id)
  48. {
  49. $this->scope_id = $scope_id;
  50. }
  51. function getScopeId()
  52. {
  53. return $this->scope_id;
  54. }
  55. function route()
  56. {
  57. $verb = \Request::getMethod();
  58. $params = [];
  59. if (count($this->segments) == 0) {
  60. $method = 'index';
  61. } else {
  62. $method = $this->segments[0];
  63. $params = array_slice($this->segments, 1);
  64. }
  65. $method_name = strtolower($verb) . ucfirst($method);
  66. if (!method_exists($this, $method_name)) {
  67. $method_name = 'getIndex';
  68. }
  69. return call_user_func_array(array($this, $method_name), $params);
  70. }
  71. protected function setupLayout()
  72. {
  73. parent::setupLayout();
  74. }
  75. protected function setLayout($layout)
  76. {
  77. $this->theme->layout($layout);
  78. }
  79. function getTheme()
  80. {
  81. return $this->theme;
  82. }
  83. function hasView($view)
  84. {
  85. return \View::exists("theme.{$this->themeFolder}::views.$view");
  86. }
  87. function handleCampaign()
  88. {
  89. $cmpCode = (int)Input::get('cmpCode', Input::get('cmpcode', ''));
  90. if ($cmpCode > 0) {
  91. $campaign = \Campaign::getObj($cmpCode);
  92. if ($campaign !== null and $campaign->id > 0 and $campaign->active == 1) {
  93. Session::put('blade_campaign_id', $campaign->id);
  94. $cookie = \Cookie::make('blade_campaign_id', $campaign->id, 60 * 24 * 7);
  95. $this->cookie = $cookie;
  96. }
  97. }
  98. }
  99. function handleCustomerGroup()
  100. {
  101. if (!Input::has('grpCode') && !Input::has('grpcode')) {
  102. return;
  103. }
  104. $found = false;
  105. $grpCode = (int)Input::get('grpCode', Input::get('grpcode', ''));
  106. if ($grpCode > 0) {
  107. $group = CustomerGroup::getObj($grpCode);
  108. if ($group !== null and $group->id > 0) {
  109. Session::put('blade_customer_group_id', $group->id);
  110. $found = true;
  111. }
  112. }
  113. if (false === $found) {
  114. $grpCode = trim((string)Input::get('grpCode', Input::get('grpcode', '')));
  115. if ($grpCode !== '') {
  116. $group = CustomerGroup::findByCode(Str::upper($grpCode));
  117. if ($group !== null and $group->id > 0) {
  118. Session::put('blade_customer_group_id', $group->id);
  119. $found = true;
  120. }
  121. }
  122. }
  123. if (false === $found) {
  124. Session::forget('blade_customer_group_id');
  125. }
  126. }
  127. function handleB2B()
  128. {
  129. $b2b = (int)Input::get('b2b', 0);
  130. if ($b2b === 1) {
  131. Session::put('blade_b2b', 1);
  132. }
  133. $empty_cart = (int)Input::get('empty_cart', 0);
  134. if ($empty_cart === 1) {
  135. Session::forget('blade_auth');
  136. \CartManager::forgetCheckout(true);
  137. $this->redirect_url = \Link::to('nav', 13);
  138. }
  139. }
  140. function render($data, $statusCode = 200)
  141. {
  142. //\Utils::log(__METHOD__);
  143. \FrontTpl::bindData();
  144. $url = \FrontTpl::checkCountryRedirect();
  145. if ($url) {
  146. //\Utils::log("Redirect by country rules", __METHOD__);
  147. return \Redirect::to($url, 302);
  148. }
  149. if ($this->redirect_url) {
  150. return \Redirect::to($this->redirect_url, 302);
  151. }
  152. $response = $this->theme->scope($this->view, $data);
  153. if ($this->cookie != null) {
  154. $response->withCookie($this->cookie);
  155. }
  156. $this->response = $response->render($statusCode);
  157. $this->loadPlugins();
  158. $this->attachHeaders();
  159. return $this->response;
  160. }
  161. protected function toHead($asset, $depends = [], $attributes = [])
  162. {
  163. $key = Str::camel($asset);
  164. if (Str::startsWith($asset, ['http', '//'])) {
  165. $this->theme->asset()->add($key, $asset, $depends, $attributes);
  166. } else {
  167. $this->theme->asset()->usePath()->add($key, $asset, $depends, $attributes);
  168. }
  169. }
  170. protected function less($asset)
  171. {
  172. $path = \Theme::path();
  173. $assetTarget = str_replace(['css', '.less'], ['cache', '.css'], $asset);
  174. $file = $path . "/assets/" . $asset;
  175. if (file_exists($file)) {
  176. if (Config::get('app.debug')) {
  177. try {
  178. touch($file);
  179. } catch (\Exception $e) {
  180. }
  181. }
  182. $less = new lessc;
  183. $target = $path . "/assets/" . $assetTarget;
  184. try {
  185. $less->checkedCompile($file, $target);
  186. return $this->toHead($assetTarget);
  187. } catch (\Exception $e) {
  188. \Utils::error($e->getMessage());
  189. \Utils::error($e->getTraceAsString());
  190. }
  191. }
  192. }
  193. public function toFooter($asset, $container = 'footer', $depends = [], $attributes = [])
  194. {
  195. $this->loadAsset($container, $asset, $depends, $attributes);
  196. }
  197. protected function toQueue($asset, $queueName = "echoBO")
  198. {
  199. $key = Str::camel($asset);
  200. $this->theme->asset()->queue($queueName)->usePath()->add($key, $asset);
  201. }
  202. private function loadAsset($container, $asset, $depends = [], $attributes = [])
  203. {
  204. $key = Str::camel($asset);
  205. if (Str::startsWith($asset, ['http', '//'])) {
  206. $this->theme->asset()->container($container)->add($key, $asset, $depends, $attributes);
  207. } else {
  208. $this->theme->asset()->container($container)->usePath()->add($key, $asset, $depends, $attributes);
  209. }
  210. }
  211. protected function loadPlugins()
  212. {
  213. $content = $this->response->getContent();
  214. $this->response->setContent(\Site::loadPlugins($content));
  215. }
  216. protected function _assets()
  217. {
  218. $version = $this->asset_versioning;
  219. if ($this->asset_min === true) {
  220. /*$browser = \FrontTpl::getBrowser();
  221. if ($browser) {
  222. if ($browser['browser_name'] == 'msie' AND (int)$browser['browser_number'] <= 9) {
  223. //$this->toHead("css/bootstrap.min.css");
  224. $this->toHead("css/ie1_blade_$version.css");
  225. $this->toHead("css/ie2_blade_$version.css");
  226. $this->toHead("css/ie3_blade_$version.css");
  227. //$this->loadAsset("defer", "js/blade_$version.min.js");
  228. $this->toFooter("js/blade_$version.min.js");
  229. return;
  230. }
  231. }*/
  232. if (Config::get('mobile', false) === false) {
  233. $this->toHead("css/blade_$version.min.css");
  234. }
  235. $main_js_params = ['defer' => 'true'];
  236. $sri = \Utils::getMetaSri($this->themeFolder, 'js');
  237. if (is_string($sri)) {
  238. $main_js_params['integrity'] = $sri;
  239. $main_js_params['crossorigin'] = 'anonymous';
  240. }
  241. $this->toHead("js/blade_$version.min.js", [], $main_js_params);
  242. return;
  243. }
  244. $assets = $this->theme->getConfig('assets');
  245. $files = $assets['head']();
  246. if (isset($files)) {
  247. foreach ($files as $asset) {
  248. if (substr($asset, -4) == 'less') {
  249. $this->less($asset);
  250. } else {
  251. $this->toHead($asset);
  252. }
  253. }
  254. }
  255. $files = $assets['footer']();
  256. if ($files) {
  257. foreach ($files as $asset) {
  258. $this->toHead($asset, [], ['defer' => 'true']);
  259. }
  260. }
  261. //enforce general assets to be compiled or included in the frontend
  262. $enforced = config('assets.enforce', []);
  263. foreach ($enforced as $item) {
  264. if (file_exists(public_path($item)))
  265. $this->toHead(Site::rootify($item), [], ['defer' => 'true']);
  266. }
  267. }
  268. protected function mobile()
  269. {
  270. \Config::set('mobile', false);
  271. if (\Cfg::get('MOBILE_ACTIVE', 0) == 0) return;
  272. $isTablet = $isMobile = 'NO';
  273. $savedTheme = Input::get('_theme', Session::get('_theme'));
  274. if ($savedTheme != null and $savedTheme != '') {
  275. if ($savedTheme == 'desktop') {
  276. $savedTheme = 'frontend';
  277. }
  278. if ($savedTheme == 'mobile') {
  279. $device = Session::get('_device');
  280. $isTablet = ($device == 'tablet') ? 'YES' : 'NO';
  281. $isMobile = ($device == 'mobile') ? 'YES' : 'NO';
  282. if ($isTablet == 'NO' and $isMobile == 'NO') {
  283. $isMobile = 'YES';
  284. }
  285. }
  286. } else {
  287. $isTablet = Frontend\Browser::isTablet();
  288. $isMobile = Frontend\Browser::isMobile();
  289. $savedTheme = 'frontend';
  290. }
  291. if ($isTablet == 'YES' or $isMobile == 'YES') {
  292. $class = 'dekstop';
  293. if ($isTablet) {
  294. $class = 'tablet';
  295. } elseif ($isMobile) {
  296. $class = 'mobile';
  297. }
  298. $savedTheme = 'mobile';
  299. \FrontTpl::addBodyClass($class);
  300. $this->themeFolder = $savedTheme;
  301. \Config::set('mobile', true);
  302. Session::set('_device', $class);
  303. } else {
  304. \Config::set('mobile', false);
  305. }
  306. Session::set('_theme', $savedTheme);
  307. }
  308. protected function attachHeaders()
  309. {
  310. /** skip under following conditions */
  311. if (Request::ajax())
  312. return;
  313. if (\Frontend\Browser::isBot()) {
  314. return;
  315. }
  316. if (\Frontend\Browser::is('YSlow') || \Frontend\Browser::is('PageSpeed')) {
  317. return;
  318. }
  319. $version = $this->asset_versioning;
  320. $storage_mode = 'session';
  321. // HTTP/2 PUSH RESOURCES
  322. if ($this->asset_min === true) {
  323. $theme = $this->theme->getThemeName();
  324. $flag_key = ($storage_mode === 'cookie')
  325. ? 'push_mtx_' . config('elasticquent.prefix') . '_' . \App::environment() . '_' . $theme
  326. : 'push_mtx_' . $theme;
  327. $skip_push = false;
  328. if ($storage_mode === 'cookie' && (string)Request::cookie($flag_key) === $version) {
  329. $skip_push = true;
  330. }
  331. if ($storage_mode === 'session' && (string)Request::session()->get($flag_key) === $version) {
  332. $skip_push = true;
  333. }
  334. $this->response->header('X-H2-Mode', $storage_mode)->header('X-H2-Push', $skip_push ? 'No' : 'Yes');
  335. if (false === $skip_push) {
  336. $asset_url = Theme::getConfig('assetUrl');
  337. $push_resources = [
  338. ["/themes/$theme/assets/css/blade_$version.min.css", 'style'],
  339. ["/themes/$theme/assets/js/blade_$version.min.js", 'script'],
  340. ];
  341. $links = [];
  342. foreach ($push_resources as $resource) {
  343. list($link, $type) = $resource;
  344. $sri = \Utils::getMetaSri($theme, $type);
  345. $uri = ($asset_url === '/' ? Site::rootify(\Utils::http2LinkUrlToRelativePath($link), false) : $asset_url . substr($link, 1));
  346. // we have a sri
  347. if (is_string($sri)) {
  348. $header = sprintf(
  349. '<%s>; rel=preload; integrity=%s; crossorigin=anonymous; as=%s',
  350. $uri,
  351. $sri,
  352. $type
  353. );
  354. } else {
  355. $header = sprintf(
  356. '<%s>; rel=preload; as=%s',
  357. $uri,
  358. $type
  359. );
  360. }
  361. //header('Link: ' . $header, false);
  362. $links[] = $header;
  363. }
  364. //audit(compact('asset_url', 'links'));
  365. $this->response->header('Link', implode(', ', $links), false);
  366. if ($storage_mode === 'cookie') {
  367. $this->response->withCookie(\Cookie::make($flag_key, $version, 60 * 24 * 7));
  368. }
  369. if ($storage_mode === 'session') {
  370. Session::put($flag_key, $version);
  371. }
  372. }
  373. }
  374. //$this->response->header('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0')->header('Pragma', 'cache');
  375. }
  376. }