<?php
/*
Plugin Name: 同策插件
Version: 1.0
Author: 同策.
Description: 同策
*/


define('PLUGIN_DIR', dirname(__FILE__) . '/');
add_action('init', 'tospur_init');

function tospur_init()
{
    require_once(PLUGIN_DIR . 'Config.php');
    require_once(PLUGIN_DIR . 'Tools/TCSync.php');
    require_once(PLUGIN_DIR . 'Tools/Image.php');
    require_once(PLUGIN_DIR . 'Dao/SearchDao.php');
    require_once(PLUGIN_DIR . 'Dao/TospurDao.php');
    require_once(PLUGIN_DIR . 'Dao/CustomerTrackingDao.php');
    require_once(PLUGIN_DIR . 'Dao/CustomerDao.php');
    require_once(PLUGIN_DIR . 'Dao/QuotaDao.php');
    require_once(PLUGIN_DIR . 'Dao/DBManager.php');
    require_once(PLUGIN_DIR . 'Admin/House.php');
    require_once(PLUGIN_DIR . 'Admin/customer.php');
    require_once(PLUGIN_DIR . 'Admin/customerList.php');
    require_once(PLUGIN_DIR . 'Admin/newHouseList.php');
    require_once(PLUGIN_DIR . 'Admin/secHandHouse.php');
    require_once(PLUGIN_DIR . 'Admin/secHandHouseList.php');
    require_once(PLUGIN_DIR . 'Admin/rentHouse.php');
    require_once(PLUGIN_DIR . 'Admin/rentHouseList.php');
    require_once(PLUGIN_DIR . 'Admin/customer.php');
    require_once(PLUGIN_DIR . 'Admin/customerList.php');
    require_once(PLUGIN_DIR . 'Admin/customerTrackingList.php');
    require_once(PLUGIN_DIR . 'Admin/QuotaYearList.php');
    require_once(PLUGIN_DIR . 'Admin/QuotaMonthList.php');
    require_once(PLUGIN_DIR . 'Admin/feature.php');
	require_once(PLUGIN_DIR . 'Admin/introduction.php');
    require_once(PLUGIN_DIR . 'Admin/Contract.php');
    require_once(PLUGIN_DIR . 'Admin/consultant_score.php');
    require_once(PLUGIN_DIR . 'Admin/view_house.php');
    require_once(PLUGIN_DIR . 'Admin/tospur_sale.php');
    require_once(PLUGIN_DIR . 'Admin/TCSyncView.php');
    require_once(PLUGIN_DIR . 'Admin/Contract_List.php');
    require_once(PLUGIN_DIR . 'Admin/commissionManage.php');
    require_once(PLUGIN_DIR . 'Admin/commissionList.php');
    require_once(PLUGIN_DIR . 'Admin/progressList.php');
    require_once(PLUGIN_DIR . 'Admin/upload.php');
    require_once(PLUGIN_DIR . 'Admin/Statistics.php');
    add_action('admin_menu', 'reset_menu');
    //Statistics::business();
    tospur_register_script_style();
    tospur_ajax_set();
    tospur_theme_format();
}
add_action('statistics', 'Statistics::business');
add_action('resetRole', 'custom_role');
function tospur_theme_format(){
    //移除
    add_filter('admin_footer_text', tospur_remove_admin_footer_text, 1000);
    add_filter('update_footer', tospur_remove_admin_footer_upgrade, 1000);
    add_action('login_enqueue_scripts', 'tospur_login_logo');
    add_action( 'admin_enqueue_scripts', 'tospur_load_resources');
    //admin bar
    add_action('wp_before_admin_bar_render', 'tospur_remove_admin_bar');
    function tospur_remove_admin_footer_text($footer_text =''){
        return '';
    }
    function tospur_remove_admin_footer_upgrade($footer_text =''){
        return '';
    }

    function tospur_remove_admin_bar(){
        global $wp_admin_bar;
        $wp_admin_bar->remove_menu('wp-logo');
        $wp_admin_bar->remove_menu('updates');
        $wp_admin_bar->remove_menu('new-content');
        $wp_admin_bar->remove_menu('edit-profile');
        $wp_admin_bar->remove_menu('view-site');
        $wp_admin_bar->remove_menu('user-info');
    }

    function tospur_login_logo()
    { ?>
        <style type="text/css">
            .login h1 a {
                background-image: url(<?php echo get_stylesheet_directory_uri(); ?>/img/fnl.jpg);
                padding-bottom: 30px;
            }
        </style>
    <?php }
}



