/** * Render item with all of its children. * * This renders the li tag to fit into the parent ul as well as its * own nested ul tag if this menu item has children * * @param integer $depth The depth each child should render * @return string */ protected function _renderItem(ioMenuItem $item, $depth = null) { // if we don't have access or this item is marked to not be shown if (!$item->shouldBeRendered()) { return; } // explode the class string into an array of classes $class = $item->getAttribute('class') ? explode(' ', $item->getAttribute('class')) : array(); if ($item->isCurrent()) { $class[] = 'current'; } elseif ($item->isCurrentAncestor($depth)) { $class[] = 'current_ancestor'; } if ($item->actsLikeFirst()) { $class[] = 'first'; } if ($item->actsLikeLast()) { $class[] = 'last'; } // retrieve the attributes and put the final class string back on it $attributes = $item->getAttributes(); if (count($class) > 0) { $attributes['class'] = implode(' ', $class); } // opening li tag $html = $this->_format($item->getLevel(), '<li' . _tag_options($attributes) . '>', 'li'); // render the text/link inside the li tag $html .= $this->_format($item->getLevel(), $item->getRoute() ? $item->renderLink() : $item->renderLabel(), 'link'); // renders the embedded ul if there are visible children $html .= $this->_render($item, $depth, true); // closing li tag $html .= $this->_format($item->getLevel(), '</li>', 'li'); return $html; }
/** * Overridden to specify what the child class should be */ protected function _createChild($name, $route = null, $attributes = array(), $class = null) { if ($class === null) { $class = $this->_childClass; } return parent::_createChild($name, $route, $attributes, $class); }
public function executeBigMenu(sfWebRequest $request) { // setup a menu with a variety of conditions difficult to test in unit tests $menu = new ioMenuItem('Root li', null, array('class' => 'root')); $pt1 = $menu->addChild('Parent 1', 'homepage'); $ch1 = $pt1->addChild('Child 1', '/parent1/ch1'); $ch2 = $pt1->addChild('Child 2', '/parent1/ch2'); $ch3 = $pt1->addChild('Child 3', '/parent1/ch3'); $pt2 = $menu->addChild('Parent 2'); $ch4 = $pt2->addChild('Child 4'); $gc1 = $ch4->addChild('Grandchild 1'); // setup ch4 to be the current menu $ch4->setRoute('@test_menu'); // setup ch3 to be hidden since we won't be authenticated $ch3->requiresAuth(true); // setup pt1 and ch1 to render absolutely, in two different ways $pt1->setUrlOptions(array('absolute' => true)); $ch1->setLinkOptions(array('absolute' => true, 'query_string' => 'test=1')); $this->menu = $menu; $this->setLayout(false); }
/** * Persists an ioMenuItem tree to the database. * * Typically, you'll persist your entire menu tree. This will save the root * menu item as a root in Doctrine's nested set with the whole tree under it: * * $menu = new ioMenuItem('root'); * $menu->addChild('Home', '@homepage'); * Doctrine_Core::getTable('ioDoctrineMenuItem')->persist($menu); * * You can also persist part of a tree or persist a full menu under an * existing Doctrine nested set node: * * $menu->addChild('Links'); * $menu['Links']->addChild('Sympal', 'http://www.sympalphp.org'); * $tbl = Doctrine_Core::getTable('ioDoctrineMenuItem'); * $node = $tbl->findOneByName('some name'); // find an existing node * // save the Links submenu under the above node * $tbl->persist($menu['Links'], $node); * * @param ioMenuItem $menu * @param ioDoctrineMenuItem $parentDoctrineMenu Optional parent node, else * it will save as root * @return ioDoctrineMenuItem * @throws sfException */ public function persist(ioMenuItem $menu, ioDoctrineMenuItem $parent = null) { // run a few sanity checks and create the root node if (!$parent) { // protect against people calling persist on non-root objects, which // would otherwise cause those items to persist as new roots if (!$menu->isRoot()) { throw new sfException('Non-root menu items as root items. Either persist the entire tree or pass an ioDoctrineMenuItem parent as the second argument.'); } // Make sure the root has a name if (!$menu->getName()) { throw new sfException('A root object cannot be persisted without a name. Call setName() on the root menu item to set its name'); } $root = $this->fetchRootByName($menu->getName()); if (!$root) { // create a new root $root = new ioDoctrineMenuItem(); $root->name = $menu->getName(); $root->save(); $this->getTree()->createRoot($root); } $parent = $root; } // merge in the menu data into the parent menu $parent->persistFromMenuArray($menu->toArray()); }
/** * Retrieves a menu identified by the given name. * * The name should correspond to a name on a root doctrine node * * @param string $name The name of the root node to retrieve * @return ioMenuItem|null */ public function getMenu($name, $class = 'ioDoctrineMenuItem') { $cacheKey = md5($name); if ($data = $this->getCache($cacheKey)) { $data = unserialize($data); return ioMenuItem::createFromArray($data); } $menu = Doctrine_Core::getTable($class)->fetchMenu($name); if ($menu) { $this->setCache($cacheKey, serialize($menu->toArray())); } return $menu; }
/** * Overridden to be displayed if at least one of the children menus * should be displayed * * @see ioMenuItem */ public function checkUserAccess(sfBasicSecurityUser $user = null) { $normalAccess = parent::checkUserAccess($user); // if we have no children, then just behave normally. This behavior // is intended to hide parents who have no children. if (count($this->getChildren()) == 0) { return $normalAccess; } // if this item is normally accessible, then it still should be if (!$normalAccess) { return false; } // if any of the children are accessible, then this should be also foreach ($this->getChildren() as $child) { if ($child->checkUserAccess($user)) { return true; } } return false; }
$t->is($result['columns'], array('“Root li”'), '$result[columns] = array(Root li) .'); $t->is(count($result['items']), 2, '$result[items] count is 2 (pt1, pt2).'); $t->is(count($result['items'][0]['children']), 3, '$result[items][0][items] count is 3 (ch1, ch2, ch3).'); $t->is(count($result['items'][1]['children']), 1, '$result[items][1][items] count is 1 (ch4).'); $t->is($result['items'][0]['id'], $arr['pt1']->id, '$result[items][0][id] is pt1\'s id'); $t->is($result['items'][0]['level'], 1, '$result[items][0][level] is 1'); $t->is($result['items'][0]['info'], array('<strong>Parent 1</strong>'), '$result[items][0][info] is array(Parent 1)'); $t->is($result['items'][0]['children'][0]['level'], 2, '$result[items][0][children][0][level] is 2'); */ $t->is($result['items'][0], $expected['items'][0], '->generateNestedSortableArray() returns the correctly formatted array.'); $t->info('5 - Test the i18n functionality'); $t->info(' 5.1 - Create an ioMenuItem with i18n labels and persist it'); Doctrine_Query::create()->from('ioDoctrineMenuItem')->delete()->execute(); sfConfig::set('sf_default_culture', 'en'); // make sure en is the default culture $menu = new ioMenuItem('primary'); $menu->setLabel('Homepage'); $menu->setLabel('Página principal', 'es'); $doctrineMenu = Doctrine_Core::getTable('ioDoctrineMenuItem')->persist($menu); $t->is($doctrineMenu['Translation']['en']['label'], 'Homepage', 'The default label is set on the sf_default_culture Translation.'); $t->is($doctrineMenu['Translation']['es']['label'], 'Página principal', 'The es label was saved on the es Translation.'); $t->info(' 5.2 - Fetch an i18n menu from the database'); $menu = Doctrine_Core::getTable('ioDoctrineMenuItem')->fetchMenu('primary'); $arr = $menu->toArray(); $t->is($arr['label'], 'Homepage', 'The sf_default_culture is set as the default label.'); $t->is($arr['i18n_labels'], array('en' => 'Homepage', 'es' => 'Página principal'), 'The i18n labels are loaded in correctly from the database.'); $menu->setCulture('es'); $t->is($menu->getLabel(), 'Página principal', 'For good measure, we can see that the spanish translation of the menu returns the spanish translation.');
$t->is($menu->render(), '<ul class="menu"><li class="first">c4</li><li>c3</li><li>c2</li><li class="last">c1</li></ul>', 'proper rendering after reorder'); // create the tree and make the variables available extract(create_test_tree($t, 'ioMenuItemTest')); $t->info('10 - Test copy'); check_test_tree($t, $menu); print_test_tree($t); // print the test tree $menu2 = $menu->copy(); $t->ok($menu2 !== $menu, 'menu2 is another instance then menu'); $t->ok($menu2['Parent 1'] !== $menu['Parent 1'], 'menu2->pt1 is another instance than menu->pt1'); $t->ok($menu2['Parent 1']['Child 2'] !== $menu['Parent 1']['Child 2'], 'menu2->pt1->ch2 is another instance than menu->pt1->ch2'); $t->ok($menu2['Parent 1']['Child 2']->getParent() === $menu2['Parent 1'], 'menu2->pt1->ch2->parent is same instance as menu2->pt1'); $t->ok($menu2['Parent 1']->getParent() === $menu2, 'menu2->pt1->parent is same instance as menu2'); //$t->ok($menu2['Parent 1']->getParent() !== $menu['Parent 1'], 'menu2->pt1->ch2->parent is same instance as menu->pt1'); $t->info('11 - Test slice'); $menu = new ioMenuItem('root'); $menu->addChild('c1'); $menu['c1']->addChild('gc1'); $menu['c1']->addChild('gc2'); $menu->addChild('c2'); $menu->addChild('c3'); $menu->addChild('c4'); $menu2 = $menu->slice(0, 3); $t->is(array_keys($menu2->getChildren()), array('c1', 'c2', 'c3'), 'slice 0, 3'); $menu2 = $menu->slice(0, "c3"); $t->is(array_keys($menu2->getChildren()), array('c1', 'c2', 'c3'), 'slice 0, c3'); $menu2 = $menu->slice("c1", "c3"); $t->is(array_keys($menu2->getChildren()), array('c1', 'c2', 'c3'), 'slice c1, c3'); $menu2 = $menu->slice($menu['c1'], $menu['c3']); $t->is(array_keys($menu2->getChildren()), array('c1', 'c2', 'c3'), 'slice c1, c3'); $menu2 = $menu->slice(1, 2);
/** * Moves child to specified position. Rearange other children accordingly. * * @param ioMenuItem $child Child to move. * @param numeric $position Position to move child to. */ public function moveChildToPosition(ioMenuItem $child, $position) { $name = $child->getName(); $order = array_keys($this->_children); $oldPosition = array_search($name, $order); unset($order[$oldPosition]); $order = array_values($order); array_splice($order, $position, 0, $name); $this->reorderChildren($order); }
/** * Creates an ioMenuItem tree where this object is the root * * @return ioMenuItem */ public function createMenu() { $hier = $this->_getMenuHierarchy(); $data = self::_convertHierarchyToMenuArray($hier); return ioMenuItem::createFromArray($data); }
function persist_menu(lime_test $t, ioDoctrineMenuItem $rt, ioMenuItem $menu) { $timer = new sfTimer(); $rt->persistFromMenuArray($menu->toArray()); $timer->addTime(); $rt->refresh(true); $t->info(sprintf('### Menu took %s to persist (%s nodes/min)', round($timer->getElapsedTime(), 4), floor(8 * 60 / $timer->getElapsedTime()))); }
require_once dirname(__FILE__) . '/../../../bootstrap/functional.php'; require_once $_SERVER['SYMFONY'] . '/vendor/lime/lime.php'; require_once sfConfig::get('sf_lib_dir') . '/test/unitHelper.php'; $t = new lime_test(183); $t->info('1 - Test getChildrenIndexedByName().'); extract(create_doctrine_test_tree($t)); // create the tree and make its vars accessible print_test_tree($t); $children = $rt->getChildrenIndexedByName(); $t->is(count($children), 2, '->getChildrenIndexedByName() returns 2 for rt'); $t->is(array_keys($children), array('Parent 1', 'Parent 2'), '->getChildrenIndexedByName() has the correct indexes'); $t->is($children['Parent 1']->name, 'Parent 1', '->getChildrenIndexedByName() returns the correct items.'); $t->is(count($pt1->getChildrenIndexedByName()), 3, '->getChildrenIndexedByName() returns 3 item for pt1.'); $t->is(count($pt2->getChildrenIndexedByName()), 1, '->getChildrenIndexedByName() returns 1 item for pt2.'); $t->info('2 - Test persistFromMenuArray() in a varierty of situations.'); $menu = new ioMenuItem('Root li'); $t->info(' 2.1 - First try it without any children - should just update root values.'); $t->info(' 2.1.1 - Persist a menu with mostly blank fields.'); $rt = create_root('rt'); $menu->setAttributes(array()); // clear the default "root" class attribute $rt->persistFromMenuArray($menu->toArray(false)); $t->is($rt->getName(), 'Root li', '->getName() returns "Root li".'); $t->is($rt->getLabel(), null, '->getLabel() returns null.'); $t->is($rt->getRoute(), null, '->getRoute() returns null.'); $t->is($rt->getAttributes(), '', '->getAttributes() returns an empty string.'); $t->is($rt->getRequiresAuth(), false, '->getRequiresAuth() returns false.'); $t->is($rt->getRequiresNoAuth(), false, '->getRequiresNoAuth() returns false.'); $t->is(count($rt->Permissions), 0, '->Permissions matches 0 items'); // setup some interesting values to persist $menu->setLabel('sympal');
/** * Recursively finds current item. * * @param ioMenuItem $item Item to start from * @return ioMenuItem current item or null */ protected function findCurrentItem(ioMenuItem $item) { if ($item->matchCurrentLocation($this)) { return $item; } foreach ($item->getChildren() as $child) { $current = $this->findCurrentItem($child); if ($current) { return $current; } } return null; }
extract($menu->split("c2")); $t->is(array(array_keys($primary->getChildren()), array_keys($secondary->getChildren())), array(array('c1', 'c2'), array('c3', 'c4')), 'split c2'); extract($menu->split(4)); $t->is(array(array_keys($primary->getChildren()), array_keys($secondary->getChildren())), array(array('c1', 'c2', 'c3', 'c4'), array()), 'split 4'); extract($menu->split("c4")); $t->is(array(array_keys($primary->getChildren()), array_keys($secondary->getChildren())), array(array('c1', 'c2', 'c3', 'c4'), array()), 'split c4'); extract($menu->split(5)); $t->is(array(array_keys($primary->getChildren()), array_keys($secondary->getChildren())), array(array('c1', 'c2', 'c3', 'c4'), array()), 'split 5'); extract($menu->split("c5")); $t->is(array(array_keys($primary->getChildren()), array_keys($secondary->getChildren())), array(array('c1', 'c2', 'c3', 'c4'), array()), 'split c5'); extract($menu->split(2)); $t->ok($primary['c1']->getParent() !== $secondary['c3']->getParent(), 'primary->c1 and secondary->c3 have two distinct fathers'); $t->is($primary->render(), '<ul class="menu"><li class="first">c1<ul class="menu_level_1"><li class="first">gc1</li><li class="last">gc2</li></ul></li><li class="last">c2</li></ul>', 'proper rendering of primary after slice'); $t->is($secondary->render(), '<ul class="menu"><li class="first">c3</li><li class="last">c4</li></ul>', 'proper rendering of secondary after slice'); $t->info('13 - Test menu tree property'); $menu = new ioMenuItem('root'); $menu->addChild('c1'); $menu['c1']->addChild('gc1'); $menu['c1']->addChild('gc2'); $menu->addChild('c2'); $menu->addChild('c3'); $menu->addChild('c4'); $c1 = $menu['c1']; $t->ok($menu->getTree() === $c1->getTree(), 'root and c1 has same tree'); $menu->removeChild($c1); $t->ok($menu->getTree() !== $c1->getTree(), 'root and c1 has different tree'); $t->ok($menu->getTree()->getRootItem() === $menu, 'root->getTree()->getRootItem() returns root'); $t->ok($c1['gc1']->getTree()->getRootItem() === $c1, 'c1[gc1]->getTree()->getRootItem() returns c1'); $menu->addChild($c1); $t->ok($menu->getTree() === $c1->getTree(), 'root and c1 has same tree'); $t->ok($c1['gc1']->getTree()->getRootItem() === $menu, 'c1[gc1]->getTree()->getRootItem() returns root');