<?php
defined('PHP168_PATH') or die();

/**
* 推送数据,只提供AJAX POST调用
**/
$this_system->check_manager($ACTION) or message('no_privilege');

if(REQUEST_METHOD == 'GET'){
    
	$stop = &$this_system->load_module('stop');

	$json = $core->CACHE->read($this_system->name .'/modules/', $stop->name, 'json');
	exit('{"json":'. $json['json'] .',"path":'. $json['path'] .'}');
	
}if(REQUEST_METHOD == 'POST'){
	$id = isset($_POST['id']) ? filter_int($_POST['id']) : array();
	$id or exit('[]');
	$dell_id = $id;
	$cid = isset($_POST['cid']) ? intval($_POST['cid']) : 0;
	$send_time_type = isset($_POST['send_time_type']) ? intval($_POST['send_time_type']) : 0;
	$send_time = isset($_POST['send_time']) ? trim($_POST['send_time']) : 0;
	$filter_word_enable = isset($_POST['filter_word_enable']) ? false : true;
	//var_dump($filter_word_enable);
	//$cid or exit('[]');
	
	if($core->CONFIG['push_allow_max_number']){
		$site = $this_system->SITE;
		$table = P8_TABLE_.'sites_stop_data';
		$this_day = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$query = $DB_master->query("SELECT count(*) as count FROM $table WHERE sc = 'c' and site = '$site' and timestamp>$this_day");
		$res = $DB_master->fetch_array($query);
		if($res['count'] >= intval($core->CONFIG['push_allow_max_number'])) exit('["message":"$P8LANG[cluster_push_fail]","count":0]');
	}	
	$stop = &$this_system->load_module('stop');
	
	//生成数据
	$data = $this_module->cluster_data($id, $cid, $send_time_type, $send_time);
	
	//上推数据
	$count = $stop->push($data,$filter_word_enable);
	$this_system->log(array(
		'title' => $P8LANG['_module_cluster_push_admin_log'],
		'request' => $_POST,
	));
	if($count>0){
		$scid=array();
		//要设置或删除属性的内容
		$query = $DB_master->query("SELECT id, cid, model, attributes FROM $this_module->main_table WHERE id IN (". implode(',', $dell_id) .")");
		//设置内容属性
		$datas = array();
		while($arr = $DB_master->fetch_array($query)){
			//原本属性
			$_attributes = array_filter(explode(',', $arr['attributes']));
			$_attributes[] = 10;
			$datas[] = array($arr['id'],10, $arr['cid'], $USERNAME, P8_TIME);
			$_attributes = implode(',', array_unique($_attributes));
			
			//更新主表属性
			$DB_master->update(
				$this_module->main_table,
				array('attributes' => $_attributes),
				"id = '$arr[id]'"
			);
			
			//更新模型表属性
			$this_module->set_model($arr['model']);
			$DB_master->update(
				$this_module->table,
				array('attributes' => $_attributes),
				"id = '$arr[id]'"
			);
			$scid[$arr['cid']]=$arr['cid'];
		}
		
		//批量replace into
		$DB_master->insert(
			$this_module->attribute_table,
			$datas,
			array(
				'multiple' => array('id', 'aid', 'cid', 'last_setter', 'timestamp'),
				'replace' => true
			)
		);
		exit(jsonencode($dell_id));
	}else{
		exit('[]');
	}	
}
exit('[]');
