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

/wp-content/plugins/nextgen-gallery/products/photocrati_nextgen/modules/mvc/class.mvc_view.php

https://bitbucket.org/kenaku/karate
PHP | 288 lines | 171 code | 62 blank | 55 comment | 31 complexity | 2d40e144a9e8af0086b5c315f7b6849c MD5 | raw file
  1. <?php
  2. class C_MVC_View extends C_Component
  3. {
  4. var $_template = '';
  5. var $_engine = '';
  6. var $_params = array();
  7. var $_queue = array();
  8. function define($template, $params=array(), $engine='php', $context=FALSE)
  9. {
  10. parent::define($context);
  11. $this->implement('I_MVC_View');
  12. $this->add_mixin('Mixin_Mvc_View_Instance_Methods');
  13. }
  14. /**
  15. * Initialize the view with some parameters
  16. * @param array $params
  17. * @param context $context
  18. */
  19. function initialize($template, $params=array(), $engine='php', $context=FALSE)
  20. {
  21. parent::initialize($context);
  22. $this->_template = $template;
  23. $this->_params = (array) $params;
  24. $this->_engine = $engine;
  25. }
  26. }
  27. class Mixin_Mvc_View_Instance_Methods extends Mixin
  28. {
  29. /**
  30. * Returns the variables to be used in the template
  31. * @return array
  32. */
  33. function get_template_vars()
  34. {
  35. $retval = array();
  36. foreach ($this->object->_params as $key => $value) {
  37. if (strpos($key, '_template') !== FALSE) {
  38. $value = $this->object->get_template_abspath($value);
  39. }
  40. $retval[$key] = $value;
  41. }
  42. return $retval;
  43. }
  44. /**
  45. * Returns the abspath of the template to be rendered
  46. * @param string $key
  47. * @return string
  48. */
  49. function get_template_abspath($value=NULL)
  50. {
  51. if (!$value) $value = $this->object->_template;
  52. if ($value[0] == '/' && @file_exists($value)) {
  53. // key is already abspath
  54. }
  55. else $value = $this->object->find_template_abspath($value);
  56. return $value;
  57. }
  58. /**
  59. * Renders the view (template)
  60. * @param string $__return
  61. * @return string|NULL
  62. */
  63. function render($return = FALSE)
  64. {
  65. $element = $this->object->render_object();
  66. $content = $this->object->rasterize_object($element);
  67. if (!$return) {
  68. echo $content;
  69. }
  70. return $content;
  71. }
  72. function render_object()
  73. {
  74. // We use underscores to prefix local variables to avoid conflicts wth
  75. // template vars
  76. $__element = $this->start_element($this->object->_template, 'template', $this->object);
  77. extract($this->object->get_template_vars());
  78. include($this->object->get_template_abspath());
  79. $this->end_element();
  80. return $__element;
  81. }
  82. function rasterize_object($element)
  83. {
  84. return $element->rasterize();
  85. }
  86. function start_element($id, $type = null, $context = null)
  87. {
  88. if ($type == null)
  89. {
  90. $type = 'element';
  91. }
  92. $count = count($this->object->_queue);
  93. $element = new C_MVC_View_Element($id, $type);
  94. if ($context != null)
  95. {
  96. if (!is_array($context))
  97. {
  98. $context = array('object' => $context);
  99. }
  100. foreach ($context as $context_name => $context_value)
  101. {
  102. $element->set_context($context_name, $context_value);
  103. }
  104. }
  105. $this->object->_queue[] = $element;
  106. if ($count > 0)
  107. {
  108. $old_element = $this->object->_queue[$count - 1];
  109. $content = ob_get_contents();
  110. ob_clean();
  111. $old_element->append($content);
  112. $old_element->append($element);
  113. }
  114. ob_start();
  115. return $element;
  116. }
  117. function end_element()
  118. {
  119. $content = ob_get_clean();
  120. $element = array_pop($this->object->_queue);
  121. if ($content != null)
  122. {
  123. $element->append($content);
  124. }
  125. return $element;
  126. }
  127. /**
  128. * Renders a sub-template for the view
  129. * @param string $__template
  130. * @param array $__params
  131. * @param string $__return
  132. * @return NULL
  133. */
  134. function include_template($__template, $__params = null, $__return=FALSE)
  135. {
  136. // We use underscores to prefix local variables to avoid conflicts wth
  137. // template vars
  138. if ($__params == null) {
  139. $__params = array();
  140. }
  141. $__params['template_origin'] = $this->object->_template;
  142. $__target = $this->object->get_template_abspath($__template);
  143. $__origin_target = $this->object->get_template_abspath($this->object->_template);
  144. $__image_before_target = $this->object->get_template_abspath('photocrati-nextgen_gallery_display#image/before');
  145. $__image_after_target = $this->object->get_template_abspath('photocrati-nextgen_gallery_display#image/after');
  146. if ($__origin_target != $__target)
  147. {
  148. if ($__target == $__image_before_target)
  149. {
  150. $__image = isset($__params['image']) ? $__params['image'] : null;
  151. $this->start_element('nextgen_gallery.image_panel', 'item', $__image);
  152. }
  153. if ($__target == $__image_after_target)
  154. {
  155. $this->end_element();
  156. }
  157. extract($__params);
  158. include($__target);
  159. if ($__target == $__image_before_target)
  160. {
  161. $__image = isset($__params['image']) ? $__params['image'] : null;
  162. $this->start_element('nextgen_gallery.image', 'item', $__image);
  163. }
  164. if ($__target == $__image_after_target)
  165. {
  166. $this->end_element();
  167. }
  168. }
  169. }
  170. /**
  171. * Gets the absolute path of an MVC template file
  172. *
  173. * @param string $path
  174. * @param string $module
  175. * @return string
  176. */
  177. function find_template_abspath($path, $module=FALSE)
  178. {
  179. $fs = $this->get_registry()->get_utility('I_Fs');
  180. $settings = C_NextGen_Settings::get_instance();
  181. // We also accept module_name#path, which needs parsing.
  182. if (!$module)
  183. list($path, $module) = $fs->parse_formatted_path($path);
  184. // Append the suffix
  185. $path = $path . '.php';
  186. $retval = $fs->join_paths(
  187. $fs->get_document_root(),
  188. $this->object->get_registry()->get_module_dir($module),
  189. $settings->mvc_template_dirname,
  190. $path
  191. );
  192. if (!@file_exists($retval))
  193. throw new RuntimeException("{$retval} is not a valid MVC template");
  194. return $retval;
  195. }
  196. /**
  197. * Adds a template parameter
  198. * @param $key
  199. * @param $value
  200. */
  201. function set_param($key, $value)
  202. {
  203. $this->object->_params[$key] = $value;
  204. }
  205. /**
  206. * Removes a template parameter
  207. * @param $key
  208. */
  209. function remove_param($key)
  210. {
  211. unset($this->object->_params[$key]);
  212. }
  213. /**
  214. * Gets the value of a template parameter
  215. * @param $key
  216. * @param null $default
  217. * @return mixed
  218. */
  219. function get_param($key, $default=NULL)
  220. {
  221. if (isset($this->object->_params[$key])) {
  222. return $this->object->_params[$key];
  223. }
  224. else return $default;
  225. }
  226. }