1: <?php
2: class Menu {
3:
4: 5: 6: 7: 8:
9: protected $menu = array();
10:
11: 12: 13: 14: 15:
16: protected $reserved = array('parent_id', 'url');
17:
18:
19: 20: 21: 22: 23:
24: public function __construct($nivel_dir=0, $sesion=null)
25: {
26: for ($temp_dir=0; $temp_dir < $nivel_dir; $temp_dir++) {
27: $this->nivel_dir .= '../';
28: }
29: $menu_cnb = $this->add('CNB', array('url' => '#'));
30: $smenu_plan = $menu_cnb->add('Planificador', array('url'=>'app/plan'));
31: $menu_cnb->add('CNB 4to Bachillerato', array('url'=>'http://bit.ly/cnb4baco', 'externo'=>true));
32: $menu_cnb->add('CNB 5to Bachillerato', array('url'=>'http://bit.ly/cnb5baco', 'externo'=>true));
33: $menu_cnb->add('CNB Magisterio', array('url'=>'http://bit.ly/cnb4mebi', 'externo'=>true));
34: $menu_cnb->add('Herramientas de evaluación', array('url'=>'http://bit.ly/cnbeval', 'externo'=>true));
35: $menu_cnb->add('Guía de contenido FUNSEPA', array('url'=>'media/cnb/guia_contenido_fsp.docx'));
36: $menu_cnb->add('Guía de relación CNB FUNSEPA', array('url'=>'media/cnb/cnbfunsepa.xlsx'));
37:
38: $menu_seminario = $this->add('Seminario', '#');
39: $menu_seminario->add('Agenda', 'app/seminario/agenda.php');
40: $menu_seminario->add('Importancia', array('url'=>'app/seminario/importancia.php'));
41: $menu_seminario->add('Lìnea de tiempo', 'app/seminario/timeline.php');
42: $menu_seminario->add('Presentación CNB', array('url'=>'media/cnb/importancia_cnb.pptx'));
43: $menu_seminario->add('Presentación evaluación', array('url'=>'http://www.powtoon.com/show/cnV3PTUOcsh/cnb-evaluacion/', 'externo'=>true));
44:
45: if($sesion instanceof Sesion){
46: $menu_perfil = $this->add('Perfil', array('url'=>'#', 'class'=>'navbar-right'));
47: $menu_perfil->add($sesion->get('nombre')." ".$sesion->get('apellido'), array('url'=>'app/usr/'));
48: $menu_perfil->add('Cerrar sesión', 'includes/libs/logout.php');
49: }
50: }
51:
52: 53: 54: 55: 56: 57: 58:
59: public function add($title, $options_in)
60: {
61: $url = $this->getUrl($options_in);
62:
63: if(is_array($options_in)){
64: $parent_id = ( !empty($options_in['parent_id']) ) ? $options_in['parent_id'] : 0;
65: }
66:
67:
68:
69: $attr = ( is_array($options_in) ) ? $this->extractAttr($options_in) : array();
70:
71:
72: $item = new Item_menu($this, $title, $url, $attr, $parent_id);
73:
74:
75: array_push($this->menu, $item);
76:
77:
78: return $item;
79: }
80:
81:
82: 83: 84: 85: 86:
87: public function roots()
88: {
89: return $this->whereParent();
90: }
91:
92: 93: 94: 95: 96: 97:
98: public function whereParent($parent = null)
99: {
100: $respuesta = array();
101: foreach ($this->menu as $key => $item) {
102: if( $item->get_parent_id() == $parent ) {
103: array_push($respuesta, $item);
104: }
105: }
106: return $respuesta;
107:
108: }
109:
110: 111: 112: 113: 114: 115:
116: public function filter($callback)
117: {
118: if( is_callable($callback) ) {
119:
120: $this->menu = array_filter($this->menu, $callback);
121:
122: }
123:
124: return $this;
125: }
126:
127: 128: 129: 130: 131: 132: 133:
134: public function render($type = 'ul', $parent_id = null)
135: {
136: $items = '';
137:
138: $element = ( in_array($type, array('ul', 'ol', 'div')) ) ? 'li' : $type;
139:
140: foreach ($this->whereParent($parent_id) as $item)
141: {
142: $items .= " \n<{$element} {$this->parseAttr($item->attributes())}>";
143: $items .= "<a ".($item->hasChildren() ? 'class="dropdown-toggle" data-toggle="dropdown"' : '')." href=\"{$item->link->url}\"{$this->parseAttr($item->link->attributes)}>{$item->link->text}</a>";
144:
145: if( $item->hasChildren() ) {
146: $items .= "<{$type} class='dropdown-menu' data-role='menu'>";
147: $items .= $this->render($type, $item->get_id());
148: $items .= "</{$type}>";
149: }
150: $items .= "</{$element}>";
151: }
152: return $items;
153: }
154:
155: 156: 157: 158: 159: 160:
161: public function getUrl($options)
162: {
163: if( ! is_array($options) ) {
164: return $options;
165: }
166:
167: elseif ( isset($options['url']) ) {
168: if(!isset($options['externo'])){
169:
170: return $this->nivel_dir.$options['url'];
171: }
172: else{
173: return $options['url'];
174: }
175: }
176:
177: return null;
178: }
179:
180: 181: 182: 183: 184: 185:
186: public function extractAttr($options){
187: return array_diff_key($options, array_flip($this->reserved));
188: }
189:
190: 191: 192: 193: 194: 195:
196: public function parseAttr($attributes)
197: {
198: $html = array();
199: foreach ( $attributes as $key => $value)
200: {
201: if (is_numeric($key)) {
202: $key = $value;
203: }
204:
205: $element = (!is_null($value)) ? $key . '="' . $value . '"' : null;
206:
207: if (!is_null($element)) $html[] = $element;
208: }
209:
210: return count($html) > 0 ? ' ' . implode(' ', $html) : '';
211: }
212:
213:
214: 215: 216: 217: 218:
219: public function length()
220: {
221: return count($this->menu);
222: }
223:
224:
225: 226: 227: 228: 229:
230: public function asUl($attributes = array())
231: {
232: return "<ul {$this->parseAttr($attributes)}>{$this->render('ul')}</ul>";
233: }
234:
235: 236: 237: 238: 239:
240: public function asOl($attributes = array())
241: {
242: return "<ol{$this->parseAttr($attributes)}>{$this->render('ol')}</ol>";
243: }
244:
245: 246: 247: 248: 249:
250: public function asDiv($attributes = array())
251: {
252: return "<div class='element-menu' {$this->parseAttr($attributes)}>{$this->render('div')}</div>";
253: }
254:
255: public function imprimir($tipo='asUl')
256: {
257: $string = '
258: <nav class="navbar navbar-default navbar-inverse" role="navigation">
259: <div class="container-fluid">
260: <div class="navbar-header">
261: <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
262: <span class="sr-only">Cambiar navegación</span>
263: <span class="icon-bar"></span>
264: <span class="icon-bar"></span>
265: <span class="icon-bar"></span>
266: </button>
267: <a class="navbar-brand" href="'.$this->nivel_dir.'">FUNSEPA</a>
268: </div>
269:
270: <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
271: <ul class="nav navbar-nav">
272: '.$this->$tipo(array('class'=>'nav navbar-nav')).'
273: </ul>
274: <ul class="nav navbar-nav navbar-right">
275: <li><a href="#" onclick="activar_ayuda()">Ayuda</a></li>
276: </ul>
277: </div>
278:
279: </div>
280: </nav>';
281: return $string;
282: }
283:
284: }
285: ?>