function tospur_load_resources(){
    wp_enqueue_script('jqueryjs');
    wp_enqueue_script('jquery-ui');
    wp_enqueue_script('bootstrapjs');
    wp_enqueue_script('publicjs');

    wp_enqueue_script('validate');
    wp_enqueue_style('jquery-ui_css');
	wp_enqueue_style('publicCss');
    wp_enqueue_style('bootstrapMinCss');

}

function tospur_register_script_style(){
    wp_register_script('jqueryjs',plugins_url('Admin/views', __FILE__)."/js/jquery.min.js");
    wp_register_script('jquery-ui',plugins_url('Admin/views', __FILE__)."/js/jquery-ui.js");
    wp_register_script('validate',plugins_url('Admin/views', __FILE__)."/js/jquery.validate.js");
    wp_register_script('bootstrapjs',plugins_url('Admin/views', __FILE__)."/js/bootstrap.min.js");
    wp_register_script('publicjs',plugins_url('Admin/views', __FILE__)."/js/public.js");

    wp_register_style('jquery-ui_css', plugins_url('Admin/views', __FILE__)."/css/jquery-ui.css");
	wp_register_style('publicCss', plugins_url('Admin/views', __FILE__)."/css/public.css");
    wp_register_style('bootstrapcss', plugins_url('Admin/views', __FILE__)."/css/bootstrap.css");
    wp_register_style('bootstrapMinCss', plugins_url('Admin/views', __FILE__)."/css/bootstrap.min.css");
}

function tospur_ajax_set()
{
    add_action('wp_ajax_serachCity', 'SearchDao::ajax_searchCity');
    add_action('wp_ajax_nopriv_serachCity', 'SearchDao::ajax_searchCity');
    add_action('wp_ajax_searchArea', 'SearchDao::ajax_searchArea');
    add_action('wp_ajax_nopriv_searchArea', 'SearchDao::ajax_searchArea');
    add_action('wp_ajax_searchBuildProperty', 'SearchDao::ajax_searchBuildProperty');
    add_action('wp_ajax_nopriv_searchBuildProperty', 'SearchDao::ajax_searchBuildProperty');
    add_action('wp_ajax_searchRoom', 'SearchDao::ajax_searchRoom');
    add_action('wp_ajax_nopriv_searchRoom', 'SearchDao::ajax_searchRoom');
    add_action('wp_ajax_searchUnitPriceRange', 'SearchDao::ajax_searchUnitPriceRange');
    add_action('wp_ajax_nopriv_searchUnitPriceRange', 'SearchDao::ajax_searchUnitPriceRange');
    add_action('wp_ajax_searchTotalPrice', 'SearchDao::ajax_searchTotalPrice');
    add_action('wp_ajax_nopriv_searchTotalPrice', 'SearchDao::ajax_searchTotalPrice');
    add_action('wp_ajax_searchRentalPrice', 'SearchDao::ajax_searchRentalPrice');
    add_action('wp_ajax_nopriv_searchRentalPrice', 'SearchDao::ajax_searchRentalPrice');
    add_action('wp_ajax_searchOrganization', 'SearchDao::ajax_searchOrganization');
    add_action('wp_ajax_nopriv_searchOrganization', 'SearchDao::ajax_searchOrganization');
    add_action( 'wp_ajax_searchHouse', 'SearchDao::ajax_searchHouse' );
    add_action( 'wp_ajax_nopriv_searchHouse', 'SearchDao::ajax_searchHouse');
    add_action( 'wp_ajax_searchConsultant', 'SearchDao::ajax_searchConsultant' );
    add_action( 'wp_ajax_nopriv_searchConsultant', 'SearchDao::ajax_searchConsultant');
	add_action('wp_ajax_add_feature', 'feature::ajax_add_feature');
	add_action('wp_ajax_nopriv_add_feature', 'feature::ajax_add_feature');
	add_action('wp_ajax_submit_introduction', 'introduction::ajax_submit_introduction');
	add_action('wp_ajax_nopriv_submit_introduction', 'introduction::ajax_submit_introduction');
    add_action('wp_ajax_searchCustomerByNameOrPhone', 'CustomerDao::searchCustomerByNameOrPhone');
	add_action('wp_ajax_nopriv_searchCustomerByNameOrPhone', 'CustomerDao::searchCustomerByNameOrPhone');

    add_action('wp_ajax_replaceQuota', 'QuotaMonthList::replaceQuota');
	//标签和特色
	add_action('wp_ajax_searchTagOrFeature', 'SearchDao::ajax_searchTagOrFeature');
	add_action('wp_ajax_nopriv_searchTagOrFeature', 'SearchDao::ajax_searchTagOrFeature');
    //后台处理 置业顾问评分
    add_action('wp_ajax_valid_consultant_score', 'valid_consultant_score');
    //后台处理 置业顾问
    add_action('wp_ajax_update_consultant', 'update_consultant');
}

