/application/libraries/Engine/Controller/Action/Helper/ViewRenderer.php

https://github.com/grandison/budo16 · PHP · 361 lines · 90 code · 17 blank · 254 comment · 11 complexity · 093c07bee2d2878708c7f03c33fa5ce7 MD5 · raw file

  1. <?php
  2. /**
  3. * SocialEngine
  4. *
  5. * @category Engine
  6. * @package Engine_Controller
  7. * @copyright Copyright 2006-2010 Webligo Developments
  8. * @license http://www.socialengine.net/license/
  9. * @version $Id: ViewRenderer.php 7244 2010-09-01 01:49:53Z john $
  10. * @todo documentation
  11. */
  12. /**
  13. * @category Engine
  14. * @package Engine_Controller
  15. * @copyright Copyright 2006-2010 Webligo Developments
  16. * @license http://www.socialengine.net/license/
  17. */
  18. class Engine_Controller_Action_Helper_ViewRenderer extends Zend_Controller_Action_Helper_ViewRenderer
  19. {
  20. protected $_viewScriptPathSpec = 'application/modules/{module}/views/scripts/{controller}/{action}.{suffix}';
  21. public function getInflector()
  22. {
  23. throw new Exception('Removed');
  24. }
  25. public function setInflector(Zend_Filter_Inflector $inflector, $reference = false)
  26. {
  27. throw new Exception('Removed');
  28. }
  29. public function getViewScript($action = null, array $vars = array())
  30. {
  31. if ((null === $action) && (!isset($vars['action']))) {
  32. $action = $this->getScriptAction();
  33. if (null === $action) {
  34. $action = $this->getRequest()->getActionName();
  35. }
  36. $vars['action'] = $action;
  37. } elseif (null !== $action) {
  38. $vars['action'] = $action;
  39. }
  40. $vars['module'] = ucfirst($this->getModule());
  41. $vars['controller'] = $this->getRequest()->getControllerName();
  42. $vars['suffix'] = $this->getViewSuffix();
  43. $newVars = array();
  44. foreach( $vars as $key => $value )
  45. {
  46. $newVars['{'.$key.'}'] = $value;
  47. }
  48. $path = $this->getViewScriptPathSpec();
  49. $path = str_replace(array_keys($newVars), array_values($newVars), $path);
  50. return $path;
  51. }
  52. public function setBasePath($path)
  53. {
  54. $this->_basePath = $path;
  55. return $this;
  56. }
  57. protected function _getBasePath()
  58. {
  59. if( null === $this->_basePath ) {
  60. $this->setBasePath(APPLICATION_PATH);
  61. }
  62. return $this->_basePath;
  63. }
  64. public function initView($path = null, $prefix = null, array $options = array())
  65. {
  66. if( null === $this->view )
  67. {
  68. throw new Engine_Exception('No view set in ViewRenderer');
  69. }
  70. // Reset some flags every time
  71. $options['noController'] = (isset($options['noController'])) ? $options['noController'] : false;
  72. $options['noRender'] = (isset($options['noRender'])) ? $options['noRender'] : false;
  73. $this->_scriptAction = null;
  74. $this->_responseSegment = null;
  75. // Set options first; may be used to determine other initializations
  76. $this->_setOptions($options);
  77. // Get base view path
  78. $path = $this->_getBasePath();
  79. // Get class path
  80. $request = $this->getRequest();
  81. $module = $request->getModuleName();
  82. if (null === $module) {
  83. $module = $this->getFrontController()->getDispatcher()->getDefaultModule();
  84. }
  85. $classPath = APPLICATION_PATH.DS.'application'.DS.'modules'.DS.$module.DS.'View'.DS;
  86. // Get class prefix
  87. if( null === $prefix )
  88. {
  89. $prefix = ucfirst($module).'_View_';
  90. }
  91. // Determine if this path has already been registered
  92. $currentPaths = $this->view->getScriptPaths();
  93. $path = str_replace(array('/', '\\'), '/', $path);
  94. $pathExists = false;
  95. foreach ($currentPaths as $tmpPath) {
  96. $tmpPath = str_replace(array('/', '\\'), '/', $tmpPath);
  97. if (strstr($tmpPath, $path)) {
  98. $pathExists = true;
  99. break;
  100. }
  101. }
  102. if (!$pathExists) {
  103. $this->view->addScriptPath($path);
  104. }
  105. $this->view->addHelperPath($classPath . 'Helper', $prefix . 'Helper');
  106. $this->view->addFilterPath($classPath . 'Filter', $prefix . 'Filter');
  107. // Register view with action controller (unless already registered)
  108. if ((null !== $this->_actionController) && (null === $this->_actionController->view)) {
  109. $this->_actionController->view = $this->view;
  110. $this->_actionController->viewSuffix = $this->_viewSuffix;
  111. }
  112. }
  113. /*
  114. public $view;
  115. protected $_frontController;
  116. protected $_neverController = false;
  117. protected $_neverRender = false;
  118. protected $_noController = false;
  119. protected $_noRender = false;
  120. protected $_responseSegment = null;
  121. protected $_scriptAction = null;
  122. protected $_viewSuffix = 'tpl';
  123. protected $_viewBasePathSpec = '{globalDir}';
  124. protected $_viewScriptPathSpec = '{moduleDir}/views/scripts/{controller}/{action}.{suffix}';
  125. // Main
  126. public function __construct(Zend_View_Interface $view = null)
  127. {
  128. if( null !== $view )
  129. {
  130. $this->setView($view);
  131. }
  132. }
  133. public function init()
  134. {
  135. if( $this->getFrontController()->getParam('noViewRenderer') )
  136. {
  137. return;
  138. }
  139. $this->initView();
  140. }
  141. public function initView($path = null, $prefix = null, array $options = array())
  142. {
  143. if( null === $this->view )
  144. {
  145. throw new Engine_Exception('No view set in ViewRenderer');
  146. }
  147. // Reset some flags every time
  148. $options['noController'] = (isset($options['noController'])) ? $options['noController'] : false;
  149. $options['noRender'] = (isset($options['noRender'])) ? $options['noRender'] : false;
  150. $this->_scriptAction = null;
  151. $this->_responseSegment = null;
  152. // Set options first; may be used to determine other initializations
  153. $this->_setOptions($options);
  154. // Get base view path
  155. $path = APPLICATION_PATH.DS.'application'.DS.'modules';
  156. // Get class path
  157. $request = $this->getRequest();
  158. $module = $request->getModuleName();
  159. if (null === $module) {
  160. $module = $this->getFrontController()->getDispatcher()->getDefaultModule();
  161. }
  162. $classPath = APPLICATION_PATH.DS.'application'.DS.'modules'.DS.$module.DS.'View'.DS;
  163. // Get class prefix
  164. if( null === $prefix )
  165. {
  166. $prefix = ucfirst($module).'_View_';
  167. }
  168. // Determine if this path has already been registered
  169. $currentPaths = $this->view->getScriptPaths();
  170. $path = str_replace(array('/', '\\'), '/', $path);
  171. $pathExists = false;
  172. foreach ($currentPaths as $tmpPath) {
  173. $tmpPath = str_replace(array('/', '\\'), '/', $tmpPath);
  174. if (strstr($tmpPath, $path)) {
  175. $pathExists = true;
  176. break;
  177. }
  178. }
  179. if (!$pathExists) {
  180. $this->view->addScriptPath($path);
  181. }
  182. $this->view->addHelperPath($classPath . 'Helper', $classPrefix . 'Helper');
  183. $this->view->addFilterPath($classPath . 'Filter', $classPrefix . 'Filter');
  184. // Register view with action controller (unless already registered)
  185. if ((null !== $this->_actionController) && (null === $this->_actionController->view)) {
  186. $this->_actionController->view = $this->view;
  187. $this->_actionController->viewSuffix = $this->_viewSuffix;
  188. }
  189. }
  190. public function postDispatch()
  191. {
  192. if( $this->_shouldRender() )
  193. {
  194. $this->render();
  195. }
  196. }
  197. // Options
  198. public function setView(Zend_View_Interface $view)
  199. {
  200. $this->view = $view;
  201. return $this;
  202. }
  203. public function setRender($action = null, $name = null, $noController = null)
  204. {
  205. if (null !== $action) {
  206. $this->setScriptAction($action);
  207. }
  208. if (null !== $name) {
  209. $this->setResponseSegment($name);
  210. }
  211. if (null !== $noController) {
  212. $this->setNoController($noController);
  213. }
  214. return $this;
  215. }
  216. public function setScriptAction($name)
  217. {
  218. $this->_scriptAction = (string) $name;
  219. return $this;
  220. }
  221. public function setResponseSegment($name)
  222. {
  223. if (null === $name) {
  224. $this->_responseSegment = null;
  225. } else {
  226. $this->_responseSegment = (string) $name;
  227. }
  228. return $this;
  229. }
  230. public function setNoController($flag = true)
  231. {
  232. $this->_noController = ($flag) ? true : false;
  233. return $this;
  234. }
  235. public function getViewScript($action = null, array $vars = array())
  236. {
  237. $request = $this->getRequest();
  238. if ((null === $action) && (!isset($vars['action']))) {
  239. $action = $this->getScriptAction();
  240. if (null === $action) {
  241. $action = $request->getActionName();
  242. }
  243. $vars['action'] = $action;
  244. } elseif (null !== $action) {
  245. $vars['action'] = $action;
  246. }
  247. $script =
  248. $inflector = $this->getInflector();
  249. if ($this->getNoController() || $this->getNeverController()) {
  250. $this->_setInflectorTarget($this->getViewScriptPathNoControllerSpec());
  251. } else {
  252. $this->_setInflectorTarget($this->getViewScriptPathSpec());
  253. }
  254. return $this->_translateSpec($vars);
  255. }
  256. public function getScriptAction()
  257. {
  258. return $this->_scriptAction;
  259. }
  260. // Rendering
  261. public function render($action = null, $name = null, $noController = null)
  262. {
  263. $this->setRender($action, $name, $noController);
  264. $path = $this->getViewScript();
  265. $this->renderScript($path, $name);
  266. }
  267. // Utility
  268. protected function _setOptions(array $options)
  269. {
  270. foreach ($options as $key => $value)
  271. {
  272. switch ($key) {
  273. case 'neverRender':
  274. case 'neverController':
  275. case 'noController':
  276. case 'noRender':
  277. $property = '_' . $key;
  278. $this->{$property} = ($value) ? true : false;
  279. break;
  280. case 'responseSegment':
  281. case 'scriptAction':
  282. case 'viewBasePathSpec':
  283. case 'viewScriptPathSpec':
  284. //case 'viewScriptPathNoControllerSpec':
  285. case 'viewSuffix':
  286. $property = '_' . $key;
  287. $this->{$property} = (string) $value;
  288. break;
  289. default:
  290. break;
  291. }
  292. }
  293. return $this;
  294. }
  295. protected function _shouldRender()
  296. {
  297. return (!$this->getFrontController()->getParam('noViewRenderer')
  298. && !$this->_neverRender
  299. && !$this->_noRender
  300. && (null !== $this->_actionController)
  301. && $this->getRequest()->isDispatched()
  302. && !$this->getResponse()->isRedirect()
  303. );
  304. }
  305. *
  306. */
  307. }