Преглед на файлове

Merge r25214 to 1.12 release branch

git-svn-id: http://framework.zend.com/svn/framework/standard/branches/release-1.12@25215 44c647ce-9c0f-0410-b52a-842ac1e357ba
frosch преди 13 години
родител
ревизия
b50779e176

+ 117 - 19
library/Zend/View/Helper/Navigation/Menu.php

@@ -59,6 +59,20 @@ class Zend_View_Helper_Navigation_Menu
     protected $_activeClass = 'active';
 
     /**
+     * CSS class to use for the parent li element
+     *
+     * @var string
+     */
+    protected $_parentClass = 'menu-parent';
+
+    /**
+     * Whether parent li elements should be rendered with parent class
+     *
+     * @var bool
+     */
+    protected $_renderParentClass = false;
+
+    /**
      * Whether only active branch should be rendered
      *
      * @var bool
@@ -191,6 +205,55 @@ class Zend_View_Helper_Navigation_Menu
     }
 
     /**
+     * Sets CSS class to use for the parent li elements when rendering
+     *
+     * @param  string $parentClass              CSS class to set to parents
+     * @return Zend_View_Helper_Navigation_Menu fluent interface, returns self
+     */
+    public function setParentClass($parentClass)
+    {
+        if (is_string($parentClass)) {
+            $this->_parentClass = $parentClass;
+        }
+
+        return $this;
+    }
+
+    /**
+     * Returns CSS class to use for the parent lie elements when rendering
+     *
+     * @return string CSS class
+     */
+    public function getParentClass()
+    {
+        return $this->_parentClass;
+    }
+
+    /**
+     * Enables/disables rendering of parent class to the li element
+     *
+     * @param bool $flag                        [optional] render with parent
+     *                                          class. Default is true.
+     * @return Zend_View_Helper_Navigation_Menu fluent interface, returns self
+     */
+    public function setRenderParentClass($flag = true)
+    {
+        $this->_renderParentClass = (bool) $flag;
+        return $this;
+    }
+
+    /**
+     * Returns flag indicating whether parent class should be rendered to the li
+     * element
+     *
+     * @return bool  whether parent class should be rendered
+     */
+    public function getRenderParentClass()
+    {
+        return $this->_renderParentClass;
+    }
+
+    /**
      * Sets a flag indicating whether only active branch should be rendered
      *
      * @param  bool $flag                        [optional] render only active
@@ -433,6 +496,13 @@ class Zend_View_Helper_Navigation_Menu
             $options['activeClass'] = $this->getActiveClass();
         }
 
+        // Parent class
+        if (isset($options['parentClass']) && $options['parentClass'] !== null) {
+            $options['parentClass'] = (string) $options['parentClass'];
+        } else {
+            $options['parentClass'] = $this->getParentClass();
+        }
+
         // Minimum depth
         if (array_key_exists('minDepth', $options)) {
             if (null !== $options['minDepth']) {
@@ -465,11 +535,16 @@ class Zend_View_Helper_Navigation_Menu
             $options['expandSiblingNodesOfActiveBranch'] = $this->getExpandSiblingNodesOfActiveBranch();
         }
 
-        // Render parents
+        // Render parents?
         if (!isset($options['renderParents'])) {
             $options['renderParents'] = $this->getRenderParents();
         }
 
+        // Render parent class?
+        if (!isset($options['renderParentClass'])) {
+            $options['renderParentClass'] = $this->getRenderParentClass();
+        }
+
         // Add page CSS class to LI element
         if (!isset($options['addPageClassToLi'])) {
             $options['addPageClassToLi'] = $this->getAddPageClassToLi();
@@ -495,6 +570,9 @@ class Zend_View_Helper_Navigation_Menu
      *                                                      page to li element
      * @param  string|null               $activeClass       CSS class for active
      *                                                      element
+     * @param  string                    $parentClass       CSS class for parent
+     *                                                      li's
+     * @param  bool                      $renderParentClass Render parent class?
      * @return string                                       rendered menu (HTML)
      */
     protected function _renderDeepestMenu(Zend_Navigation_Container $container,
@@ -504,7 +582,9 @@ class Zend_View_Helper_Navigation_Menu
                                           $maxDepth,
                                           $ulId,
                                           $addPageClassToLi,
-                                          $activeClass)
+                                          $activeClass,
+                                          $parentClass,
+                                          $renderParentClass)
     {
         if (!$active = $this->findActive($container, $minDepth - 1, $maxDepth)) {
             return '';
@@ -518,7 +598,7 @@ class Zend_View_Helper_Navigation_Menu
         } else if (!$active['page']->hasPages()) {
             // found pages has no children; render siblings
             $active['page'] = $active['page']->getParent();
-        } else if (is_int($maxDepth) && $active['depth'] +1 > $maxDepth) {
+        } else if (is_int($maxDepth) && $active['depth'] + 1 > $maxDepth) {
             // children are below max depth; render siblings
             $active['page'] = $active['page']->getParent();
         }
@@ -584,6 +664,9 @@ class Zend_View_Helper_Navigation_Menu
      *                                                      page to li element
      * @param  string|null               $activeClass       CSS class for active
      *                                                      element
+     * @param  string                    $parentClass       CSS class for parent
+     *                                                      li's
+     * @param  bool                      $renderParentClass Render parent class?
      * @return string                                       rendered menu (HTML)
      */
     protected function _renderMenu(Zend_Navigation_Container $container,
@@ -595,7 +678,9 @@ class Zend_View_Helper_Navigation_Menu
                                    $expandSibs,
                                    $ulId,
                                    $addPageClassToLi,
-                                   $activeClass)
+                                   $activeClass,
+                                   $parentClass,
+                                   $renderParentClass)
     {
         $html = '';
 
@@ -661,7 +746,7 @@ class Zend_View_Helper_Navigation_Menu
             }
 
             // make sure indentation is correct