function valid_consultant_score()
{
    $score_id = $_POST['id'];
    $valid = $_POST['valid'];
    if (isset($score_id) && isset($valid)) {
        $result = TospurDao::update_consultant_score($score_id, $valid);
        $array = array();
        if ($result) {
            $array['code'] = 2000;
        } else {
            $array['code'] = 2001;
        }
        wp_send_json($array);
    }
}
function update_consultant()
{
    $house_id = $_POST['id'];
    $consultant_id = $_POST['consultant_id'];
    if (isset($consultant_id) && isset($house_id)) {
        $result = TospurDao::update_view_house_consultant($house_id, $consultant_id);
        $array = array();
        if ($result) {
            $array['code'] = 2000;
        } else {
            $array['code'] = 2001;
        }
        wp_send_json($array);
    }
}

//自定义权限管理
function custom_role() {
    remove_role("zygw");
    remove_role("qzzy");
    remove_role("xzzl");
    remove_role("jl");

    $cRole = array(
        "xf"=>true,
        "xfList"=>true,
        "esf"=>true,
        "esfList"=>true,
        "zf"=>true,
        "zfList"=>true,
        "kh"=>true,
        "khList"=>true,
        "fygj"=>true,
        "sygl"=>true,
        "zygwpf"=>true,
        "zygwyj"=>true,
        "syjdb"=>true,
        "ywjdb"=>true,
        "addTag"=>true,
        "addFeature"=>true,
        "dataSync"=>true,
        "houseApproval"=>true,
        "ht"=>true,
        "htEdit"=>true,
        "htApproval"=>true,
        "mySet" => true,
        "fdfw_allot" => true,
        "fdfw_cs" => true,
        "fdfw_cz" => true,
        "yyList"=>true,
        "plsc"=>true,
        "canEdit"=>true
    );

    add_role("zygw","置业顾问",array(
        "xf"=>true,
        "xfList"=>true,
        "esf"=>true,
        "esfList"=>true,
        "zf"=>true,
        "zfList"=>true,
        "kh"=>true,
        "khList"=>true,
        "fygj"=>true,
        "ht"=>true,
        "fdfw_cs" => true,
        "fdfw_cz" => true,
        "mySet" => true,
        "yyList"=>true,
        "canEdit"=>true
    ));
    add_role("qzzy","权证专员",array(
        "ht"=>true,
        "htEdit"=>true,
        "xf"=>true,
        "xfList"=>true,
        "esf"=>true,
        "esfList"=>true,
        "zf"=>true,
        "zfList"=>true
    ));

    add_role("xzzl","行政助理",array(
        "xf"=>true,
        "xfList"=>true,
        "esf"=>true,
        "esfList"=>true,
        "zf"=>true,
        "zfList"=>true,
        "kh"=>true,
        "khList"=>true,
        "fygj"=>true,
        "sygl"=>true,
        "zygwpf"=>true,
        "zygwyj"=>true,
        "syjdb"=>true,
        "ywjdb"=>true,
        "addTag"=>true,
        "addFeature"=>true,
        "dataSync"=>true,
        "houseApproval"=>true,
        "ht"=>true,
        "htEdit"=>true,
        "htApproval"=>true,
        "fdfw_allot"=>true,
        "fdfw_cs" => true,
        "fdfw_cz" => true,
        "yyList"=>true,
        "plsc"=>true,
        "canEdit"=>true
    ));

    add_role("jl","经理",array(
        "xf"=>true,
        "xfList"=>true,
        "esf"=>true,
        "esfList"=>true,
        "zf"=>true,
        "zfList"=>true,
        "kh"=>true,
        "khList"=>true,
        "fygj"=>true,
        "sygl"=>true,
        "zygwpf"=>true,
        "zygwyj"=>true,
        "syjdb"=>true,
        "ywjdb"=>true,
        "addTag"=>true,
        "addFeature"=>true,
        "dataSync"=>true,
        "houseApproval"=>true,
        "ht"=>true,
        "htEdit"=>true,
        "htApproval"=>true,
        "fdfw_allot"=>true,
        "fdfw_cs" => true,
        "fdfw_cz" => true,
        "yyList"=>true,
        "plsc"=>true,
        "canEdit"=>true
    ));
}
register_activation_hook( __FILE__, 'custom_role' );

