PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/web/system/ThemeModule/Block/RenderBlock.php

https://github.com/antoniom/core
PHP | 206 lines | 111 code | 32 blank | 63 comment | 25 complexity | 858bdfbfa979f027e1ed418376c68b31 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, MIT
  1. <?php
  2. /**
  3. * Copyright Zikula Foundation 2009 - Zikula Application Framework
  4. *
  5. * This work is contributed to the Zikula Foundation under one or more
  6. * Contributor Agreements and licensed to You under the following license:
  7. *
  8. * @license GNU/LGPLv3 (or at your option, any later version).
  9. * @package Zikula
  10. *
  11. * Please see the NOTICE file distributed with this source code for further
  12. * information regarding copyright and licensing.
  13. */
  14. namespace ThemeModule\Block;
  15. use Zikula_View, SecurityUtil, ModUtil, LogUtil, BlockUtil, Zikula_View_Theme;
  16. class RenderBlock extends \Zikula\Framework\Controller\AbstractBlock
  17. {
  18. /**
  19. * Module to render.
  20. *
  21. * @var string
  22. */
  23. protected $rmodule;
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function initialize()
  28. {
  29. // do not configure the view as a normal controller
  30. }
  31. /**
  32. * Set view property.
  33. *
  34. * @param Zikula_View $view Default null means new Render instance for this module name.
  35. *
  36. * @return AbstractController
  37. */
  38. protected function setView(Zikula_View $view = null)
  39. {
  40. if (is_null($view)) {
  41. $view = Zikula_View::getInstance($this->rmodule ? $this->rmodule : 'Theme');
  42. }
  43. $this->view = $view;
  44. return $this;
  45. }
  46. /**
  47. * Initialise block
  48. */
  49. public function init()
  50. {
  51. SecurityUtil::registerPermissionSchema('Theme:Renderblock:', 'Block ID::');
  52. }
  53. /**
  54. * Get information on block
  55. *
  56. * @return array blockinfo array
  57. */
  58. public function info()
  59. {
  60. return array('module' => 'Theme',
  61. 'text_type' => $this->__('Rendering engine'),
  62. 'text_type_long' => $this->__('Custom rendering engine block'),
  63. 'allow_multiple' => true,
  64. 'form_content' => false,
  65. 'form_refresh' => false,
  66. 'show_preview' => true);
  67. }
  68. /**
  69. * Display the block
  70. *
  71. * @param $blockinfo blockinfo array
  72. * @return string HTML output string
  73. */
  74. public function display($blockinfo)
  75. {
  76. if (!SecurityUtil::checkPermission('Theme:Renderblock:', "$blockinfo[bid]::", ACCESS_OVERVIEW)) {
  77. return;
  78. }
  79. // Break out options from our content field
  80. $vars = BlockUtil::varsFromContent($blockinfo['content']);
  81. // If the module is not specified or available does nothing
  82. if (!isset($vars['module']) || empty($vars['module']) || !ModUtil::available($vars['module'])) {
  83. return;
  84. } else {
  85. $this->rmodule = $vars['module'];
  86. }
  87. $showerror = SecurityUtil::checkPermission('Theme:Renderblock:', "$blockinfo[bid]::", ACCESS_ADMIN);
  88. // If the template is not speficied or empty it register an error for the admin
  89. if (!isset($vars['template']) || empty($vars['template'])) {
  90. if ($showerror) {
  91. LogUtil::registerError($this->__f('Misconfigured block. ID: %s', $blockinfo['bid']));
  92. }
  93. return;
  94. }
  95. // configure the view object
  96. $this->configureView();
  97. // checks the existance of the template
  98. if (!$this->view->template_exists($vars['template'])) {
  99. if ($showerror) {
  100. LogUtil::registerError($this->__f('The specified template for the render block doesn\'t exists for the \'%1$s\' module. Block ID: %2$s', array($vars['module'], $blockinfo['bid'])));
  101. }
  102. return;
  103. }
  104. // Get the additional parameters and assign them
  105. if (isset($vars['parameters']) && !empty($vars['parameters'])) {
  106. $params = explode(';', $vars['parameters']);
  107. if (count($params) > 0 ) {
  108. foreach ($params as $param) {
  109. $assign = explode('=', $param);
  110. $this->view->assign(trim($assign[0]), trim($assign[1]));
  111. }
  112. }
  113. }
  114. $this->view->setCaching(Zikula_View::CACHE_DISABLED);
  115. $blockinfo['content'] = $this->view->fetch($vars['template']);
  116. return BlockUtil::themeBlock($blockinfo);
  117. }
  118. /**
  119. * Modify the block
  120. *
  121. * @param $blockinfo blockinfo array
  122. * @return string HTML output string
  123. */
  124. public function modify($blockinfo)
  125. {
  126. if (!SecurityUtil::checkPermission('Theme:Renderblock:', "$blockinfo[bid]::", ACCESS_ADMIN)) {
  127. return false;
  128. }
  129. $this->configureView();
  130. // Break out options from our content field
  131. $vars = BlockUtil::varsFromContent($blockinfo['content']);
  132. // validate the current data
  133. $valid = false;
  134. $warnings = array();
  135. if (isset($vars['module']) && $vars['module']) {
  136. if (ModUtil::available($vars['module'])) {
  137. if (isset($vars['template']) && $vars['template']) {
  138. $view = Zikula_View::getInstance($vars['module']);
  139. if (!$view->template_exists($vars['template'])) {
  140. $warnings[] = $this->__f("The specified template does not exist on the '%s' module.", $vars['module']);
  141. } else {
  142. $valid = true;
  143. }
  144. }
  145. } else {
  146. $warnings[] = $this->__f("The module '%s' is not available.", $vars['module']);
  147. }
  148. }
  149. if (!$valid) {
  150. $warnings[] = $this->__('With the current setup the block is disabled.');
  151. }
  152. // generate the output
  153. return $this->view->assign($vars)
  154. ->assign('warnings', $warnings)
  155. ->fetch('Block/render_modify.tpl');
  156. }
  157. /**
  158. * Update the block
  159. *
  160. * @param $blockinfo old blockinfo array
  161. * @return array new blockinfo array
  162. */
  163. public function update($blockinfo)
  164. {
  165. if (!SecurityUtil::checkPermission('Theme:Renderblock:', "$blockinfo[bid]::", ACCESS_ADMIN)) {
  166. return false;
  167. }
  168. $module = $this->request->request->get('rmodule');
  169. $template = $this->request->request->get('rtemplate');
  170. $parameters = $this->request->request->get('rparameters');
  171. $blockinfo['content'] = BlockUtil::varsToContent(compact('module', 'template', 'parameters' ));
  172. return($blockinfo);
  173. }
  174. }