<?php
!defined("MONKEYMAO") && die("Accessed Defined!");
@include lang('themes','themes/admin');
$locations = array(
	'top' => $lang['allpage'] . $lang['top'],
	'main' => $lang['allpage'] . $lang['main'],
	'right' => $lang['allpage'] . $lang['right'],
	'bottom' => $lang['allpage'] . $lang['bottom'],
	'index_main' => $lang['index'] . $lang['main'],
	'index_right' => $lang['index'] . $lang['right'],
	'index_bottom' => $lang['index'] . $lang['bottom'],
	'list_main' => $lang['sort'] . $lang['main'],
	'list_right' => $lang['sort'] . $lang['right'],
	'list_bottom' => $lang['sort'] . $lang['bottom'],
	'view_main' => $lang['article'] . $lang['main'],
	'view_bottom' => $lang['article'] . $lang['bottom'],
	'default' => $lang['custom'] ,
);
	$enum = array('top', 'main', 'right', 'bottom', 'index_main', 'index_right', 'index_bottom', 'list_main', 'list_right', 'list_bottom', 'view_main', 'view_bottom');
$defaultads = array('top', 'main', 'right', 'bottom');
if($menu == 'post') {
	if(cpchecksubmit('add')) {
		$newtype = in_array($newtype,$enum) && !in_array($newtype,$defaultads) ? $newtype : 'default';
		$startdate = $newstartdate ? strtotime($newstartdate) : 0;
		$endate = $newendate ? strtotime($newendate) : 0;	
		$newadpics =$newadendpics = '';
		$updfiles =  uploadfile('newadpic');
		if($updfiles) {
			$newadpics =$updfiles[0]['attachment'];
		}	
		$updfiles =  uploadfile('newadendpic');
		if($updfiles) {
			$newadendpics =$updfiles[0]['attachment'];
		}
		$db -> query("INSERT INTO {$tablepre}ad (type, subject, startdate, endate, postime, ordermode, width, height, viewmode, viewcode, adtext, adtexturl, adpic, adpicurl, target, adendmode, adendcode, adendtext, adendtexturl, adendpic, adendpicurl, endtarget) VALUES('$newtype', '$newsubject', '$startdate', '$endate', '$time', '$newordermode', '$newwidth', '$newheight', '$newviewmode', '$newviewcode', '$newadtext', '$newadtexturl', '$newadpics', '$newadpicurl', '$newtarget', '$newadendmode', '$newadendcode', '$newadendtext', '$newadendtexturl', '$newadendpics', '$newadendpicurl', '$newendtarget')");
		cpmsg('succeeded', 'index.php?action=ad');
	}else{
	
		include theme('ad_post', 'themes/admin','admin');
	}

}elseif(cpchecksubmit('updatelist')){
	if(is_array($ordermodenew)) {
		foreach($ordermodenew AS $key => $order) {
			$db -> query("UPDATE {$tablepre}ad SET ordermode='$ordermodenew[$key]' WHERE  aid='$key'");
		}		
	}
	cpmsg('succeeded', 'index.php?action=ad');

}elseif($menu == 'request'){
	if($delone) {
		$db -> query("DELETE FROM {$tablepre}adrequest WHERE  rid='$delone'");
		cpmsg('succeeded', 'index.php?action=ad&menu=request');
	}elseif($view){
		$ad = $db -> queryarray("SELECT r.*,a.subject AS adtitle FROM {$tablepre}adrequest r LEFT JOIN {$tablepre}ad a ON a.aid=r.aid WHERE r.rid='$view'");
		$ad['msg'] = nl2br($ad['msg']);
			$ad['postime'] = gmdate("$dateformat $timeformat", $ad['postime'] + ($timeoffset * 3600));
		include theme('adrequest_view', 'themes/admin','admin');	
	}else{
		$ads=array();
		$page = ispage($page) ? $page : 1;
		$pagenum = 20;
		$start_limit = ($page-1) * $pagenum;	
		$pics = array();
		$query = $db -> query("SELECT COUNT(*) FROM {$tablepre}adrequest");
		$lines = $db -> result($query, 0);
		$paging = paging($lines, $pagenum, $page, 'index.php?action=ad');
		$query = $db -> query("SELECT r.*,a.subject AS adtitle FROM {$tablepre}adrequest r LEFT JOIN {$tablepre}ad a ON a.aid=r.aid ORDER BY r.postime DESC LIMIT $start_limit, $pagenum");	
		while($ad = $db -> fetch_array($query)){
			$ad['postime'] = gmdate("$dateformat $timeformat", $ad['postime'] + ($timeoffset * 3600));
			$ad['location'] = $locations[$ad['type']];
			$ad['subject'] = addslashes($ad['subject']);
			$ad['adtitle'] = addslashes($ad['adtitle']);

			$ads[$ad['rid']] = $ad;
		}
		include theme('adrequest', 'themes/admin','admin');	
	}
}elseif($edit){
	$ad = $db -> queryarray("SELECT * FROM {$tablepre}ad WHERE aid='$edit'");
	if(!$ad) {
		cpmsg('error');
	}
	if(cpchecksubmit('update')) {
		$newtype = in_array($ad['type'],$defaultads) ? $ad['type'] : (in_array($newtype,$enum) ? $newtype : 'default');
		$startdate = $newstartdate ? strtotime($newstartdate) : 0;
		$endate = $newendate ? strtotime($newendate) : 0;	

		if($deladpic) {
			if($ad['adpic'] && @file_exists(MODEPATH.$attachdir.'/'.$ad['adpic'])) {
				@unlink(MODEPATH.$attachdir.'/'.$ad['adpic']);
			}
			$newadpics = '';
		}else{
			$newadpics =  $ad['adpic'];
		}	

		$updfiles =  uploadfile('newadpic');
		if($updfiles) {
			$newadpics =$updfiles[0]['attachment'];
			if($ad['adpic'] && @file_exists(MODEPATH.$attachdir.'/'.$ad['adpic'])) {
				@unlink(MODEPATH.$attachdir.'/'.$ad['adpic']);
			}

		}	

		if($deladendpic) {
			if($ad['adendpic'] && @file_exists(MODEPATH.$attachdir.'/'.$ad['adendpic'])) {
				@unlink(MODEPATH.$attachdir.'/'.$ad['adendpic']);
			}
			$newadendpics = '';
		}else{
			$newadendpics =  $ad['adendpic'];
		}
		$updfiles =  uploadfile('newadendpic');
		if($updfiles) {
			$newadendpics =$updfiles[0]['attachment'];
			if($ad['adendpic'] && @file_exists(MODEPATH.$attachdir.'/'.$ad['adendpic'])) {
				@unlink(MODEPATH.$attachdir.'/'.$ad['adendpic']);
			}
		}
		$db -> query("UPDATE {$tablepre}ad SET type='$newtype', subject='$newsubject', startdate='$startdate', endate='$endate', width='$newwidth', height='$newheight', viewmode='$newviewmode', viewcode='$newviewcode', adtext='$newadtext', adtexturl='$newadtexturl', adpic='$newadpics', adpicurl='$newadpicurl', target='$newtarget', adendmode='$newadendmode', adendcode='$newadendcode', adendtext='$newadendtext', adendtexturl='$newadendtexturl', adendpic='$newadendpics', adendpicurl='$newadendpicurl', endtarget='$newendtarget' WHERE  aid='$edit'");
		cpmsg('succeeded', 'index.php?action=ad');
	}else{
		${'viewmodecheck'.$ad['viewmode']} = 'checked';
		${'adendmodecheck'.$ad['adendmode']} = 'checked';
		$ad['endate'] = $ad['endate'] ? gmdate('Y-m-d',$ad['endate']):'';
		$ad['startdate'] = $ad['startdate'] ? gmdate('Y-m-d',$ad['startdate']):'';
		$ad['adpicext'] = $ad['adpic'] ? fileext($ad['adpic']) : '';
		$ad['adendpicext'] = $ad['adendpic'] ? fileext($ad['adendpic']) : '';
		//$ad['viewcode'] =htmlconvert($ad['viewcode']);
		include theme('ad_edit', 'themes/admin','admin');
	}
}elseif($del){
	$ad = $db -> queryarray("SELECT * FROM {$tablepre}ad WHERE aid='$del'");
	if(in_array($ad['type'], $defaultads)) {
		cpmsg('error');
	}
	if($ad['adpic'] && @file_exists(MODEPATH.$attachdir.'/'.$ad['adpic'])) {
		@unlink(MODEPATH.$attachdir.'/'.$ad['adpic']);
	}
	if($ad['adendpic'] && @file_exists(MODEPATH.$attachdir.'/'.$ad['adendpic'])) {
		@unlink(MODEPATH.$attachdir.'/'.$ad['adendpic']);
	}
	$db -> query("DELETE FROM {$tablepre}ad WHERE aid='$del'");
	cpmsg('succeeded','index.php?action=ad');
}else{
	$ads=array();
	$page = ispage($page) ? $page : 1;
	$pagenum = 20;
	$start_limit = ($page-1) * $pagenum;	
	$pics = array();
	$query = $db -> query("SELECT COUNT(*) FROM {$tablepre}ad");
	$lines = $db -> result($query, 0);
	$paging = paging($lines, $pagenum, $page, 'index.php?action=ad');
	$query = $db -> query("SELECT * FROM {$tablepre}ad ORDER BY ordermode LIMIT $start_limit, $pagenum");	
	while($ad = $db -> fetch_array($query)){
		$ad['postime'] = gmdate("$dateformat $timeformat", $ad['postime'] + ($timeoffset * 3600));
		$ad['location'] = $locations[$ad['type']];
		$ad['tags'] = in_array($ad['type'], $enum) ? '{ad '.$ad['type'].'}' : '{ad '.$ad['aid'].'}';

		$ads[$ad['aid']] = $ad;
	}
	include theme('ad', 'themes/admin','admin');
}
?>