<?php
session_start();
if (empty($_SESSION['namauser']) AND empty($_SESSION['passuser'])){
	header('location:../../404.php');
}else{
include_once '../../../po-library/po-database.php';
include_once '../../../po-library/po-function.php';

$val = new Povalidasi;
$mod=$_POST[mod];
$act=$_POST[act];

// Delete Pages
if ($mod=='pages' AND $act=='delete'){
	$id = $val->validasi($_POST['id'],'sql');
	$tabledel = new PoTable('pages');
	$currentDel = $tabledel->findBy(id_pages, $id);
	$currentDel = $currentDel->current();
	$picture = $currentDel->picture;
	$currentSearch = $tabledel->numRowBy(picture, $picture);
		$tablePost = new PoTable('post');
		$currentPost = $tablePost->findBy(picture, $picture);
		$currentPost = $currentPost->current();
	if($currentSearch > 1){
		$tabledel->deleteBy('id_pages', $id);
	}else{
		if($currentPost->picture==$picture){
			$tabledel->deleteBy('id_pages', $id);
		}else{
			unlink("../../../po-content/po-upload/$picture");
			unlink("../../../po-content/po-upload/medium-$picture");
			$tabledelpic = new PoTable('media');
			$tabledelpic->deleteBy('file_name', $picture);
			$tabledel->deleteBy('id_pages', $id);
		}
	}
}

// Multi Delete Pages
elseif ($mod=='pages' AND $act=='multidelete'){
	$totaldata = $val->validasi($_POST['totaldata'],'xss');
	if ($totaldata != "0"){
		$itemdel = $_POST['item'];
		$tabledel = new PoTable('pages');
		foreach ($itemdel as $item){
			$id = $val->validasi($item['deldata'],'xss');
			$currentDel = $tabledel->findBy(id_pages, $id);
			$currentDel = $currentDel->current();
			$picture = $currentDel->picture;
			$currentSearch = $tabledel->numRowBy(picture, $picture);
				$tablePost = new PoTable('post');
				$currentPost = $tablePost->findBy(picture, $picture);
				$currentPost = $currentPost->current();
			if($currentSearch > 1){
				$tabledel->deleteBy('id_pages', $id);
			}else{
				if($currentPost->picture==$picture){
					$tabledel->deleteBy('id_pages', $id);
				}else{
					unlink("../../../po-content/po-upload/$picture");
					unlink("../../../po-content/po-upload/medium-$picture");
					$tabledelpic = new PoTable('media');
					$tabledelpic->deleteBy('file_name', $picture);
					$tabledel->deleteBy('id_pages', $id);
				}
			}
		}
		header('location:../../admin.php?mod='.$mod);
	}else{
		header('location:../../404.php');
	}
}

// Input Pages
elseif ($mod=='pages' AND $act=='input'){
$title = $val->validasi($_POST['title'],'xss');
$seotitle = seo_title($title);
$data = $_POST[content];
$data = stripslashes($data);
$eutf = htmlspecialchars($data,ENT_QUOTES);

$extensionList = array("jpg", "jpeg");
$fileName = $_FILES['fupload']['name'];
$tmpName  = $_FILES['fupload']['tmp_name'];
$fileType = $_FILES['fupload']['type'];
$fileSize = $_FILES['fupload']['size'];
$pecah = explode(".", $fileName);
$ekstensi = $pecah[1];
$seotitle = seo_title($title);
$acak = rand(000000,999999);
$nama_file = "-popoji.";
$nama_file_unik = $seotitle.'-'.$acak.$nama_file.$ekstensi;

if(!empty($_POST['picture'])){
	$picture = $_POST['picture'];
	$table = new PoTable('pages');
	$table->save(array(
		'title' => $title,
		'content' => $eutf,
		'seotitle' => $seotitle,
		'picture' => $picture
	));
	header('location:../../admin.php?mod='.$mod);
}else{
if (!empty($tmpName)){
	if (in_array($ekstensi, $extensionList)){
		UploadImage($nama_file_unik);
		$table = new PoTable('pages');
		$table->save(array(
			'title' => $title,
			'content' => $eutf,
			'seotitle' => $seotitle,
			'picture' => $nama_file_unik
		));

		$tablemedia = new PoTable('media');
		$tablemedia->save(array(
			'file_name' => $nama_file_unik,
			'file_type' => $fileType,
			'file_size' => $fileSize,
			'date' => $tgl_sekarang
		));
		header('location:../../admin.php?mod='.$mod);
	}else{
		header('location:../../404.php');
	}
}else{
	$table = new PoTable('pages');
	$table->save(array(
		'title' => $title,
		'content' => $eutf,
		'seotitle' => $seotitle,
	));
	header('location:../../admin.php?mod='.$mod);
}
}
}

// Edit Pages
elseif ($mod=='pages' AND $act=='update'){
$id = $val->validasi($_POST['id'],'sql');
$title = $val->validasi($_POST['title'],'xss');
$seotitle = seo_title($title);
$data = $_POST[content];
$data = stripslashes($data);
$eutf = htmlspecialchars($data,ENT_QUOTES);
$active =  $val->validasi($_POST['active'],'xss');

$extensionList = array("jpg", "jpeg");
$fileName = $_FILES['fupload']['name'];
$tmpName  = $_FILES['fupload']['tmp_name'];
$fileType = $_FILES['fupload']['type'];
$fileSize = $_FILES['fupload']['size'];
$pecah = explode(".", $fileName);
$ekstensi = $pecah[1];
$seotitle = seo_title($title);
$acak = rand(000000,999999);
$nama_file = "-popoji.";
$nama_file_unik = $seotitle.'-'.$acak.$nama_file.$ekstensi;

if(!empty($_POST['picture'])){
	$picture = $_POST['picture'];
	$data = array(
		'title' => $title,
		'content' => $eutf,
		'seotitle' => $seotitle,
		'picture' => $picture,
		'active' => $active
	);
	$table = new PoTable('pages');
	$table->updateBy('id_pages', $id, $data);
	header('location:../../admin.php?mod='.$mod);
}else{
if (!empty($tmpName)){
	if (in_array($ekstensi, $extensionList)){
		UploadImage($nama_file_unik);
		$data = array(
			'title' => $title,
			'content' => $eutf,
			'seotitle' => $seotitle,
			'picture' => $nama_file_unik,
			'active' => $active
		);
		$table = new PoTable('pages');
		$table->updateBy('id_pages', $id, $data);

		$tablemedia = new PoTable('media');
		$tablemedia->save(array(
			'file_name' => $nama_file_unik,
			'file_type' => $fileType,
			'file_size' => $fileSize,
			'date' => $tgl_sekarang
		));
		header('location:../../admin.php?mod='.$mod);
	}else{
		header('location:../../404.php');
	}
}else{
	$data = array(
		'title' => $title,
		'content' => $eutf,
		'seotitle' => $seotitle,
		'active' => $active
	);
	$table = new PoTable('pages');
	$table->updateBy('id_pages', $id, $data);
	header('location:../../admin.php?mod='.$mod);
}
}
}
}
?>