<?php
/**
* This file is part of the easy-CMS project.
* easy-CMS 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.
* 
* easy-CMS 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 easy-CMS; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
	
include_once('classes/core/parser/tags/Tag.class.php');
include_once('classes/core/Site.class.php');

/**
* Tag element.
*
* @access   public
* @author	Cedric Francoys <cedric.francoys@easy-cms.org>
* @version	SiteMapTag.class, Mon Jan 03 22:49:41 CET 2005
* @package	Tags
*/
class SiteMapTag extends Tag{
	
	/**
	* Constructor
	* 
	* @access   public
 	* @param    string  The complete tag syntax (i.e. : <tagname attr1="value1" attr2="value2">, <br />, </a>, ...)
 	* @param    integer The position of the tag in the xml document
 	* @return   void  
	*/	 	
	function SiteMapTag($tagString, $name, $type, $offset, $length){
		parent::Tag($tagString, $name, $type, $offset, $length);
	}


	function getSubPages($pageId, $level) {
		$dbManipulator	= &$this->site->getDBManipulator();
		$langId					= $this->site->getPersistentVar('lid', $this->site->getDefaultLangId());

		$html = '';

		if($result = $dbManipulator->sendQuery("select * from easy_cms_pages inner join easy_cms_labels on easy_cms_labels.`id`= easy_cms_pages.`name_id` where easy_cms_labels.`lang_id` = '$langId' and easy_cms_pages.`parent_id` = '$pageId' and easy_cms_pages.`deleted` = 0 and easy_cms_pages.`published` = 1 order by easy_cms_labels.`value`;")) {
			while($row = &$dbManipulator->fetchArray($result)) {
				if($level < 2) {
					$html .= "<br />";
				}
				for($i=0; $i < ($level*8); ++$i) {
					$html .= "&nbsp;";
				}
				if($level > 1) {
					$html .= "&bull;&nbsp;";
				}
				if($level > 2) {
					$html .= "<a style=\"font-weight: normal;\" href=\"index.php?pid=".$row[0]."\">".$row['value']."</a>"."<br />";
				}
				else {
					$html .= "<a href=\"index.php?pid=".$row[0]."\">".$row['value']."</a>"."<br />";
				}
				$html .= $this->getSubPages($row[0], $level+1);
			}
		}
		return $html;
	}

	/**
	* Performs actions specific to the tag
	* 
	* @access   public
 	* @return   bool Returns the HTML part corresponding to the container.
	*/
	function evaluate(){
		$mapSortField		= $this->getAttribute("sortfield");
		$mapSortOrder		= $this->getAttribute("sortorder");
		$mapMaxLevel		= $this->getAttribute("maxlevel");
		$mapRootPage		= $this->getAttribute("rootpage");

		if(!$mapRootPage) {
			$mapRootPage = 0;
		}

		$html = $this->getSubPages($mapRootPage, 0);

		$this->debug("SiteMapTag.evaluate, parse 'easy-cms:siteMap' tag : success.", 10);
		return $html;
	}

} 
?>