<?php
function setLangs(){
	global $MXSETTINGS,$MXLANGS;
	if($MXSETTINGS["lang"]){
		$strL = ""; global $DB;		
		$sql = "SELECT * FROM `".$DB->pre."admin_language` WHERE status='1' ORDER BY langName ASC";
		$DB->dbRows($sql);	
		if($DB->numRows > 0) { $MXLANGS = $DB->rows; }
	}
}

function mxGetMeta(){
	global $DB,$TPL,$SKIPMOD,$METADATAARR;
	$str = "";	//print_r($TPL);
    if(!in_array($TPL->pageType,$SKIPMOD)) {
		$arr = explode("/",$TPL->pageUri);
		$sitePage = "/".$arr[1];
		$sql = "SELECT * FROM `".$DB->pre."admin_site_meta` WHERE sitePage = '".$sitePage."' AND metaID = '".$TPL->modID."' AND status='1'"; 
		$D = $DB->dbRow($sql); 
		$TPL->metaTitle = $D['metaTitle'];
		$TPL->metaKeyword = $D['metaKeyword'];
		$TPL->metaDesc = $D['metaDesc'];
	}
	
	$str = '<title>';
	if($TPL->metaTitle){
		$str .= $TPL->metaTitle;
	}elseif($TPL->tplTitle == 'Home'){
		$str .= $METADATAARR['home'];
	}elseif($TPL->pageTitle){
		$str .= $TPL->pageTitle;
	}elseif($TPL->tplTitle){
		$str .= $TPL->tplTitle;
	}else{
		$str .= $METADATAARR['other'];
	}
	$str .= '</title>';
	
	if($TPL->metaDesc){
		$str .= '<meta name="DESCRIPTION" content="'.mx_strip_all_tags(limitChars($TPL->metaDesc,160,''),true).'" />';
	}elseif($TPL->tplTitle == 'Home' && $METADATAARR['description'] != ''){
		$str .= '<meta name="DESCRIPTION" content="'.mx_strip_all_tags(limitChars($METADATAARR['description'],160,''),true).'" />';
	}
	if($TPL->metaKeyword){
		$str .= "\n".'<meta name="KEYWORDS" content="'.$TPL->metaKeyword.'" />';
	}elseif($TPL->tplTitle == 'Home' && $METADATAARR['keyword'] != ''){
		$str .= "\n".'<meta name="KEYWORDS" content="'.$METADATAARR['keyword'].'" />';
	}
	return $str;
}

function createMenu($menuID=0,$depth=100,$level=0){
	global $TPL,$DB;
	if($menuID){
		$sql  ="SELECT * FROM ".$DB->pre."menu WHERE status='1' AND parentID = '$menuID' ORDER BY xOrder ASC";
		$DB->dbRows($sql);
		if($DB->numRows){						
			if($level)
				$str .= '<ul class="tree-list">';
				foreach($DB->rows as $v) {
					$class = ''; $target = ''; $url = '';			
					if($v["seoUri"])
						if(strpos($TPL->pageUri,$v["seoUri"]) !== false) { $class = ' class="active"'; }	
					if($v["menuType"] == 'exlink') { 
						$url =  $v["seoUri"]; $target = ' target="_blank"';
					} else if($v["menuType"] == 'page') { 
						$url = SITEURL."/".$v["seoUri"]."/"; 
					} else { $url = SITEURL.$v["seoUri"]."/"; }
					
					$str .= '<li class="'.$v["menuClass"].'"><a'.$class.' href="'.$url.'"'.$target.'>' . $v["menuTitle"] . '</a>';
					$str .= createMenu($v["menuID"],$depth,$level+1); $level-1;
					$str .= '</li>';
				}
				if($level)
					$str .= '</ul>';				
			}
	}
	return $str;
}

function getMenu($menuTitle="",$depth=100){
	global $DB; $str = "";
	if($menuTitle){ 
		$sql = "SELECT menuID FROM ".$DB->pre."menu WHERE status='1' AND menuTitle = '$menuTitle'";
		$d = $DB->dbRow($sql);
		if($DB->numRows){
			$str = createMenu($d["menuID"],$depth);
		}
	}
	return $str;
}

function getPostCatId($postID="") {
	global $DB;	
	$sql = "SELECT categoryID  FROM ".$DB->pre."post_category WHERE postID= '$postID'";	
	$DB->dbRows($sql);
	$arrCat = array();
	
	if($DB->numRows > 0){
		foreach($DB->rows as $cat){
			$arrCat[] = $cat['categoryID'];
		}		
	}
	return $arrCat;
}

