/**
  * @param  array|Config $page
  * @return AbstractPage
  */
 public function addPage($page)
 {
     if (is_array($page) || $page instanceof Config) {
         if (isset($page['route']) && !isset($page['type'])) {
             $page['type'] = "Bundle\\ZendNavigationBundle\\Page\\RouterPage";
         } else {
             if (isset($page['uri']) && !isset($page['uri'])) {
                 $page['type'] = "Bundle\\ZendNavigationBundle\\Page\\UriPage";
             }
         }
         $page = AbstractPage::factory($page);
     }
     parent::addPage($page);
     return $this;
 }
示例#2
0
 public function testToArrayMethod()
 {
     $options = array('label' => 'foo', 'uri' => 'http://www.example.com/foo.html', 'fragment' => 'bar', 'id' => 'my-id', 'class' => 'my-class', 'title' => 'my-title', 'target' => 'my-target', 'rel' => array(), 'rev' => array(), 'order' => 100, 'active' => true, 'visible' => false, 'resource' => 'joker', 'privilege' => null, 'foo' => 'bar', 'meaning' => 42, 'pages' => array(array('label' => 'foo.bar', 'uri' => 'http://www.example.com/foo.html'), array('label' => 'foo.baz', 'uri' => 'http://www.example.com/foo.html')));
     $page = AbstractPage::factory($options);
     $toArray = $page->toArray();
     // tweak options to what we expect toArray() to contain
     $options['type'] = 'Zend\\Navigation\\Page\\Uri';
     // calculate diff between toArray() and $options
     $diff = array_diff_assoc($toArray, $options);
     // should be no diff
     $this->assertEquals(array(), $diff);
     // $toArray should have 2 sub pages
     $this->assertEquals(2, count($toArray['pages']));
     // tweak options to what we expect sub page 1 to be
     $options['label'] = 'foo.bar';
     $options['fragment'] = null;
     $options['order'] = null;
     $options['id'] = null;
     $options['class'] = null;
     $options['title'] = null;
     $options['target'] = null;
     $options['resource'] = null;
     $options['active'] = false;
     $options['visible'] = true;
     unset($options['foo']);
     unset($options['meaning']);
     // assert that there is no diff from what we expect
     $subPageOneDiff = array_diff_assoc($toArray['pages'][0], $options);
     $this->assertEquals(array(), $subPageOneDiff);
     // tweak options to what we expect sub page 2 to be
     $options['label'] = 'foo.baz';
     // assert that there is no diff from what we expect
     $subPageTwoDiff = array_diff_assoc($toArray['pages'][1], $options);
     $this->assertEquals(array(), $subPageTwoDiff);
 }
示例#3
0
文件: LinksTest.php 项目: noose/zf2
    public function testAclFiltersAwayPagesFromPageProperty()
    {
        $acl = new Acl\Acl();
        $acl->addRole(new Role\GenericRole('member'));
        $acl->addRole(new Role\GenericRole('admin'));
        $acl->addResource(new Resource\GenericResource('protected'));
        $acl->allow('admin', 'protected');
        $this->_helper->setAcl($acl);
        $this->_helper->setRole($acl->getRole('member'));

        $samplePage = AbstractPage::factory(array(
            'label'    => 'An example page',
            'uri'      => 'http://www.example.com/',
            'resource' => 'protected'
        ));

        $active = $this->_helper->findOneByLabel('Home');
        $expected = array(
            'alternate'  => false,
            'stylesheet' => false,
            'start'      => false,
            'next'       => 'Page 1',
            'prev'       => false,
            'contents'   => false,
            'index'      => false,
            'glossary'   => false,
            'copyright'  => false,
            'chapter'    => 'array(4)',
            'section'    => false,
            'subsection' => false,
            'appendix'   => false,
            'help'       => false,
            'bookmark'   => false
        );
        $actual = array();

        foreach ($expected as $type => $discard) {
            $active->addRel($type, $samplePage);

            $found = $this->_helper->findRelation($active, 'rel', $type);
            if (null === $found) {
                $actual[$type] = false;
            } elseif (is_array($found)) {
                $actual[$type] = 'array(' . count($found) . ')';
            } else {
                $actual[$type] = $found->getLabel();
            }
        }

        $this->assertEquals($expected, $actual);
    }
