<?php
/**
 * 广告
 *
 * @package user
 * @author 王岩 yandy@yanwee.com
 * @version 1.0
 */

require('path.inc.php');

$user->allow('advertise');
$ads = new Ads($query);

if ($page->action=='edit') {
	$page->action = 'add';
}
if ($page->action=='add') {
	$page->name = 'adsItemEdit'; //页面名字,和文件名相同
	$page->addJs('FormValid.js');
	$id = intval($_GET['id']);
	if($id){
		$dataInfo=$ads->getInfo($id);
		$page->tpl->assign('dataInfo', $dataInfo);
	}

}elseif ($page->action=='save') {
	$_POST['creater'] = $user->getAuthInfo('username');
	try{
		$ads->save($_POST);
		$page->goto('ads.php?place_id='.$_POST['place_id'],'保存成功');
	}catch (Exception $e){
		$page->back($e->getMessage());
	}
	exit;
}elseif ($page->action=='lock') {
	$ids = $_POST['ids'];
	if(!is_array($ids) || empty($ids)){
		$page->back('没有选择条目');
	}
	try{
		//锁定自己的条目
		$ads->changeStatus($ids,1);
		$page->goto('ads.php?place_id='.$_POST['place_id'],'锁定成功');
	}catch (Exception $e){
		$page->back($e->getMessage());
	}
	exit;
}elseif ($page->action=='unlock') {
	$ids = $_POST['ids'];
	if(!is_array($ids) || empty($ids)){
		$page->back('没有选择条目');
	}
	try{
		//解锁自己的条目
		$ads->changeStatus($ids,0);
		$page->goto('ads.php?place_id='.$_POST['place_id'],'解锁成功');
	}catch (Exception $e){
		$page->back($e->getMessage());
	}
	exit;
}elseif ($page->action=='delete') {
	$ids = $_POST['ids'];
	if(!is_array($ids) || empty($ids)){
		$page->back('没有选择删除条目');
	}
	try{
		//删除自己的条目
		$ads->delete($ids);
		$page->goto('ads.php?place_id='.$_POST['place_id'],'删除成功');
	}catch (Exception $e){
		$page->back($e->getMessage());
	}

	exit;
}else{
	$page->name = 'adsItemList'; //页面名字,和文件名相同
	
	$adsPlace = new AdsPlace($query);
	$adsList = $adsPlace->getAll('is_del = 0');
	$adsList = array_to_hashmap($adsList,'id','place_name');
	$page->tpl->assign('adsList', $adsList);
	
	$place_id = intval($_GET['place_id']);
	$page->tpl->assign('place_id', $place_id);
	
	$where = ' status <> 2 and place_id='.$place_id;
	
	require($cfg['path']['lib'] . 'classes/Pages.class.php');
	$pages = new Pages($ads->getCount($where));
	$pageLimit = $pages->getLimit();
	$adsList = $ads->getList($pageLimit,'*',$where,' order by add_time desc ');

	$page->tpl->assign('dataList', $adsList);
	$page->tpl->assign('pagePanel', $pages->showCtrlPanel());//分页条
}
$page->show();
?>