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

/src/system/Theme/lib/Theme/Block/Render.php

https://github.com/ThiloWitt/core
PHP | 199 lines | 109 code | 27 blank | 63 comment | 25 complexity | d40c2ffa3a263c9807f3b64905a5da64 MD5 | raw file
  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. class Theme_Block_Render extends Zikula_Controller_AbstractBlock
  15. {
  16. /**
  17. * Module to render.
  18. *
  19. * @var string
  20. */
  21. protected $rmodule;
  22. /**
  23. * {@inheritdoc}
  24. */
  25. protected function initialize()
  26. {
  27. // do not configure the view as a normal controller
  28. }
  29. /**
  30. * Set view property.
  31. *
  32. * @param Zikula_View $view Default null means new Render instance for this module name.
  33. *
  34. * @return Zikula_AbstractController
  35. */
  36. protected function setView(Zikula_View $view = null)
  37. {
  38. if (is_null($view)) {
  39. $view = Zikula_View::getInstance($this->rmodule ? $this->rmodule : 'Theme');
  40. }
  41. $this->view = $view;
  42. return $this;
  43. }
  44. /**
  45. * Initialise block
  46. */
  47. public function init()
  48. {
  49. SecurityUtil::registerPermissionSchema('Theme:Renderblock:', 'Block ID::');
  50. }
  51. /**
  52. * Get information on block
  53. *
  54. * @return array blockinfo array
  55. */
  56. public function info()
  57. {
  58. return array('module' => 'Theme',
  59. 'text_type' => $this->__('Rendering engine'),
  60. 'text_type_long' => $this->__('Custom rendering engine block'),
  61. 'allow_multiple' => true,
  62. 'form_content' => false,
  63. 'form_refresh' => false,
  64. 'show_preview' => true);
  65. }
  66. /**
  67. * Display the block
  68. *
  69. * @param $blockinfo blockinfo array
  70. * @return string HTML output string
  71. */
  72. public function display($blockinfo)
  73. {
  74. if (!SecurityUtil::checkPermission('Theme:Renderblock:', "$blockinfo[bid]::", ACCESS_OVERVIEW)) {
  75. return;
  76. }
  77. // Break out options from our content field
  78. $vars = BlockUtil::varsFromContent($blockinfo['content']);
  79. // If the module is not specified or available does nothing
  80. if (!isset($vars['module']) || empty($vars['module']) || !ModUtil::available($vars['module'])) {
  81. return;
  82. } else {
  83. $this->rmodule = $vars['module'];
  84. }
  85. $showerror = SecurityUtil::checkPermission('Theme:Renderblock:', "$blockinfo[bid]::", ACCESS_ADMIN);
  86. // If the template is not speficied or empty it register an error for the admin
  87. if (!isset($vars['template']) || empty($vars['template'])) {
  88. if ($showerror) {
  89. LogUtil::registerError($this->__f('Misconfigured block. ID: %s', $blockinfo['bid']));
  90. }
  91. return;
  92. }
  93. // configure the view object
  94. $this->configureView();
  95. // checks the existance of the template
  96. if (!$this->view->template_exists($vars['template'])) {
  97. if ($showerror) {
  98. 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'])));
  99. }
  100. return;
  101. }
  102. // Get the additional parameters and assign them
  103. if (isset($vars['parameters']) && !empty($vars['parameters'])) {
  104. $params = explode(';', $vars['parameters']);
  105. if (count($params) > 0 ) {
  106. foreach ($params as $param) {
  107. $assign = explode('=', $param);
  108. $this->view->assign(trim($assign[0]), trim($assign[1]));
  109. }
  110. }
  111. }
  112. $this->view->setCaching(Zikula_View::CACHE_DISABLED);
  113. $blockinfo['content'] = $this->view->fetch($vars['template']);
  114. return BlockUtil::themeBlock($blockinfo);
  115. }
  116. /**
  117. * Modify the block
  118. *
  119. * @param $blockinfo blockinfo array
  120. * @return string HTML output string
  121. */
  122. public function modify($blockinfo)
  123. {
  124. if (!SecurityUtil::checkPermission('Theme:Renderblock:', "$blockinfo[bid]::", ACCESS_ADMIN)) {
  125. return false;
  126. }
  127. $this->configureView();
  128. // Break out options from our content field
  129. $vars = BlockUtil::varsFromContent($blockinfo['content']);
  130. // validate the current data
  131. $valid = false;
  132. $warnings = array();
  133. if (isset($vars['module']) && $vars['module']) {
  134. if (ModUtil::available($vars['module'])) {
  135. if (isset($vars['template']) && $vars['template']) {
  136. $view = Zikula_View::getInstance($vars['module']);
  137. if (!$view->template_exists($vars['template'])) {
  138. $warnings[] = $this->__f("The specified template does not exist on the '%s' module.", $vars['module']);
  139. } else {
  140. $valid = true;
  141. }
  142. }
  143. } else {
  144. $warnings[] = $this->__f("The module '%s' is not available.", $vars['module']);
  145. }
  146. }
  147. if (!$valid) {
  148. $warnings[] = $this->__('With the current setup the block is disabled.');
  149. }
  150. // generate the output
  151. return $this->view->assign($vars)
  152. ->assign('warnings', $warnings)
  153. ->fetch('theme_block_render_modify.tpl');
  154. }
  155. /**
  156. * Update the block
  157. *
  158. * @param $blockinfo old blockinfo array
  159. * @return array new blockinfo array
  160. */
  161. function update($blockinfo)
  162. {
  163. if (!SecurityUtil::checkPermission('Theme:Renderblock:', "$blockinfo[bid]::", ACCESS_ADMIN)) {
  164. return false;
  165. }
  166. $module = FormUtil::getPassedValue('rmodule', null, 'POST');
  167. $template = FormUtil::getPassedValue('rtemplate', null, 'POST');
  168. $parameters = FormUtil::getPassedValue('rparameters', null, 'POST');
  169. $blockinfo['content'] = BlockUtil::varsToContent(compact('module', 'template', 'parameters' ));
  170. return($blockinfo);
  171. }
  172. }