<?php
/*------------------ create breadcrum by kongxiong -----------------*/
function breadcrumbs() {
    $separator = ' &raquo; ';
	$home = 'Home';
    //$path = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));
	//$base_url = ($_SERVER['HTTPS'] ? 'https' : 'http') . ':/' . $_SERVER['REQUEST_URI'] . '/';
    // $breadcrumbs = array("<a href=\"$base_url\">$home</a>");
	/*---kongxiong edited ---------*/
	$filename = "index";

	if (isset($_GET['d'])) $filename = $_GET['d'];
	
	$slashpos = stripos($filename, "/");
	
	/*---------GET THE PREVIOUS URL ---------*/
	parse_str(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY), $queries);
	$d = $queries['d'];
	$slashpos_d = stripos($d, "/");
	//return explode('/', $d);
	return $d;
	/*----------end------------*/
	if ($slashpos != false){
		
		$c_mmfilename = substr($filename, 0, $slashpos+1) . substr($filename, $slashpos+1);
		$p_mmfilename = substr($d, 0, $slashpos_d+1) . substr($d, $slashpos_d+1);
		
		$c_filename_sh = substr($filename, $slashpos+1);
		$p_filename_sh = substr($d, $slashpos_d+1);
		
		$c_d = substr($filename, 0, $slashpos+1);
		$p_d = substr($d, 0, $slashpos_d+1);
		
		
		// if(count($_SESSION['uri'] != 0)) $p_page = $_SESSION['uri'];
		
		$c_page = "$separator<a href=\"?d=$c_mmfilename\">$c_filename_sh</a>";
		
		/*
		if($c_d == $p_d &&  $filename != $d){
			if(count($_SESSION['uri']) > 0){
				$p_page = "$separator<a href=\"?d=$p_mmfilename\">$p_filename_sh</a>";
				unset($_SESSION['uri']);
			}else{
				$p_page = "";
			}
			$_SESSION['uri']  = $d;
			$n = count($_SESSION['uri']);
			$breadcrumbs = array("<a href=\"?d=index\">$home</a>$p_page $c_page");
			
			
		}else{
			unset($_SESSION['uri']);
		*/
			$breadcrumbs = array("<a href=\"?d=index\">$home</a>$c_page ");
		//}
		
		
		/*
		$mfilename = substr($filename, 0, $slashpos+1) . "m_" . substr($filename, $slashpos+1) . ".php";
		$vfilename = substr($filename, 0, $slashpos+1) . "v_" . substr($filename, $slashpos+1) . ".php";
		$cfilename = substr($filename, 0, $slashpos+1) . "c_" . substr($filename, $slashpos+1) . ".php";
		*/
	} else {
		
		$breadcrumbs = '<a href="?d=index">$home</a>';
				
		/*
		$mfilename = "m_$filename.php";
		$vfilename = "v_$filename.php";
		$cfilename = "c_$filename.php";
		*/
	}
	//if (file_exists($mfilename)) include($mfilename);
	//if (file_exists($cfilename)) include($cfilename);
	
	/*------------- end kong code --------*/
	
	/*
    $last = end(array_keys($path));
 
    foreach ($path AS $x => $crumb) {
        $title = ucwords(str_replace(array('.php', '_'), Array('', ' '), $crumb));
        if ($x != $last){
            $breadcrumbs[] = '<a href="$base_url$crumb">$title</a>';
        }else{
            $breadcrumbs[] = $title;
        }
    }
	*/
    //return implode($separator, $breadcrumbs);
}
/*------------------------------------------Create My Own Function Json_encode --------------------------------------------*/
/* ---------------- encode ISO to utf-8 entities -------------*/
/*----------------------------------------- Author by kongxiong-------------------------*/
function kong_json_encode($a=false)
  {
    if (is_null($a)) return 'null';
    if ($a === false) return 'false';
    if ($a === true) return 'true';
    if (is_scalar($a))
    {
      if (is_float($a))
      {
        // Always use "." for floats.
        return floatval(str_replace(",", ".", strval($a)));
      }
 
      if (is_string($a))
      {
        static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
        return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';
      }
      else
        return $a;
    }
    $isList = true;
    for ($i = 0, reset($a); $i < count($a); $i++, next($a))
    {
      if (key($a) !== $i)
      {
        $isList = false;
        break;
      }
    }
    $result = array();
    if ($isList)
    {
      foreach ($a as $v) $result[] = kong_json_encode($v);
      return '[' . join(',', $result) . ']';
    }
    else
    {
      foreach ($a as $k => $v) $result[] = kong_json_encode($k).':'.kong_json_encode($v);
      return '{' . join(',', $result) . '}';
    }
 }
