diff -r 000000000000 -r 4eba9c11703f web/Zend/View/Helper/Action.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/Zend/View/Helper/Action.php Mon Dec 13 18:29:26 2010 +0100 @@ -0,0 +1,164 @@ +getControllerDirectory(); + if (empty($modules)) { + require_once 'Zend/View/Exception.php'; + $e = new Zend_View_Exception('Action helper depends on valid front controller instance'); + $e->setView($this->view); + throw $e; + } + + $request = $front->getRequest(); + $response = $front->getResponse(); + + if (empty($request) || empty($response)) { + require_once 'Zend/View/Exception.php'; + $e = new Zend_View_Exception('Action view helper requires both a registered request and response object in the front controller instance'); + $e->setView($this->view); + throw $e; + } + + $this->request = clone $request; + $this->response = clone $response; + $this->dispatcher = clone $front->getDispatcher(); + $this->defaultModule = $front->getDefaultModule(); + } + + /** + * Reset object states + * + * @return void + */ + public function resetObjects() + { + $params = $this->request->getUserParams(); + foreach (array_keys($params) as $key) { + $this->request->setParam($key, null); + } + + $this->response->clearBody(); + $this->response->clearHeaders() + ->clearRawHeaders(); + } + + /** + * Retrieve rendered contents of a controller action + * + * If the action results in a forward or redirect, returns empty string. + * + * @param string $action + * @param string $controller + * @param string $module Defaults to default module + * @param array $params + * @return string + */ + public function action($action, $controller, $module = null, array $params = array()) + { + $this->resetObjects(); + if (null === $module) { + $module = $this->defaultModule; + } + + // clone the view object to prevent over-writing of view variables + $viewRendererObj = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer'); + Zend_Controller_Action_HelperBroker::addHelper(clone $viewRendererObj); + + $this->request->setParams($params) + ->setModuleName($module) + ->setControllerName($controller) + ->setActionName($action) + ->setDispatched(true); + + $this->dispatcher->dispatch($this->request, $this->response); + + // reset the viewRenderer object to it's original state + Zend_Controller_Action_HelperBroker::addHelper($viewRendererObj); + + + if (!$this->request->isDispatched() + || $this->response->isRedirect()) + { + // forwards and redirects render nothing + return ''; + } + + $return = $this->response->getBody(); + $this->resetObjects(); + return $return; + } + + /** + * Clone the current View + * + * @return Zend_View_Interface + */ + public function cloneView() + { + $view = clone $this->view; + $view->clearVars(); + return $view; + } +}