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
This commit is contained in:
nate 2006-11-25 08:55:16 +00:00
parent 1fa21aa551
commit 41c79da1c3
7 changed files with 147 additions and 83 deletions

View file

@ -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.

View file

@ -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 = '<!--cachetime:' . $cacheTime . '--><?php
loadController(\'' . $this->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)

View file

@ -45,46 +45,49 @@ class HtmlHelper extends AppHelper {
*
* @var array
*/
var $tags = array('metalink' => '<link href="%s" title="%s"%s />',
'link' => '<a href="%s" %s>%s</a>',
'mailto' => '<a href="mailto:%s" %s>%s</a>',
'form' => '<form %s>',
'input' => '<input name="data[%s][%s]" %s/>',
'textarea' => '<textarea name="data[%s][%s]" %s>%s</textarea>',
'hidden' => '<input type="hidden" name="data[%s][%s]" %s/>',
'textarea' => '<textarea name="data[%s][%s]" %s>%s</textarea>',
'checkbox' => '<input type="checkbox" name="data[%s][%s]" %s/>',
'radio' => '<input type="radio" name="data[%s][%s]" id="%s" %s />%s',
'selectstart' => '<select name="data[%s][%s]" %s>',
'selectmultiplestart' => '<select name="data[%s][%s][]" %s>',
'selectempty' => '<option value="" %s>&nbsp;</option>',
'selectoption' => '<option value="%s" %s>%s</option>',
'selectend' => '</select>',
'optiongroup' => '<optgroup label="%s"%s>',
'optiongroupend' => '</optgroup>',
'password' => '<input type="password" name="data[%s][%s]" %s/>',
'file' => '<input type="file" name="data[%s][%s]" %s/>',
'file_no_model' => '<input type="file" name="%s" %s/>',
'submit' => '<input type="submit" %s/>',
'submitimage' => '<input type="image" src="%s" %s/>',
'image' => '<img src="%s" %s/>',
'tableheader' => '<th%s>%s</th>',
'tableheaderrow' => '<tr%s>%s</tr>',
'tablecell' => '<td%s>%s</td>',
'tablerow' => '<tr%s>%s</tr>',
'block' => '<div%s>%s</div>',
'blockstart' => '<div%s>',
'blockend' => '</div>',
'para' => '<p%s>%s</p>',
'parastart' => '<p%s>',
'label' => '<label for="%s"%s>%s</label>',
'fieldset' => '<fieldset><legend>%s</legend>%s</fieldset>',
'fieldsetstart' => '<fieldset><legend>%s</legend>',
'fieldsetend' => '</fieldset>',
'legend' => '<legend>%s</legend>',
'css' => '<link rel="%s" type="text/css" href="%s" %s/>',
'style' => '<style type="text/css"%s>%s</style>',
'charset' => '<meta http-equiv="Content-Type" content="text/html; charset=%s" />');
var $tags = array(
'metalink' => '<link href="%s" title="%s"%s />',
'link' => '<a href="%s" %s>%s</a>',
'mailto' => '<a href="mailto:%s" %s>%s</a>',
'form' => '<form %s>',
'formend' => '</form>',
'input' => '<input name="data[%s][%s]" %s/>',
'textarea' => '<textarea name="data[%s][%s]" %s>%s</textarea>',
'hidden' => '<input type="hidden" name="data[%s][%s]" %s/>',
'textarea' => '<textarea name="data[%s][%s]" %s>%s</textarea>',
'checkbox' => '<input type="checkbox" name="data[%s][%s]" %s/>',
'radio' => '<input type="radio" name="data[%s][%s]" id="%s" %s />%s',
'selectstart' => '<select name="data[%s][%s]" %s>',
'selectmultiplestart' => '<select name="data[%s][%s][]" %s>',
'selectempty' => '<option value="" %s>&nbsp;</option>',
'selectoption' => '<option value="%s" %s>%s</option>',
'selectend' => '</select>',
'optiongroup' => '<optgroup label="%s"%s>',
'optiongroupend' => '</optgroup>',
'password' => '<input type="password" name="data[%s][%s]" %s/>',
'file' => '<input type="file" name="data[%s][%s]" %s/>',
'file_no_model' => '<input type="file" name="%s" %s/>',
'submit' => '<input type="submit" %s/>',
'submitimage' => '<input type="image" src="%s" %s/>',
'image' => '<img src="%s" %s/>',
'tableheader' => '<th%s>%s</th>',
'tableheaderrow' => '<tr%s>%s</tr>',
'tablecell' => '<td%s>%s</td>',
'tablerow' => '<tr%s>%s</tr>',
'block' => '<div%s>%s</div>',
'blockstart' => '<div%s>',
'blockend' => '</div>',
'para' => '<p%s>%s</p>',
'parastart' => '<p%s>',
'label' => '<label for="%s"%s>%s</label>',
'fieldset' => '<fieldset><legend>%s</legend>%s</fieldset>',
'fieldsetstart' => '<fieldset><legend>%s</legend>',
'fieldsetend' => '</fieldset>',
'legend' => '<legend>%s</legend>',
'css' => '<link rel="%s" type="text/css" href="%s" %s/>',
'style' => '<style type="text/css"%s>%s</style>',
'charset' => '<meta http-equiv="Content-Type" content="text/html; charset=%s" />'
);
/**
* 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;

View file

@ -136,7 +136,8 @@ class JavascriptHelper extends AppHelper {
if ($inline) {
return $out;
} else {
$this->view->addScript($out);
$view =& ClassRegistry::getObject('_view_');
$view->addScript($out);
}
}
/**

View file

@ -124,8 +124,10 @@ class RssHelper extends XmlHelper {
* @return string An RSS <channel />
*/
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'] = '/';

View file

@ -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
*

View file

@ -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);
}
}