From 41c79da1c39162ad49c8fdba87aca9773c389b0c Mon Sep 17 00:00:00 2001 From: nate Date: Sat, 25 Nov 2006 08:55:16 +0000 Subject: [PATCH] Removing direct reference between View and helpers, and explicitly defining all Helper object properties git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@3959 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/view/helper.php | 73 ++++++++++++++++---- cake/libs/view/helpers/cache.php | 29 ++++---- cake/libs/view/helpers/html.php | 97 +++++++++++++++------------ cake/libs/view/helpers/javascript.php | 3 +- cake/libs/view/helpers/rss.php | 6 +- cake/libs/view/helpers/session.php | 6 ++ cake/libs/view/view.php | 16 ++--- 7 files changed, 147 insertions(+), 83 deletions(-) diff --git a/cake/libs/view/helper.php b/cake/libs/view/helper.php index 3a87a710c..2c5b4b183 100644 --- a/cake/libs/view/helper.php +++ b/cake/libs/view/helper.php @@ -43,12 +43,30 @@ uses('overloadable'); */ class Helper extends Overloadable { +/** + * List of helpers used by this helper + * + * @var array + */ + var $helpers = null; /** * Base URL * * @var string */ var $base = null; +/** + * Webroot path + * + * @var string + */ + var $webroot = null; +/** + * Theme name + * + * @var string + */ + var $themeWeb = null; /** * URL to current action. * @@ -68,11 +86,36 @@ class Helper extends Overloadable { */ var $action = null; /** - * Enter description here... + * Plugin path + * + * @var string + */ + var $plugin = null; +/** + * POST data for models * * @var array */ var $data = null; +/** + * List of named arguments + * + * @var array + */ + var $namedArgs = null; +/** + * URL argument separator character + * + * @var string + */ + var $argSeparator = null; +/** + * Contains model validation errors of form post-backs + * + * @access public + * @var array + */ + var $validationErrors = null; /** * Holds tag templates. * @@ -85,8 +128,8 @@ class Helper extends Overloadable { * * @access protected */ - function __get__($name) {} - function __set__($name, $value) {} + function get__($name) {} + function set__($name, $value) {} function __call__($method, $params) { trigger_error('Method ' . get_class($this) . '::' . $method . ' does not exist', E_USER_ERROR); } @@ -212,19 +255,21 @@ class Helper extends Overloadable { * @param string $tagValue A field name, like "Modelname/fieldname" */ function setFormTag($tagValue) { + $view =& ClassRegistry::getObject('_view_'); $parts = explode("/", $tagValue); + if (count($parts) == 1) { - $this->view->field = $parts[0]; + $view->field = $parts[0]; } elseif (count($parts) == 2 && is_numeric($parts[0])) { - $this->view->modelId = $parts[0]; - $this->view->field = $parts[1]; + $view->modelId = $parts[0]; + $view->field = $parts[1]; } elseif (count($parts) == 2) { - $this->view->model = $parts[0]; - $this->view->field = $parts[1]; + $view->model = $parts[0]; + $view->field = $parts[1]; } elseif (count($parts) == 3) { - $this->view->model = $parts[0]; - $this->view->modelId = $parts[1]; - $this->view->field = $parts[2]; + $view->model = $parts[0]; + $view->modelId = $parts[1]; + $view->field = $parts[2]; } } /** @@ -233,7 +278,8 @@ class Helper extends Overloadable { * @return string */ function model() { - return $this->view->model; + $view =& ClassRegistry::getObject('_view_'); + return $view->model; } /** * Enter description here... @@ -241,7 +287,8 @@ class Helper extends Overloadable { * @return string */ function field() { - return $this->view->field; + $view =& ClassRegistry::getObject('_view_'); + return $view->field; } /** * Returns false if given FORM field has no errors. Otherwise it returns the constant set in the array Model->validationErrors. diff --git a/cake/libs/view/helpers/cache.php b/cake/libs/view/helpers/cache.php index 419622271..c76b92917 100644 --- a/cake/libs/view/helpers/cache.php +++ b/cake/libs/view/helpers/cache.php @@ -191,6 +191,7 @@ class CacheHelper extends AppHelper { */ function __writeFile($file, $timestamp) { $now = time(); + $view =& ClassRegistry::getObject('_view_'); if (is_numeric($timestamp)) { $cacheTime = $now + $timestamp; @@ -200,21 +201,21 @@ class CacheHelper extends AppHelper { $cache = convertSlash($this->here) . '.php'; $file = 'view->name . '\'); + loadController(\'' . $view->name . '\'); loadModels(); - $this->controller = new ' . $this->view->name . 'Controller(); - $this->helpers = unserialize(\'' . serialize($this->view->helpers) . '\'); - $this->base = \'' . $this->view->base . '\'; - $this->layout = \'' . $this->view->layout. '\'; - $this->webroot = \'' . $this->view->webroot . '\'; - $this->here = \'' . $this->view->here . '\'; - $this->namedArgs = \'' . $this->view->namedArgs . '\'; - $this->argSeparator = \'' . $this->view->argSeparator . '\'; - $this->params = unserialize(\'' . serialize($this->view->params) . '\'); - $this->action = unserialize(\'' . serialize($this->view->action) . '\'); - $this->data = unserialize(\'' . serialize($this->view->data) . '\'); - $this->themeWeb = \'' . $this->view->themeWeb . '\'; - $this->plugin = \'' . $this->view->plugin . '\'; + $this->controller = new ' . $view->name . 'Controller(); + $this->helpers = unserialize(\'' . serialize($view->helpers) . '\'); + $this->base = \'' . $view->base . '\'; + $this->layout = \'' . $view->layout. '\'; + $this->webroot = \'' . $view->webroot . '\'; + $this->here = \'' . $view->here . '\'; + $this->namedArgs = \'' . $view->namedArgs . '\'; + $this->argSeparator = \'' . $view->argSeparator . '\'; + $this->params = unserialize(\'' . serialize($view->params) . '\'); + $this->action = unserialize(\'' . serialize($view->action) . '\'); + $this->data = unserialize(\'' . serialize($view->data) . '\'); + $this->themeWeb = \'' . $view->themeWeb . '\'; + $this->plugin = \'' . $view->plugin . '\'; $loadedHelpers = array(); $loadedHelpers = $this->_loadHelpers($loadedHelpers, $this->helpers); foreach(array_keys($loadedHelpers) as $helper) diff --git a/cake/libs/view/helpers/html.php b/cake/libs/view/helpers/html.php index f4ffacf3b..4d144b631 100644 --- a/cake/libs/view/helpers/html.php +++ b/cake/libs/view/helpers/html.php @@ -45,46 +45,49 @@ class HtmlHelper extends AppHelper { * * @var array */ - var $tags = array('metalink' => '', - 'link' => '%s', - 'mailto' => '%s', - 'form' => '
', - 'input' => '', - 'textarea' => '', - 'hidden' => '', - 'textarea' => '', - 'checkbox' => '', - 'radio' => '%s', - 'selectstart' => '', - 'selectempty' => '', - 'selectoption' => '', - 'selectend' => '', - 'optiongroup' => '', - 'optiongroupend' => '', - 'password' => '', - 'file' => '', - 'file_no_model' => '', - 'submit' => '', - 'submitimage' => '', - 'image' => '', - 'tableheader' => '%s', - 'tableheaderrow' => '%s', - 'tablecell' => '%s', - 'tablerow' => '%s', - 'block' => '%s', - 'blockstart' => '', - 'blockend' => '', - 'para' => '%s

', - 'parastart' => '', - 'label' => '', - 'fieldset' => '
%s%s
', - 'fieldsetstart' => '
%s', - 'fieldsetend' => '
', - 'legend' => '%s', - 'css' => '', - 'style' => '', - 'charset' => ''); + var $tags = array( + 'metalink' => '', + 'link' => '%s', + 'mailto' => '%s', + 'form' => '', + 'formend' => '', + 'input' => '', + 'textarea' => '', + 'hidden' => '', + 'textarea' => '', + 'checkbox' => '', + 'radio' => '%s', + 'selectstart' => '', + 'selectempty' => '', + 'selectoption' => '', + 'selectend' => '', + 'optiongroup' => '', + 'optiongroupend' => '', + 'password' => '', + 'file' => '', + 'file_no_model' => '', + 'submit' => '', + 'submitimage' => '', + 'image' => '', + 'tableheader' => '%s', + 'tableheaderrow' => '%s', + 'tablecell' => '%s', + 'tablerow' => '%s', + 'block' => '%s', + 'blockstart' => '', + 'blockend' => '', + 'para' => '%s

', + 'parastart' => '', + 'label' => '', + 'fieldset' => '
%s%s
', + 'fieldsetstart' => '
%s', + 'fieldsetend' => '
', + 'legend' => '%s', + 'css' => '', + 'style' => '', + 'charset' => '' + ); /** * Base URL * @@ -206,7 +209,8 @@ class HtmlHelper extends AppHelper { if ($inline) { return $out; } else { - $this->view->addScript($out); + $view =& ClassRegistry::getObject('_view_'); + $view->addScript($out); } } /** @@ -275,7 +279,8 @@ class HtmlHelper extends AppHelper { if ($inline) { return $out; } else { - $this->view->addScript($out); + $view =& ClassRegistry::getObject('_view_'); + $view->addScript($out); } } /** @@ -647,9 +652,11 @@ class HtmlHelper extends AppHelper { * @param string $type FORM type (POST/GET). * @param array $htmlAttributes * @return string An formatted opening FORM tag. - * @deprecated This is very WYSIWYG unfriendly, use HtmlHelper::url() to get contents of "action" attribute. Version 0.9.2. + * @deprecated + * @see FormHelper::create */ function formTag($target = null, $type = 'post', $htmlAttributes = array()) { + trigger_error('(HtmlHelper::formTag) Deprecated: Use FormHelper::create instead', E_USER_WARNING); $htmlAttributes['action'] = $this->url($target); $htmlAttributes['method'] = low($type) == 'get' ? 'get' : 'post'; $type == 'file' ? $htmlAttributes['enctype'] = 'multipart/form-data' : null; @@ -691,9 +698,11 @@ class HtmlHelper extends AppHelper { * @param string $email E-mail address if different from title. * @param array $options * @return string Formatted A tag - * @deprecated This should be done using a content filter. Version 0.9.2. + * @deprecated + * @see HtmlHelper::link */ function linkEmail($title, $email = null, $options = null) { + trigger_error('(HtmlHelper::linkEmail) Deprecated: Use HtmlHelper::link instead', E_USER_WARNING); // if no $email, then title contains the email. if (empty($email)) { $email = $title; diff --git a/cake/libs/view/helpers/javascript.php b/cake/libs/view/helpers/javascript.php index 0a979ecad..3598c53e8 100644 --- a/cake/libs/view/helpers/javascript.php +++ b/cake/libs/view/helpers/javascript.php @@ -136,7 +136,8 @@ class JavascriptHelper extends AppHelper { if ($inline) { return $out; } else { - $this->view->addScript($out); + $view =& ClassRegistry::getObject('_view_'); + $view->addScript($out); } } /** diff --git a/cake/libs/view/helpers/rss.php b/cake/libs/view/helpers/rss.php index 7d800c36f..fcb6dabd2 100644 --- a/cake/libs/view/helpers/rss.php +++ b/cake/libs/view/helpers/rss.php @@ -124,8 +124,10 @@ class RssHelper extends XmlHelper { * @return string An RSS */ function channel($attrib = array(), $elements = array(), $content = null) { - if (!isset($elements['title']) && !empty($this->view->pageTitle)) { - $elements['title'] = $this->view->pageTitle; + $view =& ClassRegistry::getObject('_view_'); + + if (!isset($elements['title']) && !empty($view->pageTitle)) { + $elements['title'] = $view->pageTitle; } if (!isset($elements['link'])) { $elements['link'] = '/'; diff --git a/cake/libs/view/helpers/session.php b/cake/libs/view/helpers/session.php index ef8e1a821..f1e1b61a8 100644 --- a/cake/libs/view/helpers/session.php +++ b/cake/libs/view/helpers/session.php @@ -36,6 +36,12 @@ * */ class SessionHelper extends CakeSession { +/** + * List of helpers used by this helper + * + * @var array + */ + var $helpers = null; /** * Used to determine if methods implementation is used, or bypassed * diff --git a/cake/libs/view/view.php b/cake/libs/view/view.php index ab43c393b..71a12aa62 100644 --- a/cake/libs/view/view.php +++ b/cake/libs/view/view.php @@ -261,15 +261,14 @@ class View extends Object { */ function __construct(&$controller) { if(is_object($controller)) { - $this->controller =& $controller; - - $c = count($this->__passedVars); - for ($j = 0; $j < $c; $j++) { + $count = count($this->__passedVars); + for ($j = 0; $j < $count; $j++) { $var = $this->__passedVars[$j]; $this->{$var} = $controller->{$var}; } } parent::__construct(); + ClassRegistry::addObject('_view_', $this); } /** @@ -686,8 +685,9 @@ class View extends Object { ${$camelBackedHelper} =& $loadedHelpers[$helper]; - if (isset(${$camelBackedHelper}->helpers) && is_array(${$camelBackedHelper}->helpers)) { - foreach(${$camelBackedHelper}->helpers as $subHelper) { + if (is_array(${$camelBackedHelper}->helpers) && !empty(${$camelBackedHelper}->helpers)) { + $subHelpers = ${$camelBackedHelper}->helpers; + foreach($subHelpers as $subHelper) { ${$camelBackedHelper}->{$subHelper} =& $loadedHelpers[$subHelper]; } } @@ -781,9 +781,7 @@ class View extends Object { } $camelBackedHelper = Inflector::variable($helper); - ${$camelBackedHelper} =& new $helperCn(); - ${$camelBackedHelper}->view =& $this; $vars = array('base', 'webroot', 'here', 'params', 'action', 'data', 'themeWeb', 'plugin', 'namedArgs', 'argSeparator'); $c = count($vars); @@ -797,7 +795,7 @@ class View extends Object { $loaded[$helper] =& ${$camelBackedHelper}; - if (isset(${$camelBackedHelper}->helpers) && is_array(${$camelBackedHelper}->helpers)) { + if (is_array(${$camelBackedHelper}->helpers)) { $loaded = &$this->_loadHelpers($loaded, ${$camelBackedHelper}->helpers); } }