<?php

class MenuMod extends DbMod{
	var $VarTableMenuTypes = array('integer', 'integer', 'text', 'integer', 'text', 'integer', 'integer', 'integer', 'integer', 'integer', 'integer', 'text', 'text');
  var $VarLang = '';

	function MenuMod($ArrMsSp){
		parent::__construct($ArrMsSp);
		return true;
	}

  function GetFromDb($VarSqlName = 'SqlAll', $VarSqlGet = 'GetAll', $VarWhere = '', $VarOrder = '', $VarLimit = ''){
    $ArrSqlArray['SqlAll']['select'] = 'm.*, t.template_file, mn.menuname_name';
    $ArrSqlArray['SqlAll']['from'] = $this->VarTableMenuName .' AS mn, '.$this->VarTableTemplate .' AS t, '. $this->VarTableMenu .' AS m';
    $ArrSqlArray['SqlAll']['where'] = "m.menuname_id = mn.menuname_id AND m.template_id = t.template_id";
    if('' != $VarWhere){
      $ArrSqlArray['SqlAll']['where'] .= ' AND ' .$VarWhere;
    }
    $ArrSqlArray['SqlAll']['order'] = "m.menu_parent_id, m.menu_orderfield";
    if('' != $VarOrder){
      $ArrSqlArray['SqlAll']['order'] .= ', '. $VarOrder;
    }
    if('' != $VarLimit){
      $ArrSqlArray['SqlAll']['limit'] = $VarLimit;
    }
    //
    $ArrSqlArray['SqlParentAssoc']['select'] = 'm.menu_parent_id, m.menu_id';
    $ArrSqlArray['SqlParentAssoc']['from'] = $this->VarTableMenuName .' AS mn, '. $this->VarTableLang .' AS l, '. $this->VarTableMenu .' AS m';
    $ArrSqlArray['SqlParentAssoc']['where'] = "m.menuname_id = mn.menuname_id";
    if('' != $VarWhere){
      $ArrSqlArray['SqlParentAssoc']['where'] .= ' AND ' .$VarWhere;
    }
    $ArrSqlArray['SqlParentAssoc']['order'] = "m.menu_parent_id";
    if('' != $VarOrder){
      $ArrSqlArray['SqlParentAssoc']['order'] .= ', '. $VarOrder;
    }
    //
    $ArrResult = $this->DbGet($ArrSqlArray[$VarSqlName], $VarSqlGet);
  	return $ArrResult;
	}

	function GetMenuAdv($VarMenuName = '', $VarMenuType = 'smarty', $ArrWebServises = array(), $VarWhere = '', $VarOrderFielld = ''){
		if('' != $VarMenuName){
			switch ($VarMenuType) {
        case 'JQueryMbMenu':
          $VarWhereAdd = ' m.menu_access IS NULL';
          if (true === is_array($ArrWebServises) && 0 < count($ArrWebServises)){
            $VarWhereAdd .= ' OR m.menu_access IN ('. implode(",", $ArrWebServises) .')';
          }
          $VarWhereAdd = "mn.menuname_name = '". $VarMenuName ."'  AND (". $VarWhereAdd .")";
          if('' != $VarWhere){
            $VarWhereAdd .= ' AND '. $VarWhere;
          }
          $ArrMenu = $this->GetFromDb('SqlAll', 'GetAll', $VarWhereAdd, $VarOrderFielld);
//          SiteLibs::GetVarDump($ArrMenu);
          $ArrMenu = $this->array2tree($ArrMenu, 0, 'submenu', 'menu_id', 'menu_parent_id');
          if(true === is_array($ArrMenu) && 0 < count($ArrMenu)){
            return $ArrMenu;
          }
          else{
            return 0;
          }
        break;
				default:
    			echo "i is not equal to 0, 1 or 2";
			}
		}
	}

	function CreateArrayYuiMenuJs($menu = array(), &$ArrMenuJs = ''){
		$ArrMenuJs .="[";
		$VarPrizn = 0;
		foreach ($menu AS $k => $v){
			if (1 == $v['expanded']){
				//echo "zzz:". $v['expanded'] ."<br />";
				$VarPrizn = 1;

			}
		}
		if(1 == $VarPrizn){
			$ArrMenuJs .="[";
		}
		foreach ($menu AS $k => $v){
			$ArrMenuJs .= ' { text: "'. $v['text'] .'", url: "'. $v['href'] .'"';
			if(true === is_numeric($v['access']) && 0 < $v['access']){
				if(false === is_array($_SESSION['SessionAuth']['webservices']) OR false === in_array($v['access'], $_SESSION['SessionAuth']['webservices'])){
					$ArrMenuJs .= ', disabled: true ';
				}
			}
			if (true === is_array($v['submenu']) && 0 < count($v['submenu'])){
				$ArrMenuJs .= ', submenu: {
	            id: "menu'. $k .'",
	            itemdata: ';
				$this->CreateArrayYuiMenuJs($v['submenu'], $ArrMenuJs);
				$ArrMenuJs .= '} ';
			}
			//$ArrMenuJs .="}, \n";

			$ArrMenuJs .= '} ';

			if (1 == $v['expanded']){
				$ArrMenuJs .="\n], \n[";
			}
			else {
				$ArrMenuJs .=", ";
			}

			$ArrMenuJs .="\n";
		}
		if(1 == $VarPrizn){
			$ArrMenuJs = "". $ArrMenuJs ."]\n";
		}
		$ArrMenuJs .="]";
	}