function getCatTree($arrayParameter = array()){	
	global $DB;	
	$arrayDParameter = array('parentID'=>'0','type'=>'checkbox','maxLevel'=>'100','level'=>'0','strUri'=>'','strMenu'=>'',"skipCats"=>array(),"arrCurr"=>array());
	foreach($arrayDParameter as $key=>$value){
		if($arrayParameter[$key]){
			$arrayDParameter[$key] = $arrayParameter[$key];
		}
	}

	extract($arrayDParameter);
	if($skipCats){
		$removeCats = "";
		foreach($skipCats as $k=>$v){ 
			$removeCats.= " AND categoryID != '".$v."'";	
		}
	}

	if(($level+1)<= $maxLevel){	
		$sql = "SELECT * FROM ".$DB->pre."category WHERE status = 1 AND parentID='".$parentID."' ".$removeCats." ORDER BY categoryID ASC";	
		$DB->dbRows($sql);		
		if($DB->numRows > 0) {
			if($level > 0 && $type != "")
				$strMenu.="<ul class='level-".($level-1)."'>";
				
			foreach($DB->rows as $ct){
				$prefix = "";						
				$curr = "";											
				if(!$arrCurr) 
					$arrCurr = array();
					
				if($ct['colorCode']){
					$divColor = '<div class="color" style="width:12px; height:12px; margin-right: 6px; float:left; background-color:#'.$ct['colorCode'].';"></div>';	
				}
				if($type == "checkbox") {
					if(in_array($ct['categoryID'],$arrCurr))
						$curr = ' checked="checked"';

					$strMenu.= '<li>'.$divColor.'<input type="checkbox" id="categoryID'.$ct['categoryID'].'" name="categoryID[]" value="'.$ct['categoryID'].'" '.$curr.' class="checkbox" /> '.$ct['categoryTitle'];	
				} else if($type == "radio") {
					if(in_array($ct['categoryID'],$arrCurr))
						$curr = ' checked="checked"';
						
					$strMenu.= '<li><input type="radio" id="categoryID'.$ct['categoryID'].'" name="categoryID" value="'.$ct['categoryID'].'" '.$curr.' class="radio" /> '.$ct['categoryTitle'];	
				} else if($type == "treelist") {
					$newUri = $strUri."/".$ct['seoUri'];
					if(in_array($ct['categoryID'],$arrCurr)){
						$curr = ' class="active"';}
					$strMenu.="<li$curr><a $curr href='".SITEURL.$ct['seoUri']."/' rel='".$ct['categoryID']."'>".$ct['categoryTitle']."</a>";
				} else {				
					if(in_array($ct['categoryID'],$arrCurr))
						$curr = ' selected="selected"';
					$strMenu.= "<option value='".$ct['categoryID']."'$curr>".str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',$level).$ct['categoryTitle']."</option>";
				}
																			
				$strMenu = getCatTree(array("parentID"=>$ct["categoryID"],"type"=>$type,"maxLevel"=>$maxLevel,"level"=>$level+1,"strUri"=>$newUri,"strMenu"=>$strMenu,"skipCats"=>$skipCats,"arrCurr"=>$arrCurr));
				$level-1;
				
			}
			if($level > 0 && $type != "")
				$strMenu.="</ul></li>";	
				
		} else {		
			if($type != "")
				$strMenu.="</li>"; $strUri = "";
		}	
	}
	if($strMenu) 
		return $strMenu;
}