示例#4
0
 /**
  * Determines whether a page should be accepted by ACL when iterating
  *
  * Rules:
  * - If helper has no ACL, page is accepted
  * - If page has a resource or privilege defined, page is accepted
  *   if the ACL allows access to it using the helper's role
  * - If page has no resource or privilege, page is accepted
  *
  * @param  \Zend\Navigation\AbstractPage $page  page to check
  * @return bool                        whether page is accepted by ACL
  */
 protected function _acceptAcl(AbstractPage $page)
 {
     if (!($acl = $this->getAcl())) {
         // no acl registered means don't use acl
         return true;
     }
     $role = $this->getRole();
     $resource = $page->getResource();
     $privilege = $page->getPrivilege();
     if ($resource || $privilege) {
         // determine using helper role and page resource/privilege
         return $acl->hasResource($resource) && $acl->isAllowed($role, $resource, $privilege);
     }
     return true;
 }
示例#5
0
 /**
  * Returns an escaped absolute URL for the given page
  *
  * @param  \Zend\Navigation\AbstractPage $page  page to get URL from
  * @return string
  */
 public function url(AbstractPage $page)
 {
     $href = $page->getHref();
     if (!isset($href[0])) {
         // no href
         return '';
     } elseif ($href[0] == '/') {
         // href is relative to root; use serverUrl helper
         $url = $this->getServerUrl() . $href;
     } elseif (preg_match('/^[a-z]+:/im', (string) $href)) {
         // scheme is given in href; assume absolute URL already
         $url = (string) $href;
     } else {
         // href is relative to current document; use url helpers
         $curDoc = $this->view->url();
         $curDoc = '/' == $curDoc ? '' : trim($curDoc, '/');
         $url = rtrim($this->getServerUrl(), '/') . '/' . $curDoc . (empty($curDoc) ? '' : '/') . $href;
     }
     return $this->_xmlEscape($url);
 }
示例#6
0
文件: Mvc.php 项目: alab1001101/zf2
 /**
  * Returns an array representation of the page
  *
  * @return array  associative array containing all page properties
  */
 public function toArray()
 {
     return array_merge(parent::toArray(), array('action' => $this->getAction(), 'controller' => $this->getController(), 'module' => $this->getModule(), 'params' => $this->getParams(), 'route' => $this->getRoute(), 'reset_params' => $this->getResetParams()));
 }
示例#7
0
文件: Uri.php 项目: rafalwrzeszcz/zf2
 /**
  * Returns an array representation of the page
  *
  * @return array
  */
 public function toArray()
 {
     return array_merge(parent::toArray(), array('uri' => $this->getUri()));
 }
示例#8
0
 public function testShouldFailIfUnableToDetermineType()
 {
     try {
         $page = AbstractPage::factory(array('label' => 'My Invalid Page'));
     } catch (Navigation\Exception $e) {
         return;
     }
     $this->fail('An exception has not been thrown for invalid page type');
 }
