PageRenderTime 29ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/core/libraries/block_view.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 331 lines | 237 code | 42 blank | 52 comment | 69 complexity | 1a1f33033b2022acc3a78a3cf8062572 MD5 | raw file
  1. <?php
  2. defined('C5_EXECUTE') or die("Access Denied.");
  3. /**
  4. * An object corresponding to a particular view of a block. These are those of the "add" state, the block's "edit" state, or the block's "view" state.
  5. *
  6. * @package Blocks
  7. * @author Andrew Embler <andrew@concrete5.org>
  8. * @category Concrete
  9. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  10. * @license http://www.concrete5.org/license/ MIT License
  11. *
  12. */
  13. class Concrete5_Library_BlockView extends View {
  14. protected $block;
  15. protected $area;
  16. protected $blockObj;
  17. /**
  18. * Includes a file from the core elements directory. Used by the CMS.
  19. * @access private
  20. */
  21. public function renderElement($element, $args = array()) {
  22. Loader::element($element, $args);
  23. }
  24. /**
  25. * Creates a URL that can be posted or navigated to that, when done so, will automatically run the corresponding method inside the block's controller.
  26. * <code>
  27. * <a href="<?php echo $this->action('get_results')?>">Get the results</a>
  28. * </code>
  29. * @param string $task
  30. * @param strign $extraParams Adds items onto the end of the query string. Useful for anchor links, etc...
  31. * @return string $url
  32. */
  33. public function action($task, $extraParams = null) {
  34. try {
  35. if (is_object($this->block)) {
  36. if (is_object($this->block->getProxyBlock())) {
  37. $b = $this->block->getProxyBlock();
  38. } else {
  39. $b = $this->block;
  40. }
  41. if (is_object($b)) {
  42. return $b->getBlockPassThruAction() . '&amp;method=' . $task . $extraParams;
  43. }
  44. }
  45. } catch(Exception $e) {}
  46. }
  47. /**
  48. * includes file from the current block directory. Similar to php's include()
  49. * @access public
  50. * @param string $file
  51. * @param array $args
  52. * @return void
  53. */
  54. public function inc($file, $args = array()) {
  55. extract($args);
  56. $base = $this->getBlockPath($file);
  57. extract($this->controller->getSets());
  58. extract($this->controller->getHelperObjects());
  59. include($base . '/' . $file);
  60. }
  61. /**
  62. * Returns the path to the current block's directory
  63. * @access public
  64. * @return string
  65. */
  66. public function getBlockPath($filename = null) {
  67. $obj = $this->blockObj;
  68. if (file_exists(DIR_FILES_BLOCK_TYPES . '/' . $obj->getBlockTypeHandle() . '/' . $filename)) {
  69. $base = DIR_FILES_BLOCK_TYPES . '/' . $obj->getBlockTypeHandle();
  70. } else if ($obj->getPackageID() > 0) {
  71. if (is_dir(DIR_PACKAGES . '/' . $obj->getPackageHandle())) {
  72. $base = DIR_PACKAGES . '/' . $obj->getPackageHandle() . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle();
  73. } else {
  74. $base = DIR_PACKAGES_CORE . '/' . $obj->getPackageHandle() . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle();
  75. }
  76. } else {
  77. $base = DIR_FILES_BLOCK_TYPES_CORE . '/' . $obj->getBlockTypeHandle();
  78. }
  79. return $base;
  80. }
  81. /**
  82. * Returns a relative path to the current block's directory. If a filename is specified it will be appended and searched for as well.
  83. * @return string
  84. */
  85. public function getBlockURL($filename = null) {
  86. $obj = $this->blockObj;
  87. if ($obj->getPackageID() > 0) {
  88. if (is_dir(DIR_PACKAGES_CORE . '/' . $obj->getPackageHandle())) {
  89. $base = ASSETS_URL . '/' . DIRNAME_PACKAGES . '/' . $obj->getPackageHandle() . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle();
  90. } else {
  91. $base = DIR_REL . '/' . DIRNAME_PACKAGES . '/' . $obj->getPackageHandle() . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle();
  92. }
  93. } else if (file_exists(DIR_FILES_BLOCK_TYPES . '/' . $obj->getBlockTypeHandle() . '/' . $filename)) {
  94. $base = DIR_REL . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle();
  95. } else {
  96. $base = ASSETS_URL . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle();
  97. }
  98. return $base;
  99. }
  100. /**
  101. * @access private
  102. */
  103. public function setAreaObject($a) {
  104. $this->area = $a;
  105. }
  106. public function getThemePath() {
  107. $v = View::getInstance();
  108. return $v->getThemePath();
  109. }
  110. /**
  111. * Returns the template used in the block view
  112. */
  113. public function getTemplate() {
  114. return $this->template;
  115. }
  116. public function getTemplateURL() {
  117. $bvt = new BlockViewTemplate($this->blockObj);
  118. return $bvt->getBaseURL();
  119. }
  120. public function setBlockObject($obj) {
  121. $this->blockObj = $obj;
  122. }
  123. public function getBlockObject() {
  124. return $this->blockObj;
  125. }
  126. /**
  127. * Renders a particular view for a block or a block type
  128. * @param Block | BlockType $obj
  129. * @param string $view
  130. * @param array $args
  131. */
  132. public function render($obj, $view = 'view', $args = array()) {
  133. if ($this->hasRendered) {
  134. return false;
  135. }
  136. $this->blockObj = $obj;
  137. $customAreaTemplates = array();
  138. if ($obj instanceof BlockType) {
  139. $bt = $obj;
  140. $base = $obj->getBlockTypePath();
  141. } else {
  142. $bFilename = $obj->getBlockFilename();
  143. $b = $obj;
  144. $base = $b->getBlockPath();
  145. $this->block = $b;
  146. $this->c = $b->getBlockCollectionObject();
  147. if ($bFilename == '' && is_object($this->area)) {
  148. $customAreaTemplates = $this->area->getCustomTemplates();
  149. $btHandle = $b->getBlockTypeHandle();
  150. if (isset($customAreaTemplates[$btHandle])) {
  151. $bFilename = $customAreaTemplates[$btHandle];
  152. }
  153. }
  154. }
  155. $btHandle = $obj->getBlockTypeHandle();
  156. if (!isset($this->controller)) {
  157. if ($obj instanceof Block) {
  158. $this->controller = $obj->getInstance();
  159. $this->controller->setBlockObject($obj);
  160. } else {
  161. $this->controller = Loader::controller($obj);
  162. }
  163. }
  164. if (in_array($view, array('view', 'add', 'edit', 'composer'))) {
  165. $_action = $view;
  166. } else {
  167. $_action = 'view';
  168. }
  169. $u = new User();
  170. $outputContent = false;
  171. $useCache = false;
  172. $page = Page::getCurrentPage();
  173. if ($view == 'view') {
  174. if (ENABLE_BLOCK_CACHE && $this->controller->cacheBlockOutput() && ($obj instanceof Block)) {
  175. if ((!$u->isRegistered() || ($this->controller->cacheBlockOutputForRegisteredUsers())) &&
  176. (($_SERVER['REQUEST_METHOD'] != 'POST' || ($this->controller->cacheBlockOutputOnPost() == true)))) {
  177. $useCache = true;
  178. }
  179. if ($useCache) {
  180. $outputContent = $obj->getBlockCachedOutput($this->area);
  181. }
  182. }
  183. }
  184. if ($outputContent == false) {
  185. $this->controller->setupAndRun($_action);
  186. }
  187. extract($this->controller->getSets());
  188. extract($this->controller->getHelperObjects());
  189. $headerItems = $this->controller->headerItems;
  190. extract($args);
  191. if ($this->controller->getRenderOverride() != '') {
  192. $_filename = $this->controller->getRenderOverride() . '.php';
  193. }
  194. if ($view == 'scrapbook') {
  195. $template = $this->getBlockPath(FILENAME_BLOCK_VIEW_SCRAPBOOK) . '/' . FILENAME_BLOCK_VIEW_SCRAPBOOK;
  196. if (!file_exists($template)) {
  197. $view = 'view';
  198. }
  199. }
  200. if (!in_array($view, array('composer','view', 'add', 'edit', 'scrapbook'))) {
  201. // then we're trying to render a custom view file, which we'll pass to the bottom functions as $_filename
  202. $_filename = $view . '.php';
  203. $view = 'view';
  204. }
  205. switch($view) {
  206. case 'scrapbook':
  207. $header = DIR_FILES_ELEMENTS_CORE . '/block_header_view.php';
  208. $footer = DIR_FILES_ELEMENTS_CORE . '/block_footer_view.php';
  209. break;
  210. case 'composer':
  211. case 'view':
  212. if (!$outputContent) {
  213. if (!isset($_filename)) {
  214. $_filename = FILENAME_BLOCK_VIEW;
  215. }
  216. $bvt = new BlockViewTemplate($obj);
  217. if ($bFilename) {
  218. $bvt->setBlockCustomTemplate($bFilename); // this is PROBABLY already set by the method above, but in the case that it's passed by area we have to set it here
  219. } else if ($_filename != FILENAME_BLOCK_VIEW) {
  220. $bvt->setBlockCustomRender($_filename);
  221. }
  222. $template = $bvt->getTemplate();
  223. }
  224. if ($view == 'composer') {
  225. $displayEditLink = true;
  226. $header = DIR_FILES_ELEMENTS_CORE . '/block_header_composer.php';
  227. $footer = DIR_FILES_ELEMENTS_CORE . '/block_footer_composer.php';
  228. $cpFilename = $obj->getBlockComposerFilename();
  229. if ($cpFilename) {
  230. $cmpbase = $this->getBlockPath(DIRNAME_BLOCK_TEMPLATES_COMPOSER . '/' . $cpFilename);
  231. if (file_exists($cmpbase . '/' . DIRNAME_BLOCK_TEMPLATES_COMPOSER . '/' . $cpFilename)) {
  232. $template = $base . '/' . DIRNAME_BLOCK_TEMPLATES_COMPOSER . '/' . $cpFilename;
  233. $displayEditLink = false;
  234. }
  235. }
  236. if ($displayEditLink) {
  237. $cmpbase = $this->getBlockPath(FILENAME_BLOCK_COMPOSER);
  238. if (file_exists($cmpbase . '/' . FILENAME_BLOCK_COMPOSER)) {
  239. $template = $cmpbase . '/' . FILENAME_BLOCK_COMPOSER;
  240. $displayEditLink = false;
  241. }
  242. }
  243. } else {
  244. $header = DIR_FILES_ELEMENTS_CORE . '/block_header_view.php';
  245. $footer = DIR_FILES_ELEMENTS_CORE . '/block_footer_view.php';
  246. }
  247. break;
  248. case 'add':
  249. if (!isset($_filename)) {
  250. $_filename = FILENAME_BLOCK_ADD;
  251. }
  252. $header = DIR_FILES_ELEMENTS_CORE . '/block_header_add.php';
  253. $footer = DIR_FILES_ELEMENTS_CORE . '/block_footer_add.php';
  254. break;
  255. case 'edit':
  256. if (!isset($_filename)) {
  257. $_filename = FILENAME_BLOCK_EDIT;
  258. }
  259. $header = DIR_FILES_ELEMENTS_CORE . '/block_header_edit.php';
  260. $footer = DIR_FILES_ELEMENTS_CORE . '/block_footer_edit.php';
  261. break;
  262. }
  263. if (!isset($template)) {
  264. $base = $this->getBlockPath($_filename);
  265. $template = $base . '/' . $_filename;
  266. }
  267. if (isset($header)) {
  268. include($header);
  269. }
  270. if ($outputContent) {
  271. print $outputContent;
  272. } else if ($template) {
  273. ob_start();
  274. include($template);
  275. $outputContent = ob_get_contents();
  276. ob_end_clean();
  277. print $outputContent;
  278. if ($useCache) {
  279. $obj->setBlockCachedOutput($outputContent, $this->controller->getBlockTypeCacheOutputLifetime(), $this->area);
  280. }
  281. }
  282. if (isset($footer)) {
  283. include($footer);
  284. }
  285. $this->template = $template;
  286. $this->header = $header;
  287. $this->footer = $footer;
  288. }
  289. }