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

/**
* 添加模型内容入口文件
**/

$this_controller->check_action($ACTION,$this_system->SITE) or message('no_privilege');

if(isset($_REQUEST['model'])){
	$this_system->init_model();
	$this_model or message('no_such_cms_model');
	$this_model['enabled'] or message('sites_model_disabled');
	
}else{
	$models = $this_system->get_models();
	foreach($models as $k=>$m){
		if(!$m['enabled'])unset($models[$k]);
	}
	if(count($models) == 1){
		header('Location: '. $this_url .'?model='. current(array_keys($models)));
	}else{
		include template($this_module, 'select_model');
	}
	exit;
}

if(REQUEST_METHOD == 'GET'){
	$_my_addible_category = $this_controller->get_acl('my_addible_category', $this_system->SITE);
	$my_addible_category = p8_json($_my_addible_category);
	$allow_verify = $this_controller->check_action('verify');
	$allow_create_time = $this_controller->check_action('create_time');
	$allow_filter_word = $this_controller->check_action('filter_word');
	$allow_attribute = $this_system->check_manager();
	$sitemanager = $this_system->check_manager(); 
	$allow_level = $this_system->check_manager();
	$data = array();

	if(isset($_GET['cid'])){
		if(!$this_controller->check_category_action('add',intval($_GET['cid'])))message($P8LANG['sites_item']['no_category_privilege']);
		$data['cid'] = intval($_GET['cid']);
	
	}
	//预留字段
	$custom = array('custom_a','custom_b','custom_c','custom_d','custom_e','custom_f','custom_g','custom_h','custom_i','custom_j');	
	$attributes = array();
	$uploader_config = $core->get_config('core','uploader');
	$thumb_width = $uploader_config['thumb']['width'] ? intval($uploader_config['thumb']['width']) : 0;
	$thumb_height = $uploader_config['thumb']['height'] ? intval($uploader_config['thumb']['height']) : 0;	
	require $this_model['path'] .'/member/add.php';
	
	$template = empty($this_model['CONFIG']['member_edit_template']) ? 'edit' : $this_model['CONFIG']['member_edit_template'];
	
	include template($this_module, $template);
	
}else if(REQUEST_METHOD == 'POST'){
	
	//如果魔法引号开启strip掉
	$_POST = p8_stripslashes2($_POST);
	
	if(isset($_POST['cid'])){
		if(!$this_controller->check_category_action('add',intval($_POST['cid'])))message($P8LANG['sites_item']['no_category_privilege']);
	
	}
	
	//检查分类自动审核权限
	unset($_POST['verify']);
	unset($_POST['attribute'][10],$_POST['attribute'][11]);
	$_POST['verifier']='';
	$mysites = $this_system->get_manage_sites();
	if($this_controller->check_category_action('autoverify', $_POST['cid']) || in_array($this_system->SITE, $mysites)){
		$_POST['verify'] = 1;
		$_POST['verifier'] = $USERNAME;
	}
	
	require $this_model['path'] .'/member/add.php';
	if(!isset($_POST['cid']) || empty($_POST['cid'])) message('category_need');
	//防止重复提交
	if($core->CONFIG['repeatable']){
		$last_post = P8_TIME - 10;
		$last_post2 = P8_TIME - 60;
		$post_cid = $_POST['cid'];
		$title = $_POST['title'];
		$lastfile_list = $core->DB_master->fetch_one("select * from `$this_module->table` where (`timestamp` >= '$last_post' or (`timestamp` >= '$last_post2' and `title` = '$title')) and `cid` = $post_cid and `uid` = $UID order by `id` desc limit 0,1");
		if(!empty($lastfile_list)) message('post_rule');
	}
	//大分类不允许发布信息
	$category_module = $this_system->load_module('category');	
	$category_module->get_cache();
	if($category_module->categories[$_POST['cid']]['type'] == 1) message('category_post_rule');
	
	$id = $this_controller->add($_POST) or message('fail');
	
	if($_POST['verify']){
		$data = $this_module->data('read', $id);
		$data['#category'] = $this_system->fetch_category($data['cid'],true);
		$static_view_url = $this_system->site_p8_url($this_module, $data, 'view');
		message(
			array(
				array('sites_to_edit', $this_module->U_controller .'-update?id='.$id.'&model='.$_POST['model'].'&verified='.$_POST['verify']),
				array('sites_to_list', $this_module->U_controller .'-my_list?cid='.$_POST['cid'].'&model='.$_POST['model']),
				array('sites_to_view', $static_view_url.'?verify='. $_POST['verify'], '_blank'),
				array('sites_to_view_dynamic', $STATIC_URL.'/s.php/'.$this_system->SITE.'/item-view-id-'.$id .'?verify='. $_POST['verify'], '_blank'),
				array('sites_to_add', $this_module->U_controller .'-add?cid='.$_POST['cid'].'&model='.$_POST['model'])
			),
			$this_module->U_controller .'-add?cid='.$_POST['cid'].'&model='.$_POST['model'],
			10000
		);
	}else{
		message($P8LANG['sites_item']['verify']['wait'], $this_module->U_controller .'-add?cid='.$_POST['cid'].'&model='.$_POST['model']);
	}
	
}