function reset_menu()
{
	add_menu_page('新房列表','新房列表', 'xfList', 'newHouseList', 'function_newHouseList', 'dashicons-menu', 6);
	add_submenu_page('newHouseList', '添加新房', '添加新房', 'xf', 'newHouse', 'House::init_view');
	add_menu_page('二手房列表','二手房列表', 'esfList', 'secHandHouseList', 'function_secHandHouseList', 'dashicons-menu', 7);
    add_submenu_page('secHandHouseList', '添加二手房', '添加二手房', 'esf', 'secHandHouse', 'SecHandHouse::secHandHouse_html');
    add_menu_page('租房列表','租房列表', 'zfList', 'rentHouseList', 'function_rentHouseList', 'dashicons-menu', 8);
    add_submenu_page('rentHouseList', '添加租房', '添加租房', 'zf', 'rentHouse', 'RentHouse::rentHouse_html');
	add_menu_page("我的设置", "我的设置", "mySet", "introduction", "introduction::introduction_html", 'dashicons-menu', 9);
    add_menu_page('合同列表','合同列表', 'ht', 'contractList', 'Contract_List::showTableView', 'dashicons-menu', 10);
    add_submenu_page('contractList', '添加合同', '添加合同', 'htEdit', 'contract', 'Contract::init_view');
    add_menu_page('客户列表','客户列表', 'khList', 'customerList', 'function_customerList', 'dashicons-menu', 11);
    add_submenu_page('customerList', '新增客户', '新增客户', 'kh', 'customer', 'customer::customer_html');
	add_menu_page('房客跟进', '房客跟进', 'fygj', 'customerTrackingList', 'function_customerTrackingList', 'dashicons-menu', 12);
    add_menu_page('置业顾问评分', '置业顾问评分', 'zygwpf', 'consultant_score', 'consultant_score_page', 'dashicons-menu', 14);
    add_submenu_page('consultant_score', '置业顾问业绩', '置业顾问业绩', 'zygwyj', 'quotaList', 'QuotaYearList::showHtml');
    add_menu_page('收佣进度表', '收佣进度表', 'syjdb', 'commissionList', 'function_commissionList', 'dashicons-menu', 15);
    add_menu_page('业务进度表', '业务进度表', 'ywjdb', 'progressList', 'function_progressList', 'dashicons-menu', 16);
    add_menu_page('房东服务', '房东服务', 'fdfw_cs', 'tospur_sale_secondhand', 'tospur_sale_page', 'dashicons-menu', 17);
    add_submenu_page('tospur_sale_secondhand', '出售', '出售', 'fdfw_cs', 'tospur_sale_secondhand', 'tospur_sale_page');
    add_submenu_page('tospur_sale_secondhand', '出租', '出租', 'fdfw_cz', 'tospur_sale_rent', 'tospur_sale_page');
    add_menu_page('预约列表', '预约列表', 'yyList', 'view_house', 'view_house_page', 'dashicons-menu', 18);

    add_menu_page('添加标签', '添加标签', 'addTag', 'add_tag', 'feature::add_feature_html', 'dashicons-menu');
    add_menu_page('添加特色', '添加特色', 'addFeature', 'add_feature', 'feature::add_feature_html', 'dashicons-menu');
    add_menu_page("同步数据", "同步数据", "dataSync", "sync", "TCSyncView::display", 'dashicons-menu');
    add_menu_page('批量上传', '批量上传', 'plsc', 'upload', 'upload::function_upload', 'dashicons-menu');
    //移除更新信息
    remove_action( 'admin_notices', 'update_nag', 3 );
    global $menu;
    $restricted = array(
        __('Dashboard'),
        __('Posts'),
        __('latestnews'),
        __('Media'),
        __('Links'),
        __('Pages'),
        __('Appearance'),
        __('Tools'),
        __('Comments'),
    );
    if(!current_user_can("administrator")){
        $restricted[] = __('Plugins');
        $restricted[] = __('Users');
        $restricted[] = __('Settings');
    }
    end($menu);
    while (prev($menu)) {
        $value = explode(' ', $menu[key($menu)][0]);
        if (strpos($value[0], '<') === FALSE) {
            if (in_array($value[0] != NULL ? $value[0] : "", $restricted)) {
                unset($menu[key($menu)]);
            }
        } else {
            $value2 = explode('<', $value[0]);
            if (in_array($value2[0] != NULL ? $value2[0] : "", $restricted)) {
                unset($menu[key($menu)]);
            }
        }
    }
}