<?php
if (!defined("root")) {
	define("root", $_SERVER['DOCUMENT_ROOT']);
}
require(root."/___simple_config.php");
session_start();
require(root."/".adminFolder."/_common.php");
require(root."/".adminFolder."/_lib/db.php");
require(root."/".adminFolder."/_lib/main.php");
require(root."/".adminFolder."/_lib/auth.php");
require(root."/".adminFolder."/_lib/bootstraps.php");
$pageSubOf = "settings";
$pageName = "languages";
$pageTitle = "Languages";
$thisUrl = "languages";
$ajax_page = "ajax_languages.php";
$pageUrl = "/".adminFolder."/$thisUrl";
$a = cekpost(cleanQuery($_GET["a"]), "");
$err = "";
if ($a=="section_main") {
	if (!authorizedModule("games_main", $ses_kduser)) {
		unAuthorizedAccess("games_main");
	} else {
		$fil_kodelang = trim(cekpost(cleanQuery($_REQUEST["fil_kodelang"]), ""));
		$fil_namalang = trim(cekpost(cleanQuery($_REQUEST["fil_namalang"]), ""));
		$fil_isactive = trim(cekpost(cleanQuery($_REQUEST["fil_isactive"]), ""));
		$swhere = '';
		if ($fil_kodelang!="") { $swhere .= " AND a.kodelang LIKE '%$fil_kodelang%' "; }
		if ($fil_namalang!="") { $swhere .= " AND a.namalang LIKE '%$fil_namalang%' "; }
		if ($fil_isactive!="") { $swhere .= " AND a.isactive = '$fil_isactive' "; }
		
		$rs=array(); $q="SELECT a.* FROM langs a WHERE 1=1 $swhere";
		callRS($rs, $q);
		if (count($rs)!=0) {
			$ld = 20;
			$cp = cekpost(cleanQuery($_GET["cp"]), "1");
			$tp = intval(count($rs) / $ld) + (count($rs)%$ld==0?0:1);
			$sr = ($cp-1) * $ld;
			$pagination = '';
			if ($tp!=1) {
				$pagination .= ('<ul class="pagination pagination-sm">');
					$disabled = '';
					$purl = '#';
					$onclick = '';
					if ($cp<=1) {
						$disabled = 'disabled';
						$onclick = ' onclick="return false;" ';
					}
					$pagination .= ('<li class="'.$disabled.'"><a href="'.$purl.'" '.($onclick!=""?$onclick:'onclick="filterReload(\''.$pageName.'\', 1);return false;"').'>&lt;&lt;</a></li>');
					$pagination .= ('<li class="'.$disabled.'"><a href="'.$purl.'" '.($onclick!=""?$onclick:'onclick="filterReload(\''.$pageName.'\', '.($cp-1).');return false;"').'>&lt;</a></li>');
					for ($c=1;$c<=$tp;$c++) {
						$active = '';
						$purl = '#';
						$disabled = '';
						$onclick = '';
						if ($c==$cp) {
							$active = 'active';
						} else {
							$purl = '#';
							$onclick = ' onclick="filterReload(\''.$pageName.'\', '.$c.');return false;" ';
						}
						$pagination .= ('<li class="'.$active.' '.$disabled.'"><a href="'.$purl.'" '.$onclick.'>'.$c.'</a></li>');
					}
					$disabled = '';
					$onclick = '';
					if ($cp>=$tp) {
						$disabled = 'disabled';
						$onclick = ' onclick="return false;" ';
					}
					$pagination .= ('<li class="'.$disabled.'"><a href="#" '.($onclick!=""?$onclick:'onclick="filterReload(\''.$pageName.'\', '.($cp+1).');return false;"').'>&gt;</a></li>');
					$pagination .= ('<li class="'.$disabled.'"><a href="#" '.($onclick!=""?$onclick:'onclick="filterReload(\''.$pageName.'\', '.$tp.');return false;"').'>&gt;&gt;</a></li>');
				$pagination .= ('</ul>');
			}
			cout('<div class="alert alert-info" role="alert"><strong>Total whole data</strong> <span class="badge">'.count($rs).'</span></div>');
			
			if ($tp!=1) {
				cout($pagination);
			}
			cout('<table class="table table-striped table-bordered">');
				cout('<thead>');
					cout('<tr>');
						cout('<th class="text-center">#</th>');
						if (authorizedModule("games_element_name", $ses_kduser)) {
							cout('<th class="text-center">Name</th>');
							cout('<th class="text-center">URL</th>');
						}
						if (authorizedModule("games_element_active", $ses_kduser)) {
							cout('<th class="text-center">Active?</th>');
						}
						cout('<th class=""></th>');
					cout('</tr>');
				cout('</thead>');
				cout('<tbody>');
				$cnt = 0;
				for ($i=$sr;$i<count($rs) && $cnt<$ld;$i++) {
					$cnt++;
					$kdlang = $rs[$i]["kdlang"];
					if (intval($rs[$i]["isactive"])==0) {
						$isactive = '<a href="#" title="Activate" onclick="chActive(\''.$pageName.'\', '.$kdlang.', '.$rs[$i]["isactive"].');return false;">Non-Active</a>';
					} else {
						$isactive = '<a href="#" title="Deactivate" onclick="chActive(\''.$pageName.'\', '.$kdlang.', '.$rs[$i]["isactive"].');return false;">Active</a>';
					}
					$isi = str_replace("\n", "<br/>", $rs[$i]["isi"]);
					
					$links = $rs[$i]["links"];
					$arr_links = explode(",", $links);
					$tmp_link = '';
					$cnt_links = 0;
					for ($j=0;$j<count($arr_links);$j++) {
						$tmp_link = trim($arr_links[$j]);
						if ($tmp_link!="") { $cnt_links += 1; }
					}
					cout('<tr>');
						cout('<td class="text-center">'.($i+1).'</td>');
						if (authorizedModule("games_element_name", $ses_kduser)) {
							cout('<td class="text-center"><a href="'.$pageUrl.'/view/'.$rs[$i]["kdlang"].'/">'.$rs[$i]["kodelang"].'</a></td>');
							cout('<td class="text-center">'.$rs[$i]["namalang"].'</td>');
						}
						if (authorizedModule("games_element_active", $ses_kduser)) {
							cout('<td class="text-center isactive'.$kdlang.'">'.$isactive.'</td>');
						}
						cout('<td class="text-center">');
							if (authorizedModule("games_edit", $ses_kduser)) {
								cout('<a href="'.$pageUrl.'/edit/'.$kdlang.'/"><span class="glyphicon glyphicon-pencil"></span><span class="hidden-xs hidden-sm"> Edit</span></a>');
								cout(BSTextSeparator());
							}
							if (authorizedModule("games_delete", $ses_kduser)) {
								cout('<a href="'.$pageUrl.'/delete/'.$kdlang.'/"><span class="glyphicon glyphicon-trash"></span><span class="hidden-xs hidden-sm"> Delete</span></a>');
							}
						cout('</td>');
					cout('</tr>');
				}
				cout('</tbody>');
			cout('</table>');
			
			if ($tp!=1) {
				cout($pagination);
			}
			cout('<div class="alert alert-info" role="alert"><strong>Total whole data</strong> <span class="badge">'.count($rs).'</span></div>');
			$act_ket = "Languages: Main List";
			if ($swhere!="") { $act_ket .= ": ".cleanQuery($swhere).""; }
			updateUserActivities($act_ket);
		} else {
			cout('<div class="alert alert-info" role="alert">');
				cout('<strong>No data available.</strong> Please add new data for display.');
			cout('</div>');
		}
	}
}
if ($a=="chactive") {
	$kd = cekpost(cleanQuery($_REQUEST["kd"]), "");
	$ca = cekpost(cleanQuery($_REQUEST["ca"]), "");
	$kd = (trim($kd)=="undefined")?'':$kd;
	$ca = (trim($ca)=="undefined")?'':$ca;
	if ($kd!="" && $kd!=0 && $ca!="") {
		$na = $ca==0?1:0;
		$modby = $_SESSION["user"]["inisial"];
		$modtime = time();
		$qu = "UPDATE langs SET isactive='$na', modby='$modby', modtime='$modtime' WHERE kdlang='$kd'";
		execRS($qu);
		$act_ket = "Languages: Ch. Active [kd: $kd][from Act: $ca][to Act: $na]";
		updateUserActivities($act_ket);
		if (intval($na)==0) {
			$isactive = '<a href="#" title="Activate" onclick="chActive(\''.$pageName.'\', '.$kd.', '.$na.');return false;">Non-Active</a>';
		} else {
			$isactive = '<a href="#" title="Deactivate" onclick="chActive(\''.$pageName.'\', '.$kd.', '.$na.');return false;">Active</a>';
		}
		echo $isactive;
	}
}

