<?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 = "pages";
$pageName = "slides";
$pageTitle = "Slides";
$thisUrl = "slides";
$ajax_page = "ajax_slides.php";
$pageUrl = "/".adminFolder."/$thisUrl";
$a = cekpost(cleanQuery($_GET["a"]), "");
$err = "";
if ($a=="do_edit") {
	$namafile = $_FILES["namafile"]["name"];
	$tmp = $_FILES["namafile"]["tmp_name"];
	$size = $_FILES["namafile"]["size"];
	$type = $_FILES["namafile"]["type"];
	$error = $_FILES["namafile"]["error"];
	$err = '';
	if ($error === UPLOAD_ERR_OK) { 
		//uploading successfully done 
	} else { 
		$err .= BSErrorMessage(UploadException($error)); 
	}
	/*
	if (trim($namafile)=="" && !isset($namafile)) {
		$err.= BSErrorMessage('File Name are unidentified or no file are selected!');
	} else {
		$allowedTypes = array("image/png", "image/bmp", "image/jpeg", "image/jpg", "image/gif");
		$ext = ShowFileExtension($namafile);
		if (!in_array($type, $allowedTypes)) { $err.= BSErrorMessage('Uploaded file type (.'.$ext.') are not allowed!');$err = $err."Uploaded file type (.".$ext.") are not allowed!"; }
	}
	*/
	if ($err=="") {
		$mainDir = "../../files/";
		if (file_exists($mainDir)) {
			$createdDir = $mainDir;
		} else {
			$createdDir = createDirectory2($mainDir);
		}
		if ($createdDir[strlen($createdDir)-1]!="/") $createdDir=$createdDir."/";
		$createdDir = str_replace("//", "/", $createdDir);
		$now = time();
		$kdfile = getID("u_files", "kdfile");
		$newName = $kdfile.$now.".".$ext;
		$path = $createdDir;
		$nnfile = $namafile;
		$flag = 0;
		do {
			$rscek = array(); $qcek = "SELECT original_file_name FROM u_files WHERE original_file_name = '$nnfile'";
			callRS($rscek, $qcek);
			if (count($rscek)!=0) {
				$flag = 0;
				$nnfile = "_".$nnfile;
			} else {
				$flag = 1;
			}
		} while ($flag==0);
		$fullOriName = $path.$nnfile;
		move_uploaded_file($tmp, $fullOriName);
		$fullNewName = $path.$newName;
		smartCopy($fullOriName, $fullNewName);
		$maxWidth = 1000; $maxHeight = 590;
		$maxWidth = 980; $maxHeight = 440;
		$maxWidth = 975; $maxHeight = 455;
		$tmpFileName = $path.$kdfile.$now."_$maxWidth.".$ext;
		smartCopy($fullNewName, $tmpFileName);
		smart_resize_image($tmpFileName, $maxWidth, $maxHeight, true, 'file', true, false);
		$image_title = cekpost(cleanQuery($_POST["image_title"]), "");
		$image_alt = cekpost(cleanQuery($_POST["image_alt"]), "");
		$upby = $_SESSION["user"]["inisial"];
		$uptime = time();
		$kddata = getID("u_files", "kdslide");
		$qi = "INSERT INTO u_files (kdslide, file_type, full_path, original_file_name, current_file_name, file_ext, image_alt, image_title, upby, uptime) 
			VALUES ('$kddata', '$type', '$fullNewName', '$nnfile', '$newName', '$ext', '$image_alt', '$image_title', '$upby', '$uptime')";
		execRS($qi);
		#echo $qi;
		$act_ket = "Slides: Image: [kd: $kddata][nama: ".$rs[0]["nama"]."][file: ".$nnfile."]";
		updateUserActivities($act_ket);
		cout('<script type="text/javascript">parent.window.location="'.$pageUrl.'/";</script>');
	} else {
		cout('<script type="text/javascript">parent.showError(\''.$err.'\');</script>');
	}
	exit;
}
if ($a=="section_main") {
	if (!authorizedModule("about_main", $ses_kduser)) {
		unAuthorizedAccess("about_main");
	} else {
		$rs=array();$q="SELECT * FROM u_files WHERE kdslide!=0";
		callRS($rs, $q);
		
		cout('<form method="post" target="quick_iframe" action="/'.adminFolder.'/_ajax/'.$ajax_page.'?a=do_edit" role="form" enctype="multipart/form-data">');
			cout('<input type="hidden" id="kd" name="kd" value="'.$rs[0]["kdfile"].'" />');
			cout('<input type="hidden" name="kirim" id="kirim" value="1" />');
			cout('<dl class="dl-horizontal dldata">');
				cout('<dt><label for="namafile">File</label></dt>');
				cout('<dd><input class="form-control" type="file" name="namafile" id="namafile" /></dd>');
				if ($tipedata == 0) {
					cout('<dt><label for="image_title">Image Title</label></dt>');
					cout('<dd><input class="form-control" type="text" name="image_title" id="image_title" /></dd>');
					
					cout('<dt><label for="image_alt">Image Alt</label></dt>');
					cout('<dd><input class="form-control" type="text" name="image_alt" id="image_alt" /></dd>');
					$mw = 1000; $mh = 590;
					$mw = 980; $mh = 440;
					$mw = 975; $mh = 455;
					cout('<dt>Dimension</dt>');
					cout('<dd>Minimum of dimension requirement:<br/>Width: <strong>'.$mw.'px</strong> <em>x</em> Height: <strong>'.$mh.'px</strong></dd>');
				}
				cout('<dt></dt>');
				cout('<dd class="err"></dd>');
				
				cout('<dt></dt>');
				cout('<dd><button class="btn btn-primary" type="submit">Upload</button></dd>');
			cout('</dl>');
		cout('</form>');
		cout('<div class="row">');
		for ($i=0;$i<count($rs);$i++) {
			$path = str_replace("../../", "/", $rs[$i]["full_path"]);
			$resized = str_replace(".".$rs[$i]["file_ext"]."", "_975.".$rs[$i]["file_ext"]."", $path);
			$resized = $resized."?".time();
			if ($cnt%3==0) {
				cout('</div><!-- /.row -->');
				cout('<br/>');
				cout('<div class="row">');
			}
			$cnt += 1;
			cout('<div class="col-sm-4" style="margin:0;padding:5px;">
					<div style="height:200px;padding:5px;border:1px #999 solid;background:#f2f2f2;overflow:hidden;">
						<img class="img-responsive" src="'.$resized.'" alt="'.$rs[$i]["image_alt"].'" title="'.$rs[$i]["image_title"].'" />
					</div>
					<div><input class="form-control image_title'.$rs[$i]["kdfile"].'" type="text" id="image_title" placeholder="Image Title" title="Image Title" value="'.$rs[$i]["image_title"].'" /></div>
					<div><input class="form-control image_alt'.$rs[$i]["kdfile"].'" type="text" id="image_alt" placeholder="Image Alt" title="Image Alt" value="'.$rs[$i]["image_alt"].'" /></div>
					<div class="text-right">
						<input class="btn btn-primary" type="button" value="Update" onclick="doAction(\'image\', \'update\', \'slides\', \''.$rs[$i]["kdslide"].'\', \''.$rs[$i]["kdfile"].'\');" />
						<input class="btn btn-danger" type="button" value="Delete" onclick="doAction(\'image\', \'delete\', \'slides\', \''.$rs[$i]["kdslide"].'\', \''.$rs[$i]["kdfile"].'\');" />
					</div>
				</div>');
		}
		cout('</div>');
		$viewFrame = 0;
		if ($viewFrame==1) {
			cout('<iframe name="quick_iframe" id="quick_iframe" src="/'.adminFolder.'/_ajax/index.php" width="600" height="200" 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 = "Slides: Main";
		updateUserActivities($act_ket);
		exit;
	}
}
?>