-            $depth -= $minDepth;
+            $depth   -= $minDepth;
             $myIndent = $indent . str_repeat('        ', $depth);
 
             if ($depth > $prevDepth) {
@@ -701,19 +786,28 @@ class Zend_View_Helper_Navigation_Menu
             }
 
             // render li tag and page
-            $liClass = '';
-            if ($isActive && $addPageClassToLi) {
-                $liClass = $this->_htmlAttribs(
-                    array('class' => $activeClass . ' ' . $page->getClass())
-                );
-            } else if ($isActive) {
-                $liClass = $this->_htmlAttribs(array('class' => $activeClass));
-            } else if ($addPageClassToLi) {
-                $liClass = $this->_htmlAttribs(
-                    array('class' => $page->getClass())
-                );
+            $liClasses = array();
+            // Is page active?
+            if ($isActive) {
+                $liClasses[] = $activeClass;
+            }
+            // Add CSS class from page to LI?
+            if ($addPageClassToLi) {
+                $liClasses[] = $page->getClass();
             }
-            $html .= $myIndent . '    <li' . $liClass . '>' . self::EOL
+            // Add CSS class for parents to LI?
+            if ($renderParentClass && $page->hasChildren()) {
+                // Check max depth
+                if ((is_int($maxDepth) && ($depth + 1 < $maxDepth))
+                    || !is_int($maxDepth)
+                ) {
+                    $liClasses[] = $parentClass;
+                }
+            }
+
+            $html .= $myIndent . '    <li'
+                   . $this->_htmlAttribs(array('class' => implode(' ', $liClasses)))
+                   . '>' . self::EOL
                    . $myIndent . '        ' . $this->htmlify($page) . self::EOL;
 
             // store as previous depth for next iteration
@@ -769,7 +863,9 @@ class Zend_View_Helper_Navigation_Menu
                 $options['maxDepth'],
                 $options['ulId'],
                 $options['addPageClassToLi'],
-                $options['activeClass']
+                $options['activeClass'],
+                $options['parentClass'],
+                $options['renderParentClass']
             );
         } else {
             $html = $this->_renderMenu(
@@ -782,7 +878,9 @@ class Zend_View_Helper_Navigation_Menu
                 $options['expandSiblingNodesOfActiveBranch'],
                 $options['ulId'],
                 $options['addPageClassToLi'],
-                $options['activeClass']
+                $options['activeClass'],
+                $options['parentClass'],
+                $options['renderParentClass']
             );
         }
 

