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

	function qcmsMovie() {
		$this -> qmecms();
		$this->mod = 'movie';
	} 

	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['description'] = trim($_POST['description']);
		$this -> param['description'] = qbase :: get_object('html_clear', true) -> clear(trim($_POST['description']));
		$this -> param['subject_ot'] = trim($this->_formatActor($_POST['subject_ot']));
		$this -> param['play_year'] = trim($_POST['play_year']);
		$this -> param['play_area'] = trim($_POST['play_area']);
		$this -> param['play_leader'] = trim($this->_formatActor($_POST['play_leader']));
		$this -> param['play_actor'] = trim($this->_formatActor($_POST['play_actor']));
		$this -> param['play_word'] = trim($_POST['play_word']);
		$this -> param['play_status'] = intval($_POST['play_status']);

		unset($this->param['author'], $this->param['source']);
	} 

	function addAction() {
		$this -> param();
		$keyword = array();
		$this->param['keyword'] = implode(" ", $keyword);
		$sql = qbase::createSql($this -> table, $this -> param, 'insert');
		$id = qdb::query($sql, true);
		$this -> formatUrl($id);
		$this -> extend_insert($id);
		$this -> related($id, 0, 'add');
		$this-> formatActor($id);
		$this -> add($id);
		$this->moveToRecycle($id, $this->param_recycle);
		qbase::get_object('event', 'movie', array('channelid'=>$this->channelid, 'id'=>$id, 'action'=>'add', 'data'=>$this->param), true);
	} 

	function editAction() {
		$id = intval($_POST['id']);
		$this -> param();
		$this -> formatUrl($id);
		$this -> extend_update($id);
		$this-> related($id, 0, 'modify');
		$this-> formatActor($id);
		$this -> modify($id);
		qbase::get_object('event', 'movie', array('channelid'=>$this->channelid, 'id'=>$id, 'action'=>'modify', 'data'=>$this->param), true);
	} 

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

	function formatActor($id){
		qdb::query("delete from ".TABLEPRE."movie_actor_mapping where channelid = '{$this->channelid}' and id = '{$id}' ");
		$data = array_unique(array_merge(explode(',', $this->param['play_actor']), explode(',', $this->param['play_leader'])));
		if(empty($data)) return;
		$temp = array();
		foreach($data as $val){
			$val = trim($val);
			if(empty($val)) continue;
			$temp[] = "('{$val}', '{$this->channelid}',  '{$id}')";
		}
		$temp = array_unique($temp);
		if(empty($temp)) return;
		$sql = "replace into ".TABLEPRE."movie_actor_mapping  (name, channelid, id) values " . implode(',', $temp);
		qdb::query($sql);
	}

	function formatUrl($id) {
		$split = '^57)(';
		$url_name = $_POST['url_name'];
		$urls = $_POST['urls'];
		$types = $_POST['types'];
		$ordering = is_array($_POST['ordering']) ? $_POST['ordering'] : array() ;
		$temp = qdb::fetchAll("select * from ".TABLEPRE."movies_{$this->channelid} where id = '{$id}' ");
		$old = $old_keys = $new = $new_keys = $modify_diff_keys = array();
		foreach($temp as $val){
			$key = md5($val['url'] . $split . $val['type']);
			$old_keys[] = $key;
			$old[$key] = $val;
		}

		if (is_array($urls)) {
			for($m = 0; $m < count($urls); $m++) {
				$url_name[$m] = trim($url_name[$m]);
				$urls[$m] = trim($urls[$m]);
				$types[$m] = trim($types[$m]);
				if ($urls[$m] && $url_name[$m] && $types[$m]) {
					$key = md5($urls[$m] . $split . $types[$m]);
					$new_keys[] = $key;
					$new[$key] = array(
						'pname'=>	$url_name[$m],
						'url'=>$urls[$m],
						'type'=>$types[$m],
						'ordering'=>empty($ordering) ? $m : $ordering[$m],
					);
				}
			}
		}

		$old_diff_keys = array_diff($old_keys, $new_keys);
		$new_diff_keys = array_diff($new_keys, $old_keys);
		$modify_diff_keys = array_intersect($new_keys, $old_keys);

		//need to delete
		foreach($old_diff_keys as $hash){
			qdb::query("delete from ".TABLEPRE."movies_{$this->channelid} where pid = '{$old[$hash]['pid']}'");
		}

		//need to add
		foreach($new_diff_keys as $hash){
			qdb::query("replace into ".TABLEPRE."movies_{$this->channelid} (id,pname,url,type,ordering) values ('{$id}','{$new[$hash]['pname']}','{$new[$hash]['url']}', '{$new[$hash]['type']}', '{$new[$hash]['ordering']}')");
		}

		//need to modify
		foreach($modify_diff_keys as $hash){
			qdb::query("update ".TABLEPRE."movies_{$this->channelid} set pname = '{$new[$hash]['pname']}', ordering = '{$new[$hash]['ordering']}'  where pid = '{$old[$hash]['pid']}' ");
		}
	}
	
	function _formatActor($name){
		$replace = array(",","，","|");
		$name = trim(str_replace($replace, ",", $name));
		$temp = explode(",", $name);
		$temp2 = array();
		foreach($temp as $val){
			$val = trim($val);
			if($val) $temp2[] = $val;
		}
		$temp2 = array_unique($temp2);
		return implode(",", $temp2);
	}
} 
?>