Skip to content

Commit

Permalink
Making Helper::viewEntity accept any number of levels
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo authored and markstory committed Oct 22, 2009
1 parent 31f0cdc commit 2941e89
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 7 deletions.
Empty file removed app/tmp/cache/models/empty
Empty file.
Empty file removed app/tmp/cache/persistent/empty
Empty file.
Empty file removed app/tmp/cache/views/empty
Empty file.
44 changes: 37 additions & 7 deletions cake/libs/view/helper.php
Expand Up @@ -365,7 +365,7 @@ function setEntity($entity, $setScope = false) {
} elseif (join('.', $view->entity()) == $entity) {
return;
}

if ($entity === null) {
$view->model = null;
$view->association = null;
Expand All @@ -381,18 +381,31 @@ function setEntity($entity, $setScope = false) {
if (empty($parts)) {
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]))) {
Expand Down Expand Up @@ -451,6 +464,23 @@ function setEntity($entity, $setScope = false) {
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)) {
Expand Down
40 changes: 40 additions & 0 deletions cake/tests/cases/libs/view/helper.test.php
Expand Up @@ -592,5 +592,45 @@ function testClean() {
$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');
}


}
?>

0 comments on commit 2941e89

Please sign in to comment.