CodeIgniter Forums

Full Version: Need advice for my Breadcrumb library
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]phoenixg[/eluser]
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
* Bread Crumb Generator
*
* @author 爱业星辰
*/
/* usage
append_crumb();
append_crumb('alias');
output();
*/
class Breadcrumb {
private $_CI;
private static $_breadcrumb = array();
private $_delimiter = ' > ';
protected $home = 'HOME';
protected $base_url;
protected $index = 'index.php';

/**
  * initialize
  */
public function __construct()
{
  $this->_CI = & get_instance();
  
  $this->base_url = $this->_CI->config->item('base_url');
  
  $this->_init_crumb();
  
  log_message('debug', "Breadcrumb Class Initialized");
}

/**
  * initialize crumb array
  */
private function _init_crumb()
{
  $this->_breadcrumb = array(
        '0' => array($this->home => $this->base_url . $this->index . '/')
  );
}

/**
  * add crumb to array
  * @param $alias
  */
public function append_crumb($alias = '')
{
  $cnt = count($this->_breadcrumb);
  
  if (!in_array($cnt, array('1','2','3')))
  {
   return;
  }
  
  $seg1 = $this->_CI->uri->segment(1,'');
  $seg2 = $this->_CI->uri->segment(2,'');
  $seg3 = $this->_CI->uri->segment(3,'');
  
  $base_url_full = $this->_breadcrumb['0'][$this->home];
  
  if ($cnt == 1)
  {
   $display = ($alias == '') ? strtoupper($seg1) : strtoupper($alias);
   $this->_breadcrumb[] = array($display => $base_url_full . $seg1 . '/');
  }
  
  if ($cnt == 2)
  {
   $display = ($alias == '') ? strtoupper($seg2) : strtoupper($alias);
   $this->_breadcrumb[] = array($display => $base_url_full . $seg1 . '/' . $seg2 . '/');
  }
  
  if ($cnt == 3)
  {
   $display = ($alias == '') ? strtoupper($seg3) : strtoupper($alias);
   $this->_breadcrumb[] = array($display => $base_url_full . $seg1 . '/' . $seg2 . '/' . $seg3 . '/');
  }
  
}

/**
  * generate crumb HTML
  */
public function output()
{
  $html = '<div>';
  foreach ($this->_breadcrumb as $crumb)
  {
   foreach ($crumb as $display => $addr)
   {
    $html .= '<a href='.$addr.'>' . $display . '</a>' . $this->_delimiter;
   }
  }
  
  $html = rtrim($html, $this->_delimiter).'</div>';
  return $html;
}
}

El Forum

[eluser]Silviu[/eluser]
You should give the user some liberty in configuring the library. For example, I use another delimiter for breadcrumbs, and I would like not to have to edit other people's code. You can pass an associative array to the constructor with user configuration values, just like other libraries do.

Even better, you can define a 'global' configuration file that should reside with other config files in /application/config.

Ideally, you should do both Smile

Other than that, the class looks good in my opinion.

El Forum

[eluser]Glazz[/eluser]
Change the output, so it can be more "dynamic", for example, i prefer using unorder lists ( ul > li ), currently i need to edit your output so i can have it working, maybe you can change it so it works different for other people needs, it can be done .

El Forum

[eluser]phoenixg[/eluser]
Thank you!

@Silviu I defined a config file for breadcrumb library which provide some options for user configuration

@Glazz I made an alternative option, now UL or anchor are both supported

Here is my improved version of breadcrumb library and global config file, usage in comments.

Code:
//application/config/breadcrumb.php
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$config['breadcrumb_delimiter'] = '&nbsp;&gt;&nbsp;';
$config['breadcrumb_css_id'] = 'breadcrumb';
$config['breadcrumb_html_type'] = 'anchor';//anchor , ul
$config['breadcrumb_home'] = 'HOME';
$config['breadcrumb_index'] = 'index.php';


