PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/sys/Layout/Block.php

https://github.com/pixelplant/ZeroG
PHP | 487 lines | 218 code | 51 blank | 218 comment | 12 complexity | d06c21955283be6fff64dfe8795f076b MD5 | raw file
  1. <?php
  2. /**
  3. * Base Block class, used by layouts
  4. *
  5. * @author radu.mogos
  6. */
  7. namespace Sys\Layout
  8. {
  9. class Block extends \Sys\Model
  10. {
  11. /**
  12. * Holds an array of \Sys\Block chidren
  13. *
  14. * @var <array>
  15. */
  16. protected $_children = array();
  17. /**
  18. * The parent this block belongs to
  19. *
  20. * @var <\Sys\Layout\Block>
  21. */
  22. protected $_parent;
  23. /**
  24. * The block's name
  25. *
  26. * @var <string>
  27. */
  28. protected $_name;
  29. /**
  30. * The template name used by the block for content rendering, if required
  31. *
  32. * @var <string>
  33. */
  34. protected $_template;
  35. /**
  36. * The actual template resource (php code for the view)
  37. *
  38. * @var <string>
  39. */
  40. protected $_templateResource;
  41. /**
  42. * The PHP/HTML code loaded from the block's template
  43. *
  44. * @var <string>
  45. */
  46. protected $_code;
  47. /**
  48. * The module name this block is linked to
  49. *
  50. * @var <string>
  51. */
  52. protected $_moduleName;
  53. /**
  54. * Block type, eg: 'core/text'
  55. *
  56. * @var <string>
  57. */
  58. protected $_type;
  59. /**
  60. * Block type identifier (the first part of $this->_type)
  61. * @var <type>
  62. */
  63. protected $_identifier;
  64. /**
  65. * Event prefix used when loading blocks
  66. *
  67. * @var <type>
  68. */
  69. protected $_eventPrefix = 'block';
  70. /**
  71. * Reference to the parent layout it belongs to
  72. * @var <Sys\Layout>
  73. */
  74. protected $_layout;
  75. /**
  76. * Create a new Block
  77. *
  78. * @param <Sys\Layout> $layout The layout object it belongs to
  79. * @param <string> $name Block name, eg: content
  80. * @param <string> $type Block type, eg: core/base
  81. * @param <string> $template Block template file
  82. */
  83. public function __construct($layout, $name, $type, $template = '')
  84. {
  85. $this->_layout = $layout;
  86. $this->_children = array();
  87. $this->_parent = null;
  88. $this->_name = $name;
  89. $this->_templateResource = NULL;
  90. $this->_code = NULL;
  91. $this->_type = $type;
  92. $parts = explode('/', $type);
  93. $this->_identifier = $parts[0];
  94. $this->setTemplate($template);
  95. $this->_setModuleName();
  96. $this->_construct();
  97. }
  98. /**
  99. * Please overwrite this in your extended classes and not the default
  100. * constructor
  101. */
  102. protected function _construct()
  103. {
  104. }
  105. /**
  106. * Link the block to it's calling module so we know what translation
  107. * to use
  108. */
  109. protected function _setModuleName()
  110. {
  111. $this->_moduleName = \Z::getConfig()->getBlock($this->_identifier)->getModule();
  112. }
  113. /**
  114. * Adds a child to the children list
  115. * @param \Sys\Layout\Block $value
  116. */
  117. public function addChild(\Sys\Layout\Block $value)
  118. {
  119. $value->setParent($this);
  120. $this->_children[$value->getName()] = $value;
  121. }
  122. /**
  123. * Set a block to be it's child
  124. * @param <string> $name Child name
  125. * @param \Sys\Layout\Block $value Child block
  126. */
  127. public function setChild($name, \Sys\Layout\Block $value)
  128. {
  129. $value->setParent($this);
  130. $this->_children[$name] = $value;
  131. }
  132. // processing
  133. /**
  134. * Get the relative view path
  135. *
  136. * @return <string> the relative path to the app/views directory
  137. */
  138. protected function getPath($location = 'adminhtml')
  139. {
  140. return sprintf('app/design/%s/%s/%s/template/',
  141. $location,
  142. \Z::getConfig('config/global/default/package'),
  143. \Z::getConfig('config/global/default/template'));
  144. }
  145. /**
  146. * @param <string> $template the location + name of the template file
  147. * @return <Sys\Layout\Block> reference to the current instance
  148. */
  149. public function loadTemplate($template = '')
  150. {
  151. if ($template != '')
  152. $this->_template = $this->getPath().$template;
  153. // old code, where we first loaded the php/html code
  154. /*if ($template != '')
  155. {
  156. $this->_template = $this->getPath().$template;
  157. $filesize = filesize($this->_template);
  158. $file = fopen($this->_template, 'r');
  159. $this->_code = fread($file, $filesize);
  160. fclose($file);
  161. }*/
  162. //else
  163. // throw new \Sys\Exception('Block file missing: Please specify the block template filename!');
  164. return $this;
  165. }
  166. protected function _beforeToHtml() {}
  167. protected function _afterToHtml() {}
  168. /**
  169. * Render the current php/html block
  170. * @return <string> The final generated HTML code
  171. */
  172. public function render()
  173. {
  174. \Z::dispatchEvent($this->_eventPrefix.'_block_render_before', array('object' => $this));
  175. \Z::dispatchEvent('block_render_before', array('object' => $this));
  176. $code = '';
  177. if ($this->_template != NULL || $this->_code != NULL)
  178. {
  179. $this->_beforeToHtml();
  180. ob_start();
  181. if ($this->_code == NULL)
  182. {
  183. include $this->_template;
  184. }
  185. else
  186. echo $this->_code;
  187. //$renderedCode = ob_get_contents();
  188. //ob_end_clean();
  189. $renderedCode = ob_get_clean();
  190. if ($this->getShowTemplateHints())
  191. {
  192. $renderedCode = $this->wrapTemplateHints($renderedCode);
  193. }
  194. $code = $renderedCode;
  195. $this->_afterToHtml();
  196. }
  197. else
  198. $code = '';
  199. \Z::dispatchEvent($this->_eventPrefix.'_block_render_after', array('object' => $this));
  200. \Z::dispatchEvent('block_render_after', array('object' => $this));
  201. return $code;
  202. }
  203. /**
  204. * Render a block and all it's children
  205. * @param <string> $childName The name of the parent block
  206. * @return <string> The HTML code generated by executing all the children blocks
  207. */
  208. public function getChildHtml($childName)
  209. {
  210. if (!array_key_exists($childName, $this->_children))
  211. {
  212. //throw new \Sys\Exception("Error in: $this->_template. Make sure the child block '$childName' with parent block '$this->_name' is defined in the xml file.");
  213. return;
  214. }
  215. $child = $this->_children[$childName];
  216. $html = '';
  217. /*if ($child->getChildrenCount() > 0)
  218. {
  219. foreach ($child->getChildren() as $block)
  220. {
  221. $html .= $block->render();
  222. }
  223. }
  224. else*/
  225. $html = $child->render();
  226. return $html;
  227. }
  228. /**
  229. * Return the number of children blocks this parent block has
  230. * @param <string> $childName If you want to return the block count for another block, instead of the current one, specify it's name
  231. * @return <int> Number of children blocks
  232. */
  233. public function getChildrenCount($childName = '')
  234. {
  235. if ($childName != '')
  236. {
  237. return count($this->_children[$childName]->getChildren());
  238. }
  239. return count($this->_children);
  240. }
  241. /**
  242. * Return a child block by it's name
  243. * @param <string> $name The name of the child block to return
  244. * @return <Sys\Layout\Block> An instance of Block classs
  245. */
  246. public function getChild($name)
  247. {
  248. return $this->_children[$name];
  249. }
  250. /**
  251. * Remove a child from the list of children blocks
  252. * @param <string> $name The child name to remove
  253. */
  254. public function unsetChild($name)
  255. {
  256. unset($this->_children[$name]);
  257. }
  258. // getters, setters
  259. /**
  260. * Return all the children blocks this block has
  261. * @return <array>
  262. */
  263. public function getChildren()
  264. {
  265. return $this->_children;
  266. }
  267. /**
  268. * Set all the block children for this block
  269. * @param <array> $value An array of \Sys\Layout\Block instances
  270. */
  271. public function setChildren($value)
  272. {
  273. $this->_children = $value;
  274. }
  275. /**
  276. * Sets the block's name
  277. * @param <string> $value
  278. */
  279. public function setName($value)
  280. {
  281. $this->_name = $value;
  282. return $this;
  283. }
  284. /**
  285. * Returns the block's name
  286. * @return <string>
  287. */
  288. public function getName()
  289. {
  290. return $this->_name;
  291. }
  292. /**
  293. * Sets the template file for this block and ALSO loads the template code
  294. * @param <string> $value The path to the php template. eg: 'page/left.phtml'
  295. */
  296. public function setTemplate($value)
  297. {
  298. $this->loadTemplate($value);
  299. return $this;
  300. }
  301. /**
  302. * Returns the template filename used by this block
  303. * @return <string> eg: 'page/left.phtml'
  304. */
  305. public function getTemplate()
  306. {
  307. return $this->_template;
  308. }
  309. /**
  310. * Return the PHP/Html code for this block
  311. * @return <string>
  312. */
  313. public function getHtml()
  314. {
  315. return $this->_code;
  316. }
  317. /**
  318. * Set the PHP/Html code for this block
  319. * @param <string> $value
  320. */
  321. public function setHtml($value)
  322. {
  323. $this->_code = $value;
  324. return $this;
  325. }
  326. /**
  327. * Return the parent block name
  328. * @return <string>
  329. */
  330. public function getParent()
  331. {
  332. return $this->_parent;
  333. }
  334. /**
  335. * Set the parent block
  336. * @param <\Sys\Layout\Block> $value
  337. */
  338. public function setParent($value)
  339. {
  340. $this->_parent = $value;
  341. return $this;
  342. }
  343. // shortcuts to be used in every block phtml file
  344. /**
  345. * Return a specific helper
  346. * @param <string> $name
  347. * @return <Object>
  348. */
  349. public function helper($name)
  350. {
  351. return \Z::getHelper($name);
  352. }
  353. /**
  354. * Return a translated label
  355. *
  356. * @param <string> $label the label to translate
  357. * @return <string> The translation for the curent locale
  358. */
  359. public function __($label)
  360. {
  361. $arguments = func_get_args();
  362. return \Z::getLocale()->__($label, $this->_moduleName, $arguments);
  363. }
  364. public function formatdate($type, $timestamp)
  365. {
  366. return \Z::getLocale()->formatDate($type, $timestamp);
  367. }
  368. /**
  369. * Returns an absolute url containing the module+controller+action to be called
  370. * @param <string> $path
  371. * @return <string>
  372. */
  373. public function getUrl($path, $parameters = null)
  374. {
  375. return $this->helper('Sys\Helper\Html')->url($path, $parameters);
  376. }
  377. /**
  378. * Get Request data
  379. *
  380. * @return <array>
  381. */
  382. public function getRequest()
  383. {
  384. return \Z::getRequest();
  385. }
  386. /**
  387. * Returns the absolute path to a resource (image, file, etc)
  388. * located in the current package/theme/skin directory
  389. * @param <string> $resource
  390. * @return <string>
  391. */
  392. public function getSkinUrl($resource = '')
  393. {
  394. return $this->helper('Sys\Helper\Html')->skinUrl($resource);
  395. }
  396. public function getMediaUrl($resource = '')
  397. {
  398. return $this->helper('Sys\Helper\Html')->mediaUrl($resource);
  399. }
  400. /**
  401. * Return current layout object
  402. *
  403. * @return <\Sys\Layout>
  404. */
  405. public function getLayout()
  406. {
  407. //return \Z::getController()->getLayout();
  408. return $this->_layout;
  409. }
  410. public function getParam($name, $defaultValue = null)
  411. {
  412. return \Z::getRequest()->getParam($name, $defaultValue);
  413. }
  414. /**
  415. * Are path hints enabled? If so, then write the block data too...
  416. *
  417. * @return <bool>
  418. */
  419. private function getShowTemplateHints()
  420. {
  421. return \Z::getConfig('config/global/default/developer/block_hints');
  422. }
  423. private function wrapTemplateHints($code)
  424. {
  425. $code = sprintf('<div style="border: 1px solid red; margin: 1px"><div style="padding: 3px; background: red !important; color: white !important; font-size: 12px; font-weight: normal; text-shadow: none"><span style="float:left">%s</span> <span style="float:right">%s</span><p style="clear:both"></p></div> %s</div>',
  426. get_class($this),
  427. $this->_template,
  428. $code);
  429. return $code;
  430. }
  431. protected function _escape($text)
  432. {
  433. return htmlspecialchars($text);
  434. }
  435. }
  436. }