Current Version of Codefight CMS Available for Download:

            « Version 1.1.4 »

            NOTE: The code available is same as the code used for this site at the time of release.

            Codefight CMS is based on Codeigniter PHP Framework which is very easy to learn.

            It would be nice to hear back some feedback. Also you can contribute with code and helping translating language files in your language.

            You can use this CMS in anyway you want. You can modify as you like and use commercially for free.

 

Select Language.[TESTING For Next Release.]

English | नेपाली | French | German | Korean

Login | Select Language | Mon, 08 Feb 10 16:50:35 -0700

Multilevel menu library for codefight cms

2009-05-25 21:11:35Damodar Bashyal

 

Codefight cms is built with the help of codeigniter cms. Last weekend i wrote a multi-level menu library. You can use it for codefight cms or wherever you want.

This may not be perfect but it works. If you have a better idea i would like to know. This is how it looks like.

CODE:

1-<?php
2-//If BASEPATH is not defined, simply exit.
3- if (!defined('BASEPATH')) exit('No direct script access allowed');
4-/*
5->menus_id | menus_active | menus_parent_id | menus_link
6->menus_title | menu_type | menus_meta_title | menus_meta_keywords
7->menus_meta_description | menus_sort
8-
9-Parameters that can be passed in an array:
10-$parameters = array (
11-'ul_param' => 'class=“xyz“...',
12-'li_param' => '...',
13-'a_param' => '...',
14-)
15- multi level menu created for codefight cms by damodar bashyal
16- visit codefight.org
17- */

18- class Menu {
19-
20- var $CI = '';
21- var $menu = array();
22- var $counter = 0;
23- var $holder = array();
24- var $menu_type = 'pages';
25- var $ul_param = '';
26- var $li_param = '';
27- var $a_param = '';
28- var $rtn=FALSE;
29- var $menu_list = '';
30- var $echo_list = '';
31- var $last_id = 0;
32- var $is_last = FALSE;
33-
34- function get($parameters=array('menu_type' => 'pages'), $rtn=FALSE) {
35-
36-if(!is_array($parameters)) $parameters = array($parameters);
37-foreach($parameters as $k=>$v) {
38-$this->$k = $v;
39- }
40-//Do you want to get menu as array items
41-$this->rtn = $rtn;
42-
43-//reset to empty. because found some issue.
44-$this->echo_list = '';
45-
46-$this->CI =& get_instance();
47-
48-$this->CI->db->where('menus_type', $this->menu_type);
49-$this->CI->db->where('menus_active',1);
50-$this->CI->db->order_by('menus_sort', 'asc');
51-$query = $this->CI->db->get('menus');
52-
53-$rows = $query->result_array();
54-
55- return $this->_prepare($rows);
56- }
57-
58- function _prepare($data=array()) {
59-//if $data is no array, return false
60-if(!is_array($data)) return false;
61-
62-$menu_array = array();
63-//Group Menu By Parent ID.
64-//Top Level Menu has always parent ID = 0
65-foreach($data as $v) {
66-$this->menu[$v['menus_parent_id']][$v['menus_id']] = array(
67-'title'=>$v['menus_title'],
68-'url' => $v['menus_link'],
69-'id' => $v['menus_id'])
;
70- }
71-
72-$this->menu[0][0]['id'] = 0;//last item::needed until fix found
73-
74-$this->last_id = 0;
75- return $this->_list();
76- }
77-
78- function _list($child=array(), $space=0) {
79-//if $menu is no array or empty, return false
80-if(!is_array($this->menu) || empty($this->menu)) return FALSE;
81-
82-//Top Level Menu has always parent ID = 0
83-if((isset($this->menu[0]) && is_array($this->menu[0])) || (is_array($child) && !empty($child))) {
84-//increment counter
85-$this->counter++;
86-
87-//set current menu data array to holder
88-$this->holder[$this->counter] = (is_array($child) && !empty($child))? $child : $this->menu[0];
89-
90-//get|set params
91-$ul_param = $li_param = $a_param = '';
92-if(!empty($this->li_param)) $li_param = ' ' . $this->li_param;
93-if(!empty($this->a_param)) $a_param = ' ' . $this->a_param;
94-if($this->counter===1 && !empty($this->ul_param)) {
95-if(preg_match('/class=“.+“/',$this->ul_param))
96-$this->ul_param = preg_replace('/class=“(.+)“/','class=“$1 cfm_level'.$this->counter.'“', $this->ul_param);
97-$ul_param = ' ' . $this->ul_param;
98- }
99- else {
100-$ul_param = ' class=“cfm_level'.$this->counter.'“';
101- }
102-
103-$this->echo_list .= “\n“.str_repeat(' ',$space).“<ul$ul_param>“;

104-//parent menu list
105-foreach($this->holder[$this->counter] as $v) {
106-if($v['id']>0) {
107-//menu lists
108-$this->echo_list .= “\n“.str_repeat(' ',($space+3)).“<li$li_param>“;
109-$this->echo_list .= “\n“.str_repeat(' ',($space+6));
110-
111-if(preg_match('|http(s)?:\/\/|',$v['url']))
112-$this->echo_list .= '<a' . $a_param . ' href=“' . $v['url'] . '“>' . $v['title'] . '</a>';
113- else
114-$this->echo_list .= anchor('page/' . $v['id'] . '/' . $v['url'], $v['title'], $a_param);
115-
116-if($this->rtn) {
117-//START:: return menu items as array
118-$this->menu_list[$v['id']] = array(
119-'id' => $v['id'],
120-'title' => str_repeat('-', $space)
. $v['title'],
121-'url' => $v['url'],
122- );
123-//END::
124- }
125-
126-if(isset($this->menu[$v['id']])) {
127-$this->_list($this->menu[$v['id']], $space+6);
128-$reduce_space = false;
129- } else {
130-$reduce_space = true;
131- }
132-
133-$this->echo_list .= “\n“.str_repeat(' ',($space+3)).“</li>“;
134- }
135-if($v['id']==0)$this->is_last = TRUE;//Check to see if it is last one
136- }
137-
138-if($this->counter > 1)$this->counter--;//taking level counter back to previous one.
139-
140-$this->echo_list .= “\n“.str_repeat(' ', $space).“</ul>“;
141-
142-//just to make it look nice
143-if($reduce_space)
144-if($space >= 3)$space = $space-3;
145-
146-if($this->is_last) { //if all menus listing completed, return or echo it
147-if($this->rtn) {//if return=true for array | this is used for admin menu manager
148- return $this->menu_list;
149- } else {
150- return $this->echo_list;
151- }
152- }
153- }
154- }
155- }
156-?>

Bookmark and Share
 

 





Javascript must be enabled to post comments!