+ 103 - 0
tests/Zend/View/Helper/Navigation/MenuTest.php

@@ -805,4 +805,107 @@ class Zend_View_Helper_Navigation_MenuTest
         $this->assertContains('<li class="current">', $html);
         $this->assertNotContains('<li class="active">', $html);
     }
+
+    /**
+     * @group ZF-8951
+     */
+    public function testSetRenderParentClass()
+    {
+        $this->_helper->setRenderParentClass(true);
+
+        $this->assertTrue($this->_helper->getRenderParentClass());
+    }
+
+    /**
+     * @group ZF-8951
+     */
+    public function testSetParentClass()
+    {
+        $this->_helper->setParentClass('foo');
+
+        $this->assertEquals('foo', $this->_helper->getParentClass());
+    }
+
+    /**
+     * @group ZF-8951
+     */
+    public function testOptionRenderParentClass()
+    {
+        $expected = $this->_getExpected('menu/parentclass_standard.html');
+        $actual   = $this->_helper->renderMenu(
+            null ,
+            array(
+                 'renderParentClass' => true,
+            )
+        );
+
+        $this->assertEquals($expected, $actual);
+    }
+
+    /**
+     * @group ZF-8951
+     */
+    public function testOptionRenderParentClassAndParentClass()
+    {
+        $expected = $this->_getExpected('menu/parentclass_custom.html');
+        $actual   = $this->_helper->renderMenu(
+            null ,
+            array(
+                 'renderParentClass' => true,
+                 'parentClass'       => 'foo',
+            )
+        );
+
+        $this->assertEquals($expected, $actual);
+    }
+
+    /**
+     * @group ZF-8951
+     */
+    public function testRenderingWithStandardParentClass()
+    {
+        $this->_helper->setRenderParentClass(true);
+        $expected = $this->_getExpected('menu/parentclass_standard.html');
+
+        $this->assertEquals($expected, $this->_helper->render());
+    }
+
+    /**
+     * @group ZF-8951
+     */
+    public function testRenderingWithCustomParentClass()
+    {
+        $this->_helper->setRenderParentClass(true);
+        $this->_helper->setParentClass('foo');
+        $expected = $this->_getExpected('menu/parentclass_custom.html');
+
+        $this->assertEquals($expected, $this->_helper->render());
+    }
+
+    /**
+     * @group ZF-8951
+     */
+    public function testRenderingWithParentClassAndBothDepts()
+    {
+        $this->_helper->setRenderParentClass(true);
+
+        $expected = $this->_getExpected('menu/parentclass_bothdepts.html');
+        $actual   = $this->_helper->setMinDepth(1)->setMaxDepth(2)->render();
+
+        $this->assertEquals($expected, $actual);
+    }
+
+    /**
+     * @group ZF-8951
+     */
+    public function testRenderingWithParentClassAndOnlyActiveBranchAndBothDepts()
+    {
+        $this->_helper->setRenderParentClass(true);
+        $this->_helper->setOnlyActiveBranch(true);
+
+        $expected = $this->_getExpected('menu/parentclass_onlyactivebranch_bothdepts.html');
+        $actual   = $this->_helper->setMinDepth(1)->setMaxDepth(2)->render();
+
+        $this->assertEquals($expected, $actual);
+    }
 }

+ 52 - 0
tests/Zend/View/Helper/Navigation/_files/expected/menu/parentclass_bothdepts.html

