/concrete/src/Block/View/BlockViewTemplate.php

https://gitlab.com/koodersmiikka/operaatio-terveys · PHP · 227 lines · 196 code · 26 blank · 5 comment · 46 complexity · 0956ae5893e046cfadc8eea34853cc4f MD5 · raw file

  1. <?php
  2. namespace Concrete\Core\Block\View;
  3. use Loader;
  4. use Environment;
  5. use CacheLocal;
  6. use AssetList;
  7. use View;
  8. use Block;
  9. use \Concrete\Core\Package\PackageList;
  10. use \Concrete\Core\Asset\JavascriptAsset;
  11. use \Concrete\Core\Asset\CssAsset;
  12. class BlockViewTemplate {
  13. protected $basePath = '';
  14. protected $bFilename;
  15. protected $btHandle;
  16. protected $obj;
  17. protected $baseURL;
  18. protected $checkAssets = true;
  19. protected $itemsToCheck = array(
  20. 'CSS' => 'view.css',
  21. 'JAVASCRIPT' => 'view.js'
  22. );
  23. protected $render = FILENAME_BLOCK_VIEW;
  24. public function __construct($obj) {
  25. $this->btHandle = $obj->getBlockTypeHandle();
  26. $this->obj = $obj;
  27. if ($obj instanceof Block) {
  28. $this->bFilename = $obj->getBlockFilename();
  29. }
  30. $this->computeView();
  31. }
  32. protected function computeView() {
  33. $bFilename = $this->bFilename;
  34. $obj = $this->obj;
  35. // if we've passed in "templates/" as the first part, we strip that off.
  36. if (strpos($bFilename, 'templates/') === 0) {
  37. $bFilename = substr($bFilename, 10);
  38. }
  39. // The filename might be a directory name with .php-appended (BlockView does that), strip it.
  40. $bFilenameWithoutDotPhp = $bFilename;
  41. if ( substr( $bFilename, -4 ) === ".php" ) {
  42. $bFilenameWithoutDotPhp = substr( $bFilename, 0, strlen( $bFilename ) -4 );
  43. }
  44. if ($bFilename) {
  45. if (is_file(DIR_FILES_BLOCK_TYPES . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename)) {
  46. $template = DIR_FILES_BLOCK_TYPES . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename;
  47. $bv = new BlockView($obj);
  48. $this->baseURL = $bv->getBlockURL();
  49. $this->basePath = $bv->getBlockPath($this->render);
  50. } else if (is_file(DIR_FILES_BLOCK_TYPES_CORE . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename)) {
  51. $template = DIR_FILES_BLOCK_TYPES_CORE . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename;
  52. $this->baseURL = ASSETS_URL . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle();
  53. $this->basePath = DIR_FILES_BLOCK_TYPES_CORE . '/' . $obj->getBlockTypeHandle();
  54. } else if (is_dir(DIR_FILES_BLOCK_TYPES . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename)) {
  55. $template = DIR_FILES_BLOCK_TYPES . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename . '/' . $this->render;
  56. $this->basePath = DIR_FILES_BLOCK_TYPES . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename;
  57. $this->baseURL = REL_DIR_APPLICATION . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename;
  58. } else if (is_dir(DIR_FILES_BLOCK_TYPES_CORE . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename)) {
  59. $template = DIR_FILES_BLOCK_TYPES_CORE . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename . '/' . $this->render;
  60. $this->basePath = DIR_FILES_BLOCK_TYPES_CORE . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename;
  61. $this->baseURL = ASSETS_URL . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename;
  62. } else if ( $bFilename !== $bFilenameWithoutDotPhp ) {
  63. if (is_dir(DIR_FILES_BLOCK_TYPES . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilenameWithoutDotPhp)) {
  64. $template = DIR_FILES_BLOCK_TYPES . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilenameWithoutDotPhp . '/' . $this->render;
  65. $this->basePath = DIR_FILES_BLOCK_TYPES . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilenameWithoutDotPhp;
  66. $this->baseURL = REL_DIR_APPLICATION . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilenameWithoutDotPhp;
  67. } else if (is_dir(DIR_FILES_BLOCK_TYPES_CORE . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilenameWithoutDotPhp)) {
  68. $template = DIR_FILES_BLOCK_TYPES_CORE . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilenameWithoutDotPhp . '/' . $this->render;
  69. $this->basePath = DIR_FILES_BLOCK_TYPES_CORE . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilenameWithoutDotPhp;
  70. $this->baseURL = ASSETS_URL . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilenameWithoutDotPhp;
  71. }
  72. }
  73. // we check all installed packages
  74. if (!isset($template)) {
  75. $pl = PackageList::get();
  76. $packages = $pl->getPackages();
  77. foreach($packages as $pkg) {
  78. $d = '';
  79. if (is_dir(DIR_PACKAGES . '/' . $pkg->getPackageHandle())) {
  80. $d = DIR_PACKAGES . '/'. $pkg->getPackageHandle();
  81. } else if (is_dir(DIR_PACKAGES_CORE . '/'. $pkg->getPackageHandle())) {
  82. $d = DIR_PACKAGES_CORE . '/'. $pkg->getPackageHandle();
  83. }
  84. if ($d != '') {
  85. $baseStub = (is_dir(DIR_PACKAGES . '/' . $pkg->getPackageHandle())) ? DIR_REL . '/' . DIRNAME_PACKAGES . '/'. $pkg->getPackageHandle() : ASSETS_URL . '/'. DIRNAME_PACKAGES . '/' . $pkg->getPackageHandle();
  86. if (is_file($d . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle() . '/' . $bFilename)) {
  87. $template = $d . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle() . '/' . $bFilename;
  88. $this->baseURL = ASSETS_URL . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle();
  89. $this->basePath = DIR_FILES_BLOCK_TYPES_CORE . '/' . $obj->getBlockTypeHandle();
  90. } else if (is_file($d . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename)) {
  91. $template = $d . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename;
  92. $this->baseURL = $baseStub . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle();
  93. $this->basePath = $d . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle();
  94. } else if (is_dir($d . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename)) {
  95. $template = $d . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename . '/' . $this->render;
  96. $this->baseURL = $baseStub . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename;
  97. }
  98. }
  99. if ($this->baseURL != '') {
  100. continue;
  101. }
  102. }
  103. }
  104. } else if (file_exists(DIR_FILES_BLOCK_TYPES . '/' . $obj->getBlockTypeHandle() . '.php')) {
  105. $template = DIR_FILES_BLOCK_TYPES . '/' . $obj->getBlockTypeHandle() . '.php';
  106. $bv = new BlockView($obj);
  107. $this->baseURL = $bv->getBlockURL();
  108. $this->basePath = $bv->getBlockPath($this->render);
  109. } else if (file_exists(DIR_FILES_BLOCK_TYPES . '/' . $obj->getBlockTypeHandle() . '/' . $this->render)) {
  110. $template = DIR_FILES_BLOCK_TYPES . '/' . $obj->getBlockTypeHandle() . '/' . $this->render;
  111. $this->baseURL = REL_DIR_APPLICATION . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle();
  112. }
  113. if (!isset($template)) {
  114. $bv = new BlockView($obj);
  115. $template = $bv->getBlockPath($this->render) . '/' . $this->render;
  116. $this->baseURL = $bv->getBlockURL($this->render);
  117. }
  118. if ($this->basePath == '') {
  119. $this->basePath = dirname($template);
  120. }
  121. $this->template = $template;
  122. }
  123. public function getBasePath() {return $this->basePath;}
  124. public function getBaseURL() {return $this->baseURL;}
  125. public function setBlockCustomTemplate($bFilename) {
  126. $this->bFilename = $bFilename;
  127. $this->computeView();
  128. }
  129. public function setBlockCustomRender($renderFilename) {
  130. // if we've passed in "templates/" as the first part, we strip that off.
  131. if (strpos($renderFilename, 'templates/') === 0) {
  132. $bFilename = substr($renderFilename, 10);
  133. $this->setBlockCustomTemplate($bFilename);
  134. } else {
  135. $this->render = $renderFilename;
  136. }
  137. $this->computeView();
  138. }
  139. public function getTemplate() {
  140. return $this->template;
  141. }
  142. public function registerTemplateAssets() {
  143. $items = array();
  144. $h = Loader::helper("html");
  145. $dh = Loader::helper('file');
  146. if ($this->checkAssets == false) {
  147. return $items;
  148. } else {
  149. $al = AssetList::getInstance();
  150. $v = View::getInstance();
  151. foreach($this->itemsToCheck as $t => $i) {
  152. if (file_exists($this->basePath . '/' . $i)) {
  153. $identifier = substr($this->basePath, strpos($this->basePath, 'blocks'));
  154. // $identifier = 'blocks/page_list', 'blocks/feature', 'blocks/page_list/templates/responsive', etc...
  155. switch($t) {
  156. case 'CSS':
  157. $asset = new CssAsset($identifier);
  158. $asset->setAssetURL($this->getBaseURL() . '/' . $i);
  159. $asset->setAssetPath($this->basePath . '/' . $i);
  160. $al->registerAsset($asset);
  161. $v->requireAsset('css', $identifier);
  162. break;
  163. case 'JAVASCRIPT':
  164. $asset = new JavascriptAsset($identifier);
  165. $asset->setAssetURL($this->getBaseURL() . '/' . $i);
  166. $asset->setAssetPath($this->basePath . '/' . $i);
  167. $al->registerAsset($asset);
  168. $v->requireAsset('javascript', $identifier);
  169. break;
  170. }
  171. }
  172. }
  173. $css = $dh->getDirectoryContents($this->basePath . '/' . DIRNAME_CSS);
  174. $js = $dh->getDirectoryContents($this->basePath . '/' . DIRNAME_JAVASCRIPT);
  175. if (count($css) > 0) {
  176. foreach($css as $i) {
  177. if(substr($i,-4)=='.css') {
  178. $identifier = substr($this->basePath, strpos($this->basePath, 'blocks')) . '/' . $i;
  179. $asset = new CssAsset($identifier);
  180. $asset->setAssetURL($this->getBaseURL() . '/' . DIRNAME_CSS . '/' . $i);
  181. $asset->setAssetPath($this->basePath . '/' . DIRNAME_CSS . '/' . $i);
  182. $al->registerAsset($asset);
  183. $v->requireAsset('css', $identifier);
  184. }
  185. }
  186. }
  187. if (count($js) > 0) {
  188. foreach($js as $i) {
  189. if (substr($i,-3)=='.js') {
  190. $identifier = substr($this->basePath, strpos($this->basePath, 'blocks')) . '/' . $i;
  191. $asset = new JavascriptAsset($identifier);
  192. $asset->setAssetURL($this->getBaseURL() . '/' . DIRNAME_JAVASCRIPT . '/' . $i);
  193. $asset->setAssetPath($this->basePath . '/' . DIRNAME_JAVASCRIPT . '/' . $i);
  194. $al->registerAsset($asset);
  195. $v->requireAsset('javascript', $identifier);
  196. }
  197. }
  198. }
  199. }
  200. }
  201. }