<?php
#CMS - CMS Made Simple
#
# Type:		Tag
# Name:		jmbreadcrumbs
# Purpose:	Extended breadcrumbs.
# Version:	1.1
# Date:		7 December 2011
# Author:	Fernando Morgado (JoMorg)
# Link:		NA
# Example:	{jmbreadcrumbs}
#
# Original Breadcrumbs Tag Author: Marcus Deglos
#
#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; version 2 of the License
#
#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.
#
#CMS - CMS Made Simple
#(c)2004 by Ted Kulp (wishy@users.sf.net)
#This project's homepage is: http://www.cmsmadesimple.org
#
#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 2 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, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

function smarty_cms_function_jmbreadcrumbs($params, &$smarty)
{
	$gCms = cmsms(); 
	$manager = $gCms->GetHierarchyManager();

	$thispage = $gCms->variables['content_id'];

	$trail = "";

#Check if user has specified a delimiter, otherwise use default
	if (isset($params['delimiter'])) {
		$delimiter = $params['delimiter'];
	}	else {
		$delimiter = "&gt;&gt;";
	}

#Check if user has requested an initial delimiter
	if (isset($params['initial'])) {
		if ($params['initial'] == "1") {
			$trail .= $delimiter . " ";
		}
	}

#Check if user has requested a start_level
	if (!isset($params['start_level'])) {
		$params['start_level'] = 0;
	}
	
#Check if user has requested an incremental class id
	if (!isset($params['inc_classid'])) {
		$params['inc_classid'] = false;
	}

#Check if user has requested an incremental class id
    if (!isset($params['use_list'])) {
        $params['use_list'] = false;
    }

	$root='##ROOT_NODE##';
#Check if user has requested the list to start with a specific page
	if (isset($params['root']))	{
		$root = $params['root'];
	}
	$root_url='';
#Check if user has requested to overrided the root URL
	if (isset($params['root_url']))	{
		$root_url = $params['root_url'];
	}


	$endNode = $manager->sureGetNodeById($thispage);

# build path
	if (isset($endNode))
	{
	    $content = $endNode->getContent();
		$path=array($endNode);
		$currentNode = $endNode->getParentNode();
		while (isset($currentNode) && $currentNode->getLevel() >= $params['start_level'])
		{
			$content = $currentNode->getContent();
			if (isset($content))
			{
			  //Add current node to the path and then check to see if
			  //current node is the set root
			  //as long as it's not hidden
			  if( $content->ShowInMenu() && $content->Active() )
			    {
				$path[] = $currentNode;
			    }
			  if (strtolower($content->Alias())!=strtolower($root))
			    {
			      //Get the parent node and loop
			      $currentNode = $currentNode->getParentNode();
			    }
			  else
			    {
			      //No need to get the parent node -- we're the set root already
			      break;
			    }
			}
			else
			{
			  //There are more serious problems here, dump out while we can
			  break;
			}
		}

		if ($root!='##ROOT_NODE##') 
		  {
		    // check if the last added is root. if not, add it
		    $currentNode = $manager->sureGetNodeByAlias($root);
		    
		    if (isset($currentNode))
		      {
			    $top_alias = '##garbage##';
			    if( count($path) ) $top_alias = $path[count($path)-1]->get_tag('alias');
			    
			    if( strtolower($top_alias) != strtolower($root) )
			      {
			        $content = $currentNode->getContent();
			        if( is_object($content) && $content->Active() && $content->ShowInMenu() )
			          {
				    $path[] = $currentNode;
			          }
			      }
		      }
		  }
		$classid=isset($params['classid'])?(' class="' . $params['classid']):'';
		$currentclassid=isset($params['currentclassid'])?(' class="' . $params['currentclassid'] . '"'):'';

		// now create the trail (by iterating through the path we built, backwards)
        if ($params['use_list'])
        {
            $trail .= '<ul>';
        }
		for ($i=count($path)-1;$i>=0;$i--) {
			$node = $path[$i];
			if (isset($node))
			{
				$onecontent = $node->getContent();
				if ($onecontent->Id() != $thispage && $onecontent->Type() != 'seperator') {
					if (($onecontent->getURL() != "") && ($onecontent->Type() != 'sectionheader')) {
					  if ($onecontent->DefaultContent() && false == empty($root_url))
					    {
                            if ($params['use_list'])
                            {
                                $trail .= '<li>';
                            }
					        $trail .= '<a href="' . $root_url . '"';     
					    }
					      else
						{
                            if ($params['use_list'])
                            {
                                $trail .= '<li>';
                            }
						    $trail .= '<a href="' . $onecontent->getURL() . '"';
						}
						$trail .= $classid;
						if (isset($params['classid'])){
							if ($params['inc_classid']){
								$trail .= $i;
								}
							$trail .= '"';
							}
						$trail .= '>';
						$trail .= cms_htmlentities($onecontent->MenuText()!=''?$onecontent->MenuText():$onecontent->Name());
						$trail .= '</a>';
                        if ($params['use_list'])
                        {
                            $trail .= '</li>';
                        }
                        
					}
                    else 
                    {
                        if ($params['use_list'])
                        {
                            $trail .= '<li>';
                        }
						$trail .= "<span $classid>";
						$trail .= cms_htmlentities($onecontent->MenuText()!=''?$onecontent->MenuText():$onecontent->Name());
						$trail .= '</span>';
						$trail .= ' ';
                        if ($params['use_list'])
                        {
                            $trail .= '</li>';
                        }
					}
                    if (!$params['use_list'])
                    {
                        $trail .= $delimiter . '';
                    }					
				}
                else
                {
                    if ($params['use_list'])
                        {
                            $trail .= '<li>';
                        }
					if (isset($params['currentclassid'])) {
						$trail .= "<span $currentclassid>";
					} else {
						$trail .= '<span class="lastitem">';
					}
					$trail .= cms_htmlentities($onecontent->MenuText()!=''?$onecontent->MenuText():$onecontent->Name());
					if (isset($params['currentclassid'])) {
						$trail .= '</span>';
					} else {
						$trail .= '</span>';
					}
                    if ($params['use_list'])
                    {
                        $trail .= '</li>';
                    }
				}
			}
		}
	}
    if ($params['use_list'])
    {
        $trail .= '</ul>';
    }

	if (isset($params['starttext']) && $params['starttext'] != '')
	{
		$trail = $params['starttext'] . ': ' . $trail;
	}
	if( isset($params['assign_count']) ){
	    $smarty->assign(trim($params['assign_count']),count($path));
	}
	if( isset($params['assign']) ){
	    $smarty->assign(trim($params['assign']),$trail);
	    return;
	}
	if( isset($params['assign_count']) ){
	    $smarty->assign(trim($params['assign_count']),count($path));
	}
	
	return $trail;  

}
	
