<?php
if(!defined('MOD'))  die("access die");
$action = $_GET['action'] ? $_GET['action'] : 'manage';
switch($action)
{
    case 'manage':
		$data = qdb::fetchAll("select p.*, a.username as p_username from ".TABLEPRE."portal as p left join ".TABLEPRE."admin as a on a.uid = p.p_uid  order by p.pid desc");
		$pid = intval($_GET['pid']);
    break;

    case 'add':
		if($_POST['myFormAc'] == 'update'){
			$param = get_portal_param();
			$sql = qbase::createSql(TABLEPRE . 'portal', $param, 'insert');
			$pid = qdb::query($sql, true);
			update_editor_purview($pid, $_POST['editor']);
			qbase::showmsg("数据块添加成功", "default", "system.php?sys=portal");
		}else{
			$admins = qdb::fetchAll("select uid, username  from ".TABLEPRE."admin order by uid asc");
		}
	break;

    case 'edit':
		$pid = intval($_GET['pid']);
		$item = qdb::queryFirst("select * from ".TABLEPRE."portal where pid = '{$pid}' ");
		if(empty($item) || $item['p_uid'] != $admin_info['uid']) qbase::showmsg("您没有权限修该数据");
		if($_POST['myFormAc'] == 'update'){
			$param = get_portal_param();
			unset($param['p_uid'], $param['p_dateline']);
			$sql = qbase::createSql(TABLEPRE . 'portal', $param, 'update') . " where pid = '{$pid}' ";
			qdb::query($sql);
			update_editor_purview($pid, $_POST['editor']);
			cache::delete("pm_" . $pid);
			qbase::showmsg("数据块修改成功", "default", "system.php?sys=portal");
		}else{
			$admins = qdb::fetchAll("select uid, username  from ".TABLEPRE."admin order by uid asc");
			$editor = qdb::fetchAll("select a.uid, a.username from ".TABLEPRE."portal_purview as p left join ".TABLEPRE."admin as a on p.uid = a.uid  where p.pid = '{$pid}' ");
		}
    break;

	case 'delete':
		checkAdminReferer();
		$pid = intval($_GET['pid']);
		qdb::query("delete from ".TABLEPRE."portal where pid = '{$pid}' ");
		qdb::query("delete from ".TABLEPRE."portal_mapping where pid = '{$pid}' ");
		qdb::query("delete from ".TABLEPRE."portal_purview where pid = '{$pid}' ");
		cache::delete("pm_" . $pid);
		qbase::showmsg("数据块删除成功", "default", "system.php?sys=portal");
	break;

	case 'data':
		$pid = intval($_GET['pid']);
		$ordering = intval($_GET['ordering']);
		$order = $ordering ? " asc " : " desc ";
		$data = qdb::fetchAll("select * from ".TABLEPRE."portal_mapping where pid = '{$pid}' order by id {$order} ");
	break;

	case 'add_item':
		$pid = intval($_GET['pid']);
		$item = qdb::queryFirst("select * from ".TABLEPRE."portal where pid = '{$pid}' ");
		if(empty($item)) qbase::showmsg("出错了，没有找到相关数据");
		if(empty($item['p_status'])) qbase::showmsg("出错了，该数据块已经关闭，不能操作");
		$purview = 0;
		if($item['p_uid'] == $admin_info['uid']) $purview = 1;
		else $purview = qdb::fetchAll("select count(*) as num from ".TABLEPRE."portal_purview where uid = '{$admin_info['uid']}' and pid = '{$pid}' ");
		if(!$purview) qbase::showmsg("您没有权限添加数据");
		if($_POST['myFormAc'] == 'update'){
			$param = array();
			$param['pid'] = $pid;
			$param['subject'] = trim($_POST['subject']);
			$param['url'] = trim($_POST['url']);
			$param['thumb'] = trim($_POST['thumb']);
			$param['ordering'] = intval($_POST['ordering']);
			$param['description'] = trim($_POST['description']);
			$param['target'] = trim($_POST['target']);
			$param['subject_color'] = trim($_POST['subject_color']);
			if(empty($param['subject']) || empty($param['url'])) qbase::showmsg("出错了，标题和链接是必须要的");
			$sql = qbase::createSql(TABLEPRE . 'portal_mapping', $param, 'insert');
			qdb::query($sql);
			cache::delete("pm_" . $pid);
			die(qbase::xmlprint(qbase::jsonencode(array('status'=>200))));
		}
	break;

	case 'edit_item':
		$id = intval($_GET['id']);
		$item = qdb::queryFirst("select * from ".TABLEPRE."portal_mapping where id = '{$id}' ");
		if(empty($item)) qbase::showmsg("出错了，没有找到相关数据");

		$portal = qdb::queryFirst("select * from ".TABLEPRE."portal where pid = '{$item['pid']}' ");
		if(empty($portal)) qbase::showmsg("出错了，没有找到相关数据");
		if(empty($portal['p_status'])) qbase::showmsg("出错了，该数据块已经关闭，不能操作");

		$purview = 0;
		if($portal['p_uid'] == $admin_info['uid']) $purview = 1;
		else $purview = qdb::fetchAll("select count(*) as num from ".TABLEPRE."portal_purview where uid = '{$admin_info['uid']}' and pid = '{$portal['pid']}' ");
		if(!$purview) qbase::showmsg("您没有权限修改数据");
		
		$pid = $portal['pid'];

		if($_POST['myFormAc'] == 'update'){
			$param = array();
			$param['subject'] = trim($_POST['subject']);
			$param['url'] = trim($_POST['url']);
			$param['thumb'] = trim($_POST['thumb']);
			$param['ordering'] = intval($_POST['ordering']);
			$param['description'] = trim($_POST['description']);
			$param['target'] = trim($_POST['target']);
			$param['subject_color'] = trim($_POST['subject_color']);
			if(empty($param['subject']) || empty($param['url'])) qbase::showmsg("出错了，标题和链接是必须要的");
			$sql = qbase::createSql(TABLEPRE . 'portal_mapping', $param, 'update') . " where id = '{$id}'  ";
			qdb::query($sql);
			cache::delete("pm_" . $pid);
			die(qbase::xmlprint(qbase::jsonencode(array('status'=>200))));
		}
	break;

	case 'delete_item':
		checkAdminReferer();
		$id = intval($_GET['id']);
		qdb::query("delete from ".TABLEPRE."portal_mapping where id = '{$id}' ");
		cache::delete("pm_" . $pid);
		die(qbase::xmlprint(qbase::jsonencode(array('status'=>200))));
	break;

	case 'ordering':
		$id = intval($_GET['id']);
		$val = intval($_GET['val']);
		$item = qdb::queryFirst("select * from ".TABLEPRE."portal_mapping where id = '{$id}' ");
		if(!empty($item)){
			qdb::query("update ".TABLEPRE."portal_mapping set ordering = '{$val}' where id = '{$id}' ");
			cache::delete("pm_" . $item['pid']);
		}
	break;

    default:
		qbase::showmsg("bad request");
    break;
}
include qbase::template($path . 'portal');


