<?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 Gallery
if ($mod=='gallery' AND $act=='deletegallery'){
	$id = $val->validasi($_POST['id'],'sql');
	$tabledel = new PoTable('gallery');
	$currentDel = $tabledel->findBy(id_gallery, $id);
	$currentDel = $currentDel->current();
	$picture = $currentDel->picture;
	$currentSearch = $tabledel->numRowBy(picture, $picture);
	if($currentSearch > 1){
		$tabledel->deleteBy('id_gallery', $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_gallery', $id);
	}
}

// Delete Album
if ($mod=='gallery' AND $act=='deletealbum'){
	$id = $val->validasi($_POST['id'],'sql');
	$tabledel = new PoTable('album');
	$tabledel->deleteBy('id_album', $id);
}

// Input Gallery
elseif ($mod=='gallery' AND $act=='inputgallery'){
$id_album = $val->validasi($_POST['id_album'],'xss');
$title_id = $val->validasi($_POST['title_id'],'xss');
$title_en = $val->validasi($_POST['title_en'],'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 = "-pogallery.";
$nama_file_unik = $seotitle.'-'.$acak.$nama_file.$ekstensi;

if(!empty($_POST['picture'])){
	$picture = $_POST['picture'];
	$table = new PoTable('gallery');
	$table->save(array(
		'id_album' => $id_album,
		'title_id' => $title_id,
		'title_en' => $title_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('gallery');
		$table->save(array(
			'id_album' => $id_album,
			'title_id' => $title_id,
			'title_en' => $title_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('gallery');
	$table->save(array(
		'id_album' => $id_album,
		'title_id' => $title_id,
		'title_en' => $title_en
	));
	header('location:../../admin.php?mod='.$mod);
}
}
}

// Input Album
elseif ($mod=='gallery' AND $act=='inputalbum'){
$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);

	$table = new PoTable('album');
	$table->save(array(
		'title_id' => $title_id,
		'title_en' => $title_en,
		'seotitle_id' => $seotitle_id,
		'seotitle_en' => $seotitle_en
	));
	header('location:../../admin.php?mod='.$mod.'&act=addnew');
}

// Update Gallery
elseif ($mod=='gallery' AND $act=='editgallery'){
$id = $val->validasi($_POST['id'],'sql');
$id_album = $val->validasi($_POST['id_album'],'sql');
$title_id = $val->validasi($_POST['title_id'],'xss');
$title_en = $val->validasi($_POST['title_en'],'xss');

 	$data = array(
		'id_album' => $id_album,
		'title_id' => $title_id,
		'title_en' => $title_en
	);
	$table = new PoTable('gallery');
	$table->updateBy('id_gallery', $id, $data);
	header('location:../../admin.php?mod='.$mod);
}

// Update Album
elseif ($mod=='gallery' AND $act=='editalbum'){
$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);
$activealb = $val->validasi($_POST['activealb'],'xss');

 	$data = array(
		'title_id' => $title_id,
		'title_en' => $title_en,
		'seotitle_id' => $seotitle_id,
		'seotitle_en' => $seotitle_en,
		'active' => $activealb
	);
	$table = new PoTable('album');
	$table->updateBy('id_album', $id, $data);
	header('location:../../admin.php?mod='.$mod.'&act=album');
}
}
?>