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

	function qmecmsDownload() {
		$this -> qmecms();
		$this->mod = 'download';
	} 

	function param() {
		parent :: param();
		if ($this -> param['cid'] < 1) _output_js("请选择栏目");
		$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("此栏目有子栏目，不能添加内容");

		$this -> param['size'] = intval($_POST['size']);
		if (strlen($this -> param['subject']) < 4) _output_js($this -> channeltitle . "名称不能为空");

		$systems = $_POST['systems'];
		if (is_array($systems)) $this -> param['systems'] = implode(",", $systems);

		$this -> param['type'] = intval($_POST['type']);
		$this -> param['softtype'] = trim($_POST['softtype']);
		$this -> param['language'] = trim($_POST['language']);
		$this -> param['use_type'] = trim($_POST['use_type']);
		$this -> param['plugin'] = trim($_POST['plugin']);

		$this -> param['description'] = qbase::FilterString(trim($_POST['content']));
		$search = array ("'<script[^>]*?>.*?</script>'si" , "'<iframe[^>]*?>.*?</iframe>'si");
		$this -> param['description'] = preg_replace($search, '', $this -> param['description']);

		$str = $this -> param['subject'];
		$p = ord(substr($str, 0, 1));
		if ($p > 160) {
			$this -> param['a-z'] = strtoupper(substr(c($str), 0, 1)) . "-Z" ;
		} else if (($p < 48 && $p > 0) || ($p < 160 && $p > 57)) {
			$this -> param['a-z'] = strtoupper(substr($str, 0, 1));
		} else if ($p > 47 && $p < 58) {
			$this -> param['a-z'] = "0-9";
		} else {
			$this -> param['a-z'] = "xxx";
		} 
	
		$temp = array();
		if(function_exists('keyword')) $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();
		$item = $this -> formatUrl();
		if(empty($item)) _output_js("你至少需要填写一个软件地址");
		
		$sql = qbase::createSql($this -> table, $this -> param, 'insert');
		$id = qdb::query($sql, true);
		
		$this -> updateUrl($id, $item);
		$this -> extend_insert($id);
		$this -> related($id, 0, 'add');
		$this -> add($id);
		$this->moveToRecycle($id, $this->param_recycle);
		qbase::get_object('event', 'download', array('channelid'=>$this->channelid, 'id'=>$id, 'action'=>'add', 'data'=>$this->param), true);
	} 

	function editAction() {
		$id = intval($_POST['id']);
		$this -> param();
		$item = $this -> formatUrl();
		if(empty($item)) _output_js("你至少需要填写一个软件地址");		
		$this -> updateUrl($id, $item);
		$this -> extend_update($id);
		$this -> related($id, 0, 'modify');
		$this -> modify($id);
		qbase::get_object('event', 'download', array('channelid'=>$this->channelid, 'id'=>$id, 'action'=>'modify', 'data'=>$this->param), true);
	} 

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

	function updateUrl($id, &$data){
		if(empty($data) || !is_array($data)) qbase::showmsg("error param data");
		qdb::query("delete from ".TABLEPRE."downloads_{$this->channelid} where id = '{$id}' ");
		foreach($data as $val)
		{
			$val['id'] = $id;
			$sql = qbase::createSql("".TABLEPRE."downloads_{$this->channelid}", $val, 'insert');
			qdb::query($sql);
		}
	}
	
	function formatUrl() {
		$type = intval($_POST['type']);
		$url_name = $_POST['url_name'];
		$urls = $_POST['urls'];
		$serverid = $_POST['serverid'];
		$data = array();

		if ($type == 0) {
			if (is_array($urls)) {
				for($m = 0; $m < count($urls); $m++) {
					$urls[$m] = trim($urls[$m]);
					if ($urls[$m] && $urls[$m] != 'http://') {
						$data[] = array(
							'url_name' => $url_name[$m],
							'urls' => $urls[$m], 
							'serverid' => ''
						);
					}
				}
			}
		} else {
			if (isset($serverid) && is_array($serverid) && !empty($serverid)) {
				$serverids = implode(',', $serverid);
			} else {
				$serverids = intval($serverid);
			}
			$urls = trim($urls);
			if ($serverids && $urls != 'http://'){
				$data[] = array(
					'urls' => $urls,
					'serverid' => $serverids
				);
			}
		}
		return $data;
	}
} 

?>