/*function getCatTree($parentID=0,$type="checkbox",$arrCurr=array(),$skipCats = array(),$maxLevel=100,$level=0,$strUri="",$strMenu = ""){	
	global $DB;	
	$removeCats = "";
	
	if($skipCats){
		$removeCats = " AND categoryID NOT IN ('".implode("', '",$skipCats)."')";
	}

	if(($level+1)<= $maxLevel){			
		$sql = "SELECT * FROM ".$DB->pre."category WHERE status = 1 AND parentID='".$parentID."' ".$removeCats." ORDER BY categoryID ASC";	
		$DB->dbRows($sql);		
		if($DB->numRows > 0) {
			if($level > 0 && $type != "")
				$strMenu.="<ul class='level-".($level-1)."'>";
				
			foreach($DB->rows as $ct){						
				$curr = "";											
				if(!$arrCurr) 
					$arrCurr = array();
					
				if($type == "checkbox") {
					if(in_array($ct['categoryID'],$arrCurr))
						$curr = ' checked="checked"';
						
					$strMenu.= '<li><input type="checkbox" id="categoryID'.$ct['categoryID'].'" name="categoryID[]" value="'.$ct['categoryID'].'" '.$curr.' class="checkbox" /> '.$ct['categoryTitle'];	
				} else if($type == "radio") {
					if(in_array($ct['categoryID'],$arrCurr))
						$curr = ' checked="checked"';
					$strMenu.= '<li><input type="radio" id="categoryID'.$ct['categoryID'].'" name="categoryID" value="'.$ct['categoryID'].'" '.$curr.' class="radio" /> '.$ct['categoryTitle'];	
				} else if($type == "treelist") {
					$newUri = $strUri."/".$ct['seoUri'];
					if(in_array($ct['categoryID'],$arrCurr)){
						$curr = ' class="active"';}
					$strMenu.="<li$curr><a $curr href='".SITEURL.$ct['seoUri']."/' rel='".$ct['categoryID']."'>".$ct['categoryTitle']."</a>";
				} else {				
					if(in_array($ct['categoryID'],$arrCurr))
						$curr = ' selected="selected"';
					$strMenu.= "<option value='".$ct['categoryID']."'$curr>".str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',$level).$ct['categoryTitle']."</option>";
				}
						
				$strMenu = getCatTree($ct["categoryID"],$type,$arrCurr,$skipCats,$maxLevel,$level+1,$newUri,$strMenu);
				$level-1;
				
			}
			if($level > 0 && $type != "")
				$strMenu.="</ul></li>";	
				
		} else {		
			if($type != "")
				$strMenu.="</li>"; $strUri = "";
		}	
	}
	if($strMenu) 
		return $strMenu;
}*/

function queryString($params, $name=null) {
	$ret = "";
	foreach($params as $key=>$val) {
		if(is_array($val)) {
			if($name==null) $ret .= queryString($val, $key);
			else $ret .= queryString($val, $name."[$key]");   
		} else {
			if($name!=null)
				$ret.=$name."[$key]"."=$val&";
			else $ret.= "$key=$val&";
		}
	}
	return $ret;   
}

function getCatParents($categoryID=0,$arrCats=array()){
	if($categoryID){
		global $DB;
		$sql  ="SELECT parentID FROM ".$DB->pre."category WHERE categoryID='".$categoryID."' AND parentID!='0'";	
		//echo "<br>".$sql;
		$DB->dbRow($sql);		
		if($DB->numRows > 0) {						
			$arrCats[] = $DB->row["parentID"];
			$arrCats = getCatParents($DB->row["parentID"],$arrCats);			
		}
	}	
	return $arrCats;
}

function getCategoryMenu($parentID,$categoryID){
		global $DB;
		$sql  ="SELECT * FROM ".$DB->pre."category WHERE parentID='".$parentID."' ORDER BY categoryID ASC";	
		$DB->dbRows($sql);
		if($DB->numRows > 0) {						
			foreach($DB->rows as $ct){
				$curr = '';
				if($ct['categoryID']==$categoryID){
					$curr = ' class="active"';
				}
				$strMenu.="<li$curr><a $curr href='".SITEURL.$ct['seoUri']."/' rel='".$ct['categoryID']."' id='".makeSeoUri($ct['categoryTitle'])."'>".$ct['categoryTitle']."</a></li>";
			}
		}
	return $strMenu;
}

function getCatUrl($categoryID=0){
	$str = "";
	if($categoryID){
		global $DB;
		$sql  ="SELECT seoUri FROM ".$DB->pre."category WHERE categoryID='".$categoryID."'";	
		//echo "<br>".$sql;
		$DB->dbRow($sql);		
		if($DB->numRows > 0) {						
			$str = SITEURL.$DB->row["seoUri"]."/";					
		}
	}	
	return $str;
}

function getPostUrl($postArr = array()){
	global $POSTARR;
	$str = '';
	if(!empty($postArr)) {
		if(count($POSTARR) == 3){
			$str = SITEURL.'/'.date("Y/m/d",strtotime($postArr['datePublish'])).'/'.$postArr['seoUri'].'/'.$postArr['postID'].'/';
		}elseif(count($POSTARR) == 2) {
			$str = SITEURL.'/'.$postArr['seoUri'].'/'.$postArr['postID'].'/';
		}elseif(count($POSTARR) == 1) {
			$str = SITEURL.'/'.$postArr['seoUri'].'/';
		}
	}
	return $str;
}

?>