<?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);
		}
	}
}

// Input Pages
elseif ($mod=='pages' AND $act=='input'){
$title_id = $val->validasi($_POST['title_id'],'xss');
$title_en = $val->validasi($_POST['title_en'],'xss');
$seotitle_id = seo_title($title_id);
$seotitle_en = seo_title($title_en);
$dataid = $_POST[content_id];
$eutfid = htmlspecialchars($dataid,ENT_QUOTES);
$dataen = $_POST[content_en];
$eutfen = htmlspecialchars($dataen,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_id);
$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_id' => $title_id,
		'title_en' => $title_en,
		'content_id' => $eutfid,  
		'content_en' => $eutfen,
		'seotitle_id' => $seotitle_id,
		'seotitle_en' => $seotitle_en,
		'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_id' => $title_id,
			'title_en' => $title_en,
			'content_id' => $eutfid,  
			'content_en' => $eutfen,
			'seotitle_id' => $seotitle_id,
			'seotitle_en' => $seotitle_en,
			'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_id' => $title_id,
		'title_en' => $title_en,
		'content_id' => $eutfid,  
		'content_en' => $eutfen,
		'seotitle_id' => $seotitle_id,
		'seotitle_en' => $seotitle_en
	));
	header('location:../../admin.php?mod='.$mod);
}
}
}

// Edit Pages
elseif ($mod=='pages' AND $act=='update'){
$id = $val->validasi($_POST['id'],'sql');
$title_id = $val->validasi($_POST['title_id'],'xss');
$title_en = $val->validasi($_POST['title_en'],'xss');
$seotitle_id = seo_title($title_id);
$seotitle_en = seo_title($title_en);
$dataid = $_POST[content_id];
$eutfid = htmlspecialchars($dataid,ENT_QUOTES);
$dataen = $_POST[content_en];
$eutfen = htmlspecialchars($dataen,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_id);
$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_id' => $title_id,
		'title_en' => $title_en,
		'content_id' => $eutfid,  
		'content_en' => $eutfen,
		'seotitle_id' => $seotitle_id,
		'seotitle_en' => $seotitle_en,
		'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_id' => $title_id,
			'title_en' => $title_en,
			'content_id' => $eutfid,  
			'content_en' => $eutfen,
			'seotitle_id' => $seotitle_id,
			'seotitle_en' => $seotitle_en,
			'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_id' => $title_id,
		'title_en' => $title_en,
		'content_id' => $eutfid,  
		'content_en' => $eutfen,
		'seotitle_id' => $seotitle_id,
		'seotitle_en' => $seotitle_en,
		'active' => $active
	);
	$table = new PoTable('pages');
	$table->updateBy('id_pages', $id, $data);
	header('location:../../admin.php?mod='.$mod);
}
}
}
}
?>