@@ -0,0 +1,52 @@
+<ul class="navigation">
+    <li>
+        <a href="page1/page1_1">Page 1.1</a>
+    </li>
+    <li>
+        <a href="page2/page2_1">Page 2.1</a>
+    </li>
+    <li class="active menu-parent">
+        <a href="page2/page2_2">Page 2.2</a>
+        <ul>
+            <li>
+                <a href="page2/page2_2/page2_2_1">Page 2.2.1</a>
+            </li>
+            <li class="active">
+                <a href="page2/page2_2/page2_2_2">Page 2.2.2</a>
+            </li>
+        </ul>
+    </li>
+    <li class="active menu-parent">
+        <a href="page2/page2_3">Page 2.3</a>
+        <ul>
+            <li>
+                <a href="page2/page2_3/page2_3_1">Page 2.3.1</a>
+            </li>
+            <li class="active">
+                <a href="page2/page2_3/page2_3_3">Page 2.3.3</a>
+            </li>
+        </ul>
+    </li>
+    <li>
+        <a href="page3/page3_1">Page 3.1</a>
+    </li>
+    <li class="menu-parent">
+        <a href="page3/page3_2">Page 3.2</a>
+        <ul>
+            <li>
+                <a href="page3/page3_2/page3_2_1">Page 3.2.1</a>
+            </li>
+            <li>
+                <a href="page3/page3_2/page3_2_2">Page 3.2.2</a>
+            </li>
+        </ul>
+    </li>
+    <li class="menu-parent">
+        <a href="page3/page3_3">Page 3.3</a>
+        <ul>
+            <li>
+                <a href="page3/page3_3/page3_3_2">Page 3.3.2</a>
+            </li>
+        </ul>
+    </li>
+</ul>

+ 81 - 0
tests/Zend/View/Helper/Navigation/_files/expected/menu/parentclass_custom.html

@@ -0,0 +1,81 @@
+<ul class="navigation">
+    <li>
+        <a title="Go home" href="index">Home</a>
+    </li>
+    <li class="foo">
+        <a href="page1">Page 1</a>
+        <ul>
+            <li>
+                <a href="page1/page1_1">Page 1.1</a>
+            </li>
+        </ul>
+    </li>
+    <li class="active foo">
+        <a href="page2">Page 2</a>
+        <ul>
+            <li>
+                <a href="page2/page2_1">Page 2.1</a>
+            </li>
+            <li class="active foo">
+                <a href="page2/page2_2">Page 2.2</a>
+                <ul>
+                    <li>
+                        <a href="page2/page2_2/page2_2_1">Page 2.2.1</a>
+                    </li>
+                    <li class="active">
+                        <a href="page2/page2_2/page2_2_2">Page 2.2.2</a>
+                    </li>
+                </ul>
+            </li>
+            <li class="active foo">
+                <a href="page2/page2_3">Page 2.3</a>
+                <ul>
+                    <li>
+                        <a href="page2/page2_3/page2_3_1">Page 2.3.1</a>
+                    </li>
+                    <li class="active foo">
+                        <a href="page2/page2_3/page2_3_3">Page 2.3.3</a>
+                        <ul>
+                            <li class="active">
+                                <a href="page2/page2_3/page2_3_3/1">Page 2.3.3.1</a>
+                            </li>
+                            <li class="active">
+                                <a href="page2/page2_3/page2_3_3/2">Page 2.3.3.2</a>
+                            </li>
+                        </ul>
+                    </li>
+                </ul>
+            </li>
+        </ul>
+    </li>
+    <li class="foo">
+        <a href="page3">Page 3</a>
+        <ul>
+            <li>
+                <a href="page3/page3_1">Page 3.1</a>
+            </li>
+            <li class="foo">
+                <a href="page3/page3_2">Page 3.2</a>
+                <ul>
+                    <li>
+                        <a href="page3/page3_2/page3_2_1">Page 3.2.1</a>
+                    </li>
+                    <li>
+                        <a href="page3/page3_2/page3_2_2">Page 3.2.2</a>
+                    </li>
+                </ul>
+            </li>
+            <li class="foo">
+                <a href="page3/page3_3">Page 3.3</a>
+                <ul>
+                    <li>
+                        <a href="page3/page3_3/page3_3_2">Page 3.3.2</a>
+                    </li>
+                </ul>
+            </li>
+        </ul>
+    </li>
+    <li>
+        <a href="http://www.zym-project.com/">Zym</a>
+    </li>
+</ul>

