<?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
 */
 
/**
 * @package    Core
 * @subpackage Menus
 */

include_once('config.inc.php');
include_once('classes/core/CoreObject.class.php');
include_once('classes/core/DBManipulator.class.php');
include_once('classes/core/menuNodes/MenuNodesManager.class.php');
include_once('classes/core/menuNodes/MenuNode.class.php');
include_once('classes/core/templates/Template.class.php');
include_once('classes/core/templates/TemplatesManager.class.php');
include_once('classes/core/pages/Page.class.php');
include_once('classes/core/pages/PagesManager.class.php');
include_once('classes/utils/SortedList.class.php');
include_once('classes/utils/SortedListIterator.class.php');
include_once('classes/core/ACL/ACL.class.php');
include_once('classes/core/ACL/ACLManager.class.php');


/**
 * Menus are particular containers containing tree or subtree of references to pages
 *
 * @access   public
 * @author	Cedric Francoys <cedric.francoys@easy-cms.org>
 * @version	Menu.class, Sun Nov 28 13:30:31 CET 2004
 * @package	Menus
 */
class Menu extends CoreObject {

	var $id;

	/**
	 * @var array
	 */
	var $namesTable;

  var $nameId;	

  var $template;

  var $templateId;

  var $rootPageId;

  var $sortingType;

  var $sortField;

  var $sortOrder;

  var $typeName;

  var $published;

  var $deleted;

  var $maxLevel;

	var $countShown;

  var $menuNodesManager;

	var $menuTemplatesManager;

	var $currentPageId;

  var $rootMenuNode;

  var $pagesOrder;

  var $pagesLists;

	var $acl;

	var $aclId;

	function Menu(&$id, &$nameId, &$templateId, &$rootPageId, &$sortField, &$sortOrder) {
		$this->id         = $id;
		$this->nameId     = $nameId;
		$this->templateId = $templateId;
		$this->rootPageId = $rootPageId;
		$this->maxLevel   = 0;
		$this->countShown = 0;
		$this->typeName   = 'easy_cms_page';
		$this->sortOrder  = $sortOrder;
		$this->sortField  = $sortField;
	}
	
	/**
	 * @deprecated use {@link getMenuId}
	 */
	function getId() {
		return $this->id;
	}
	
	/**
	 * @return integer
	 */
	function getMenuId() {
		return $this->id;
	}

	/**
	 * @return integer
	 */
	function getMenuAclId() {
		return $this->aclId;
	}

	/**
	 * alias for getMenuAclId
	 *
	 * @return integer
	 */
	function getAclId(){
		return $this->aclId;
	}

	/**
	 * @return string
	 */
	function getMenuName($langId) {
    $result = '';
    if(isset($this->namesTable[$langId])) {
      $result = $this->namesTable[$langId];
    }
		return $result;
	}

	/**
	 * @deprecated use {@link getMenuNameId}
	 */
	function getNameId() {
		return $this->nameId;
	}
	
	/**
	 * @return integer
	 */
	function getMenuNameId() {
		return $this->nameId;
	}
	
	/**
	 * @deprecated use {@link getMenuSortField}
	 */
	function getSortField() {
		return $this->sortField;
	}
	
	/**
	 * @return string
	 */
	function getMenuSortField() {
		return $this->sortField;
	}

	/**
	 * @deprecated use {@link getMenuSortOrder}
	 */
	function getSortOrder() {
		return $this->sortOrder;
	}
	
	/**
	 * @return integer
	 */
	function getMenuSortOrder() {
		return $this->sortOrder;
	}
	
	/**
	 * @return integer
	 */
	function getMenuSortingType() {
		return $this->sortingType;
	}

	/**
	 * @deprecated use {@link getMenuTemplateId}
	 */
	function getTemplateId() {
		return $this->templateId;
	}
	
	/**
	 * @return integer
	 */
	function getMenuTemplateId() {
		return $this->templateId;
	}

	function getMenuTemplate() {
		return $this->template;
	}

	/**
	 * @return integer
	 */
	function getMenuCurrentPageId() {
		return $this->currentPageId;
	}

	/**
	 * @deprecated use {@link getMenuPageId}
	 */
	function getPageId() {
		return $this->pageId;
	}
	
	/**
	 * @return integer
	 */
	function getMenuPageId() {
		return $this->pageId;
	}

	/**
	 * @deprecated use {@link getMenuRootPageId}
	 */
	function getRootPageId() {
		return $this->rootPageId;
	}
	
	/**
	 * @return integer
	 */
	function getMenuRootPageId() {
		return $this->rootPageId;
	}

	/**
	 * @return integer
	 */
	function getMenuMaxLevel() {
		return $this->maxLevel;
	}

	/**
	 * @return integer
	 */
	function getMenuCountShown() {
		return $this->countShown;
	}

	/**
	 * @deprecated {@link getMenuTypeName}
	 */
	function getTypeName() {
		return $this->typeName;
	}
	
