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

/**
* 推送数据,只提供AJAX POST调用
**/

$this_controller->check_admin_action('cluster_push') or message('clustered');

$this_controller->check_admin_action($ACTION) or exit('[]');

if(REQUEST_METHOD == 'GET'){
	
	$sites = $core->load_system('sites');
	$stop = $sites->load_module('stop');

	$json = $stop->get_json();
	$allsites  = $sites->get_sites();
	$allsites  = p8_json($allsites);
	exit('{"json":'. $json['json'] .',"path":'. $json['path'] .',"sites":'.$allsites.'}');
	
}if(REQUEST_METHOD == 'POST'){
	
	$id = isset($_POST['id']) ? filter_int($_POST['id']) : array();
	$id or exit('[]');
	$cid = isset($_POST['cid']) ? intval($_POST['cid']) : 0;
	$push_site = isset($_POST['push_site']) ? $_POST['push_site'] : '';
    $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;
	//$cid or exit('[]');
	$sites = $core->load_system('sites');
	$stop = $sites->load_module('stop');

	//生成数据
	$data = $this_module->sites_data($id, $cid,$push_site, $send_time_type, $send_time);
	
	//上推数据
	$count = $stop->push($data,$filter_word_enable);
	if($count>0){
		//要设置属性的内容
		$query = $DB_master->query("SELECT id, cid, model, attributes FROM $this_module->main_table WHERE id IN (". implode(',', $id) .")");
		$scid = array();		
		//设置内容属性
		$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($id));
	}else{
		exit('[]');
	}	
}
exit('[]');