+ 21 - 0
tests/Zend/View/Helper/Navigation/_files/expected/menu/parentclass_onlyactivebranch_bothdepts.html

@@ -0,0 +1,21 @@
+<ul class="navigation">
+    <li class="active menu-parent">
+        <a href="page2/page2_2">Page 2.2</a>
+        <ul>
+            <li class="active">
+                <a href="page2/page2_2/page2_2_2">Page 2.2.2</a>
+            </li>
+        </ul>
+    </li>
+    <li class="active menu-parent">
+        <a href="page2/page2_3">Page 2.3</a>
+        <ul>
+            <li>
+                <a href="page2/page2_3/page2_3_1">Page 2.3.1</a>
+            </li>
+            <li class="active">
+                <a href="page2/page2_3/page2_3_3">Page 2.3.3</a>
+            </li>
+        </ul>
+    </li>
+</ul>

+ 81 - 0
tests/Zend/View/Helper/Navigation/_files/expected/menu/parentclass_standard.html

@@ -0,0 +1,81 @@
+<ul class="navigation">
+    <li>
+        <a title="Go home" href="index">Home</a>
+    </li>
+    <li class="menu-parent">
+        <a href="page1">Page 1</a>
+        <ul>
+            <li>
+                <a href="page1/page1_1">Page 1.1</a>
+            </li>
+        </ul>
+    </li>
+    <li class="active menu-parent">
+        <a href="page2">Page 2</a>
+        <ul>
+            <li>
+                <a href="page2/page2_1">Page 2.1</a>
+            </li>
+            <li class="active menu-parent">
+                <a href="page2/page2_2">Page 2.2</a>
+                <ul>
+                    <li>
+                        <a href="page2/page2_2/page2_2_1">Page 2.2.1</a>
+                    </li>
+                    <li class="active">
+                        <a href="page2/page2_2/page2_2_2">Page 2.2.2</a>
+                    </li>
+                </ul>
+            </li>
+            <li class="active menu-parent">
+                <a href="page2/page2_3">Page 2.3</a>
+                <ul>
+                    <li>
+                        <a href="page2/page2_3/page2_3_1">Page 2.3.1</a>
+                    </li>
+                    <li class="active menu-parent">
+                        <a href="page2/page2_3/page2_3_3">Page 2.3.3</a>
+                        <ul>
+                            <li class="active">
+                                <a href="page2/page2_3/page2_3_3/1">Page 2.3.3.1</a>
+                            </li>
+                            <li class="active">
+                                <a href="page2/page2_3/page2_3_3/2">Page 2.3.3.2</a>
+                            </li>
+                        </ul>
+                    </li>
+                </ul>
+            </li>
+        </ul>
+    </li>
+    <li class="menu-parent">
+        <a href="page3">Page 3</a>
+        <ul>
+            <li>
+                <a href="page3/page3_1">Page 3.1</a>
+            </li>
+            <li class="menu-parent">
+                <a href="page3/page3_2">Page 3.2</a>
+                <ul>
+                    <li>
+                        <a href="page3/page3_2/page3_2_1">Page 3.2.1</a>
+                    </li>
+                    <li>
+                        <a href="page3/page3_2/page3_2_2">Page 3.2.2</a>
+                    </li>
+                </ul>
+            </li>
+            <li class="menu-parent">
+                <a href="page3/page3_3">Page 3.3</a>
+                <ul>
+                    <li>
+                        <a href="page3/page3_3/page3_3_2">Page 3.3.2</a>
+                    </li>
+                </ul>
+            </li>
+        </ul>
+    </li>
+    <li>
+        <a href="http://www.zym-project.com/">Zym</a>
+    </li>
+</ul>