PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/src/classes/XLite/Module/CDev/DrupalConnector/Drupal/Controller.php

https://github.com/ckdimka/core
PHP | 347 lines | 148 code | 52 blank | 147 comment | 13 complexity | cf530edb014948e01208eecd9805bebe MD5 | raw file
  1. <?php
  2. // vim: set ts=4 sw=4 sts=4 et:
  3. /**
  4. * LiteCommerce
  5. *
  6. * NOTICE OF LICENSE
  7. *
  8. * This source file is subject to the Open Software License (OSL 3.0)
  9. * that is bundled with this package in the file LICENSE.txt.
  10. * It is also available through the world-wide-web at this URL:
  11. * http://opensource.org/licenses/osl-3.0.php
  12. * If you did not receive a copy of the license and are unable to
  13. * obtain it through the world-wide-web, please send an email
  14. * to licensing@litecommerce.com so we can send you a copy immediately.
  15. *
  16. * PHP version 5.3.0
  17. *
  18. * @category LiteCommerce
  19. * @author Creative Development LLC <info@cdev.ru>
  20. * @copyright Copyright (c) 2011 Creative Development LLC <info@cdev.ru>. All rights reserved
  21. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  22. * @link http://www.litecommerce.com/
  23. * @see ____file_see____
  24. * @since 1.0.0
  25. */
  26. namespace XLite\Module\CDev\DrupalConnector\Drupal;
  27. /**
  28. * Controller
  29. *
  30. * @see ____class_see____
  31. * @since 1.0.0
  32. */
  33. class Controller extends \XLite\Module\CDev\DrupalConnector\Drupal\ADrupal
  34. {
  35. /**
  36. * Instance of the LC viewer
  37. *
  38. * @var \XLite\View\Controller
  39. * @see ____var_see____
  40. * @since 1.0.0
  41. */
  42. protected $viewer;
  43. /**
  44. * Flag to determine if some common actions are already performed
  45. *
  46. * @var boolean
  47. * @see ____var_see____
  48. * @since 1.0.0
  49. */
  50. protected $arePreinitialized = false;
  51. // ------------------------------ Menu callbacks -
  52. /**
  53. * Return page title
  54. *
  55. * @return string
  56. * @see ____func_see____
  57. * @since 1.0.0
  58. */
  59. public function getTitle()
  60. {
  61. // Perform some common actions
  62. $this->performCommonActions();
  63. return $this->getViewer()->getTitle();
  64. }
  65. /**
  66. * Update variables before they pass to the template
  67. *
  68. * @param array &$variables Array of variables
  69. *
  70. * @return void
  71. * @see ____func_see____
  72. * @since 1.0.0
  73. */
  74. public function updateTemplateVars(&$variables)
  75. {
  76. // Get page title for current target
  77. $title = $this->getPageTitle();
  78. // Assign title variable if it's defined
  79. if (isset($title)) {
  80. $variables['title'] = $title;
  81. }
  82. }
  83. /**
  84. * Update meta tags array before this pass to the template
  85. *
  86. * @param array &$elements Array of meta tags
  87. *
  88. * @return void
  89. * @see ____func_see____
  90. * @since 1.0.0
  91. */
  92. public function updateMetaTags(&$elements)
  93. {
  94. $viewer = $this->getViewer();
  95. if ($viewer->getMetaDescription()) {
  96. $elements['lc_connector_meta_description'] = array(
  97. '#type' => 'html_tag',
  98. '#tag' => 'meta',
  99. '#attributes' => array(
  100. 'name' => 'description',
  101. 'content' => htmlspecialchars($viewer->getMetaDescription(), ENT_QUOTES, 'UTF-8'),
  102. ),
  103. );
  104. }
  105. if ($viewer->getKeywords()) {
  106. $elements['lc_connector_meta_keywords'] = array(
  107. '#type' => 'html_tag',
  108. '#tag' => 'meta',
  109. '#attributes' => array(
  110. 'name' => 'keywords',
  111. 'content' => htmlspecialchars($viewer->getKeywords(), ENT_QUOTES, 'UTF-8'),
  112. ),
  113. );
  114. }
  115. }
  116. /**
  117. * Return content for central region
  118. *
  119. * @return string
  120. * @see ____func_see____
  121. * @since 1.0.0
  122. */
  123. public function getContent()
  124. {
  125. // Perform some common actions
  126. $this->performCommonActions();
  127. // Current viewer
  128. $viewer = $this->getViewer();
  129. $content = $viewer->getContent();
  130. $this->registerResources($viewer);
  131. $title = $this->getTitle();
  132. $trail = array();
  133. if ($viewer->isTitleVisible() && $title) {
  134. $trail[] = array(
  135. 'title' => $title,
  136. 'link_path' => '',
  137. 'localized_options' => array('html' => true),
  138. 'type' => 0,
  139. );
  140. }
  141. menu_set_active_trail($trail);
  142. // Set value for <title> tag
  143. drupal_set_title($viewer->getPageTitle());
  144. return $this->isAJAX() ? $this->displayAJAXContent($content) : $content;
  145. }
  146. /**
  147. * The "access callback"
  148. *
  149. * @return boolean
  150. * @see ____func_see____
  151. * @since 1.0.0
  152. */
  153. public function checkAccess()
  154. {
  155. return true;
  156. }
  157. // ------------------------------ Ancillary methods -
  158. /**
  159. * Return LC viewer for current controller
  160. *
  161. * @return \XLite\View\Controller
  162. * @see ____func_see____
  163. * @since 1.0.0
  164. */
  165. protected function getViewer()
  166. {
  167. if (!isset($this->viewer)) {
  168. $this->viewer = $this->getHandler()->getViewer();
  169. }
  170. return $this->viewer;
  171. }
  172. /**
  173. * Check if current request is an AJAX one
  174. *
  175. * @return boolean
  176. * @see ____func_see____
  177. * @since 1.0.0
  178. */
  179. protected function isAJAX()
  180. {
  181. return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && 'XMLHttpRequest' == $_SERVER['HTTP_X_REQUESTED_WITH'];
  182. }
  183. /**
  184. * Display content for the AJAX requests
  185. *
  186. * @param string $content Content to display
  187. *
  188. * @return void
  189. * @see ____func_see____
  190. * @since 1.0.0
  191. */
  192. protected function displayAJAXContent($content)
  193. {
  194. // Dispatch events
  195. \XLite\Core\Event::getInstance()->display();
  196. \XLite\Core\Event::getInstance()->clear();
  197. // Display content
  198. echo ('<h2 class="ajax-title-loadable">' . $this->getTitle() . '</h2>');
  199. echo ('<div class="ajax-container-loadable">' . $content . '</div>');
  200. exit (0);
  201. }
  202. /**
  203. * Set no-cache headers
  204. *
  205. * @return void
  206. * @see ____func_see____
  207. * @since 1.0.0
  208. */
  209. protected function setNocacheHeaders()
  210. {
  211. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
  212. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
  213. if (\XLite\Core\Request::getInstance()->isHTTPS()) {
  214. header('Cache-Control: private, must-revalidate');
  215. } else {
  216. header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
  217. header('Pragma: no-cache');
  218. }
  219. }
  220. /**
  221. * Set LC breadcrumbs
  222. *
  223. * @return void
  224. * @see ____func_see____
  225. * @since 1.0.0
  226. */
  227. protected function setBreadcrumbs()
  228. {
  229. $widget = $this->getHandler()->getWidget('\XLite\\View\\Location');
  230. $lcNodes = array_map(
  231. function (\XLite\View\Location\Node $node) {
  232. return $node->getContent();
  233. },
  234. $widget->getNodes()
  235. );
  236. array_shift($lcNodes);
  237. // Add store root node
  238. $trails = menu_get_active_trail();
  239. array_splice(
  240. $trails,
  241. 1,
  242. 0,
  243. array(
  244. array(
  245. 'title' => t('Store'),
  246. 'href' => \XLite\Core\Converter::buildFullURL(),
  247. 'link_path' => '',
  248. 'localized_options' => array(),
  249. 'type' => MENU_VISIBLE_IN_BREADCRUMB,
  250. ),
  251. )
  252. );
  253. menu_set_active_trail($trails);
  254. $drupalNodes = array_slice(drupal_get_breadcrumb(), 0, 2);
  255. drupal_set_breadcrumb(array_merge($drupalNodes, $lcNodes));
  256. if ($widget->getProtectedWidget()) {
  257. $this->registerResources($widget->getProtectedWidget());
  258. }
  259. }
  260. /**
  261. * Get page title
  262. *
  263. * @return string
  264. * @see ____func_see____
  265. * @since 1.0.0
  266. */
  267. protected function getPageTitle()
  268. {
  269. $title = null;
  270. $viewer = $this->getViewer();
  271. if (!$viewer->isTitleVisible()) {
  272. $title = '';
  273. } elseif ($viewer->getTitle()) {
  274. $title = $viewer->getTitle();
  275. }
  276. return $title;
  277. }
  278. /**
  279. * Common actions for "getTitle()" and "getContent()"
  280. *
  281. * @return void
  282. * @see ____func_see____
  283. * @since 1.0.0
  284. */
  285. protected function performCommonActions()
  286. {
  287. if (!$this->arePreinitialized) {
  288. // Set no-cache headers
  289. headers_sent() ?: $this->setNocacheHeaders();
  290. // Set LC breadcrumbs
  291. $this->setBreadcrumbs();
  292. $this->arePreinitialized = true;
  293. }
  294. }
  295. }