	function GetMenu($VarMenuName = ''){
		if('' != $VarMenuName){
			//require_once($_SERVER['DOCUMENT_ROOT'] .'/script/menuname.mod.php');
			$ObjMenuNameMod = new MenuNameMod($this->ArrMsSp);
			$ArrCurr = $ObjMenuNameMod->GetFromDb('GetById', "t1.mn_name = '". $VarMenuName ."'");
			$ArrMenu = $this->GetFromDb('GetAll', "mn.menuname_name = '". $VarMenuName ."'");
			//SitePage::GetVarDump($ArrMenu);
			if (true === is_array($ArrMenu) && 0 < count($ArrMenu)){
				$ArrTree = array2tree($ArrMenu, 1, 'sub', 'menu_id', 'menu_parent_id');
				$this->VarSmarty->assign('Arr', $ArrMenu);
				$VarReturn = $this->VarSmarty->fetch($ArrCurr['template_file']);
				$this->VarSmarty->assign('Var'. $ArrCurr['menuname_name'], $VarReturn);
				return $VarReturn;
			}
			else {
				return '';
			}
		}
		else {
			return '';
		}
	}

	function GetTree($ArrSqlResult, $VarMenuId = '', $VarMenuCurrentId = '', $VarUid = '-1', $VarGid = '-1'){
		if(false === isset($VarMenuId) || '' == $VarMenuId){
			$VarMenuId = 0;
		}
		//SitePage::GetVarDump($ArrSqlResult);
		//$ArrSqlResult = $this->GetMenuArray($VarMenuName, $VarUid, $VarGid, '');
		$ArrTree = $this->array2tree($ArrSqlResult, $VarMenuId, 'submenu');
		$ArrOpenTreeIn = array();

		$this->GetKeyByValue($ArrSqlResult, $VarMenuId, $VarMenuCurrentId, $ArrOpenTreeIn);
		$ArrOpenTree = $ArrOpenTreeIn;
		$ArrOpenTreeIn = array();
		$ArrOpenTree = array_reverse($ArrOpenTree, true);
		$VarReturn = array(
			'ArrTree' => $ArrTree,
			'ArrOpenTree' => $ArrOpenTree,
		);

		return $VarReturn;
	}

	function array2tree($source_arr, $parent_id, $key_children='child_nodes', $key_id='menu_id', $key_parent_id='menu_parent_id'){
  	$tree=array();
  	if (empty($source_arr)){
  		return $tree;
  	}
  	$this->_array2treer($source_arr, $tree, $parent_id, $parent_id, $key_children, $key_id, $key_parent_id);
  	return $tree;
	}

	function _array2treer($source_arr, &$_this, $parent_id, $_this_id, $key_children, $key_id, $key_parent_id){
		foreach ($source_arr as $value)
  		if ($value[$key_parent_id] == $_this_id){
    		$_this[$key_children][$value[$key_id]]=$value;
    	}
    	if (isset($_this[$key_children])){
				foreach ($_this[$key_children] as $value)
      		$this->_array2treer($source_arr, $_this[$key_children][$value[$key_id]], $parent_id, $value[$key_id], $key_children, $key_id, $key_parent_id);
				if ($_this_id == $parent_id)
      		$_this=$_this[$key_children];
    	}
	}

	function GetKeyByValue($ArrSqlResult, $VarMenuId, $VarMenuCurrentId, &$ArrOpenTreeIn){
    foreach ($ArrSqlResult AS $key => $value){
      if ($VarMenuCurrentId == $value['menu_id']) {
        $ArrOpenTreeIn[$value['menu_id']] = $value['menu_parent_id'];
        if($VarMenuId == $value['menu_parent_id']){
          return true;
        }
        $this->GetKeyByValue($ArrSqlResult, $VarMenuId, $value['menu_parent_id'], $ArrOpenTreeIn);
      }
    }
    return false;
	}
  
  function GetPagesPathArray(){
		$VarWhileControl = 1;
		$VarWhileFirst = true;
		while (0 < $VarWhileControl){
			if (true === $VarWhileFirst){
				//$ArrRow = $this->GetPageMenuRow('href', $this->SP_CurrentHref);
				$ArrRow = $this->GetFromDb('SqlAll', 'GetRow', "m.menu_href = '". $this->ArrMsSp->SP_CurrentHref ."'");
				$VarWhileFirst = false;
			}
			else {
				//$ArrRow = $this->GetPageMenuRow('id', $VarPrevId);
        $ArrRow = $this->GetFromDb('SqlAll', 'GetRow', "m.menu_id = '". $VarPrevId ."'");
			}
			if (true === isset($ArrRow['menu_parent_id']) && true === is_numeric($ArrRow['menu_parent_id']) && '/index.php' != $ArrRow['menu_href']){
				$VarPrevId = $ArrRow['menu_parent_id'];
				$ArrReturn[] = $ArrRow;
				if (1 >= $ArrRow['menu_parent_id']){
					$VarWhileControl = 0;
				}
			}
			else {
				$VarWhileControl = 0;
			}
		}
		//$ArrReturn[] = $this->GetPageMenuRow('href', '/index.php');
		$ArrReturn[] = $this->GetFromDb('SqlAll', 'GetRow', "m.menu_href = '/index.php'");
		return $ArrReturn;
	}
}
?>