<?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 Post
if ($mod=='post' AND $act=='delete'){
	$id = $val->validasi($_POST['id'],'sql');
	$tabledel = new PoTable('post');
	$currentDel = $tabledel->findBy(id_post, $id);
	$currentDel = $currentDel->current();
	$picture = $currentDel->picture;
	$currentSearch = $tabledel->numRowBy(picture, $picture);
		$tablePag = new PoTable('pages');
		$currentPag = $tablePag->findBy(picture, $picture);
		$currentPag = $currentPag->current();
	if($currentSearch > 1){
		$tabledel->deleteBy('id_post', $id);
	}else{
		if($currentPag->picture==$picture){
			$tabledel->deleteBy('id_post', $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_post', $id);
		}
	}
}

// Input Post
elseif ($mod=='post' AND $act=='input'){
$id_category = $val->validasi($_POST['id_category'],'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);
if (!empty($_POST['tag'])){
   $tag_seo = $_POST['tag'];
   $tag=implode(',',$tag_seo);
}

$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('post');
	$table->save(array(
		'id_category' => $id_category,
		'title_id' => $title_id,
		'title_en' => $title_en,
		'content_id' => $eutfid,  
		'content_en' => $eutfen,
		'seotitle_id' => $seotitle_id,
		'seotitle_en' => $seotitle_en,
		'tag' => $tag,
		'date' => $tgl_sekarang,
		'editor' => $_SESSION['namauser'],
		'picture' => $picture
	));
	$jml=count($tag_seo);
	for($i=0;$i<$jml;$i++){
		$datatag = array(
			'count' => count+1
		);
		$tabletag = new PoTable('tag');
		$tabletag->updateBy('tag_seo', $tag_seo[$i], $datatag);
	}
	header('location:../../admin.php?mod='.$mod);
}else{
if (!empty($tmpName)){
	if (in_array($ekstensi, $extensionList)){
		UploadImage($nama_file_unik);
		$table = new PoTable('post');
		$table->save(array(
			'id_category' => $id_category,
			'title_id' => $title_id,
			'title_en' => $title_en,
			'content_id' => $eutfid,  
			'content_en' => $eutfen,
			'seotitle_id' => $seotitle_id,
			'seotitle_en' => $seotitle_en,
			'tag' => $tag,
			'date' => $tgl_sekarang,
			'editor' => $_SESSION['namauser'],
			'picture' => $nama_file_unik
		));
		$jml=count($tag_seo);
		for($i=0;$i<$jml;$i++){
			$datatag = array(
				'count' => count+1
			);
			$tabletag = new PoTable('tag');
			$tabletag->updateBy('tag_seo', $tag_seo[$i], $datatag);
		}
		$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('post');
	$table->save(array(
		'id_category' => $id_category,
		'title_id' => $title_id,
		'title_en' => $title_en,
		'content_id' => $eutfid,  
		'content_en' => $eutfen,
		'seotitle_id' => $seotitle_id,
		'seotitle_en' => $seotitle_en,
		'tag' => $tag,
		'date' => $tgl_sekarang,
		'editor' => $_SESSION['namauser']
	));
	$jml=count($tag_seo);
	for($i=0;$i<$jml;$i++){
		$datatag = array(
			'count' => count+1
		);
		$tabletag = new PoTable('tag');
		$tabletag->updateBy('tag_seo', $tag_seo[$i], $datatag);
	}
	header('location:../../admin.php?mod='.$mod);
}
}
}

// Edit Post
elseif ($mod=='post' AND $act=='update'){
$id_category = $val->validasi($_POST['id_category'],'sql');
$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');
if (!empty($_POST['tag'])){
   $tag_seo = $_POST['tag'];
   $tag=implode(',',$tag_seo);
}

$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(
		'id_category' => $id_category,
		'title_id' => $title_id,
		'title_en' => $title_en,
		'content_id' => $eutfid,  
		'content_en' => $eutfen,
		'seotitle_id' => $seotitle_id,
		'seotitle_en' => $seotitle_en,
		'tag' => $tag,
		'picture' => $picture,
		'active' => $active
	);
	$table = new PoTable('post');
	$table->updateBy('id_post', $id, $data);
	$jml=count($tag_seo);
	for($i=0;$i<$jml;$i++){
		$datatag = array(
			'count' => count+1
		);
		$tabletag = new PoTable('tag');
		$tabletag->updateBy('tag_seo', $tag_seo[$i], $datatag);
	}
	header('location:../../admin.php?mod='.$mod);
}else{
if (!empty($tmpName)){
	if (in_array($ekstensi, $extensionList)){
		UploadImage($nama_file_unik);
		$data = array(
			'id_category' => $id_category,
			'title_id' => $title_id,
			'title_en' => $title_en,
			'content_id' => $eutfid,  
			'content_en' => $eutfen,
			'seotitle_id' => $seotitle_id,
			'seotitle_en' => $seotitle_en,
			'tag' => $tag,
			'picture' => $nama_file_unik,
			'active' => $active
		);
		$table = new PoTable('post');
		$table->updateBy('id_post', $id, $data);
		$jml=count($tag_seo);
		for($i=0;$i<$jml;$i++){
			$datatag = array(
				'count' => count+1
			);
			$tabletag = new PoTable('tag');
			$tabletag->updateBy('tag_seo', $tag_seo[$i], $datatag);
		}
		$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(
		'id_category' => $id_category,
		'title_id' => $title_id,
		'title_en' => $title_en,
		'content_id' => $eutfid,  
		'content_en' => $eutfen,
		'seotitle_id' => $seotitle_id,
		'seotitle_en' => $seotitle_en,
		'tag' => $tag,
		'active' => $active
	);
	$table = new PoTable('post');
	$table->updateBy('id_post', $id, $data);
	$jml=count($tag_seo);
	for($i=0;$i<$jml;$i++){
		$datatag = array(
			'count' => count+1
		);
		$tabletag = new PoTable('tag');
		$tabletag->updateBy('tag_seo', $tag_seo[$i], $datatag);
	}
	header('location:../../admin.php?mod='.$mod);
}
}
}

// Subscribe Post
elseif ($mod=='post' AND $act=='subscribe'){
$idsub = $_POST['idsub'];
$titlesub = $_POST['titlesub'];
$seotitlesub = $_POST['seotitlesub'];
	$tableset = new PoTable('setting');
	$currentSet = $tableset->findBy(id_setting, '1');
	$currentSet = $currentSet->current();
	$website_name = $currentSet->website_name;
	$website_url = $currentSet->website_url;
	$website_email = $currentSet->website_email;
		$tablesubs = new PoTable('subscribe');
		$subs = $tablesubs->findAll(id_subscribe, ASC);
		foreach($subs as $sub){
		$emailto = $sub->email;
			$to = "$emailto";
			$from = "$website_name <$website_email>";
			$subject = "Post Terbaru - $titlesub";
			$message = "<html>
				<body>
					Hi $sub->email<br />
					Kami memiliki update terbaru kepada Anda<br />
					Silahkan klik link di bawah ini untuk mulai membacanya :<br />
					<a href='$website_url/detailpost-$idsub-$seotitlesub-lang-id.html'>$titlesub</a><br /><br />
					Terima kasih telah berlangganan,<br />
					$website_name
				</body>
			</html>"; 
			$headers  = "MIME-Version: 1.0" . "\r\n";
			$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
			$headers .= "From: " . $from . "\r\n";
	
			mail($to, $subject, $message, $headers);
		}
	header('location:../../admin.php?mod='.$mod);
}
}
?>