<?php

$cms['modules']['base.mod.php'] = array(
    'name' => __('Data Base', 'base.mod.php'),
    'description' => __('Data Base Module', 'base.mod.php'),
    'url_title' => __('Module page'),
    'url_link' => __('https://coffee-cms.com/en/modules/base/', 'base.mod.php'),
    'author' => __('Coffee CMS team'),
    'version' => '20.07',
    'files' => array(
        '.cms/mod/base.mod.php',
    ),
    'sort'    => 10,
);

// Return if module disabled
if ( isset($cms['config']['base.mod.php']['disabled'])
&& $cms['config']['base.mod.php']['disabled'] === true ) return;


// Install
if (empty($cms['config']['base.mod.php']['user'])) {
    $cms['config']['base.mod.php']['host']     = 'localhost';
    $cms['config']['base.mod.php']['port']     = '3306';
    $cms['config']['base.mod.php']['disabled'] = false;
    cms_save_config();
}

cms_add_function('admin', 'cms_base_admin');
cms_add_function('query', 'cms_base_connect', 5);

function cms_base_connect() {
    global $cms;
    if (!empty($cms['config']['base.mod.php']['user'])) {
        // return false on failure
        $cms['base'] = mysqli_connect(
            $cms['config']['base.mod.php']['host'],
            $cms['config']['base.mod.php']['user'],
            $cms['config']['base.mod.php']['password'],
            $cms['config']['base.mod.php']['base'],
            $cms['config']['base.mod.php']['port']
        );
    } else {
        return false;
    }

    if ( $cms['base'] === false ) {
        return false;
    } else {
        mysqli_query( $cms['base'], "SET NAMES UTF8;" );
        return true;
    }
}

function cms_base_admin() {
    global $cms;
    if (!is_admin()) return;
    // Save settings
    if ( !empty($_POST['save_base_config']) ) {
        $cms['config']['base.mod.php'] = array(
            'host' => $_POST['host'],
            'port' => $_POST['port'],
            'base' => $_POST['base'],
            'user' => $_POST['user'],
            'password' => $_POST['password'],
        );
        if (cms_base_connect()) {
            cms_do_stage( 'create_tables' );
        }
        cms_save_config();
        header('Location: ' . $cms["config"]["admin.mod.php"]["admin_url"]);
        exit;
    }
    $page = "
<div class='db-grid'>
    <div class='dbbox'>
    
        <form class='bdf' method='post'>
            <div class='field'>
                <div class='field-name-bd'>".__('Host', 'base.mod.php')."</div>
                <input class='settingx sto' name='host' type='text' value='{$cms['config']['base.mod.php']['host']}' placeholder='localhost'>
            </div>
            <div class='field'>
                <div class='field-name-bd'>".__('Port', 'base.mod.php')."</div>
                <input class='settingx sto' name='port' type='text' value='{$cms['config']['base.mod.php']['port']}' placeholder='3306'>
            </div>
            <div class='field'>
                <div class='field-name-bd'>".__('Base', 'base.mod.php')."</div>
                <input class='settingx sto' name='base' type='text' value='{$cms['config']['base.mod.php']['base']}'>
            </div>
            <div class='field'>
                <div class='field-name-bd'>".__('User', 'base.mod.php')."</div>
                <input class='settingx sto' name='user' type='text' value='{$cms['config']['base.mod.php']['user']}'>
            </div>
            <div class='field'>
                <div class='field-name-bd'>".__('Password', 'base.mod.php')."</div>
                <input class='settingx sto' name='password' type='text' value='{$cms['config']['base.mod.php']['password']}'>
            </div>
            <input type=submit name=save_base_config value='".__('Save')."' class='button-save'>
        </form>
      
    </div>
    
    <div class='dbmanual'>
        <div class=info>
            <h1>".__('Setting up a database connection', 'base.mod.php')."</h1>
            <p>".__('Enter the credentials obtained from the hosting provider', 'base.mod.php').".</p>
        </div>
    </div>

</div>
    ";

    // Создаем пункт меню, если еще не создан
    if ( empty( $cms['config']['base.mod.php']['menu']['base'] ) ) {
        $cms['config']['base.mod.php']['menu']['base'] = array(
            'title'    => 'Data Base',
            'sort'     => 20,
            'class'    => '',
            'section'  => 'Settings',
        );
        cms_save_config();
    }

    if (cms_base_connect() === false) {
        $cms['config']['base.mod.php']['menu']['base']['class'] = 'red';
    }

    $cms['admin_pages']['base'] = $page;
}
