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

// Multi Delete Gallery
elseif ($mod=='gallery' AND $act=='multidelete'){
	$totaldata = $val->validasi($_POST['totaldata'],'xss');
	if ($totaldata != "0"){
		$itemdel = $_POST['item'];
		$tabledel = new PoTable('gallery');
		foreach ($itemdel as $item){
			$id = $val->validasi($item['deldata'],'xss');
			$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);
			}
		}
		header('location:../../admin.php?mod='.$mod);
	}else{
		header('location:../../404.php');
	}
}

// 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 = $val->validasi($_POST['title'],'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 = "-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' => $title,
		'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' => $title,
			'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' => $title
	));
	header('location:../../admin.php?mod='.$mod);
}
}
}

// Input Album
elseif ($mod=='gallery' AND $act=='inputalbum'){
$id = $val->validasi($_POST['id'],'sql');
$title = $val->validasi($_POST['title'],'xss');
$addalb = $val->validasi($_POST['addalb'],'xss');
$seotitle = seo_title($title);

	$table = new PoTable('album');
	$table->save(array(
		'title' => $title,
		'seotitle' => $seotitle
	));
	if ($id == ''){
		header('location:../../admin.php?mod='.$mod.'&act='.$addalb);
	}else{
		header('location:../../admin.php?mod='.$mod.'&act='.$addalb.'&id='.$id);
	}
}

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

 	$data = array(
		'id_album' => $id_album,
		'title' => $title
	);
	$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 = $val->validasi($_POST['title'],'xss');
$seotitle = seo_title($title);

 	$data = array(
		'title' => $title,
		'seotitle' => $seotitle
	);
	$table = new PoTable('album');
	$table->updateBy('id_album', $id, $data);
	header('location:../../admin.php?mod='.$mod.'&act=album');
}

// Active Album
elseif ($mod=='gallery' AND $act=='activealbum'){
$id = $val->validasi($_POST['id'],'sql');
$active =  $val->validasi($_POST['active'],'xss');
	$data = array(
		'active' => $active
	);
	$table = new PoTable('album');
	$table->updateBy('id_album', $id, $data);
	echo "$active";
}
}
?>