<?php
	define('thisscript','contactmessage');
	
	require_once 'globalad.php';
	require_once '../class/Contact.php';
	$contact = new Contact();
	
	if (empty($userid)){
		redirecting("index.php");
	}
	
	if (empty($useraccess['manage_contact_message'])){
		redirecting('index.php');
	}
	
	/* sanitize post, get, request */
	$_REQUEST['id'] = ((empty($_REQUEST['id']))?"":$_REQUEST['id']);
	$_GET['getlist'] = ((empty($_GET['getlist']))?"":$_GET['getlist']);
	$_GET['keyword'] = ((empty($_GET['keyword']))?array():$_GET['keyword']);
	$_GET['field'] = ((empty($_GET['field']))?array():$_GET['field']);
	$_POST['cek'] = ((empty($_POST['cek']))?array():$_POST['cek']);
	$_POST['does'] = ((empty($_POST['does']))?"":$_POST['does']);
	/* end sanitize */
	
	if (empty($general['showperpageadmin'])){
		$general['showperpageadmin'] = 20;
	}
	
	$pagelink = '';
	$listr = '';
	$listaction = '';
	$options = '';
	$sql = '';
	$sqls = '';
	$type_message = '';
	
	$locationarray = array("0" => "");
	$getmessage = $db->fetch_all("SELECT * FROM messagetype ORDER BY messageid");
	if (sizeof($getmessage) > 0){
		foreach ($getmessage as $gln){
			$options .= '<option value="'.$gln['messageid'].'">'.htmlspecialchars($gln['typemessage_1']).'</option>';
			$locationarray[$gln['messageid']] = htmlspecialchars($gln['typemessage_1']);
		}
	}
	$type_message = '<select name="message_id" id="message_id" onchange="searchnow()" style="width: 90%; font-size: 10px"><option value ="">Semua</option>'.$options.'</select>';
		
	if ($_GET['getlist'] == 'pagenav'){
		$keywords = $_GET['keyword'];
		$fields = $_GET['field'];
		$alls = $contact->searchMessage($keywords,$fields,'','','pagenav',$_GET['p']);
		echo $alls;
		exit;
	}
	else if ($_GET['getlist'] == 'ajax'){
		$keywords = $_GET['keyword'];
		$fields = $_GET['field'];
		$alls = $contact->searchMessage($keywords,$fields,'','','data');
		$totalrows = sizeof($alls);
		$totalpgs = ceil($totalrows / $general['showperpageadmin']);
		$pgs = handlepage($_GET['p'],$totalpgs);
		$dblast = $contact->searchMessage($keywords,$fields,$_GET['sortf'],$_GET['sortd'],'data',$pgs);

		if (sizeof($dblast) > 0){
			foreach ($dblast as $result){
				$regdate = date("d M Y, H:i:s",$result['dateline']);			
				$msgshort = substr($result['message'],0,50);
				if (strlen($result['message']) > 50)
				$msgshort =substr($msgshort,0,strrpos($msgshort," "))."<font color='#FF0000'> <em><b>[more..]</b></em></font>";
				
				$listr .= '
				<!--<div class="hovertr" ondblclick="window.open(\'cmdetail.php?id='.$result['contact_id'].'\',\'_self\')">
					<div align="center">
						<table border="0" width="100%" cellspacing="3" cellpadding="0">-->
							<tr ondblclick="window.open(\'cmdetail.php?id='.$result['contact_id'].'\',\'_self\')">
								<td width="3%" height="25" align="center">
								<input type="checkbox" name="cek[]" value="'.$result['contact_id'].'"></td>
								<td width="12%" height="25" align="center">
								<a href="cmdetail.php?id='.$result['contact_id'].'">'.$regdate.'</a></td>
								<td width="13%" height="25" align="left">
								'.$locationarray[$result['typemessage']].'</td>
								<td width="12%" height="25" align="left">
								'.$result['name'].'</td>
								<td width="16%" height="25" align="left">
								'.$result['email'].'</td>
								<td width="9%" height="25" align="center">
								'.$result['telp'].'</td>
								<td width="20%" height="25" align="left">
								'.$msgshort.'</td>											
							</tr>
							<!--</tr>
						</table>
					</div>
				</div>
			<div style="width: 100%; height: 3px" class="line_h"></div>-->
			';
			}
			/* $listr .= '<div align="right">
						With selected : 
						<select name="does">
						<option value="delete">Delete</option>
						</select>&nbsp;&nbsp;<input type="submit" value="Go" class="button"></div><br>
					  '; */
		}
		else{
			$listr = '<tr><td colspan="7" align="center">NO DATA</td></tr>';
		}
			
		echo $listr;
		exit;
	}
	
	$listaction .= '<div align="right">
						With selected : 
						<select name="does">
						<option value="delete">Delete</option>
						</select>&nbsp;&nbsp;<input type="submit" value="Go" class="btn btn-success btn-sm"></div><br>
					  ';
	
	$menu = $_POST['does'];
	if ($menu == 'delete'){
		$alldep = $_POST['cek'];
		$sizealldep = sizeof($alldep);
		if ($sizealldep > 0){
			for ($ax = 0; $ax < $sizealldep; $ax++){
				$contact->setId($alldep[$ax]);
				$contact->deleteMessage();
			}
		}
	}
		
	require_once 'incl/global_template.php';
	
	$theme = gettemplate('contactmessage');
	eval("\$theme = \"$theme\";");
	echo $theme;
?>