Welcome Guest, Not a member yet? Register   Sign In
Forum breadcrumb class - where to start?
#3

[eluser]TheFuzzy0ne[/eluser]
Hi, Jedd!

Yes, the structure is flat, I've been playing with about 100 different ways to do what I want, but they all seem quite messy. I think I have it sussed, however. I'm just stuck on what to name my model method, and how it should work. It uses the MPTtree library, and this is what I have so far (it really needs changing I feel, as does the method name):

Code:
function get_breadcrumb_array($forum_id=0)
    {
        if ($forum_id)
        {
            if ($parent_row = $this->get_forum_row($forum_id))
            {
                $rows = array();
                foreach ($this->mptt->get_parents($parent_row['lft'], $parent_row['rgt']) as $row)
                {
                    $rows[] = array('id' => $row['id'], 'title' => $row['title']);
                }
                $rows[] = array('id' => $parent_row['id'], 'title' => $parent_row['title']);
                
                unset($rows[0]);
                $rows = array_reverse($rows);
                
                return $rows;
            }
        }
        return array();
    }
    
    function get_forum_row($id=0)
    {
        if ($id)
        {
            $res = $this->db->get_where('forums', array('id' => $id));
            if ($res->num_rows())
            {
                return $res->row_array();
            }
        }
        return FALSE;
    }

Your thoughts are welcome as always.

Here's the breadcrumb class I've developed so far. It probably needs some of the rough edges files down a bit, but it works, and is fairly straight forward.

Code:
<?php

class Breadcrumb {
    
    var $_segments = array();
    var $separator = ' > ';
    var $breadcrumb_prefix = '';
    var $breadcrumb_suffix = '';
    var $separator_prefix = '';
    var $separator_suffix = '';
    
    function initialize($config=array())
    {
        foreach ($config as $key=>$value)
        {
            if (method_exists($this, $key))
            {
                $this->$key = $value;
            }
        }
    }
    
    /**
     * Set's the separator for the breadcrumb
     *
     * @return void
     * @param string $separator
     */
    function set_separator($separator="")
    {
        $this->separator = $separator;
    }
    
    /**
     * Allows you to set the prefix and suffix for the separator.
     *
     * @return void
     * @param string $prefix
     * @param string $suffix
     */
    function set_separator_delimiters($prefix='', $suffix='')
    {
        $this->_separator_prefix = $prefix;
        $this->_separator_suffix = $suffix;
    }
    
    /**
     * Allows you to set the prefix and suffix for the breadcrumbs.
     *
     * @return void
     * @param array $prefix
     * @param array $suffix
     */
    function set_breadcrumb_delimiters($prefix='', $suffix='')
    {
        $this->_breadcrumb_prefix = $prefix;
        $this->_breadcrumb_suffix = $suffix;
    }
    
    /**
     * Returns an HTML string containing the generated breadcrumbs
     *
     * @return string
     * @param array $config[optional] This is just for convenience
     */
    function get($config=array())
    {
        if (is_array($config) && count($config) > 0)
        {
            $this->initialize($config);
        }
        
        $output = array();
        $count = 0;
        foreach ($this->_segments as $bc)
        {
            $count++;
            $str = $bc['text'];
            if ($bc['url'] != '' && $count != count($this->_segments))
            {
                $str = '<a href="'
                . $bc['url']
                . '" title="' . $bc['title'] .'">'
                . $str
                . '</a>';
            }
            
            $str = $this->breadcrumb_prefix . $str . $this->breadcrumb_suffix;
            
            $output[] = $str;
        }
        
        return implode(
            $this->separator_prefix    . $this->separator . $this->separator_suffix,
            $output
        );
        
    }
    
    /**
     * Adds the specified array to the already existing segments.
     *
     * array(
     *         'text' => 'breadcrumb text',
     *         'url' => 'breadcrumb URL',
     *         'title' => 'anchor title' // Optional
     *     );
     *
     * You can also specify a string (usually for the last breadcrumb),
     * which will not be turned into a link, or an array of arrays
     * in the above format.
     *
     * @return bool Just in case you want to do some extra checking.
     * @param mixed $data
     */
    function add($data=array())
    {
        if (is_array($data) && count($data) > 0)
        {
            if (isset($data['text']))
            {
                $this->_segments[] = array(
                    'text' => $data['text'],
                    'url' => (isset($data['url'])) ? $data['url'] : '',
                    'title' => (isset($data['title'])) ? $data['title'] : ''
                );
            }
            else if (isset($data[0]) && is_array($data[0]))
            {
                foreach ($data as $arr)
                {
                    $this->add($arr);
                }
            }
            
            return TRUE;
        }
        else if (is_string($data) && $data != '')
        {
            $this->add(array(
                    'text' => $data,
                    'url' => '',
                    'title' => ''
                ));
            return TRUE;
        }
        
        return FALSE;
    }
}

Thanks again for your comments.


Messages In This Thread
Forum breadcrumb class - where to start? - by El Forum - 04-12-2009, 09:20 AM
Forum breadcrumb class - where to start? - by El Forum - 04-12-2009, 10:10 AM
Forum breadcrumb class - where to start? - by El Forum - 04-12-2009, 11:14 AM
Forum breadcrumb class - where to start? - by El Forum - 04-12-2009, 07:48 PM
Forum breadcrumb class - where to start? - by El Forum - 04-12-2009, 08:49 PM
Forum breadcrumb class - where to start? - by El Forum - 04-13-2009, 07:10 AM
Forum breadcrumb class - where to start? - by El Forum - 04-29-2009, 08:05 PM
Forum breadcrumb class - where to start? - by El Forum - 04-30-2009, 06:00 AM



Theme © iAndrew 2016 - Forum software by © MyBB