PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/blocks/core_stack_display/controller.php

http://github.com/concrete5/concrete5
PHP | 304 lines | 240 code | 44 blank | 20 comment | 37 complexity | 96715175aeee80cf7e56e50eb81b2417 MD5 | raw file
Possible License(s): MIT, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. <?php
  2. namespace Concrete\Block\CoreStackDisplay;
  3. use Concrete\Core\Statistics\UsageTracker\TrackableInterface;
  4. use Concrete\Core\Support\Facade\Application;
  5. use Stack;
  6. use Permissions;
  7. use Page;
  8. use Concrete\Core\Block\BlockController;
  9. use Concrete\Core\Multilingual\Page\Section\Section;
  10. /**
  11. * The controller for the stack display block. This is an internal proxy block that is inserted when a stack's contents are displayed in a page.
  12. *
  13. * @package Blocks
  14. * @subpackage Core Stack Display
  15. *
  16. * @author Andrew Embler <andrew@concrete5.org>
  17. * @copyright Copyright (c) 2003-2012 Concrete5. (http://www.concrete5.org)
  18. * @license http://www.concrete5.org/license/ MIT License
  19. */
  20. class Controller extends BlockController implements TrackableInterface
  21. {
  22. protected $btCacheBlockRecord = true;
  23. protected $btTable = 'btCoreStackDisplay';
  24. protected $btIsInternal = true;
  25. protected $btCacheSettingsInitialized = false;
  26. public $stID = null;
  27. protected $stIDNeutral = null;
  28. protected function load()
  29. {
  30. parent::load();
  31. $this->set('stIDNeutral', null);
  32. $stack = Stack::getByID($this->stID);
  33. if ($stack && $stack->isNeutralStack()) {
  34. $detector = isset($this->app) ? $this->app->make('multilingual/detector') : \Core::make('multilingual/detector');
  35. /* @var \Concrete\Core\Multilingual\Service\Detector $detector */
  36. if ($detector->isEnabled()) {
  37. $section = Section::getCurrentSection();
  38. if ($section) {
  39. $localized = $stack->getLocalizedStack($section);
  40. if ($localized) {
  41. $this->stIDNeutral = $this->stID;
  42. $this->stID = $localized->getCollectionID();
  43. $this->set('stIDNeutral', $this->stIDNeutral);
  44. $this->set('stID', $this->stID);
  45. }
  46. }
  47. }
  48. }
  49. }
  50. public function getBlockTypeDescription()
  51. {
  52. return t("Proxy block for stacks added through the UI.");
  53. }
  54. public function getBlockTypeName()
  55. {
  56. return t("Stack Display");
  57. }
  58. public function getOriginalBlockID()
  59. {
  60. return $this->bOriginalID;
  61. }
  62. public function getSearchableContent()
  63. {
  64. $searchableContent = '';
  65. $stack = Stack::getByID($this->stID);
  66. if (is_object($stack)) {
  67. $blocks = $stack->getBlocks();
  68. if (!empty($blocks)) {
  69. foreach ($blocks as $block) {
  70. if (method_exists($block->instance, 'getSearchableContent')) {
  71. $searchableContent .= $block->instance->getSearchableContent();
  72. }
  73. }
  74. }
  75. }
  76. return $searchableContent;
  77. }
  78. public function getImportData($blockNode, $page)
  79. {
  80. $args = array();
  81. $content = (string) $blockNode->stack;
  82. $stack = Stack::getByName($content);
  83. $args['stID'] = 0;
  84. if (is_object($stack)) {
  85. $args['stID'] = $stack->getCollectionID();
  86. }
  87. return $args;
  88. }
  89. public function isValidControllerTask($method, $parameters = array())
  90. {
  91. $b = $this->findBlockForAction($method, $parameters);
  92. return !empty($b);
  93. }
  94. public function runAction($action, $parameters = array())
  95. {
  96. parent::runAction($action, $parameters); // handles on_page_view
  97. $b = $this->findBlockForAction($action, $parameters);
  98. if (empty($b)) {
  99. return;
  100. }
  101. $controller = $b->getController();
  102. return $controller->runAction($action, $parameters);
  103. }
  104. public function registerViewAssets($outputContent = '')
  105. {
  106. $stack = $this->getStack(true);
  107. if ($stack === null) {
  108. return null;
  109. }
  110. $blocks = $stack->getBlocks();
  111. foreach ($blocks as $b) {
  112. $controller = $b->getController();
  113. if ($controller) {
  114. $outputContent = $controller->registerViewAssets($outputContent);
  115. }
  116. }
  117. return $outputContent;
  118. }
  119. /**
  120. * Returns the Stack instance (if found).
  121. *
  122. * @param bool $localized Set to true to look for a localized version of the stack (if not found return the neutral version).
  123. *
  124. * @return Stack|null
  125. */
  126. protected function getStack($localized)
  127. {
  128. if ($this->stIDNeutral === null || $localized) {
  129. $result = Stack::getByID($this->stID);
  130. } else {
  131. $result = Stack::getByID($this->stIDNeutral);
  132. }
  133. return $result;
  134. }
  135. public function findBlockForAction($method, $parameters)
  136. {
  137. $stack = $this->getStack(true);
  138. if ($stack === null) {
  139. return null;
  140. }
  141. $blocks = $stack->getBlocks();
  142. foreach ($blocks as $b) {
  143. $controller = $b->getController();
  144. if ($controller->isValidControllerTask($method, $parameters)) {
  145. return $b;
  146. }
  147. }
  148. return null;
  149. }
  150. public function export(\SimpleXMLElement $blockNode)
  151. {
  152. $stack = $this->getStack(false);
  153. if ($stack !== null) {
  154. $cnode = $blockNode->addChild('stack');
  155. $node = dom_import_simplexml($cnode);
  156. $no = $node->ownerDocument;
  157. $node->appendChild($no->createCDataSection($stack->getCollectionName()));
  158. }
  159. }
  160. public function on_page_view($page)
  161. {
  162. $stack = $this->getStack(true);
  163. if ($stack === null) {
  164. return false;
  165. }
  166. $p = new Permissions($stack);
  167. if ($p->canViewPage()) {
  168. $blocks = $stack->getBlocks();
  169. foreach ($blocks as $b) {
  170. $bp = new Permissions($b);
  171. if ($bp->canViewBlock()) {
  172. $btc = $b->getInstance();
  173. if ('Controller' != get_class($btc)) {
  174. $btc->outputAutoHeaderItems();
  175. }
  176. $csr = $b->getCustomStyle();
  177. if (is_object($csr)) {
  178. $css = $csr->getCSS();
  179. if ($css !== '') {
  180. $styleHeader = $csr->getStyleWrapper($css);
  181. $btc->addHeaderItem($styleHeader);
  182. }
  183. }
  184. $btc->runTask('on_page_view', array($page));
  185. }
  186. }
  187. }
  188. }
  189. protected function setupCacheSettings()
  190. {
  191. if ($this->btCacheSettingsInitialized || Page::getCurrentPage()->isEditMode()) {
  192. return;
  193. }
  194. $this->btCacheSettingsInitialized = true;
  195. //Block cache settings are only as good as the weakest cached item inside. So loop through and check.
  196. $btCacheBlockOutput = true;
  197. $btCacheBlockOutputOnPost = true;
  198. $btCacheBlockOutputLifetime = 0;
  199. $stack = $this->getStack(true);
  200. if ($stack === null) {
  201. return false;
  202. }
  203. $p = new Permissions($stack);
  204. if ($p->canViewPage()) {
  205. $blocks = $stack->getBlocks();
  206. foreach ($blocks as $b) {
  207. if ($b->overrideAreaPermissions()) {
  208. $btCacheBlockOutput = false;
  209. $btCacheBlockOutputOnPost = false;
  210. $btCacheBlockOutputLifetime = 0;
  211. break;
  212. }
  213. $btCacheBlockOutput = $btCacheBlockOutput && $b->cacheBlockOutput();
  214. $btCacheBlockOutputOnPost = $btCacheBlockOutputOnPost && $b->cacheBlockOutputOnPost();
  215. //As soon as we find something which cannot be cached, entire block cannot be cached, so stop checking.
  216. if (!$btCacheBlockOutput) {
  217. return;
  218. }
  219. if ($expires = $b->getBlockOutputCacheLifetime()) {
  220. if ($expires && $btCacheBlockOutputLifetime < $expires) {
  221. $btCacheBlockOutputLifetime = $expires;
  222. }
  223. }
  224. }
  225. }
  226. $this->btCacheBlockOutput = $btCacheBlockOutput;
  227. $this->btCacheBlockOutputOnPost = $btCacheBlockOutputOnPost;
  228. $this->btCacheBlockOutputLifetime = $btCacheBlockOutputLifetime;
  229. }
  230. public function cacheBlockOutput()
  231. {
  232. $this->setupCacheSettings();
  233. return $this->btCacheBlockOutput;
  234. }
  235. public function cacheBlockOutputOnPost()
  236. {
  237. $this->setupCacheSettings();
  238. return $this->btCacheBlockOutputOnPost;
  239. }
  240. public function getBlockTypeCacheOutputLifetime()
  241. {
  242. $this->setupCacheSettings();
  243. return $this->btCacheBlockOutputLifetime;
  244. }
  245. public function getStackID()
  246. {
  247. return $this->stID;
  248. }
  249. public function save($args)
  250. {
  251. parent::save($args);
  252. $this->stID = $args['stID'];
  253. Application::getFacadeApplication()->make('statistics/tracker')->track($this);
  254. }
  255. public function delete()
  256. {
  257. Application::getFacadeApplication()->make('statistics/tracker')->forget($this);
  258. parent::delete();
  259. }
  260. }