function get_portal_param(){
	global $admin_info;
	$param = array();
	$param['p_title'] = trim($_POST['p_title']);
	$param['p_type'] = intval($_POST['p_type']);
	$param['p_status'] = intval($_POST['p_status']);
	$param['p_html'] = trim($_POST['p_html']);
	$param['p_dateline'] = time();
	$param['p_uid'] = $admin_info['uid'];
	if(empty($param['p_title'])) qbase::showmsg("需要填写标题说明");
	if($param['p_type'] == 1 && empty($param['p_html']))  qbase::showmsg("使用HTML代码方式，需要填写相应的HTML代码");
	return $param;
}

function update_editor_purview($pid, $editor){
	qdb::query("delete from " . TABLEPRE . "portal_purview where pid = '{$pid}' ");
	if(!is_array($editor) || empty($editor)) return;
	$param = array();
	foreach($editor as $val){
		$uid = intval($val);
		$uid = qdb::queryAlone("select uid from " . TABLEPRE . "admin where uid = '{$uid}' ");
		if($uid > 0) $param[] = " ({$pid}, {$uid}) ";
	}
	$param = array_unique($param);
	if(empty($param)) return;
	$sql = "replace into " . TABLEPRE . "portal_purview (pid, uid) values " . implode(" , ", $param);
	qdb::query($sql);
}
?>