/*========================== this a small function but that important ======================================================================================*/
/*========== this function is used to Convert UTF-8 to ISO-8859-1 and HTML Entities so that the data can be used for the web. */
/*======================== Author by KONGXIONG =================*/
/*============ used in Reminder and planning system when send mail, solve Yahoo problem show msg =========*/
function JB_utf8_to_html($data) {
    return preg_replace("/([\\xC0-\\xF7]{1,1}[\\x80-\\xBF]+)/e", '_utf8_to_html("\\1")', $data);
}

function _utf8_to_html ($data) {
    $ret = 0;
	
    foreach ((str_split(strrev(chr((ord($data{0}) % 252 % 248 % 240 % 224 % 192) + 128) . substr($data, 1)))) as $k => $v)
        $ret += (ord($v) % 128) * pow(64, $k);
	if ($ret<256) return chr($ret); // no need to convert to entities
    return "&#$ret;";
}
/*===============================================*/
/*===============================================*/
/*--------set top menu for document system ==> kongxiong---------============================================================================*/
function doc_top_menu($uri){
	if(isset($_SESSION['sys_user'])){
		$cnt = 0;
		$top_menu = "";
		/*
		foreach($_SESSION['sys_user'] as $key => $item){
			
			if(strpos($uri, $item['app_name'])!= NULL){
				$top_menu = "<a href='#'><li>ໜ້າຫຼັກເອກະສານ</li></a>";
			}
			
		}
		*/
		/*------------------- fixed access---------------------*/
		if(strpos($uri, "document")!= NULL){
			$top_menu = "<a href='?d=document/index'><li>ໜ້າຫຼັກເອກະສານ</li></a>";
			$top_menu .= "<a href='?d=document/docin/docin'><li>ເອກະສານຂາເຂົ້າ</li></a>";
			$top_menu .= "<a href='?d=document/docout/docout'><li>ເອກະສານຂາອອກ</li></a>";
		}
		return  $top_menu;
		
	}
}
/*--------set top menu for PLANNING system ==> kongxiong---------============================================================================*/
function planing_top_menu($uri){
	if(isset($_SESSION['sys_user'])){
		$cnt = 0;
		$top_menu = "";
		/*------------------- fixed access---------------------*/
		if(strpos($uri, "planning")!= NULL){
			$top_menu = "<a href='?d=planning/index'><li>ໜ້າຫຼັກແຜນວຽກ</li></a>";
			$top_menu .= "<a href='?d=planning/statistic'><li>ສະຖິຕິການລາຍງານວຽກ</li></a>";
			// only admin can manage this menu
			foreach($_SESSION['sys_user'] as $key => $item){
				if($item['app_name'] == "planning"){
					$ptn_id = $_SESSION['sys_user'][$cnt]['position_id'];
					if($_SESSION['sys_user'][$cnt]['role_id'] == 1 || $ptn_id == 1 || $ptn_id == 2 || $ptn_id == 3 || $ptn_id ==4){
						$top_menu .= "<a href='?d=planning/chwork'><li>ຈັດການສະຖານະວຽກ & ເວລາ</li></a>";
						$top_menu .= "<a href='?d=planning/addwork'><li>ເພີ່ມໜ້າວຽກ</li></a>";
					}
				}
				$cnt++;
			}
			
		}
		return  $top_menu;
		
	}
}
/*--------set top menu for REMINDER system ==> kongxiong---------============================================================================*/
function reminder_top_menu($uri){
	if(isset($_SESSION['sys_user'])){
		$cnt = 0;
		$top_menu = "";
		/*------------------- fixed access---------------------*/
		if(strpos($uri, "reminder")!= NULL){
			$top_menu = "<a href='?d=reminder/index'><li>ໜ້າຫຼັກ ເວບໄຊ Reminder</li></a>";
			//$top_menu .= "<a href='?d=reminder/plan'><li>ແຜນປະຊຸມ</li></a>";
			// only admin can manage this menu
			foreach($_SESSION['sys_user'] as $key => $item){
				if($item['app_name'] == "reminder"){
					$ptn_id = $_SESSION['sys_user'][$cnt]['position_id'];
					if($_SESSION['sys_user'][$cnt]['role_id'] == 1){
						$top_menu .= "<a href='?d=reminder/admin/meet'><li>ຫົວຂໍ້ປະຊຸມ</li></a>";
						$top_menu .= "<a href='?d=reminder/admin/manage'><li>ຜູ້ເຂົ້າຮ່ວມ</li></a>";
						//$top_menu .= "<a href='?d=reminder/admin/managedata'><li>ຈັດການຂໍ້ມູນ</li></a>";
						$top_menu .= "<a href='?d=reminder/admin/room'><li>ຈັດການຂໍ້ມູນຫ້ອງປະຊຸມ</li></a>";
						$top_menu .= "<a href='?d=reminder/admin/report'><li>ລາຍງານ</li></a>";
					}
				}
				$cnt++;
			}
			
		}
		return  $top_menu;
		
	}
}
/*-------- Check the agenda has aleady in REMINDER system ==> kongxiong---------============================================================================*/
function chDelayMail(){
	$sql_ch = "SELECT m.m_id, mm.start_date FROM tbl_meet m INNER JOIN tbl_m_reserve mm ON (mm.m_id = m.m_id) WHERE m.status <> 1 AND (timediff(mm.start_date, NOW()) < '00:00:00')";
	$result_ch = mysql_query($sql_ch);
	$num_ch = mysql_num_rows($result_ch);
	if($num_ch != 0){
		while($row1 = mysql_fetch_array($result_ch, MYSQL_ASSOC)){
			$sql_up = "UPDATE tbl_meet SET status = 1 WHERE m_id = $row1[m_id]";
			$result = mysql_query($sql_up);
			//return $result;
		}
	}
}
/*--------set top menu for webboard ==> maisouk---------============================================================================*/
function webboard_top_menu($uri){
	if(isset($_SESSION['sys_user'])){
		$cnt = 0;
		$top_menu = "";
		/*------------------- fixed access---------------------*/
		if(strpos($uri, "webboard")!= NULL){
			$top_menu = "<a href='?d=webboard/index'><li>ໜ້າຫຼັກເວບບອດ</li></a>";
			$top_menu .="<a href='?d=webboard/addboard'><li><img src='img/newtopic.png' width='20px' height='20px'>ຕັ້ງກະທູ້ໃໝ່</li></a>";			
			}
		return  $top_menu;
		
	}
}

/*--------set top menu for presentation ==> maisouk---------============================================================================*/
function present_top_menu($uri){
	if(isset($_SESSION['sys_user'])){
		$cnt = 0;
		$top_menu = "";
		/*------------------- fixed access---------------------*/
		if(strpos($uri, "present")!= NULL){
			$top_menu = "<a href='?d=present/index'><li>ໜ້າຫຼັກ ຫົວຂໍ້ Presentation</li></a>";
			$top_menu .="<a href='?d=present/addpresent&start=".$_GET['start']."'><li><img src='img/newtopic.png' width='20px' height='20px'>ເພີ່ມຫົວຂໍ້ Presentation</li></a>";			
			}
		return  $top_menu;
		
	}
}

