<?php
class qmecmsSpecial{
	var $channelid = 0;
	var $param = array();
	var $extend = array();
	
	function qmecmsSpecial($channelid){
		$this->channelid = $channelid;
	}
	
	function _request(){
		global $_MODULE;
		$this->param['subject'] = trim( $_POST['subject'] );
		$this->param['showphp'] = trim( $_POST['showphp'] );
		$this->param['description'] = trim( $_POST['description'] );
		$this->param['filename'] = trim( $_POST['filename'] );
		$this->param['thumb'] = trim( $_POST['thumb'] );
		$this->param['status'] = intval($_POST['status']);
		$this->param['mid'] = intval($_POST['mid']);
		$this->param['channelid'] = intval($_GET['channelid']);
		if(empty($this->param['subject'])) qbase::showmsg("专题名称不能为空");
		if(empty($_MODULE[$this->param['mid']])) qbase::showmsg("请选择所属内容模型");
		if($this->param['mid'] != 5 && $_MODULE[$this->param['mid']]['pid'] != 5) qbase::showmsg("错误的请求数据");
	}
	
	function add(){
		$this->_request();
		$sql = qbase::createSql("".TABLEPRE."special", $this->param, 'insert');
		$sid = qdb::query($sql, true);
		$this->extend_insert($sid, $this->param['mid']);
	}
	
	function modify(){
		$this->_request();
		$sid = intval($_POST['sid']);
		$item = qdb::queryFirst("select * from ".TABLEPRE."special where sid = '{$sid}' ");
		if(empty($item) || $item['mid'] != $this->param['mid'] || $item['channelid'] != $this->param['channelid']) return;	
		unset($this->param['mid'], $this->param['channelid']);
		$sql = qbase::createSql("".TABLEPRE."special", $this->param, 'update') . " where sid = '{$sid}'  ";
		qdb::query($sql);	
		$this->extend_update($sid, $item['mid']);
	}	
	
	function getModule(){
		return qdb::fetchAll("select id, name from ".TABLEPRE."module where id = 5 or pid = 5");
	}
	
	function insert(){
		$this->_request();
		$this->param['addtime'] = $_SERVER['REQUEST_TIME'];
		$sql = qbase::createSql(TABLEPRE.'special', $this->param, 'insert');
		qdb::Query($sql);
	}
	
	function update($id){
		$this->_request();
		$sql = qbase::createSql(TABLEPRE.'special', $this->param, 'update') . " where specialid = '{$specialid}' ";
		qdb::Query($sql);
	}
	
	function extend_insert($id, $mid)
	{
		$this->get_extend($mid);
		if(empty($this->extend)) return false;
		$this->extend['channelid'] = 0;
		$this->extend['id'] = $id;
		qdb::query(qbase::createSql("".TABLEPRE."extend_data_{$mid}", $this->extend , 'replace'));
	}

	function extend_update($id, $mid)
	{
		$this->get_extend($mid);
		if(empty($this->extend)) return false;
		$table = "".TABLEPRE."extend_data_{$mid}";
		$where = " where channelid = 0 and id = '{$id}' ";
		$num = qdb::queryAlone("select count(*) as num from {$table} {$where} ");
		if($num > 0){
			qdb::query(qbase::createSql($table, $this->extend , 'update') . $where);
		}else{
			$this->extend_insert($id, $mid);
		}
	}
	
	function get_extend($mid)
	{
		$extend = array();
		$exts = array();
		$temp = $this->getExtend($mid);
		foreach($temp as $val){
			$extend[$val['ext']] = $val['eid'];
		}
		$exts = array_keys($extend);
		$data = $_POST['extend'];
		$param = array();
		if(!is_array($data) || empty($data)) return array();
		foreach($data as $key=>$val){
			if(!in_array($key, $exts)) continue;
			$value = is_array($val) ? implode(',', $val) : trim($val); 
			$this->extend['ext_' . $key] = $value;
		}
	}	

	function getExtend($mid)
	{
		return qdb::fetchAll("select * from ".TABLEPRE."extend where id = '{$mid}' order by ordering desc");
	}	

	function getExtendData($channelid, $id)
	{
		$data = qdb::queryFirst("select * from ".TABLEPRE."extend_data_{$this->channelselect} where channelid = '{$channelid}' and id = '{$id}' ");
		unset($data['channelid'], $data['id']);
		return empty($data) ? array() : $data;
	}
}
?>