示例#9
0
文件: Menu.php 项目: heiglandreas/zf2
 /**
  * Returns an HTML string containing an 'a' element for the given page if
  * the page's href is not empty, and a 'span' element if it is empty
  *
  * Overrides {@link Zend\View\Helper\Navigation\AbstractHelper::htmlify()}.
  *
  * @param  \Zend\Navigation\AbstractPage $page  page to generate HTML for
  * @return string                      HTML string for the given page
  */
 public function htmlify(AbstractPage $page)
 {
     // get label and title for translating
     $label = $page->getLabel();
     $title = $page->getTitle();
     // translate label and title?
     if ($this->getUseTranslator() && ($t = $this->getTranslator())) {
         if (is_string($label) && !empty($label)) {
             $label = $t->translate($label);
         }
         if (is_string($title) && !empty($title)) {
             $title = $t->translate($title);
         }
     }
     // get attribs for element
     $attribs = array('id' => $page->getId(), 'title' => $title, 'class' => $page->getClass());
     // does page have a href?
     if ($href = $page->getHref()) {
         $element = 'a';
         $attribs['href'] = $href;
         $attribs['target'] = $page->getTarget();
     } else {
         $element = 'span';
     }
     return '<' . $element . $this->_htmlAttribs($attribs) . '>' . $this->view->escape($label) . '</' . $element . '>';
 }
示例#10
0
文件: Links.php 项目: noose/zf2
    /**
     * Renders the given $page as a link element, with $attrib = $relation
     *
     * @param  \Zend\Navigation\AbstractPage $page      the page to render the link for
     * @param  string               $attrib    the attribute to use for $type,
     *                                         either 'rel' or 'rev'
     * @param  string               $relation  relation type, muse be one of;
     *                                         alternate, appendix, bookmark,
     *                                         chapter, contents, copyright,
     *                                         glossary, help, home, index, next,
     *                                         prev, section, start, stylesheet,
     *                                         subsection
     * @return string                          rendered link element
     * @throws \Zend\View\Exception             if $attrib is invalid
     */
    public function renderLink(AbstractPage $page, $attrib, $relation)
    {
        if (!in_array($attrib, array('rel', 'rev'))) {
            $e = new View\Exception(sprintf(
                    'Invalid relation attribute "%s", must be "rel" or "rev"',
                    $attrib));
            $e->setView($this->view);
            throw $e;
        }

        if (!$href = $page->getHref()) {
            return '';
        }

        // TODO: add more attribs
        // http://www.w3.org/TR/html401/struct/links.html#h-12.2
        $attribs = array(
            $attrib  => $relation,
            'href'   => $href,
            'title'  => $page->getLabel()
        );

        return '<link' .
               $this->_htmlAttribs($attribs) .
               $this->getClosingBracket();
    }
示例#11
0
 /**
  * Checks if the container has the given page
  *
  * @param  \Zend\Navigation\AbstractPage $page       page to look for
  * @param  bool                 $recursive  [optional] whether to search
  *                                          recursively. Default is false.
  * @return bool                             whether page is in container
  */
 public function hasPage(AbstractPage $page, $recursive = false)
 {
     if (array_key_exists($page->hashCode(), $this->_index)) {
         return true;
     } elseif ($recursive) {
         foreach ($this->_pages as $childPage) {
             if ($childPage->hasPage($page, true)) {
                 return true;
             }
         }
     }
     return false;
 }
示例#12
0
 /**
  * Removes the given page from the container
  *
  * @param  \Zend\Navigation\AbstractPage|int $page  page to remove, either a page
  *                                         instance or a specific page order
  * @return bool                            whether the removal was
  *                                         successful
  */
 public function removePage($page)
 {
     if ($page instanceof AbstractPage) {
         $hash = $page->hashCode();
     } elseif (is_int($page)) {
         $this->_sort();
         if (!($hash = array_search($page, $this->_index))) {
             return false;
         }
     } else {
         return false;
     }
     if (isset($this->_pages[$hash])) {
         unset($this->_pages[$hash]);
         unset($this->_index[$hash]);
         $this->_dirtyIndex = true;
         return true;
     }
     return false;
 }
示例#13
0
 public function testGetChildrenShouldReturnTheCurrentPage()
 {
     $container = new Navigation\Navigation();
     $page = AbstractPage::factory(array('type' => 'uri'));
     $container->addPage($page);
     $this->assertEquals($page, $container->getChildren());
 }