PageRenderTime 28ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/components/View.php

http://github.com/Shadez/Framework
PHP | 245 lines | 130 code | 47 blank | 68 comment | 27 complexity | 0bd9199b657055231f27fc8982213e2d MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Copyright (C) 2011-2012 Shadez <https://github.com/Shadez/Framework>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. **/
  19. class View extends Component
  20. {
  21. private $m_regions = array();
  22. private $m_tplVars = array();
  23. private $m_htmlContents = array();
  24. public function initialize()
  25. {
  26. $this->setAllVars();
  27. return $this;
  28. }
  29. /**
  30. * Renders all regions
  31. * @return View_Component
  32. **/
  33. private function renderRegions()
  34. {
  35. foreach ($this->m_regions as $region)
  36. $this->m_htmlContents[$region->getName()] = $region->renderRegion($this->m_tplVars)->getRegionHTML();
  37. return $this;
  38. }
  39. /**
  40. * Sets all variables to template (from core & controller)
  41. * @return View_Component
  42. **/
  43. private function setAllVars()
  44. {
  45. $this->m_tplVars['core'] = $this->getCore()->getVars();
  46. $this->m_tplVars['controller'] = $this->getCore()->getActiveController()->getVars();
  47. return $this;
  48. }
  49. /**
  50. * Builds all provided blocks and blocks of current controller's group
  51. * @param array $blocks = array()
  52. * @throws \Exceptions\ViewCrash
  53. * @return View_Component
  54. **/
  55. public function buildBlocks($blocks = array())
  56. {
  57. $controller = $this->getCore()->getActiveController();
  58. if (!$controller)
  59. throw new \Exceptions\ViewCrash('unable to create controller blocks: controller does not exists');
  60. if ($blocks)
  61. {
  62. if (is_string($blocks))
  63. $blocks = array($blocks);
  64. foreach ($blocks as $name)
  65. if (method_exists($controller, 'block_' . $name))
  66. $block = call_user_func(array($controller, 'block_' . $name));
  67. unset($block, $name);
  68. }
  69. // Check & build controller group's blocks (if exists)
  70. $groupBlocks = $this->c('ControllerGroups')->getBlocksFromGroup($controller->getControllerGroup());
  71. if ($groupBlocks)
  72. {
  73. foreach ($groupBlocks as $name => $block)
  74. {
  75. if (!$block || !isset($block['template']) || !isset($block['region']))
  76. continue;
  77. $bl = $this->i('Block')
  78. ->setTemplate($block['template'][0], $block['template'][1])
  79. ->setRegion($block['region'])
  80. ->setName($name);
  81. if (isset($block['vars']) && $block['vars'])
  82. {
  83. foreach ($block['vars'] as $var => $value)
  84. $bl->setVar($var, $value);
  85. }
  86. }
  87. }
  88. return $this;
  89. }
  90. /**
  91. * Includes template file
  92. * @param string $tpl_name
  93. * @param string $tpl_path = 'default'
  94. * @throws \Exceptions\ViewCrash
  95. * @return View_Component
  96. **/
  97. public function displayTemplate($tpl_name, $tpl_path = 'default')
  98. {
  99. $tpl_path = str_replace('.', DS, $tpl_path);
  100. $tpl = TEMPLATES_DIR . $tpl_path . DS . $tpl_name . '.' . TPL_EXT;
  101. if (!file_exists($tpl))
  102. throw new \Exceptions\ViewCrash('unable to find template file (' . $tpl_name . ')');
  103. require_once($tpl);
  104. return $this;
  105. }
  106. /**
  107. * Adds region
  108. * @param Region $region
  109. * @return View_Component
  110. **/
  111. public function addRegion(Region $region)
  112. {
  113. $this->m_regions[$region->getName()] = $region;
  114. return $this;
  115. }
  116. /**
  117. * Checks if region exists
  118. * @param string $name
  119. * @return bool
  120. **/
  121. public function regionExists($name)
  122. {
  123. return isset($this->m_regions[$name]);
  124. }
  125. /**
  126. * Returns Region_Component instance by region name
  127. * @param mixed $region
  128. * @throws \Exceptions\ViewCrash
  129. * @return Region_Component
  130. **/
  131. public function getRegion($region)
  132. {
  133. if (is_object($region) && isset($this->m_regions[$region->getName()]))
  134. return $this->m_regions[$region->getName()];
  135. elseif (is_string($region) && isset($this->m_regions[$region]))
  136. return $this->m_regions[$region];
  137. else
  138. throw new \Exceptions\ViewCrash('region ' . (is_object($region) ? $region->getName() : $region) . ' was not found');
  139. return null;
  140. }
  141. /**
  142. * Returns region contents
  143. * @param string $name
  144. * @return string
  145. **/
  146. public function getRegionContents($name)
  147. {
  148. return isset($this->m_htmlContents[$name]) ? $this->m_htmlContents[$name] : '';
  149. }
  150. /**
  151. * Builds page
  152. * @param string $layoutFile = ''
  153. * @param string $layoutPath = ''
  154. * @throws \Exceptions\ViewCrash
  155. * @return View_Component
  156. **/
  157. public function buildPage($layoutFile = false, $layoutPath = false)
  158. {
  159. if (!$layoutFile || !$layoutPath)
  160. {
  161. $controller = $this->getCore()->getActiveController();
  162. $defaultLayout = $this->c('Config')->getValue('controller.default_layout');
  163. if (!$controller)
  164. throw new \Exceptions\ViewCrash('unable to build page: active controller does not exists');
  165. // Check controller group
  166. $group = $controller->getControllerGroup();
  167. if ($group)
  168. {
  169. $layout = $this->c('ControllerGroups')->getGroupLayout($group);
  170. if ($layout)
  171. {
  172. $layoutFile = isset($layout[0]) ? $layout[0] : false;
  173. $layoutPath = isset($layout[1]) ? $layout[1] : false;
  174. }
  175. }
  176. if (!$layoutFile)
  177. $layoutFile = isset($defaultLayout[0]) ? $defaultLayout[0] : false;
  178. if (!$layoutPath)
  179. $layoutPath = isset($defaultLayout[1]) ? $defaultLayout[1] : false;
  180. }
  181. if (!$layoutFile || !$layoutPath)
  182. throw new \Exceptions\ViewCrash('layout file was not defined');
  183. $path = str_replace('.', DS, $layoutPath);
  184. if ($this->getCore()->getActiveController()->isAjaxPage())
  185. $layoutFile .= '_ajax';
  186. $tpl = TEMPLATES_DIR . $path . DS . $layoutFile . '.' . TPL_EXT;
  187. if (!file_exists($tpl))
  188. throw new \Exceptions\ViewCrash('unable to find layout file (' . $layoutPath . '.' . $layoutFile . ')');
  189. $this->renderRegions();
  190. $this->c('Layout')->loadClientFiles();
  191. if ($this->m_tplVars['core'])
  192. extract($this->m_tplVars['core'], EXTR_SKIP);
  193. if ($this->m_tplVars['controller'])
  194. extract($this->m_tplVars['controller'], EXTR_SKIP);
  195. require_once($tpl);
  196. return $this;
  197. }
  198. };