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

$this_system->check_manager($ACTION) or message('no_privilege');
if(REQUEST_METHOD == 'GET'){
	$allsites = $this_system->get_sites();
	$sitename_alias = !empty($allsites[$this_system->SITE]['sitename']) ? $allsites[$this_system->SITE]['sitename']  : '';
}
if(isset($_REQUEST['cid'])){
	$this_system->init_model();
	$data['cid'] = isset($_REQUEST['cid']) ? intval($_REQUEST['cid']) : '';
	$data['type'] = isset($_REQUEST['type']) ? intval($_REQUEST['type']) : '';
	if($data['cid'] && !$this_controller->check_category_action('add', $data['cid'])) message($P8LANG['sites_item']['no_category_privilege']);
	
	$this_model or message('no_such_sites_model');
	
	$this_model['enabled'] or message('sites_model_disabled');
	
	if($data['type']==4){
		$page_id = $this_module->get_page($data['cid'],$MODEL);
		if($page_id){
			header('Location:'.$this_router.'-update?model='.$MODEL.'&id='.$page_id['id']);
			exit;
		}
			
	}
	$CAT = $this_system->fetch_category($data['cid']);
}else{
	$category = &$this_system->load_module('category');
	$category->get_cache(false);
	$path = array();
	$models = $this_system->get_models();
	foreach($category->categories as $v){
		$parents = $category->get_parents($v['id']);
		foreach($parents as $vv){
			$path[$v['id']][] = $vv['id'];
		}
		$path[$v['id']][] = $v['id'];
	}

	$json = array(
		'json' => p8_json($category->top_categories),
		'path' => p8_json($path),
		'models' => p8_json($models)
	);
	
	include template($this_module, 'select_model', 'admin');
	
	exit;
}

if(REQUEST_METHOD == 'GET'){
	$my_addible_category = p8_json($this_controller->get_acl('my_addible_category'));
	$allow_verify = $this_controller->check_admin_action('verify');
	$allow_attribute = $this_controller->check_admin_action('attribute');
	$allow_level = $this_controller->check_admin_action('level');
	$allow_filter_word = $this_controller->check_admin_action('filter_word');
	$allow_list_order = $this_controller->check_admin_action('list_order');
	$allow_create_time = $this_controller->check_admin_action('create_time');
	
	$data = array();
	
	isset($_GET['cid']) && $data['cid'] = $_GET['cid'];
	
	$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'] .'/admin/add.php';
	
	$template = empty($this_model['CONFIG']['admin_edit_template']) ? 'edit' : $this_model['CONFIG']['admin_edit_template'];
	
	include template($this_module, $template, 'admin');
	
}else if(REQUEST_METHOD == 'POST'){
	
	//如果魔法引号开启strip掉
	$_POST = p8_stripslashes2($_POST);
	
	if(!$this_controller->check_admin_action('verify')){
		unset($_POST['verify']);
	}
	$_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'] .'/admin/add.php';
	
	$ADMIN_LOG = array('title' => $P8LANG['_module_add_admin_log']);
	
	//分页
	$content = array();
	if(isset($_POST['field#']['content'])){
		$content = preg_split('#<div style="page-break-after:\s*?always;?">\s*?<span style="display: none;?">.*?</span>\s*?</div>#is', $_POST['field#']['content']);
		
		$_POST['field#']['content'] = array_shift($content);
	}
	if(!isset($_POST['cid']) || empty($_POST['cid'])) message('category_need');
	//防止重复提交
	$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');
	foreach($content as $v){
		$_POST['field#']['content'] = $v;
		$_POST['iid'] = $id;
		$_POST['action'] = 'addon';
		$this_controller->addon($_POST);
	}
	$this_system->log(array(
		'title' => $P8LANG['_module_add_admin_log'],
		'request' => $_POST,
	));
	$view_url = $this_module->controller; 
	if($core->CONFIG['static_enable'] && $STATIC_URL){
		$view_url = $STATIC_URL.'/s.php/'.$this_system->SITE.'/item';
	}	
	$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->admin_controller .'-update?id='. $id .'&model='. $_POST['model']),
			array('sites_to_list', $this_module->admin_controller .'-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->admin_controller .'-add?cid='. $_POST['cid']. '&model='. $_POST['model'])
		),
		$this_module->admin_controller .'-add?cid='. $_POST['cid'] .'&model='. $_POST['model'],
		10000
	);
	
}