	/**
	 * @return string
	 */
	function getMenuTypeName() {
		return $this->typeName;
	}
	
	function &getMenuAcl() {
		return $this->acl;
	}

	function getMenuDeleted() {
		return $this->deleted;
	}

	function getMenuPublished() {
		return $this->published;
	}

	function isPublished() {
		return (bool) $this->getMenuPublished();
	}

	function setMenuMenuNodesManager(&$menuNodesManager) {
		$this->menuNodesManager = &$menuNodesManager;
	}

	function &getMenuMenuNodesManager() {
		return $this->menuNodesManager;
	}

	function setMenuTemplatesManager(&$menuTemplatesManager) {
		$this->menuTemplatesManager = &$menuTemplatesManager;
	}

	function &getMenuTemplatesManager() {
		return $this->menuTemplatesManager;
	}

	/**
	 * @param integer $menuId
	 */
	function setMenuId($menuId) {
		$this->id = $menuId;
	}

	function setMenuName($name, $langId) {
		$this->namesTable[$langId] = $name;
	}

	function setMenuTemplateId($id) {
		$this->templateId = $id;
	}
	
	function setMenuSortingType($sortingType) {
		$this->sortingType = $sortingType;
	}

	function setMenuSortField($sortField) {
		$this->sortField = $sortField;
	}

	function setMenuSortOrder($sortOrder) {
		$this->sortOrder = $sortOrder;
	}

	function setMenuRootPageId($rootPageId) {
		$this->rootPageId = $rootPageId;
	}

	function setMenuPageId($pageId) {
		$this->pageId = $pageId;
	}
	
	function setMenuAcl(&$acl) {
		$this->acl = $acl;
	}

	function setMenuAclId($aclId) {
		$this->aclId = $aclId;
	}

	function setMenuMaxLevel($maxLevel) {
		$this->maxLevel = $maxLevel;
	}

	function setMenuCountShown($countShown) {
		$this->countShown = $countShown;
	}

	function setMenuDeleted($deleted) {
		$this->deleted = $deleted;
	}

	function setMenuPublished($published) {
		$this->published = $published;
	}

	/**
	 * Sets Menu Values
	 *
	 * @param  array $array
	 * @return boolean always true
	 */
	function setMenuValues($array) {
		if(!is_array($array)) {
			return;
		}
    foreach($array as $key => $valuesTable) {
			if(is_int($key)) {
				$langId = $key;
				if(isset($valuesTable['name'])) {
					$this->setMenuName($valuesTable['name'], $langId);
				}
			}
    }
		if(isset($array['id'])) {
			$this->id = $array['id'];
		}
		if(isset($array['acl_id'])) {
			$this->aclId = $array['acl_id'];
		}
		if(isset($array['name_id'])) {
			$this->nameId = $array['name_id'];
		}
		if(isset($array['root_page_id'])) {
			$this->rootPageId = $array['root_page_id'];
		}
		if(isset($array['template'])) {
			$this->template = $array['template'];
		}
		if(isset($array['template_id'])) {
			$this->templateId = $array['template_id'];
		}
		if(isset($array['sorting_type'])) {
			$this->sortingType = $array['sorting_type'];
		}
		if(isset($array['sort_field'])) {
			$this->sortField = $array['sort_field'];
		}
		if(isset($array['sort_order'])) {
			$this->sortOrder = $array['sort_order'];			
		}
		if(isset($array['max_level'])) {
			$this->maxLevel = $array['max_level'];
		}
		if(isset($array['count_shown'])) {
			$this->countShown = $array['count_shown'];
		}
		if(isset($array['deleted'])) {
			$this->deleted = $array['deleted'];
		}
		if(isset($array['published'])) {
			$this->published = $array['published'];
		}
		return 1;
	}

	function getMenuValuesTable() {
    $valuesTable = array();

    foreach($this->namesTable as $langId => $name) {
      $valuesTable[$langId]['name'] = $name;
    }

		$valuesTable['id'] = $this->id;
		$valuesTable['name_id'] = $this->nameId;
		$valuesTable['root_page_id'] = $this->rootPageId;
		$valuesTable['sort_field'] = $this->sortField;
		$valuesTable['sort_order'] = $this->sortOrder;
		$valuesTable['sorting_type'] = $this->sortingType;
		$valuesTable['max_level'] = $this->maxLevel;
		$valuesTable['count_shown'] = $this->countShown;
		$valuesTable['template'] = $this->template;
		$valuesTable['template_id'] = $this->templateId;
		$valuesTable['acl_id'] = $this->aclId;
		$valuesTable['published'] = $this->published;
		$valuesTable['deleted'] = $this->deleted;

    return $valuesTable;
  }

  function setValues($array) {
		$this->setMenuValues($array);
	}

	function loadcontent() {
		$this->rootMenuNode = &$this->menuNodesManager->getRootMenuNode($this->getMenuId(), $this->getMenuRootPageId());
	}