Code:
//application/libraries/Breadcrumb.php
&lt;?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
* Bread Crumb Generator
*
* @author 爱业星辰
*
* Usage:
  $this->breadcrumb->init_crumb(array('DELIMITER' => '&nbsp;&gt;&nbsp;', 'HTML_TYPE' => 'anchor'));//optional (default)
  or
  $this->breadcrumb->init_crumb(array('DELIMITER' => '|', 'HTML_TYPE' => 'anchor')); //optional
  or
  $this->breadcrumb->init_crumb(array('HTML_TYPE' => 'ul')); //optional

    $this->breadcrumb->append_crumb();
    or
  $this->breadcrumb->append_crumb('aliasA', 1);
  $this->breadcrumb->append_crumb('aliasB', 2);
  $this->breadcrumb->append_crumb('aliasC', 3);
  
  $this->breadcrumb->generateHTML();
*/

class Breadcrumb {
private static $_breadcrumb = array();
private $_CI;
private $_delimiter;
private $_css_id;
private $_html_type;
protected $home;
protected $index;
protected $base_url;

/**
  * initialize
  */
public function __construct()
{
  //get instance of CI
  $this->_CI = & get_instance();

  //load global config file
  $this->_CI->config->load('breadcrumb');
  
  //get config item
  $this->_delimiter = $this->_CI->config->item('breadcrumb_delimiter');
  $this->_css_id = $this->_CI->config->item('breadcrumb_css_id');
  $this->_html_type = $this->_CI->config->item('breadcrumb_html_type');
  $this->home = $this->_CI->config->item('breadcrumb_home');
  $this->index = $this->_CI->config->item('breadcrumb_index');
  $this->base_url = $this->_CI->config->item('base_url');
  
  //initialize crumb array
  $this->_init_crumb_arr();
  
  //add log
  log_message('debug', "Breadcrumb Class Initialized");
}

/**
  * initialize user defined configuration
  */
public function init_crumb($config = array())
{
  $this->_delimiter = isset($config['DELIMITER']) ? $config['DELIMITER'] : $this->_delimiter;
  $this->_html_type = isset($config['HTML_TYPE']) ? $config['HTML_TYPE'] : $this->_html_type;
}

/**
  * initialize crumb array
  */
private function _init_crumb_arr()
{
  $this->_breadcrumb = array(
        '0' => array($this->home => $this->base_url . $this->index . '/')
  );
}

/**
  * append a crumb
  * @param $alias
  */
public function append_crumb($alias = '', $segments = null)
{
  $cnt = count($this->_breadcrumb);

  $seg_num = ($segments == null) ? $cnt : $segments;
  
  if (!in_array($seg_num, array('1','2','3')))
  {
   return;
  }
  
  $seg1 = $this->_CI->uri->segment(1,'');
  $seg2 = $this->_CI->uri->segment(2,'');
  $seg3 = $this->_CI->uri->segment(3,'');
  
  $base_url_full = $this->_breadcrumb['0'][$this->home];
  
  if ($seg_num == 1)
  {
   $display = ($alias == '') ? strtoupper($seg1) : strtoupper($alias);
   $this->_breadcrumb[] = array($display => $base_url_full . $seg1 . '/');
  }
  
  if ($seg_num == 2)
  {
   $display = ($alias == '') ? strtoupper($seg2) : strtoupper($alias);
   $this->_breadcrumb[] = array($display => $base_url_full . $seg1 . '/' . $seg2 . '/');
  }
  
  if ($seg_num == 3)
  {
   $display = ($alias == '') ? strtoupper($seg3) : strtoupper($alias);
   $this->_breadcrumb[] = array($display => $base_url_full . $seg1 . '/' . $seg2 . '/' . $seg3 . '/');
  }
  
}

/**
  * generate crumb HTML
  */
public function generateHTML()
{
  switch ($this->_html_type) {
   case 'anchor':
    $html = '<div id='.$this-&gt;_css_id.'>';
    foreach ($this->_breadcrumb as $crumb)
    {
     foreach ($crumb as $display => $addr)
     {
      $html .= '<a href='.$addr.' target="_blank">' . $display . '</a>' . $this->_delimiter;
     }
    }
    
    $html = rtrim($html, $this->_delimiter).'</div>';
   break;
  
   case 'ul':
    $html = '<ul id='.$this-&gt;_css_id.'>';
    foreach ($this->_breadcrumb as $crumb)
    {
     foreach ($crumb as $display => $addr)
     {
      $html .= '<li><a href='.$addr.' target="_blank">' . $display . '</a></li>';
     }
    }
    $html .= '</ul>';
   break;
  }

  return $html;
}
}