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

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

$this_controller->check_action($ACTION) 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('cms_model_disabled');
	
}else{
	$class[1] = 'class="over"';
	$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');
	$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_attribute = $this_controller->check_action('attribute');
	$allow_level = $this_controller->check_action('level');
	
	$data = array();

	if(isset($_GET['cid'])){
		if(!$this_controller->check_category_action('add',intval($_GET['cid'])))message($P8LANG['cms_item']['no_category_privilege']);
		$data['cid'] = intval($_GET['cid']);
	
	}
	$data['suoyin'] = 'xxgk'.date('Ymd',P8_TIME).rand(1000,9999);
	$attributes = array();
    $authority_enable = $this_module->CONFIG['authority'];
    if($authority_enable) $core->get_cache('role');
    $authority = array();
	
	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);
    unset($_POST['verify']);

	//检查是否有初审权限
    /*$my_category_to_verify_first = $this_controller->get_acl('my_category_to_verify_first');
    if(isset($my_category_to_verify_first[0])){
        $_POST['verifier'] = $USERNAME;
        $_POST['verified'] = 2;
    }elseif(!empty($my_category_to_verify_first) && in_array($_POST['cid'],array_keys(my_category_to_verify_first))){
        $_POST['verifier'] = $USERNAME;
        $_POST['verified'] = 2;
    }
    //检查是否有终审权限
    $my_category_to_verify = $this_controller->get_acl('my_category_to_verify');
    if(isset($my_category_to_verify[0])){
        $_POST['verifier'] = $USERNAME;
        $_POST['verify'] = 1;
    }elseif(!empty($my_category_to_verify) && in_array($_POST['cid'],array_keys($my_category_to_verify))){
        $_POST['verifier'] = $USERNAME;
        $_POST['verify'] = 1;
    }*/
    //检查是否有自动终审权限

	if($this_controller->check_category_action('autoverify', $_POST['cid'])){
		$_POST['verify'] = 1;
		$_POST['verifier'] = $USERNAME;
	}

	require $this_model['path'] .'/member/add.php';

	$id = $this_controller->add($_POST) or message('fail');

	if($_POST['verify']){
        $flag = !empty($_POST['create_time_release']) && (!empty($_POST['timestamp']) && strtotime($_POST['timestamp']) > P8_TIME)  ? 0 :$_POST['verify'];
		message(
			array(
				array('cms_to_edit', $this_module->U_controller .'-update?id='.$id.'&model='.$_POST['model'].'&verified='.$flag),
				array('cms_to_list', $this_module->U_controller .'-my_list'),
				array('cms_to_view', $this_module->controller .'-view-id-'.$id.'?verified='.$_POST['verify'], '_blank'),
				array('cms_to_add', $this_module->U_controller .'-add?cid='.$_POST['cid'].'&model='.$_POST['model'])
			),
			$this_module->U_controller .'-add?cid='.$_POST['cid'].'&model='.$_POST['model'],
			1000000
		);
	}else{
        message(
            array(
                array('cms_to_edit', $this_module->U_controller .'-update?id='.$id.'&model='.$_POST['model'].'&verified=0'),
                array('cms_to_list', $this_module->U_controller .'-my_list?verified=0'),
                array('cms_continue_add', $this_module->U_controller .'-add?cid='.$_POST['cid'].'&model='.$_POST['model']),
                array('cms_to_view', $this_module->controller .'-view-id-'.$id.'?verified=0', '_blank')
            ),
            $this_module->U_controller .'-add?cid='.$_POST['cid'].'&model='.$_POST['model'],
            10000,
            array(),'',$P8LANG['cms_item']['verify']['wait']
        );
	//    message($P8LANG['cms_item']['verify']['wait'], $this_module->U_controller .'-add?cid='.$_POST['cid'].'&model='.$_POST['model']);
	}
}
