El campo men_id es el identificador de la tabla que contiene la información del menu como titulo, contenido, fecha de creación,etc. el campo nivel es el grado de profundidad de la opción en la estructura de etiquetas html. nivel 0 es el enlace de primer orden.
Creamos el modelo que interactúa con la tabla menudinamicos.
1
<?php
2
3 class Application_Model_DbTable_Menudinamico extends Zend_Db_Table_Abstract
4 {
5
6 protected $_name = 'menudinamicos';
7 protected $_primary = 'id';
8 protected $_menu = array();
9 protected $_path;
10
11 public function baseUrl()
12 {
13
14 $ruta = $front = Zend_Controller_Front::getInstance();
15 $this->_path = $ruta->getBaseUrl();
16 }
17
18 public function menuPrincipal()
19 {
20
21 $this->_menu[] = '<div class="navbar navbar-fixed-top">
22 <div class="navbar-inner">
23 <div class="container-fluid">
24 <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
25 <span class="icon-bar"></span>
26 <span class="icon-bar"></span>
27 <span class="icon-bar"></span>
28 </a>
29 <a class="brandImg" href="#"><img src="/img/logo.png"></a>
30 <div class="btn-group pull-right">
31 <a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
32 <i class="icon-user"></i> Atención al Ciudadano
33 <span class="caret"></span>
34 </a>
35 <ul class="dropdown-menu">
36 <li><a href="/index/">Quejas y Reclamos</a></li>
37 <li class="divider"></li>
38 <li><a href="/index/id/8">Preguntas Frecuentes</a></li>
39 <li><a href="/index/id/2">Glosario</a></li>
40 <li><a href="http://www.colombiaparticipa.com/armenia/?pagina=contratos.php">Contratación COVI</a></li>
41 <li><a href="/index/id/5">Registrarse</a></li>
42 </ul>
43 </div>';
44
45 $this->_menu[] = '<div class="nav-collapse">
46 <ul class="nav">
47 <li class="active"><a href="/index"><i class="icon-home"> </i> Inicio</a></li>
48 <li><a href="index/menu/id/mapa"><i class="icon-list-alt"></i> Mapa del Sitio</a></li>
49 <li><a href="index/menu/id/correo"><i class="icon-envelope"></i> Correo Institucional</a></li>
50 <li><a href="index/menu/id/glosario"><i class="icon-question-sign"></i> Glosario</a></li>
51 <li class="dropdown">
52 <a data-toggle="dropdown" class="dropdown-toggle" href="#">
53 <i class="icon-info-sign"></i> Menú Principal<b class="caret"></b></a>';
54
55 $this->_menu[] = '<ul class="dropdown-menu">';
56 $query = $this->select();
57 $query->where('eliminar = ?', '0');
58 $query->where('nivel = ?','0');
59 $results = $this->fetchAll($query);
60 foreach($results AS $result) {
61
62 $sql = "SELECT COUNT(*) FROM menudinamicos where eliminar = 0 and nivel =1 and parent =".$result->id;
63 $resul = $this->fetchAll($sql);
64 $rowCount = count($resul);
65 if($rowCount == 0){
66
67 $this->_menu[] = ' <li><a href="'.$this->_path.'/index/menu/id/'.$result->men_id.'">'.$result->nombre.'</a></li>';
68 // opciones de nivel 0 sin Submenu
69 }else{
70
71 // opciones de nivel 0 CON SUBMENU
72 // aqui entran ROW[]-> ID = 1-3-8-9
73 $this->_menu[] = ' <li class="dropdown submenu"><a href="#" class="dropdown-toggle" data-toggle="dropdown">'.$result->nombre.'</a>';
74 $query = $this->select();
75 $query->where('eliminar = ?', '0');
76 $query->where('nivel = ?','1');
77 $query->where('parent = ?',$result->id);
78
79 $resul = $this->fetchAll($query);
80 $this->_menu[] ='<ul class="dropdown-menu submenu-show submenu-hide">';
81 foreach($resul AS $subnivelOne) {
82 $sql = "SELECT COUNT(*) FROM menudinamicos where eliminar = 0 and nivel =2 and parent =".$subnivelOne->id;
83 $resultOne = $this->fetchAll($sql);
84 $rowCountOne = count($resultOne);
85 if($rowCountOne==0){
86 // Opciones de Nivel 1 sin SubMenu Nivel 2
87 $this->_menu[]='<li><a href="'.$this->_path.'/index/menu/id/'.$subnivelOne->men_id.'">'.$subnivelOne->nombre.'</a></li>';
88
89 }else{
90
91 $this->_menu[] ='<li class="dropdown submenu"><a href="#" class="dropdown-toggle" data-toggle="dropdown">'.$subnivelOne->nombre.'</a>';
92 $this->_menu[] ='<ul class="dropdown-menu submenu-show submenu-hide">';
93 $query = $this->select();
94 $query->where('eliminar = ?', '0');
95 $query->where('nivel = ?','2');
96 $query->where('parent = ?',$subnivelOne->id);
97 $resul = $this->fetchAll($query);
98
99 foreach($resul AS $subnivelTwo) {
100
101 $sql = "SELECT COUNT(*) FROM menudinamicos where eliminar = 0 and nivel =3 and parent =".$subnivelTwo->id;
102 $resultTwo = $this->fetchAll($sql);
103 $rowCountTwo = count($resultTwo);
104 if($rowCountTwo==0){
105 $this->_menu[] ='<li><a href="'.$this->_path.'/index/menu/id/'.$subnivelTwo->men_id.'">'.$subnivelTwo->nombre.'</a></li>';
106 }else{
107 $this->_menu[] ='<li class="dropdown submenu"><a href="#" class="dropdown-toggle" data-toggle="dropdown">'.$subnivelTwo->nombre.'</a>';
108 $this->_menu[] ='<ul class="dropdown-menu submenu-show submenu-hide">';
109 $query = $this->select();
110 $query->where('eliminar = ?', '0');
111 $query->where('nivel = ?','3');
112 $query->where('parent = ?',$subnivelTwo->id);
113 $resul = $this->fetchAll($query);
114 foreach($resul AS $subnivelThree) {
115
116 $this->_menu[] ='<li><a href="'.$this->_path.'/index/menu/id/'.$subnivelThree->men_id.'">'.$subnivelThree->nombre.'</a></li>';
117
118 }
119
120 $this->_menu[] ='</ul>';
121 $this->_menu[] = '</li>';
122 }
123
124
125 }// final foreach subnivelTwo
126 $this->_menu[] ='</ul>';
127 $this->_menu[] = '</li>';
128 }
129
130
131
132 }// fin foreach interno subnivel 1
133
134 $this->_menu[]='</ul>';
135 $this->_menu[]= '</li>';
136
137 }//fin del else
138
139
140 }// foreach principal
141
142 $this->_menu[] = '</ul><!--/class="dropdown-menu -->';
143 $this->_menu[] = '</li><!--/class="dropdown -->';
144 $this->_menu[] = '</ul><!--/class NAV -->';
145 $this->_menu[] = '<form class="navbar-search pull-left">'
146 .'<input type="text" class="search-query" placeholder="Buscar..."></form>'
147 .'</div></div></div></div><!--/.nav-collapse -->';
148
149 return $this->_menu;
150
151 }
152
153
154
155 }
En el Bootstrapprotected function _initDatabase()
{
date_default_timezone_set('America/Bogota');
$dbResource = $this->getPluginResource('db');
$this->_db = $dbResource->getDbAdapter();
Zend_Registry::set('db', $this->_db);
}
protected function _initMenuPrincipal()
{
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
$dbAdapter = Zend_Registry::get("db");
$menu = new Application_Model_DbTable_Menudinamico($dbAdapter);
$navigation = $menu->menuPrincipal();
$view->menuBootrap = $navigation;
}
En el Layout:
foreach ($this->menuBootrap as $item) {
echo $item;
}
Listo !! quedo atento a sus comentarios