PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Blocks/View/Helper/RegionsHelper.php

https://github.com/kareypowell/croogo
PHP | 98 lines | 56 code | 12 blank | 30 comment | 11 complexity | 47b832d344ce7c89d1f51a556303371e MD5 | raw file
  1. <?php
  2. App::uses('AppHelper', 'View/Helper');
  3. /**
  4. * Regions Helper
  5. *
  6. * @category Helper
  7. * @package Croogo.Blocks.View.Helper
  8. * @version 1.0
  9. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  10. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  11. * @link http://www.croogo.org
  12. */
  13. class RegionsHelper extends AppHelper {
  14. /**
  15. * Region is empty
  16. *
  17. * returns true if Region has no Blocks.
  18. *
  19. * @param string $regionAlias Region alias
  20. * @return boolean
  21. */
  22. public function isEmpty($regionAlias) {
  23. if (isset($this->_View->viewVars['blocks_for_layout'][$regionAlias]) &&
  24. count($this->_View->viewVars['blocks_for_layout'][$regionAlias]) > 0) {
  25. return false;
  26. } else {
  27. return true;
  28. }
  29. }
  30. /**
  31. * Show Blocks for a particular Region
  32. *
  33. * By default block are rendered using Blocks.block element. If `Block.element` is
  34. * set and exists, the render process will pass it through given element before wrapping
  35. * it inside the Blocks.block container. You disable the wrapping by setting
  36. * `enclosure=false` in the `params` field.
  37. *
  38. * @param string $regionAlias Region alias
  39. * @param array $options
  40. * @return string
  41. */
  42. public function blocks($regionAlias, $options = array()) {
  43. $output = '';
  44. if ($this->isEmpty($regionAlias)) {
  45. return $output;
  46. }
  47. $options = Hash::merge(array(
  48. 'elementOptions' => array(),
  49. ), $options);
  50. $elementOptions = $options['elementOptions'];
  51. $defaultElement = 'Blocks.block';
  52. $blocks = $this->_View->viewVars['blocks_for_layout'][$regionAlias];
  53. foreach ($blocks as $block) {
  54. $element = $block['Block']['element'];
  55. $exists = $this->_View->elementExists($element);
  56. $blockOutput = '';
  57. Croogo::dispatchEvent('Helper.Regions.beforeSetBlock', $this->_View, array(
  58. 'content' => &$block['Block']['body'],
  59. ));
  60. if ($exists) {
  61. $blockOutput = $this->_View->element($element, compact('block'), $elementOptions);
  62. } else {
  63. if (!empty($element)) {
  64. $this->log(sprintf('Missing element `%s` in block `%s` (%s)',
  65. $block['Block']['element'],
  66. $block['Block']['alias'],
  67. $block['Block']['id']
  68. ), LOG_WARNING);
  69. }
  70. $blockOutput = $this->_View->element($defaultElement, compact('block'), array('ignoreMissing' => true) + $elementOptions);
  71. }
  72. Croogo::dispatchEvent('Helper.Regions.afterSetBlock', $this->_View, array(
  73. 'content' => &$blockOutput,
  74. ));
  75. $enclosure = isset($block['Params']['enclosure']) ? $block['Params']['enclosure'] === "true" : true;
  76. if ($exists && $element != $defaultElement && $enclosure) {
  77. $block['Block']['body'] = $blockOutput;
  78. $block['Block']['element'] = null;
  79. $output .= $this->_View->element($defaultElement, compact('block'), $elementOptions);
  80. } else {
  81. $output .= $blockOutput;
  82. }
  83. }
  84. return $output;
  85. }
  86. }