Permalink
Browse files

Added a new param 'case' with the options 'upper','lower','upper_firs…

…t' to optionally adjust the outputted text cases, also small bug fix
  • Loading branch information...
1 parent 401d546 commit cc58a54f01555bf7eafc1a797a9afa51f469161a @joelbradbury committed Jul 14, 2011
Showing with 118 additions and 27 deletions.
  1. +118 −27 ee2/jb_breadcrumb/pi.jb_breadcrumb.php
@@ -2,9 +2,9 @@
$plugin_info = array(
'pi_name' => 'JB Breadcrumb',
- 'pi_version' => '2.4.1',
+ 'pi_version' => '2.5',
'pi_author' => 'Joel Bradbury',
- 'pi_author_url' => 'http://joelbradbury.net/code/breadcrumb/',
+ 'pi_author_url' => 'http://jo3l.net/code/breadcrumb/',
'pi_description' => 'Taking the pain out of breadcrumbs',
'pi_usage' => Jb_breadcrumb::usage()
);
@@ -16,55 +16,74 @@ class Jb_breadcrumb
var $parse_dates = FALSE;
var $parse_cats = FALSE;
- var $bread_sep = "»";
+ var $bread_sep = ">";
var $css_class = "breadcrumbs";
var $css_id = "";
- var $intro_text = "You are here: ";
+ var $intro_text = "";
var $uri;
var $separator = '/';
+ var $case = '';
+ var $home_text = "Home";
// -------------------------------------------------------------
- function Jb_breadcrumb($text = '')
+ function Jb_breadcrumb()
{
- $this->EE =& get_instance();
-
- $return = '';
+ $this->EE =& get_instance();
+ $return = '';
+
// Get parameters passed in, override the defaults as needed.
- if($this->EE->TMPL->fetch_param('parse_dates')=='yes') $this->parse_dates = TRUE;
+ if($this->EE->TMPL->fetch_param('parse_dates')=='yes') $parse_dates = TRUE;
if($this->EE->TMPL->fetch_param('parse_cats')=='yes') $this->parse_cats = TRUE;
if($this->EE->TMPL->fetch_param('bread_sep')!='') $this->bread_sep = $this->EE->TMPL->fetch_param('bread_sep');
+ if($this->EE->TMPL->fetch_param('case')=='upper' OR $this->EE->TMPL->fetch_param('case')=='lower' OR $this->EE->TMPL->fetch_param('case')=='upper_first' ) $this->case = $this->EE->TMPL->fetch_param('case');
if($this->EE->TMPL->fetch_param('css_class')!='') $this->css_class = $this->EE->TMPL->fetch_param('css_class');
if($this->EE->TMPL->fetch_param('css_id')!='') $this->css_id = $this->EE->TMPL->fetch_param('css_id');
if($this->EE->TMPL->fetch_param('intro_text')!='') $this->intro_text = $this->EE->TMPL->fetch_param('intro_text');
if($this->EE->TMPL->fetch_param('uri')!='') $this->uri = $this->EE->TMPL->fetch_param('uri');
else $this->uri = $this->EE->uri->uri_string;
-
+
+ $this->home_text = $this->_adjust_case($this->home_text);
+
//now star work building the results
- $crumbs = array();
- $crumbs[] = '<p class="$this->css_class"><a href="/">Home</a>';
+ $crumbs = array();
+
+ $inital_entry = '<p class="'.$this->css_class.'"';
+ if($this->css_id!='') $inital_entry .= ' id="'.$this->css_id.'"';
+ $inital_entry .= '>'.$this->intro_text.'<a href="/">'.$this->home_text.'</a>';
+
+ $crumbs[] = $inital_entry;
$uri_segs = $this->EE->uri->segments;
//get rid of things we dont want from the array
if(is_numeric(end($uri_segs)) && $uri_segs[sizeof($uri_segs)-1]=='page') {
unset($uri_segs[sizeof($uri_segs)]);
unset($uri_segs[sizeof($uri_segs)]);
- } else if(isset($uri_segs[1]) && isset($uri_segs[2]) && $uri_segs[1]=='search' && $uri_segs[2]=='results'){
+ } else if(isset($uri_segs[1]) && isset($uri_segs[2]) && $uri_segs[1]=='search' && $uri_segs[2]=='results'){
unset($uri_segs[sizeof($uri_segs)]);
} else if(is_numeric(end($uri_segs)) && $uri_segs[sizeof($uri_segs)-1]=='gallery'){
unset($uri_segs[sizeof($uri_segs)]);
- }
-
+ }
$i = 1;
$length = sizeof($uri_segs);
- $sql = "SELECT url_title,title,entry_id
+ $sql = "SELECT url_title,title
FROM exp_channel_titles
WHERE url_title IN (";
foreach($uri_segs as $uri_seg => $val){
$sql .= "'".$val."'" . ", ";
}
- $sql .= " '' )";
+ $sql .= " '' )";
+
+ if($this->parse_cats){
+ $sql .= " UNION SELECT cat_url_title AS url_title, cat_name AS title
+ FROM exp_categories
+ WHERE cat_url_title IN (";
+ foreach($uri_segs as $uri_seg => $val){
+ $sql .= "'".$val."'" . ", ";
+ }
+ $sql .= " '' )";
+ }
$query = $this->EE->db->query($sql);
$results = $query->result_object();
@@ -76,32 +95,104 @@ function Jb_breadcrumb($text = '')
for($j=1; $j<$i;$j++){
$path .= $uri_segs[$j].'/';
}
-
- $this_title = ucfirst($this_seg);
+
+ if($this->parse_dates){
+ //we'd like to turn certain segs into dates
+ //if this seg is numeric well do some checking
+ $have_year = FALSE;
+ $have_month = FALSE;
+ $have_day = FALSE;
+ if(is_numeric($this_seg) && $this->seg>0){
+ //some basic checks
+ //rule 1 : only parse things from the second seg on
+ if($i>2){
+ if(!$have_year && ($this_seg<100 || $this_seg >1900)){
+ //we might have a year
+ if($this_seg<100) $this_seg = "20".$this_seg;
+ $have_year=TRUE;
+ }
+ }
+ }
+ }
+ $this_title = $this->_uri_prettify($this_seg);
+
foreach($results as $row){
if($row->url_title==$this_seg) $this_title = $row->title;
}
-
- //we want to clear up the filter options
- if($prev_seg=='filter') $this_title = "Custom Option";
-
+
//we dont want the final segment a link (it'll be the current page)
- if($i == $length+1) $crumbs[] = $this_title;
- else $crumbs[] = '<a href="'.$path.'">'.$this_title.'</a>';
+ if($i == $length+1) $crumbs[] = $this->_adjust_case($this_title);
+ else $crumbs[] = '<a href="'.$path.'">'.$this->_adjust_case($this_title).'</a>';
}
-
+
//return $html;
for($i=0, $count=count($crumbs); $i < $count; $i++) {
if(!empty( $this->bread_sep ) && $i != ($count-1)) {
$crumbs[$i] = "{$crumbs[$i]} {$this->bread_sep} ";
}
}
$crumbs[] = '</p>';
- $this->return_data = implode('', $crumbs);
+ $this->return_data = implode('', $crumbs);
+ }
+
+ function _adjust_case($text)
+ {
+ $ret = $text;
+
+ if( $this->case != '')
+ {
+ if( $this->case == 'upper' )
+ {
+ $ret = strtoupper( $ret );
+ }
+ elseif( $this->case == 'lower' )
+ {
+ $ret = strtolower( $ret );
+ }
+ elseif( $this->case == 'upperfirst' )
+ {
+ $ret = ucfirst( $ret );
+ }
+ }
+
+ return $ret;
}
+
+ function _uri_prettify($url_title = '')
+ {
+ $uncap_keywords = "no";
+
+ // Set default words to uncapitalize
+ $keywords = "and|to|with|for|the|or";
+ $keywords = explode("|", $keywords);
+
+ foreach ($keywords as $keyword)
+ {
+ $lowered[] = ucwords(strtolower($keyword));
+ }
+ $keywords = $lowered;
+
+ $str = $url_title;
+ $str = str_replace('_', ' ', $str);
+ $str = str_replace('-', ' ', $str);
+ $str = trim($str);
+ $str = ucwords(strtolower($str));
+
+ // Don't capitalize articles, prepositions and
+ // coordinate conjunctions unless they're the first word
+ if ($uncap_keywords != "no")
+ {
+ foreach ($keywords as $keyword)
+ {
+ $str = str_replace(' '.$keyword, ' '.strtolower($keyword), $str);
+ }
+ }
+ return $str;
+
+ }
// ----------------------------------------
// Plugin Usage
// ----------------------------------------

0 comments on commit cc58a54

Please sign in to comment.