Welcome Guest, Not a member yet? Register   Sign In
A better menu/breadcrumb model
#1

[eluser]neen[/eluser]
Hi there, below you'll find the menu class I am using for my custom CMS built on CI...

This has only been tested with PHP5. It will probably work with PHP4, with some major edits.

Just drop the model in your models directory and drop the below code in the __construct() function of your controller. This can be used to generate and display several menus at once such as here: http://mjohn.krob.ath.cx/menu.php, or just one menu.

This model handles side menus (ie just one menu at a time, instead of several). If you specify more than one menu in your controller, pass the $id of the menu you want to render to $this->menu->generateSidemenu() .

It also handles breadcrumbs, and lets you specify custom names for any of the URIs you may have. This is primarily meant for the backend/admin area.

Each controller can have it's own separate menu/breadcrumb initialization, or you can dump them all in one file and use a foreach() on $this->menu->menuList().

This also has a little bit of HTML hard coded into it, but it shouldn't be too hard to change it to suit your needs. This model also doesn't handle any sort of display, either, so that will be up to you to take care of.

The model is in my second post (since I hit the character limit). If you have any questions or suggestions, let me know!
Implementation:
Code:
$this->load->model('display_model', 'display');
            // setup the menu and breadcrumbs
            $this->menu->item("Content","Manage Content")->
                create("Add Content")->
                listitems("List Content")->
                delete("Delete Content")->
                registerBreadcrumb('manage', 'Manage Content')->
                registerBreadcrumb('create', 'Add Content')->
                registerBreadcrumb('list', 'List Content')->
                registerBreadcrumb('edit', 'Edit Content')->
                registerBreadcrumb('delete', 'Delete Content');
#2

[eluser]neen[/eluser]
Model:
Code:
<?php
    /*
    | ********************************************************************** |
    |  This code is Copyright (c) 2008 neen and c0mpub0mb                    |
    |                                                                        |
    |                                                                        |
    |  This program is free software: you can redistribute it and/or modify  |
    | it under the terms of the GNU General Public License as published by   |
    | the Free Software Foundation, either version 3 of the License, or      |
    | (at your option) any later version.                                    |
    |                                                                        |
    | This program is distributed in the hope that it will be useful,        |
    | but WITHOUT ANY WARRANTY; without even the implied warranty of         |
    | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          |
    | GNU General Public License for more details.                           |
    |                                                                        |
    | You should have received a copy of the GNU General Public License      |
    | along with this program.  If not, see <http://www.gnu.org/licenses/>.  |
    |                                                                        |
    | ********************************************************************** |
    */
    
    class Menu_model extends Model {
        
        private $items    = null;
        private $item_id  = null;
        private $sidemenu;
        private $breadcrumbs;
        public function __construct()
        {
            parent::Model();
            
        }
        
        public function item($classname,$title)
        {
            if(is_null($this->item_id))
                $this->item_id = 0;
            else
                $this->item_id++;

            $this->items[$this->item_id]['classname'] = $classname;
            $this->items[$this->item_id]['title'] = $title;
            return $this;
        }

        public function create($title)
        {
            $this->items[$this->item_id]['items']['create'] = $title;
            return $this;
        }

        public function edit($title)
        {
            $this->items[$this->item_id]['items']['edit'] = $title;
            return $this;
        }

        public function listitems($title)
        {
            $this->items[$this->item_id]['items']['list'] = $title;
            return $this;
        }

        public function delete($title)
        {
            $this->items[$this->item_id]['items']['delete'] = $title;
            return $this;
        }

        public function menuList()
        {
            return $this->items;
        }

        public function registerBreadcrumb($uri, $title)
        {
            $this->items[$this->item_id]['breadcrumbs'][$uri] = $title;
            return $this;    
        }
        public function generateSidemenu($id = 0)
        {
            // set the indent
            $indent = '';
            // $i = 2, because we only want this to activate when there are more than 2 segments in the url
            for($i = 2; $i < $this->uri->total_segments(); $i++)
            {
                $indent .= '../';
            }
            // get the menu list
            $this->sidemenu = $this->menuList();
            $this->sidemenu = $this->sidemenu[$id];
            $this->sidemenu['indent'] = $indent;
            return $this->sidemenu;
        }
        
        public function generateBreadcrumbs()
        {
            $this->breadcrumbs = '';
            // iterate through the levels to build the anchor
            // get the uris
            $uri = explode('/', $this->uri->uri_string());
            // dump($this->items);
            
            for($i = $this->uri->total_segments(); $i > 0; $i--)
            {
                // repeat the string
                $anchor = str_repeat('../', $i - 1);
                switch($i)
                {
                    // first iteration through loop
                    case $this->uri->total_segments():
                        $this->breadcrumbs = '<a href="' . $anchor . '">Administration Home</a><span> / </span>';
                        break;
                    // 2nd iteration through the loop
                    case 2:
                        $this->title = $this->items[0]['breadcrumbs']['manage'];
                        $this->breadcrumbs .= '<a href="' . $anchor . '">' . $this->title . '</a><span> / </span>';
                        break;
                    // last iteration through loop
                    case 1:
                        $this->title = $this->items[0]['breadcrumbs'][$this->uri->segment(2)];
                        $this->breadcrumbs .= '<strong>'. $this->title . '</strong>';
                        break;
                }
            }

            return $this->breadcrumbs;
        }

        public function generateGlobalMenu()
        {
            
            
        }
        
    }
?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB