<?php
/**
 * Breadcrumb Class
 *
 * This class manages the breadcrumb object
 *
 * @package		Breadcrumb
 * @version		1.0
 * @author 		Richard Davey <info@richarddavey.com>
 * @copyright 	Copyright (c) 2011, Richard Davey
 * @link		https://github.com/richarddavey/codeigniter-breadcrumb
 */
class Breadcrumb {

    /**
     * Breadcrumbs stack
     *
     */
    static private $breadcrumbs = array();

    /**
     * Options
     *
     */
    static private $_tag_open = '<ol class="breadcrumb">';
    static private $_tag_close = '</ol>';
    static private $_item_tag_open = '<li>';
    static private $_item_tag_close = '</li>';

    /**
     * Constructor
     *
     * @access	public
     * @param	array	initialization parameters
     */
    public function __construct($params = array()) {
        
    }

    // --------------------------------------------------------------------

    /**
     * Append crumb to stack
     *
     * @access	public
     * @param	string $title
     * @param	string $href
     * @return	void
     */
    static function append_crumb($title, $href = '') {
        // no title provided
        if (!$title)
            return;

        // add to end
        self::$breadcrumbs[] = array('title' => $title, 'href' => $href);
    }

    // --------------------------------------------------------------------

    /**
     * Prepend crumb to stack
     *
     * @access	public
     * @param	string $title
     * @param	string $href
     * @return	void
     */
    static function prepend_crumb($title, $href = '') {
        // no title provided
        if (!$title)
            return;

        // add to start
        array_unshift(self::$breadcrumbs, array('title' => $title, 'href' => $href));
    }

    // --------------------------------------------------------------------

    /**
     * Generate breadcrumb
     *
     * @access	public
     * @return	string
     */
    static function output($home_title = '', $home_href = '') {
        if ($home_title != '')
            self::prepend_crumb($home_title, $home_href);

        // breadcrumb found
        if (self::$breadcrumbs) {

            // set output variable
            $output = self::$_tag_open;

            // add html to output
            foreach (self::$breadcrumbs as $key => $crumb) {
                if ($key > 0 && $crumb['title'] == $home_title && $crumb['href'] == $home_href)
                    continue;

                $output .= self::$_item_tag_open;

                // if last element
                $arraykeys = array_keys(self::$breadcrumbs);
                if (end($arraykeys) == $key || $crumb['href'] == '') {
                    $output .= $crumb['title'];

                    // else add link and divider
                } else {
                    $output .= sprintf('<a href="%s">%s</a>', url($crumb['href']), $crumb['title']);
                }

                $output .= self::$_item_tag_close;
            }

            // return html
            return $output . self::$_tag_close . PHP_EOL;
        }

        // return blank string
        return '';
    }

    static function get() {
        return self::$breadcrumbs;
    }

}

// END Breadcrumb Class