Making Helper::viewEntity accept any number of levels

This commit is contained in:
José Lorenzo Rodríguez 2009-10-20 23:19:23 -04:30 committed by mark_story
parent 31f0cdc214
commit 2941e89da3
5 changed files with 77 additions and 7 deletions

View file

View file

View file

View file

@ -382,17 +382,30 @@ class Helper extends Overloadable {
return;
}
if (count($parts) === 1 || is_numeric($parts[0])) {
$count = count($parts);
if ($count === 1) {
$sameScope = true;
} else {
if (ClassRegistry::isKeySet($parts[0])) {
$model = $parts[0];
if (is_numeric($parts[0])) {
$sameScope = true;
}
$reverse = array_reverse($parts);
$field = array_shift($reverse);
while(!empty($reverse)) {
$subject = array_shift($reverse);
if (is_numeric($subject)) {
continue;
}
if (ClassRegistry::isKeySet($subject)) {
$model = $subject;
break;
}
}
}
if (ClassRegistry::isKeySet($model)) {
$ModelObj =& ClassRegistry::getObject($model);
for ($i = 0; $i < count($parts); $i++) {
for ($i = 0; $i < $count; $i++) {
if ($ModelObj->hasField($parts[$i]) || array_key_exists($parts[$i], $ModelObj->validate)) {
$hasField = $i;
if ($hasField === 0 || ($hasField === 1 && is_numeric($parts[0]))) {
@ -451,6 +464,23 @@ class Helper extends Overloadable {
list($view->association, $view->modelId, $view->field, $view->fieldSuffix) = $parts;
}
break;
default:
$reverse = array_reverse($parts);
if ($hasField) {
$view->field = $field;
if (!is_numeric($reverse[1]) && $reverse[1] != $model) {
$view->field = $reverse[1];
$view->fieldSuffix = $field;
}
}
if (is_numeric($parts[0])) {
$view->modelId = $parts[0];
} elseif ($view->model == $parts[0] && is_numeric($parts[1])) {
$view->modelId = $parts[1];
}
$view->association = $model;
break;
}
if (!isset($view->model) || empty($view->model)) {

View file

@ -592,5 +592,45 @@ class HelperTest extends CakeTestCase {
$result = $this->Helper->clean('&lt;script&gt;alert(document.cookie)&lt;/script&gt;');
$this->assertEqual($result, '&amp;lt;script&amp;gt;alert(document.cookie)&amp;lt;/script&amp;gt;');
}
/**
* testMultiDimensionalField method
*
* @access public
* @return void
*/
function testMultiDimensionalField() {
// PHP4 reference hack
ClassRegistry::removeObject('view');
ClassRegistry::addObject('view', $this->View);
$this->Helper->setEntity('HelperTestPost', true);
$this->Helper->setEntity('HelperTestPost.2.HelperTestComment.1.title');
$this->assertEqual($this->View->model, 'HelperTestPost');
$this->assertEqual($this->View->association, 'HelperTestComment');
$this->assertEqual($this->View->modelId,2);
$this->assertEqual($this->View->field, 'title');
$this->Helper->setEntity('HelperTestPost.1.HelperTestComment.1.HelperTestTag.1.created');
$this->assertEqual($this->View->field,'created');
$this->assertEqual($this->View->association,'HelperTestTag');
$this->assertEqual($this->View->modelId,1);
$this->Helper->setEntity('HelperTestPost.0.HelperTestComment.1.HelperTestTag.1.fake');
$this->assertEqual($this->View->model,'HelperTestPost');
$this->assertEqual($this->View->association,'HelperTestTag');
$this->assertEqual($this->View->field,null);
$this->Helper->setEntity('1.HelperTestComment.1.HelperTestTag.created.year');
$this->assertEqual($this->View->model,'HelperTestPost');
$this->assertEqual($this->View->association,'HelperTestTag');
$this->assertEqual($this->View->field,'created');
$this->assertEqual($this->View->modelId,1);
$this->assertEqual($this->View->fieldSuffix,'year');
}
}
?>