Multilevel menu library for codefight cms

Posted by Damodar Bashyal on May 26, 2009

 

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.

<?php
//If BASEPATH is not defined, simply exit.
if (!defined('BASEPATH')) exit('No direct script access allowed');
/*
>menus_id | menus_active | menus_parent_id | menus_link
>menus_title | menu_type | menus_meta_title | menus_meta_keywords
>menus_meta_description | menus_sort

Parameters that can be passed in an array:
$parameters = array (
'ul_param' => 'class=“xyz“...',
'li_param' => '...',
'a_param' => '...',
)
multi level menu created for codefight cms by damodar bashyal
visit codefight.org
*/
class Menu {
var $CI = '';
var $menu = array();
var $counter = 0;
var $holder = array();
var $menu_type = 'pages';
var $ul_param = '';
var $li_param = '';
var $a_param = '';
var $rtn=FALSE;
var $menu_list = '';
var $echo_list = '';
var $last_id = 0;
var $is_last = FALSE;

function get($parameters=array('menu_type' => 'pages'), $rtn=FALSE)
{

if(!is_array($parameters)) $parameters = array($parameters);

foreach($parameters as $k=>$v)
{
$this->$k = $v;
}

//Do you want to get menu as array items
$this->rtn = $rtn;

//reset to empty. because found some issue.
$this->echo_list = '';

$this->CI =& get_instance();

$this->CI->db->where('menus_type', $this->menu_type);
$this->CI->db->where('menus_active',1);
$this->CI->db->order_by('menus_sort', 'asc');
$query = $this->CI->db->get('menus');

$rows = $query->result_array();

return $this->_prepare($rows);
}

function _prepare($data=array())
{
//if $data is no array, return false
if(!is_array($data)) return false;

$menu_array = array();

//Group Menu By Parent ID.
//Top Level Menu has always parent ID = 0
foreach($data as $v)
{
$this->menu[$v['menus_parent_id']][$v['menus_id']] = array(
'title'=>$v['menus_title'],
'url' => $v['menus_link'],
'id' => $v['menus_id']);
}

$this->menu[0][0]['id'] = 0;//last item::needed until fix found

$this->last_id = 0;
return $this->_list();
}

function _list($child=array(), $space=0)
{
//if $menu is no array or empty, return false
if(!is_array($this->menu) || empty($this->menu)) return FALSE;

//Top Level Menu has always parent ID = 0
if((isset($this->menu[0]) && is_array($this->menu[0])) || (is_array($child) && !empty($child)))
{
//increment counter
$this->counter++;

//set current menu data array to holder
$this->holder[$this->counter] = (is_array($child) && !empty($child))? 
$child : $this->menu[0];

//get|set params
$ul_param = $li_param = $a_param = '';
if(!empty($this->li_param)) $li_param = ' ' . $this->li_param;

if(!empty($this->a_param)) $a_param = ' ' . $this->a_param;

if($this->counter===1 && !empty($this->ul_param))
{
if(preg_match('/class=“.+“/',$this->ul_param))
$this->ul_param = preg_replace('/class=“(.+)“/','class=“$1 cfm_level'.$this->counter.'“', $this->ul_param);

$ul_param = ' ' . $this->ul_param;
}
else
{
$ul_param = ' class=“cfm_level'.$this->counter.'“';
}

$this->echo_list .= “\n“.str_repeat(' ',$space).“<ul$ul_param>“;

//parent menu list
foreach($this->holder[$this->counter] as $v)
{
if($v['id']>0)
{
//menu lists
$this->echo_list .= “\n“.str_repeat(' ',($space+3)).“<li$li_param>“;
$this->echo_list .= “\n“.str_repeat(' ',($space+6));

if(preg_match('|http(s)?:\/\/|',$v['url']))
$this->echo_list .= '<a' . $a_param . ' href=“' . $v['url'] . '“>' . $v['title'] . '</a>';
else
$this->echo_list .= anchor('page/' . $v['id'] . '/' . $v['url'], $v['title'], $a_param);

if($this->rtn)
{
//START:: return menu items as array
$this->menu_list[$v['id']] = array(
'id' => $v['id'],
'title' => str_repeat('-', $space) . $v['title'],
'url' => $v['url'],
);
//END::
}

if(isset($this->menu[$v['id']]))
{
$this->_list($this->menu[$v['id']], $space+6);
$reduce_space = false;
}
else
{
$reduce_space = true;
}

$this->echo_list .= “\n“.str_repeat(' ',($space+3)).“</li>“;
}
if($v['id']==0)$this->is_last = TRUE;//Check to see if it is last one
}

if($this->counter > 1)$this->counter--;//taking level counter back to previous one.

$this->echo_list .= “\n“.str_repeat(' ', $space).“</ul>“;

//just to make it look nice
if($reduce_space)
if($space >= 3) $space = $space-3;

if($this->is_last)
{
//if all menus listing completed, return or echo it
if($this->rtn)
{
//if return=true for array | this is used for admin menu manager
return $this->menu_list;
} else {
return $this->echo_list;
}
}
}
}
}
?>
 
not published on website


QR Code: Multilevel menu library for codefight cms