forked from phiamo/MopaBootstrapBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractNavbarMenuBuilder.php
85 lines (77 loc) · 2.44 KB
/
AbstractNavbarMenuBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace Mopa\Bundle\BootstrapBundle\Navbar;
use Knp\Menu\ItemInterface;
use Knp\Menu\FactoryInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Base class for Navbar Menubuilder's which has some useful methods for bootstrap generation
* @author phiamo
*
*/
abstract class AbstractNavbarMenuBuilder
{
protected $factory;
/**
* @param FactoryInterface $factory
*/
public function __construct(FactoryInterface $factory)
{
$this->factory = $factory;
}
/**
* get a preconfigured menu item for navbar where to easily add childs
*
* @param string $title Title of the item
* @param boolean $push_right Make if float right default: true
*/
protected function createNavbarMenuItem($push_right = true){
$rootItem = $this->factory->createItem('root');
$rootItem
->setChildrenAttributes(array('class' => 'nav'))
;
if($push_right){
$this->pushRight($rootItem);
}
return $rootItem;
}
/**
* get a preconfigured Dropdown menu where to easily add childs
*
* @param string $title Title of the item
* @param boolean $push_right Make if float right default: true
*/
protected function createDropdownMenuItem(ItemInterface $rootItem, $title, $push_right = true){
$rootItem
->setAttribute('class', 'nav')
;
if($push_right){
$this->pushRight($rootItem);
}
$dropdown = $rootItem->addChild($title, array('uri'=>'#'))
->setLinkattribute('class', 'dropdown-toggle')
->setLinkattribute('data-toggle', 'dropdown')
->setAttribute('class', 'dropdown')
->setChildrenAttribute('class', 'dropdown-menu')
;
return $dropdown;
}
protected function pushRight(ItemInterface $item){
$item->setAttribute('class', 'nav pull-right');
return $item;
}
/**
* add a divider to the dropdown Menu
*
* @param ItemInterface $dropdown The dropdown Menu
* @param bool $vertical Whether to add a vertical or horizontal divider.
*
* @return ItemInterface
*/
protected function addDivider(ItemInterface $dropdown, $vertical = false){
$class = $vertical ? 'divider-vertical' : 'divider';
return $dropdown->addChild('divider_'.rand())
->setLabel('')
->setAttribute('class', $class)
;
}
}