From 2941e89da35b2ddfba2a417322693d13cb4d37e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Lorenzo=20Rodr=C3=ADguez?= Date: Tue, 20 Oct 2009 23:19:23 -0430 Subject: [PATCH] Making Helper::viewEntity accept any number of levels --- app/tmp/cache/models/empty | 0 app/tmp/cache/persistent/empty | 0 app/tmp/cache/views/empty | 0 cake/libs/view/helper.php | 44 ++++++++++++++++++---- cake/tests/cases/libs/view/helper.test.php | 40 ++++++++++++++++++++ 5 files changed, 77 insertions(+), 7 deletions(-) delete mode 100755 app/tmp/cache/models/empty delete mode 100755 app/tmp/cache/persistent/empty delete mode 100755 app/tmp/cache/views/empty diff --git a/app/tmp/cache/models/empty b/app/tmp/cache/models/empty deleted file mode 100755 index e69de29bb..000000000 diff --git a/app/tmp/cache/persistent/empty b/app/tmp/cache/persistent/empty deleted file mode 100755 index e69de29bb..000000000 diff --git a/app/tmp/cache/views/empty b/app/tmp/cache/views/empty deleted file mode 100755 index e69de29bb..000000000 diff --git a/cake/libs/view/helper.php b/cake/libs/view/helper.php index 038ec8b2c..ed83f9ac6 100644 --- a/cake/libs/view/helper.php +++ b/cake/libs/view/helper.php @@ -365,7 +365,7 @@ class Helper extends Overloadable { } elseif (join('.', $view->entity()) == $entity) { return; } - + if ($entity === null) { $view->model = null; $view->association = null; @@ -381,18 +381,31 @@ class Helper extends Overloadable { 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]))) { @@ -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)) { diff --git a/cake/tests/cases/libs/view/helper.test.php b/cake/tests/cases/libs/view/helper.test.php index 9bf9d37c9..f625db6d3 100644 --- a/cake/tests/cases/libs/view/helper.test.php +++ b/cake/tests/cases/libs/view/helper.test.php @@ -592,5 +592,45 @@ class HelperTest extends CakeTestCase { $result = $this->Helper->clean('<script>alert(document.cookie)</script>'); $this->assertEqual($result, '&lt;script&gt;alert(document.cookie)&lt;/script&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'); + } + + } ?> \ No newline at end of file