<?php
if (!defined('R_P')) exit("access Die");
require_once(R_P . "/include/class.qmecms.php");
class qmecmsPic extends qmecms {
	var $cate = array();

	function qmecmsPic() {
		$this -> qmecms();
		$this->mod = 'pic';
	} 

	function param() {
		parent :: param();
		$this -> param['showtype'] = intval($_POST['showtype']);
		$this -> cate = qdb::queryFirst("select child, pur_browse from ".TABLEPRE."acategory where cid='{$this->param['cid']}'  limit 1");
		if (empty($this -> cate)) _output_js("出错了，当前所属栏目不存在");
		if ($this -> cate['child'] > 0) _output_js("此栏目有子栏目，不能添加内容");
		if (strlen($this -> param['subject']) < 4) _output_js($this -> channeltitle . "名称不能为空");
		if (strlen($this -> param['thumb']) < 3) _output_js("请选择一张图片作为封面");
		$this -> param['description'] = qbase::FilterString(trim($_POST['message']));
		$search = array ("'<script[^>]*?>.*?</script>'si" , "'<iframe[^>]*?>.*?</iframe>'si");
		$this -> param['description'] = preg_replace($search, '', $this -> param['description']);
		if (empty($_POST['pics'])) _output_js("该图集怎么能够没有图呢?");
		$this -> param['page'] = $this -> param['showtype'] == 0 ? count($_POST['pics']) : 1 ;
		$temp =  keyword($this -> param['description'], $this->param['subject']);
		$keyword = array();
		foreach($temp as $val){
			if(trim($val['name']) && $val['rank'] > 10) $keyword[] = trim($val['name']);
		}
		$this->param['keyword'] = implode(" ", $keyword);
	} 

	function addAction() {
		$this -> param();
		$sql = qbase::createSql($this -> table, $this -> param, 'insert');
		$id = qdb::query($sql, true);

		foreach($_POST['pics'] as $ordering => $val) {
			$temp = explode(',', $val);
			$temp[0] = $this->get_file_path($temp[0]);
			$ext = qbase::fileext($temp[0]);
			$temp[0] = substr($temp[0], 0, strrpos($temp[0], '.'));
			$is_thumb = $this -> param['thumb'] == $temp[0] .'.'. $ext ? 1 : 0;
			$sql = "insert into ".TABLEPRE."pics_{$this->channelid} (id, ordering, url, ext, is_thumb) values ('{$id}','{$ordering}','{$temp[0]}','{$ext}', '{$is_thumb}')";
			qdb::Query($sql);
		} 
		$this -> extend_insert($id);
		$this->related($id, 0, 'add');		
		$this -> add($id);
		$this->moveToRecycle($id, $this->param_recycle);
		qbase::get_object('event', 'pic', array('channelid'=>$this->channelid, 'id'=>$id, 'action'=>'add', 'data'=>$this->param), true);
	} 

	function editAction() {
		$id = intval($_POST['id']);
		$this -> param();
		if( $this -> param['showtype'] == 0){
			$this -> param['page'] = qdb::queryAlone("select count(*) as num from ".TABLEPRE."pics_{$this->channelid} where id = '{$id}' ");
		}else{
			 $this -> param['page'] ==  1;
		}
		$this -> extend_update($id);
		$this->related($id, 0, 'modify');
		$this -> modify($id);		
		qbase::get_object('event', 'pic', array('channelid'=>$this->channelid, 'id'=>$id, 'action'=>'modify', 'data'=>$this->param), true);
	} 

	function ordering($id) {
		$data = qdb::fetchAll("select * from ".TABLEPRE."pics_{$this->channelid} where id = '{$id}' order by ordering asc");
		foreach($data as $ordering => $val) {
			qdb::query("update ".TABLEPRE."pics_{$this->channelid}  set ordering = '{$ordering}' where pid = '{$val['pid']}' ");
		}
		$showtype = qdb::queryAlone("select showtype from ".TABLEPRE."pic_{$this->channelid} where id = '{$id}' ");
		if($showtype == 0){
			$num = count($data);
			qdb::query("update ".TABLEPRE."pic_{$this->channelid}  set page = $num where id = '{$id}' ");
		}else{
			qdb::query("update ".TABLEPRE."pic_{$this->channelid}  set page = 1 where id = '{$id}' ");
		}
	} 

	function del($id){
		$data = parent::del($id);
		qdb::Query("delete from ".TABLEPRE."pics_{$this->channelid}  where id='$id'" );
		qbase::get_object('event', 'pic', array('channelid'=>$this->channelid, 'id'=>$id, 'action'=>'delete', 'data'=>$data), true);
		return $data;
	}

	function del_item($pid){
		$data = parent::del_item($pid);
		if(!empty($data) && !$data['is_thumb']){
			$this->unlinkpic($data['id'], $data['url'], $data['ext']);
			$this->ordering($data['id']);
			$this->update($data['id'], 'edit_time', $_SERVER['REQUEST_TIME']);
		}
		return $data;
	}

	function ajax_add($id, $url){
		$ordering = qdb::queryAlone("select max(ordering) from ".TABLEPRE."pics_{$this->channelid} where id = '{$id}' ");
		$ordering = intval($ordering);
		$ordering++;
		$url = $this->get_file_path($url);
		$ext = qbase::fileext($url);
		$url = substr($url, 0, strrpos($url, '.'));
		$sql = "insert into ".TABLEPRE."pics_{$this->channelid} (id, ordering, url, ext) values ('{$id}','{$ordering}','{$url}','{$ext}')";
		$pid = qdb::query($sql, true);
		$this->update($id, 'edit_time', $_SERVER['REQUEST_TIME']);
		$this->ordering($id);
		return $pid;
	}

	function unlinkpic($id, $url, $ext) {
		@unlink(R_P . '/' . $url . "." . $ext);
		$file = C_P . "/config/pic_mod_set.php";
		$pic_mod_set = array();
		@Include_once($file);
		if(is_array($pic_mod_set) && is_array($pic_mod_set[$this->channelid])) foreach($pic_mod_set[$this->channelid] as $val){
			@unlink(R_P . '/' . $url . $val['name'] . "." . $ext);
		}
		clearstatcache();
		return true;
	}
	
	function get_file_path($url){
		$len = strlen(W_P);
		return substr($url, $len);
	}
} 

?>