	/**
	 * @access private
	 * @param string $model
	 * @param integer $level
	 * @return string
	 */
	function toInnerXML(&$user, $mode, $langId, $level) {
// deprecated
		$this->debug("Menu.toInnerXML invoked, parameters : mode = $mode, level = $level.", 10);
		$xml = '';

		if($level > $this->maxLevel) {
			return $xml;			
		}

		$pagesList = &$this->pagesLists[$level];
		
		$iterator = &$pagesList->getIterator();
		for($elem = null; $iterator->hasNext(); ){
			$elem = &$iterator->next();
			$xml .= "<page>\n";
			$xml .= $elem->toInnerXML($langId, $mode, $this->getId());
			if(isset($this->pagesOrder[$level+1]) && $elem->getPageId() == $this->pagesOrder[$level+1]) {
				$xml .= $this->toInnerXML($user, $mode, $langId, $level+1);
			}
			$xml .= "</page>\n";
      $elem->getControls($user, $this->getMenuId(), $mode);
		}
		return $xml;
	}

	/**
	 * Exports Menu Object Into XML
	 *
	 * @param  string $mode
	 * @return string
	 */
	function toXML(&$user, $langId, $page_id, $mode=VIEW_MODE) {
		$xml = '';

		if(isset($this->rootMenuNode) && $this->acl->userHasViewRight($user) && ($mode == EDIT_MODE || $this->isPublished())) {
			$xml .= "<easy_cms_menu>\n";
			$xml .= "<menu_id>".$this->getMenuId()."</menu_id>\n";
			//$xml .= "<page_id>".$this->getMenuCurrentPageId()."</page_id>\n";
			$xml .= "<name>".$this->getMenuName($langId)."</name>\n";
			$xml .= "<page_id>".$page_id."</page_id>\n";
			$xml .= "<root_page_id>".$this->getMenuRootPageId()."</root_page_id>\n";
			$xml .= "<pages>\n";
			$xml .= $this->rootMenuNode->toSubXML($user, $mode, $langId, $page_id, $this->getMenuCountShown());
			$xml .= "</pages>\n";
			$xml .= "</easy_cms_menu>\n";
		}
		return $xml;
	}

	function &getControls(&$user, $mode = VIEW_MODE) {
		$menuId = $this->getMenuId();

		$html = '';

		if ($this->acl->userHasEditRight($user) && $mode == EDIT_MODE) {
      $menu = &new egContextMenu("menu_menu_".$menuId, 0, 0, 150, 350);
			if(ENABLE_PAGES_CREATION == 1) {
				// add page
  	    $menu->addListItem(new egMenuItem("mnu_mn_".$menuId."_1", LANG_MENU_MENU_ADD, "/interface/icons/add.png", "launchPageEditionWindow(\'parent_id=".$this->getRootPageId()."\');"));
	  	  $menu->addSeparator();
			}
			// menu properties
	    $menu->addListItem(new egMenuItem("mnu_mn_".$menuId."_2", LANG_MENU_MENU_EDIT, "/interface/icons/edit.png", "launchMenuEditionWindow(\'menu_id=".$this->getMenuId()."\');"));

      $main = &new egContainer("eg_ctn_mn_".$menuId, 0, 0, 0, 0);

      $main->addObject($menu);

      $manager = &egManager::getInstance();
    	$manager->addContainer($main);

			$html .= "<a class=\"easy-CMS_UI_propertiesIcon\" href=\"javascript:egShowContextMenu(contextMenu_"."menu_menu_".$menuId.");\"><img src=\"/interface/icons/properties.png\" /></a>\n";
    }
		return $html;
	}
		
	function &toHTML(&$user, $langId, $page_id, $mode = VIEW_MODE, $drawBorder = true) {
		$templateManager = &$this->getMenuTemplatesManager();
		$this->template = &$templateManager->getTemplate($this->getTemplateId());

		$xml = &$this->toXML($user, $langId, $page_id, $mode);

		$html = &$this->template->processXMl($xml, $mode);

		if ($mode == EDIT_MODE && $drawBorder) {
			$head = '';
			$tail = '';
		
			$head .= "<div class=\"easy-CMS_UI_outer_frame\">\n";
			$head .= "	<div class=\"easy-CMS_UI_header\">\n";
			$head .= "		<div class=\"easy-CMS_UI_left\"><div class=\"easy-CMS_UI_title\">".$this->getMenuName($langId)."</div></div>\n";
			$head .= "		<div class=\"easy-CMS_UI_right\">\n";
			$head .= "			<div class=\"easy-CMS_UI_properties\">\n";
			$head .= $this->getControls($user, $mode);
			$head .= "			</div>\n";
			$head .= "		</div>\n";
			$head .= "	</div>\n";
			$head .= "	<div class=\"easy-CMS_UI_inner_frame\">\n";

			$tail .= "	</div>\n";
			$tail .= "</div>\n";
							
			$html = $head.$html.$tail;
		}

		return $html;
	}

} 
?>