<?php

/*
  PHP version 5
  Copyright (c) 2002-2019ECISP.CN、EarcLink.COM
  警告：这不是一个免费的软件，请在许可范围内使用，请尊重知识产权，侵权必究，举报有奖
  作者：黄祥云 E-mail:6326420@qq.com  QQ:6326420 TEL:18665655030 / 13797552220
*/


use application\ESPCMS_AdminAuthority;
use application\ESPCMS_Core;
use modul_application\ESPCMS_ArticleCreateDB;
use admin_application\ArticleModelDB;
use admin_application\ArticleTypeDB;
use admin_application\AdminPublicDB;
use admin_application\link\CreateMainLink;

class CreateHtmlSpecial {

	public function __construct() {
		$exclude_method = array('saveCreateHtmlSpecial', 'loadCreateWindow', 'countCreateHtmlSpecial', 'ajaxArticleType');
		ESPCMS_AdminAuthority::authorityAdminVerify($exclude_method);
	}

	public static function indexBase() {
		global $espcms_admin_templates;
		$admin_member_id = ESPCMS_AdminAuthority::getUserLoginInfo('admin_member_id');
		if (empty($admin_member_id) || !espcms_ismatches($admin_member_id)) {
			espcms_message_err('public_pack-espcms_input_parameter_fail', array($admin_member_id));
		}
		$lng = AdminPublicDB::get_lancode();
		$lng_array = ESPCMS_Core::get_lan_list($lng, false);
		$espcms_admin_templates->into('lng_array', $lng_array);

		$model_list = ArticleModelDB::get_model_list();
		$espcms_admin_templates->into('model_list', $model_list);
		$espcms_admin_templates->into('link', CreateMainLink::CreateSpecial_link_array());
		$espcms_admin_templates->output('create/create_special');
	}

	public static function loadCreateWindow() {
		global $espcms_admin_templates;
		if (!$_GET['lng'] || !preg_match("/^[a-z0-9]{2,50}$/i", $_GET['lng'])) {
			espcms_message_err('public_pack-espcms_input_parameter_fail', array($_GET['lng']));
		}
		if (!$_GET['mid'] || !espcms_ismatches($_GET['mid'])) {
			espcms_message_err('public_pack-espcms_input_parameter_fail', array($_GET['mid']));
		}
		if ($_GET['sid'] && !espcms_ismatches($_GET['sid'])) {
			espcms_message_err('public_pack-espcms_input_parameter_fail', array($_GET['sid']));
		}
		$espcms_admin_templates->into('lng', $_GET['lng']);
		$espcms_admin_templates->into('mid', $_GET['mid']);
		$espcms_admin_templates->into('sid', $_GET['sid']);
		$espcms_admin_templates->into('link', CreateMainLink::CreateSpecial_link_array());
		$espcms_admin_templates->output('create/create_special_load');
	}

	public static function saveCreateHtmlSpecial() {
		if (!$_POST['lng'] || !preg_match("/^[a-z0-9]{2,50}$/i", $_POST['lng'])) {
			espcms_public_dialog('espcms_public_dialog', 'create_pack-espcms_create_lng_validation_msg', 'false');
		}
		if (!$_POST['sid'] || !espcms_ismatches($_POST['sid'])) {
			espcms_public_dialog('espcms_public_dialog', 'create_pack-espcms_create_sid_validation_msg', 'false');
		}
		$creat_result = ESPCMS_ArticleCreateDB::create_Special_html($_POST['sid'], $_POST['lng']);
		if (!$creat_result) {
			espcms_public_dialog('espcms_info_save_ok', 'create_pack-espcms_create_type_save_no_message', 'false');
		} else {
			espcms_public_dialog('espcms_info_save_ok', 'create_pack-espcms_create_type_save_ok_message', 'true');
		}
	}

	public static function countCreateHtmlSpecial() {
		if (!$_POST['lng'] || !preg_match("/^[a-z0-9]{2,50}$/i", $_POST['lng'])) {
			return false;
		}
		$lng = $_POST['lng'] == 'big5' ? 'cn' : $_POST['lng'];
		$mid = $_POST['mid'] && espcms_ismatches($_POST['mid']) ? $_POST['mid'] : 0;
		$sid = $_POST['sid'];
		if (espcms_ismatches($sid) && $sid) {
			$create_num = 1;
			$sid_list = $sid;
		} else {
			$subjectlist_array = ArticleTypeDB::get_document_subjectlist_list($lng, 0, $mid, 1);
			if (is_array($subjectlist_array) && count($subjectlist_array) > 0) {
				$sid_list = implode('_', espcms_array_findkey($subjectlist_array, 'sid'));
				$create_num = count($subjectlist_array);
			} else {
				$create_num = 0;
				$sid_list = '';
			}
		}
		espcms_public_dialog('espcms_info_save_ok', 'create_pack-espcms_create_special_num', 'true', array($create_num), 'json', array('num' => $create_num, 'sid' => $sid_list));
	}

	public static function ajaxSpecial() {
		if (!$_GET['mid'] || !espcms_ismatches($_GET['mid'])) {
			return false;
		}
		if (!$_GET['lng'] || !preg_match("/^[a-z0-9]{2,50}$/i", $_GET['lng'])) {
			return false;
		}
		$_GET['lng'] = $_GET['lng'] == 'big5' ? 'cn' : $_GET['lng'];
		$subjectlist_array = ArticleTypeDB::get_document_subjectlist_list($_GET['lng'], 0, $_GET['mid'], 1);
		$title = espcms_lan_pack('create_pack-espcms_create_sid_title');
		$data_validation_msg = espcms_lan_pack('create_pack-espcms_create_sid_validation_msg');
		if (is_array($subjectlist_array)) {
			$option_str = '<select class="selectpicker form-control" name="sid" id="sid" data-size="8" data-isvalidation="true" title="' . $title . '" data-validation="^[1-9]{1}[0-9]*$" data-validation-msg="' . $data_validation_msg . '">';
			foreach ($subjectlist_array as $key => $value) {
				$option_str .= '<option value="' . $value['sid'] . '">' . $value['subjectname'] . '</option>';
			}
			$option_str .= '</select>';
		}
		exit($option_str);
	}

}
