<?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	MenuTag.class, Mon Jan 03 22:49:41 CET 2005
* @package	Tags
*/
class MenuTag 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 MenuTag($tagString, $name, $type, $offset, $length){
		parent::Tag($tagString, $name, $type, $offset, $length);
	}

	/**
	* Performs actions specific to the tag
	* 
	* @access   public
 	* @return   bool Returns the HTML part corresponding to the container.
	*/
	function evaluate(){
		$frameId       		= $this->page->getPageFrameId();
		$pageId         	= $this->page->getPageId();
		$menusManager   	= &$this->site->getMenusManager();
		$aclManager    		= &$this->site->getACLManager();
		$dbManipulator  	= &$this->site->getDBManipulator();
		
		$drawBorder				= $this->getAttribute('border');
		$frameMenuName		= $this->getAttribute('id');
		$menuTemplate			= $this->getAttribute('template');
		$menuSortField		= $this->getAttribute('sortfield');
		$menuSortOrder		= $this->getAttribute('sortorder');
		$menuSortingType	= $this->getAttribute('sortingtype');
		$isStatic			  	= $this->getAttribute('static');

		$langId 					= $this->site->getPersistentVar('lid', $this->site->getDefaultLangId());

		if( ($drawBorder == null) || ($drawBorder == '0') || (strcasecmp($drawBorder, 'no') == 0) ){
			$drawBorder = false;
		}
		else {
			$drawBorder = true;
		}
				
		if( ($isStatic == null) || ($isStatic == '0') || (strcasecmp($isStatic, 'no') == 0) ) {
			$isStatic = false;
		}
		else {
			$isStatic = true;
		}

		if (($result = &$dbManipulator->getRecord(FRAME_MENUS_TABLE, $frameId, "menu_name = '$frameMenuName'", "frame_id")) < 0 ){
			$this->debug("MenuTag.evaluate, error : unable to read DB for frame '".$frameId."'.", 2);
			return '';
		}
		
		if(!($row = &$dbManipulator->fetchArray($result))){
			//menu does not exist : create it
  		$this->debug("MenuTag.evaluate, notice : menu does not exist yet, creating a new menu.", 5);
			$menuId 			= 0;
			$sortField 		= $menuSortField;
			$sortOrder  	= $menuSortOrder;
			$sortingType	= $menuSortingType;
			$maxLevel   	= 0;
      $rootPageId 	= $this->site->getRootPageId();
			$templateId 	= &$dbManipulator->getTemplateId($menuTemplate, 'easy_cms_page');
			$aclId        = $this->acl->getAclId();
      $menu = &$menusManager->getNewMenu($aclId);
			$menu->setMenuName($frameMenuName, $langId);
			$menu->setMenuSortField($sortField);
			$menu->setMenuTemplateId($templateId);
      $menu->setMenuAcl($this->acl);
      $menu->setMenuRootPageId($rootPageId);
			$menu->setMenuMenuNodesManager($menusManager->getMenuNodesManager());
			$menu->setMenuTemplatesManager($menusManager->getTemplatesManager());
			$menusManager->setMenu($menu);
			$menuId = $menusManager->storeMenu($menuId);
			$dbManipulator->addFrameMenu($frameId, $menuId, $frameMenuName);
		}
		else {       
			$menuId = $row["menu_id"];
		}
		
		if(is_null($menu = &$menusManager->getMenu($menuId))){
			$this->debug("MenuTag.evaluate, error : unable to get menu '$frameMenuName' for frame '".$frameId."'.", 2);
			return '';
		}

		$menu->loadContent();

		$this->debug("MenuTag.evaluate, parse 'easy-cms:menu' tag : success.", 10);
		return $menu->toHTML($this->user, $langId, $pageId, $this->mode, $drawBorder);
	}
}
?>