if ($a=="section_view") {
	if (!authorizedModule("games_view", $ses_kduser)) {
		unAuthorizedAccess("games_view");
	} else {
		$id = cekpost(cleanQuery($_GET["id"]), "0");
		$rs=array(); $q="SELECT a.* FROM langs a WHERE kdlang='$id'";
		callRS($rs, $q);
		if (intval($rs[0]["isactive"])==0) {
			$isactive = '<a href="#" title="Activate" onclick="chActive(\''.$pageName.'\', '.$rs[0]["kdlang"].', '.$rs[0]["isactive"].');return false;">Non-Active</a>';
		} else {
			$isactive = '<a href="#" title="Deactivate" onclick="chActive(\''.$pageName.'\', '.$rs[0]["kdlang"].', '.$rs[0]["isactive"].');return false;">Active</a>';
		}
		$creby = (trim($rs[0]["creby"])!=""?'By: <strong>'.$rs[0]["creby"].'</strong>':'By: &lt;<em>no data</em>&gt;').(trim($rs[0]["cretime"])?' ('.date('d M Y, H:i:s A', $rs[0]["cretime"]).')':' &lt;<em>no data</em>&gt;');
		$modby = (trim($rs[0]["modby"])!=""?'By: <strong>'.$rs[0]["modby"].'</strong>':'By: &lt;<em>no data</em>&gt;').(trim($rs[0]["modtime"])?' ('.date('d M Y, H:i:s A', $rs[0]["modtime"]).')':' &lt;<em>no data</em>&gt;');
		cout('<dl class="dl-horizontal dldata">');
			if (authorizedModule("games_element_name", $ses_kduser)) {
				cout('<dt>Language Code</dt>');
				cout('<dd>'.$rs[0]["kodelang"].'</dd>');
				
				cout('<dt>Language Name</dt>');
				cout('<dd>'.$rs[0]["namalang"].'</dd>');
				
				cout('<dt>Language Flag</dt>');
				cout('<dd><span class="flag flag-'.$rs[0]["flag"].'"></span></dd>');
			}
			if (authorizedModule("games_element_active", $ses_kduser)) {
				cout('<dt>Active Status</dt>');
				cout('<dd class="isactive'.$rs[0]["kdlang"].'">'.$isactive.'</dd>');
			}
			if (authorizedModule("games_element_creation", $ses_kduser)) {
				cout('<dt>Created</dt>');
				cout('<dd>'.$creby.'</dd>');
			}
			if (authorizedModule("games_element_modification", $ses_kduser)) {
				cout('<dt>Modified</dt>');
				cout('<dd>'.$modby.'</dd>');
			}
			
		cout('</dl>');
		
		$act_ket = "Languages: View: [".cleanQuery(printR($rs, 1))."]";
		updateUserActivities($act_ket);
		exit;
	}
	exit;
}
if ($a=="do_delete") {
	$kirim = cekpost(cleanQuery($_REQUEST["kirim"]), "");
	if ($kirim=="1") {
		$delby = $_SESSION["user"]["inisial"];
		$deltime = time();
		$kdlang = cekpost(cleanQuery($_POST["kdlang"]), "0");
		$rs=array(); $q="SELECT * FROM langs WHERE kdlang='$kdlang'";
		callRS($rs, $q);
		
		$q = "DELETE FROM langs WHERE kdlang='$kdlang'";
		execRS($q);
		$act_ket = "Languages: Delete: Deleted [kd: $kdlang][".cleanQuery(printR($rs, 1))."]";
		updateUserActivities($act_ket);
		cout('<script type="text/javascript">parent.window.location="'.$pageUrl.'/";</script>');
		exit;
	}
}
if ($a=="section_delete") {
	if (!authorizedModule("games_delete", $ses_kduser)) {
		unAuthorizedAccess("games_delete");
	} else {
		$id = cekpost(cleanQuery($_GET["id"]), "0");
		$rs=array(); $q="SELECT * FROM langs WHERE kdlang='$id'";
		callRS($rs, $q);
		
		cout('<form method="post" target="quick_iframe" action="/'.adminFolder.'/_ajax/'.$ajax_page.'?a=do_delete" role="form">');
			cout('<input type="hidden" name="kirim" id="kirim" value="1" />');
			cout('<input type="hidden" name="kdlang" id="kdlang" value="'.$rs[0]["kdlang"].'" />');
			cout('<dl class="dl-horizontal dldata">');
				if (authorizedModule("games_element_name", $ses_kduser)) {
					cout('<dt>Language Code</dt>');
					cout('<dd>'.$rs[0]["kodelang"].'</dd>');
					cout('<dt>Language Name</dt>');
					cout('<dd>'.$rs[0]["namalang"].'</dd>');
					cout('<dt>Flag</dt>');
					cout('<dd><span class="flag flag-'.$rs[0]["flag"].'"></span></dd>');
				}
				cout('<dt></dt>');
				cout('<dd><button class="btn btn-primary" type="submit">Delete</button></dd>');
			cout('</dl>');
		cout('</form>');
		$viewFrame = 0;
		if ($viewFrame==1) {
			cout('<iframe name="quick_iframe" id="quick_iframe" src="/'.adminFolder.'/_ajax/index.php" width="300" height="100" style="overflow:;border:1px red solid;" visibility="" scrolling="yes" frameborder="1"></iframe>');
		} else {
			cout('<iframe name="quick_iframe" id="quick_iframe" src="/'.adminFolder.'/_ajax/index.php" width="300" height="11" style="overflow:hidden;" visibility="hidden" scrolling="no" frameborder="0"></iframe>');
		}
		$act_ket = "Languages: Deleting... [".cleanQuery(printR($rs, 1))."]";
		updateUserActivities($act_ket);
		exit;
	}
}
if ($a=="do_edit") {
	$kirim = cekpost(cleanQuery($_REQUEST["kirim"]), "");
	$err = "";
	if ($kirim=="1") {
		$kdlang = cekpost(cleanQuery($_REQUEST["kdlang"]), "0");
		$kodelang = cekpost(cleanQuery($_REQUEST["kodelang"]), "");
		$namalang = cekpost(cleanQuery($_REQUEST["namalang"]), "");
		$flag = cekpost(cleanQuery($_REQUEST["flag"]), "");
		$isactive = cekpost(cleanQuery($_REQUEST["isactive"]), 0);
		if ($kodelang=="") { $err.= BSErrorMessage('Language Code must be filled'); }
		if ($namalang=="") { $err.= BSErrorMessage('Language Name must be filled'); }
		if ($err=="") {
			$modby = $_SESSION["user"]["inisial"];
			$modtime = time();
			$q = "UPDATE langs SET kodelang='$kodelang', namalang='$namalang', flag='$flag', isactive='$isactive', modby='$modby', modtime='$modtime' 
				WHERE kdlang='$kdlang'";
			execRS($q);
			$act_ket = "Languages: Edit: Updated [query: ".cleanQuery($q)."]";
			updateUserActivities($act_ket);
			cout('<script type="text/javascript">parent.window.location="'.$pageUrl.'/view/'.$kdlang.'/";</script>');
		} else {
			cout('<script type="text/javascript">parent.showError(\''.$err.'\');</script>');
		}
		exit;
	}
	exit;
}
if ($a=="section_edit") {
	if (!authorizedModule("games_edit", $ses_kduser)) {
		unAuthorizedAccess("games_edit");
	} else {
		$id = cekpost(cleanQuery($_GET["id"]), "0");
		$rs=array(); $q="SELECT * FROM langs WHERE kdlang='$id'";
		callRS($rs, $q);
		cout('<form method="post" target="quick_iframe" action="/'.adminFolder.'/_ajax/'.$ajax_page.'?a=do_edit" role="form">');
			cout('<input type="hidden" name="kirim" id="kirim" value="1" />');
			cout('<input type="hidden" name="kdlang" id="kdlang" value="'.$rs[0]["kdlang"].'" />');
			cout('<dl class="dl-horizontal dldata">');
				if (authorizedModule("games_element_name", $ses_kduser)) {
					cout('<dt><label for="kodelang">Language Code</label></dt>');
					cout('<dd><input class="form-control" type="text" name="kodelang" id="kodelang" value="'.$rs[0]["kodelang"].'" /></dd>');
					cout('<dt><label for="namalang">Language Name</label></dt>');
					cout('<dd><input class="form-control" type="text" name="namalang" id="namalang" value="'.$rs[0]["namalang"].'" /></dd>');
				}
				cout('<dt><label for="flag">Language Flag</label></dt>');
				cout('<dd>
					<div class="row">
						<div class="col-xs-1 text-center">
							<span class="flag flag-'.$rs[0]["flag"].'"></span>
						</div><!--/.col-xs-1-->
						<div class="col-xs-11">
							<select name="flag" id="flag" class="form-control" onchange="selectFlag(\'flag\');">
								<option value="">-</option>
								<option value="id" '.($rs[0]["flag"]=="id"?'selected="selected"':'').'>ID</option>
								<option value="uk" '.($rs[0]["flag"]=="uk"?'selected="selected"':'').'>UK</option>
								<option value="cn" '.($rs[0]["flag"]=="cn"?'selected="selected"':'').'>CN</option>
								<option value="us" '.($rs[0]["flag"]=="us"?'selected="selected"':'').'>US</option>
								<option value="sg" '.($rs[0]["flag"]=="sg"?'selected="selected"':'').'>SG</option>
								<option value="my" '.($rs[0]["flag"]=="my"?'selected="selected"':'').'>MY</option>
							</select>
						</div><!--/.col-xs-11-->
					</div><!--/.row-->
				</dd>');
				
				if (authorizedModule("games_element_active", $ses_kduser)) {
					$isactive0 = intval($rs[0]["isactive"])==0?'selected="selected"':'';
					$isactive1 = intval($rs[0]["isactive"])==1?'selected="selected"':'';
					cout('<dt><label for="isactive">Active Status</label></dt>');
					cout('<dd>
						<select name="isactive" id="isactive" class="form-control">
							<option value="0" '.$isactive0.'>Non-Active</option>
							<option value="1" '.$isactive1.'>Active</option>
						</select>
					</dd>');
				}
				cout('<dt></dt>');
				cout('<dd class="err"></dd>');
				
				cout('<dt></dt>');
				cout('<dd><button class="btn btn-primary" type="submit">Update</button></dd>');
			cout('</dl>');
		cout('</form>');
		$viewFrame = 0;
		if ($viewFrame==1) {
			cout('<iframe name="quick_iframe" id="quick_iframe" src="/'.adminFolder.'/_ajax/index.php" width="300" height="100" style="overflow:;border:1px red solid;" visibility="" scrolling="yes" frameborder="1"></iframe>');
		} else {
			cout('<iframe name="quick_iframe" id="quick_iframe" src="/'.adminFolder.'/_ajax/index.php" width="300" height="11" style="overflow:hidden;" visibility="hidden" scrolling="no" frameborder="0"></iframe>');
		}
		$act_ket = "Languages: Editing... [".cleanQuery(printR($rs, 1))."]";
		updateUserActivities($act_ket);
		exit;
	}
}
if ($a=="do_add") {
	$kirim = cekpost(cleanQuery($_REQUEST["kirim"]), "");
	$err = "";
	if ($kirim=="1") {
		$kodelang = cekpost(cleanQuery($_REQUEST["kodelang"]), "");
		$namalang = cekpost(cleanQuery($_REQUEST["namalang"]), "");
		$flag = cekpost(cleanQuery($_REQUEST["flag"]), "");
		$isactive = cekpost(cleanQuery($_REQUEST["isactive"]), 0);
		if ($kodelang=="") { $err.= BSErrorMessage('Language Code must be filled'); }
		if ($namalang=="") { $err.= BSErrorMessage('Language Name must be filled'); }
		if ($err=="") {
			$creby = $_SESSION["user"]["inisial"];
			$cretime = time();
			$kdlang = getID("langs", "kdlang");
			$q = "INSERT INTO langs (kdlang, kodelang, namalang, flag, isactive, creby, cretime) 
				VALUES ('$kdlang', '$kodelang', '$namalang', '$flag', '$isactive', '$creby', '$cretime')";
			execRS($q);
			$act_ket = "Languages: Add [query: ".cleanQuery($q)."]";
			updateUserActivities($act_ket);
			cout('<script type="text/javascript">parent.window.location="'.$pageUrl.'/view/'.$kdlang.'/";</script>');
		} else {
			cout('<script type="text/javascript">parent.showError(\''.$err.'\');</script>');
		}
		exit;
	}
	exit;
}
if ($a=="section_add") {
	if (!authorizedModule("games_add", $ses_kduser)) {
		unAuthorizedAccess("games_add");
	} else {
		cout('<form method="post" target="quick_iframe" action="/'.adminFolder.'/_ajax/'.$ajax_page.'?a=do_add" role="form">');
			cout('<input type="hidden" name="kirim" id="kirim" value="1" />');
			cout('<dl class="dl-horizontal dldata">');
				cout('<dt><label for="kodelang">Language Code</label></dt>');
				cout('<dd><input class="form-control" type="text" name="kodelang" id="kodelang" /></dd>');
				
				cout('<dt><label for="namalang">Language Name</label></dt>');
				cout('<dd><input class="form-control" type="text" name="namalang" id="namalang" /></dd>');
				
				cout('<dt><label for="flag">Language Flag</label></dt>');
				cout('<dd>
					<div class="row">
						<div class="col-xs-1 text-center">
							<span class="flag"></span>
						</div><!--/.col-xs-1-->
						<div class="col-xs-11">
							<select name="flag" id="flag" class="form-control" onchange="selectFlag(\'flag\');">
								<option value="">-</option>
								<option value="id">ID</option>
								<option value="uk">UK</option>
								<option value="cn">CN</option>
								<option value="us">US</option>
								<option value="sg">SG</option>
								<option value="my">MY</option>
							</select>
						</div><!--/.col-xs-11-->
					</div><!--/.row-->
				</dd>');
				
				cout('<dt><label for="isactive">Active Status</label></dt>');
				cout('<dd>
					<select name="isactive" id="isactive" class="form-control">
						<option value="0">Non-Active</option>
						<option value="1">Active</option>
					</select>
				</dd>');
				
				cout('<dt></dt>');
				cout('<dd class="err"></dd>');
				
				cout('<dt></dt>');
				cout('<dd><button class="btn btn-primary" type="submit">Add</button></dd>');
			cout('</dl>');
		cout('</form>');
		$viewFrame = 1;
		if ($viewFrame==1) {
			cout('<iframe name="quick_iframe" id="quick_iframe" src="/'.adminFolder.'/_ajax/index.php" width="300" height="100" style="overflow:;border:1px red solid;" visibility="" scrolling="yes" frameborder="1"></iframe>');
		} else {
			cout('<iframe name="quick_iframe" id="quick_iframe" src="/'.adminFolder.'/_ajax/index.php" width="300" height="11" style="overflow:hidden;" visibility="hidden" scrolling="no" frameborder="0"></iframe>');
		}
		$act_ket = "Languages: Adding...";
		updateUserActivities($act_ket);
		exit;
	}
}
?>