<script type="text/javascript">
$(document).ready( function() {
    $('#alertdel').dialog({
		autoOpen: false,
		width: 400,
		modal: true,
		buttons: {
			"<?php echo $langdelete3; ?>": function() {
				var id = $('#delid').val();
				var mod = 'theme';
				var act =  'delete';
				var dataString = 'id='+ id + '&mod='+ mod + '&act='+ act;
				$.ajax({
					type: "POST",
					url: "po-component/po-theme/proses.php",
					data: dataString,
					cache: false,
					success: function(){
						$('#del'+id).slideUp('slow', function() {$(this).remove();});
				}
				});
				$( this ).dialog( "close" );
			},
			"<?php echo $langdelete4; ?>": function() {
				$( this ).dialog( "close" );
			}
		}
	});
    $('.alertdel').click(function () {
		var id = $(this).attr("id");
        $('#alertdel').dialog('open');
		$('#delid').val(id);
        return false;
    });
});
</script>

<script type="text/javascript" src="js/plugins/ui/jquery-linedtextarea.js"></script>
<script type="text/javascript">
$(document).ready( function() {
	$(".linedtext").linedtextarea();
});
</script>
<?php
session_start();
if (empty($_SESSION['namauser']) AND empty($_SESSION['passuser'])){
	header('location:404.php');
}else{
$aksi="po-component/po-theme/proses.php";
?>
	<div class="contentTop">
        <span class="pageTitle"><span class="icon-screen"></span><?php echo $langtheme1; ?></span>
    </div>
    <!-- Breadcrumbs line -->
    <div class="breadLine">
        <div class="bc">
            <ul id="breadcrumbs" class="breadcrumbs">
                <li><a href="admin.php?mod=home"><?php echo $langmenu1; ?></a></li>
                <li class="current"><a><?php echo $langtheme2; ?></a></li>
            </ul>
        </div>
    </div>
    <!-- Main content -->
    <div class="wrapper">
<?php
switch($_GET[act]){
  default:
	if ($_SESSION[leveluser]=='admin'){
			echo "<div class='fluid'>";
				$tabletheme = new PoTable('theme');
				$themes = $tabletheme->findAll(id_theme, DESC);
				$jml_kolom = 2;
				$cnt = 0;
					foreach($themes as $theme){
						if ($cnt >= $jml_kolom){
							echo "</div><div class='fluid'>";
							$cnt = 0;
						}
						$cnt++;
						echo "<div class='widget grid6' id='del$theme->id_theme'>
							<div class='whead'><h6>$theme->title - $langtheme4 $theme->author</h6>
								<div class='alertdel' id='$theme->id_theme'><a class='buttonH bRed' href='#delete$theme->id_theme'>$langtheme5</a></div>
								<form method='post' action='$aksi' autocomplete='off'>
									<input type='hidden' name='mod' value='theme'>
									<input type='hidden' name='act' value='active'>
									<input type='hidden' name='id' value='$theme->id_theme'>";
										if ($theme->active=='Y'){
											echo "<input type='hidden' name='active' value='N'>
											<button class='buttonH bBlack mb10' type='submit'>$langtheme6</button><div class='clear'></div>";
										}else{
											echo "<input type='hidden' name='active' value='Y'>
											<button class='buttonH bBlue' type='submit'>$langtheme7</button><div class='clear'></div>";
										}
								echo "</form>
							</div>
							<ul class='niceList params'>
								<li>
									<img src='../po-content/$theme->folder/preview.jpg' alt='' style='width:100%;'>
									<div class='clear'></div>
								</li>
							</ul>
                        </div>";
					}
?>
			</div>
	<div id="alertdel" title="<?php echo $langdelete1; ?>" style="display:none;">
		<p><input type='hidden' id='delid' name='delid'><span class='icon-warning'></span> <?php echo $langdelete2; ?></p>
	</div>
<?php
	}else{
?>
    <div class="wrapper" id="errorWrapperadmin">
		<span class="errorNum">404</span>
		<div class="errorContent">
			<span class="errorDesc"><span class="icon-warning"></span>Ooops! Error Somethings Wrong</span>
			<div class="fluid">
				<?php
					$url = rtrim("http://".$_SERVER['HTTP_HOST'], "/").$_SERVER['PHP_SELF'];
					$url2 = preg_replace("/\/po-admin\/(admin\.php$)/","",$url);
					$siteurl = $url2;
				?>
				<a title="Back to Previous page" class="buttonM bLightBlue grid6" onClick="history.back();">Back to previous page</a>
				<a href="<?php echo $siteurl; ?>" title="Back to the website" class="buttonM bRed grid6">Back to the website</a>
			</div>
		</div>
	</div>
<?php
	}
    break;


	case "addnew":
	if ($_SESSION[leveluser]=='admin'){
?>
		<form id="usualValidate" class="main" method="post" action="<?php echo $aksi; ?>" enctype="multipart/form-data" autocomplete="off">
            <fieldset>
				<div class="widget fluid">
				<div class="whead"><h6>Add New</h6><div class="clear"></div></div>
					<input type="hidden" name="mod" value="theme">
					<input type="hidden" name="act" value="input">
					<div class="formRow">
						<div class="grid3"><label>Title</label></div>
						<div class="grid9"><input class="required" type="text" id="title" name="title" required></div>
						<div class="clear"></div>
					</div>
					<div class="formRow">
						<div class="grid3"><label>Author</label></div>
						<div class="grid9"><input class="required" type="text" id="author" name="author" required></div>
						<div class="clear"></div>
					</div>
					<div class="formRow">
						<div class="grid3"><label>Folder</label></div>
						<div class="grid9"><input class="required" type="text" id="folder" name="folder" required></div>
						<div class="clear"></div>
					</div>
					<div class="formRow">
						<div class="grid3"><label>Please select theme <i>(.zip)</i></label></div>
						<div class="grid9"><input style="opacity: 0;" size="24" class="fileInput" id="fileInput" name="fupload" type="file" required></div>
						<div class="clear"></div>
					</div>
					<div class="formRow">
						<input class="buttonM bBlue" type="submit" value="Submit">
						<input class="buttonM bRed formSubmit" type="button" value="Cancel" onclick=self.history.back()><div class="clear"></div>
					</div>
				</div>
            </fieldset>
		</form>
<?php
	}else{
?>
    <div class="wrapper" id="errorWrapperadmin">
		<span class="errorNum">404</span>
		<div class="errorContent">
			<span class="errorDesc"><span class="icon-warning"></span>Ooops! Error Somethings Wrong</span>
			<div class="fluid">
				<?php
					$url = rtrim("http://".$_SERVER['HTTP_HOST'], "/").$_SERVER['PHP_SELF'];
					$url2 = preg_replace("/\/po-admin\/(admin\.php$)/","",$url);
					$siteurl = $url2;
				?>
				<a title="Back to Previous page" class="buttonM bLightBlue grid6" onClick="history.back();">Back to previous page</a>
				<a href="<?php echo $siteurl; ?>" title="Back to the website" class="buttonM bRed grid6">Back to the website</a>
			</div>
		</div>
	</div>
<?php
	}
    break;


	case "edit":
	if ($_SESSION[leveluser]=='admin'){
	$table = new PoTable('theme');
	$currentTheme = $table->findBy(active, 'Y');
	$currentTheme = $currentTheme->current();
	$valid = $val->validasi($_GET['id'],'xss');
	$filename = "../po-content/$currentTheme->folder/$valid";
	if (file_exists("$filename")){
	$fh = fopen($filename, "r") or die("Could not open file!");
	$data = fread($fh, filesize($filename)) or die("Could not read file!");
	$data = str_replace("textarea", "textareapopojicms", $data);
	fclose($fh);
?>
	<div class="widget">
		<div class="whead"><h6><?php echo "$langtheme8 - $currentTheme->title <i>(File $valid)</i>"; ?></h6>
            <div class="titleOpt">
                <a href="#" data-toggle="dropdown"><span class="icos-cog3"></span><span class="clear"></span></a>
                    <ul class="dropdown-menu pull-right">
<?php
					$str = rtrim($_SERVER['DOCUMENT_ROOT'], '/').$_SERVER['PHP_SELF'];
					$strlink = preg_replace("/\/po-admin\/(admin\.php$)/","",$str);
					$folderpath = "$strlink/po-content/$currentTheme->folder/";
						if ($handle = opendir($folderpath)) {
							while (false !== ($file = readdir($handle))) {
								if ($file != "." && $file != "..") {
									$filename = substr($file, 0);
									$pecah = explode(".", $filename);
									$ekstensi = $pecah[1];
									if ($ekstensi!='' AND $ekstensi!='jpg' AND $filename!='index.html'){
										echo "<li><a href='?mod=theme&act=edit&id=$filename'><span class='icos-pencil'></span>$filename</a></li>";
									}else{ }
								}
							}
						}
						closedir($handle);
					echo "</ul>
					</div>
                <div class='clear'></div>
			</div>
				<form method='post' action='$aksi' enctype='multipart/form-data' autocomplete='off'>
					<input type='hidden' name='mod' value='theme'>
					<input type='hidden' name='act' value='edit'>
					<input type='hidden' name='folder' value='$currentTheme->folder'>
					<input type='hidden' name='file' value='$valid'>
					<fieldset>
						<div class='formRow'>
							<textarea name='code_content' class='linedtext' style='height:600px;'>$data</textarea>
						</div>
						<div class='formRow'>
							<input class='buttonM bBlue' type='submit' value='Submit'>
							<input class='buttonM bRed formSubmit' type='button' value='Cancel' onclick=self.history.back()><div class='clear'></div>
						</div>
					</fieldset>
				</form>";
?>
		<div class="clear"></div>
	</div>
<?php
	}else{
?>
    <div class="wrapper" id="errorWrapperadmin">
		<span class="errorNum">404</span>
		<div class="errorContent">
			<span class="errorDesc"><span class="icon-warning"></span>Ooops! Error Somethings Wrong</span>
			<div class="fluid">
				<?php
					$url = rtrim("http://".$_SERVER['HTTP_HOST'], "/").$_SERVER['PHP_SELF'];
					$url2 = preg_replace("/\/po-admin\/(admin\.php$)/","",$url);
					$siteurl = $url2;
				?>
				<a title="Back to Previous page" class="buttonM bLightBlue grid6" onClick="history.back();">Back to previous page</a>
				<a href="<?php echo $siteurl; ?>" title="Back to the website" class="buttonM bRed grid6">Back to the website</a>
			</div>
		</div>
	</div>
<?php
	}
	}else{
?>
    <div class="wrapper" id="errorWrapperadmin">
		<span class="errorNum">404</span>
		<div class="errorContent">
			<span class="errorDesc"><span class="icon-warning"></span>Ooops! Error Somethings Wrong</span>
			<div class="fluid">
				<?php
					$url = rtrim("http://".$_SERVER['HTTP_HOST'], "/").$_SERVER['PHP_SELF'];
					$url2 = preg_replace("/\/po-admin\/(admin\.php$)/","",$url);
					$siteurl = $url2;
				?>
				<a title="Back to Previous page" class="buttonM bLightBlue grid6" onClick="history.back();">Back to previous page</a>
				<a href="<?php echo $siteurl; ?>" title="Back to the website" class="buttonM bRed grid6">Back to the website</a>
			</div>
		</div>
	</div>
<?php
	}
    break;  
}
?>
	</div>
<?php
}
?>