function smarty_cms_help_function_jmbreadcrumbs() {
  echo ('<h3>What does this do?</h3>
<p>Prints a breadcrumbs trail.</p>
<p>This is a modded version of the original breadcrumbs tag with additional functionality. It allows defining a starting level as the menu tag.
Additionally, there is an option to render the class with increments for each block: <tt>&lt;... class="breadcrumbs1"&gt;, &lt;... class="breadcrumbs2"&gt;, ..., &lt;... class="breadcrumbs<em>n</em>"&gt;&lt;span...&gt;...</tt> and so on.</p>
<h3>How do I use it?</h3>
<p>Just insert the tag into your template/page like: <code>{jmbreadcrumbs}</code></p>
<p>Another example: <code>{jmbreadcrumbs classid="breadcrumbs" inc_classid="true" start_level=1 starttext="You are here" root="Home" delimiter="" assign_count="jmbc_inc_id_count"}</code></p>
<p>The number of iteractions can be found in a smarty variable: <code>$jmbc_inc_id_count</code></p>
<p>This would allow you to code your css with something like this: 
<code>
<p>[[if isset($jmbc_inc_id_count)]]</p>
	<p>/* assign starting values */</p>
	<p>[[assign var=\'Grad_r\' value=80]]</p>
	<p>[[assign var=\'Grad_g\' value=90]]</p>
	<p>[[assign var=\'Grad_b\' value=85]]</p>
	<p>/* assign step value */</p>
	<p>[[assign var=\'c_step\' value=-5]]</p>
    <p></p>
	<p>[[section name=jmbc_id start=1 loop=$jmbc_inc_id_count]]</p>
	<p>
		.breadcrumbs[[$jmbc_inc_id_count-$smarty.section.jmbc_id.index]]{
			background-color: rgb([[$Grad_r]]%,[[$Grad_g]]%,[[$Grad_b]]%);}
		</p>
		<p>[[assign var=\'Grad_r\' value=$Grad_r+$smarty.section.jmbc_id.index*$c_step]]</p>
		<p>[[assign var=\'Grad_g\' value=$Grad_g+$smarty.section.jmbc_id.index*$c_step]]</p>
		<p>[[assign var=\'Grad_b\' value=$Grad_b+$smarty.section.jmbc_id.index*$c_step]]</p>
	<p>[[/section]]</p>
  <p>.lastitem{
		background-color: rgb([[$Grad_r]]%,[[$Grad_g]]%,[[$Grad_b]]%);}</p>
<p>[[/if]]</p>
<p>div.jm_breadcrumb{background-color:#cce6d9;}</p>
</code></p>
<p>There are, probably, better ways to code this but it works... :)<br />
Anyway, this is only an idea... other crazy things can be done.
</p>
<p>Also meant to be used with Babel multi language solution, by giving a <em>start_level</em> parameter which should be set to 1 to hide the odd link at the beginning of the breadcrumbs.</p>
<p>If you use <tt>use_list=1</tt> the plugin overrides the default behavior and renders an unordered HTML list, enabling you to use some of the CSS breadcrumbs styling found around the WWW.</p>
<h3>What parameters does it take?</h3>
<ul>
<li><em>(optional)</em> <tt>delimiter</tt> - Text to separate entries in the list (default "&gt;&gt;").</li>
<li><em>(optional)</em> <tt>initial</tt> - 1/0 If set to 1 start the breadcrumbs with a delimiter (default 0).</li>
<li><em>(optional)</em> <tt>root</tt> - Page alias of a page you want to always appear as the first page in
    the list. Can be used to make a page (e.g. the front page) appear to be the root of everything even though it is not.</li>
<li><em>(optional)</em> <tt>root_url</tt> - Override the URL of the root page. Useful for making link be to \'/\' instead of \'/home/\'. This requires that the root page be set as the default page.</li>
<li><em>(optional)</em> <tt>classid</tt> - The CSS class for the non current page names, i.e. the first n-1 pages in the list. If the name is a link it is added to the &lt;a href&gt; tags, otherwise it is added to the &lt;span&gt; tags.</li>
<li><em>(optional)</em> <tt>currentclassid</tt> - The CSS class for the &lt;span&gt; tag surrounding the current page name.</li>
<li><em>(optional)</em> <tt>starttext</tt> - Text to append to the front of the breadcrumbs list, something like &quot;You are here&quot;.</li>
<li><em>(optional)</em> <tt>assign</tt> - Assign the results to the named smarty variable.</li>
<li><em>(added)(optional)</em> <tt>start_level</tt> - works like in the menu tag (default 0).</li>
<li><em>(added)(optional)</em> <tt>inc_classid</tt> - true/false or 1/0. Renders an incremental number appended to the <tt>classid</tt>. Doesn\'t affect <tt>currentclassid</tt>. (default false).</li>
<li><em>(added)(optional)</em> <tt>assign_count</tt> - Assign the number of iteractions ocurred in incremental <tt>classid</tt> to the named smarty variable.</li>
<li><em>(added)(optional)</em> <tt>use_list</tt> - true/false or 1/0. Overrides the default behavior and renders an unordered HTML list.</li>
</ul>');
}

function smarty_cms_about_function_jmbreadcrumbs() {
    echo ('<h3>Who?</h3>
<p>Author of this MOD: Jo Morg </p>
<p>Original Breadcrumbs Tag Author: Marcus Deglos &lt;<a href="mailto:md@zioncore.com">md@zioncore.com</a>&gt;</p>
<p>Based on Original Breadcrumbs Tag Version: 1.7</p>
<p>Version: 1.1</p>
<p> Release Date: 7 December 2011</p>
<h3>Why?</h3>
<em>Moded</em> out of need, it\'s just a share for anyone interested.</p>
<p>Change History:</p>
<p><em>Please reffer to the original Breadcrumbs Tag History.</em></p>
<p>Version: 1.1</p>
<ul>
<li>Added <tt>use_list</tt> parameter</li>
</ul>
');
}
# vim:ts=4 sw=4 noet
?>
