<?php
global $config, $smarty;

run_query("UPDATE " . $config['table_prefix'] . "config SET value = '15' WHERE name = 'logo_bottom_margin'");
run_query("UPDATE " . $config['table_prefix'] . "config SET value = '15' WHERE name = 'logo_top_margin'");
run_query("UPDATE " . $config['table_prefix'] . "config SET value = '75' WHERE name = 'logo_y'");
run_query("UPDATE " . $config["table_prefix"] . "schemes SET radius_menu=1");
run_query("UPDATE " . $config["table_prefix"] . "schemes SET gradient_stat=1");
run_query("UPDATE " . $config["table_prefix"] . "schemes SET size_opacity=0.5");
run_query("UPDATE " . $config["table_prefix"] . "schemes SET img_select='Origami.png' WHERE id NOT IN (102,103)");
run_query("UPDATE " . $config["table_prefix"] . "schemes SET img_select='Light.png' WHERE id=102");
run_query("UPDATE " . $config["table_prefix"] . "schemes SET img_select='Creation.png' WHERE id=103");

// Составляем сайм файл схемы

$sqlQuery = "SELECT * FROM " . $config['table_prefix'] . "schemes WHERE active=1";
$result = sql_query($sqlQuery);
$color_scheme = sql_fetch_assoc($result);
$add_button_color= $config['add_button_color'];
$color1= $color_scheme['color1'];
$color2= $color_scheme['color2'];
$color3= $color_scheme['color3'];
$color2rgb= hexToRgb($color_scheme['color2']);
$color3rgb= hexToRgb($color_scheme['color3']);
$color4= $color_scheme['color4'];
$gradient_stat= $color_scheme['gradient_stat'];
$size_opacity= $color_scheme['size_opacity'];
$bgImgSelect= $color_scheme['img_select'];
$tableBg= $color_scheme['table_bg'];
$radiusMenu= $color_scheme['radius_menu'];
$font_select= $color_scheme['font_select'];
$color_scheme= $color_scheme;
$new_css = '.table_title_head{ color:{$color1}; } .subtable__title-cell { color:{$color1}; } .continue-reading-link { {*background: {$add_button_color};*} } .user_table th { } .progress-bar { background-color: {$color1}; } #f_row .table-header-cell { background: {$color1}; } .fields__cell--header:first-child, .fields__cell--header:nth-child(2){ background-color:{$color1}; } .fixed-panel__item { background-color: {$color1}; }';
if ($tableBg == 1) {
    $new_css .= ' #f_row { background: {$color1}; background: url(../images/backgroundImages/{$bgImgSelect}), linear-gradient(to right, {$color1} 0%, {$color4} 100%); }';
} else {
    $new_css .= ' #f_row { background: {$color1}; }';
}
$new_css .= '#f_row > th { background: none; } .th_background{ {*background-color: {$color1};*} } .head { /*background-color:{$color1}; z-index: -1000;*/ } .otstup3 a:hover { color: {$color1}; } .otstup3 a:hover span { color: {$color1}; } .headline a:hover { color: {$color1}; } #mainmenu { background-color: {$color1}; } #mainmenu ul li a.plus { /*background-color: {$color1};*/ } #mainmenu ul li a.plus:hover, #mainmenu ul li a.plus:focus { background-color: {$color1}; } .filters ul li a:hover { background-color: {$color1}; } #submenu ul li a { color: {$color1}; } #bizproc_done { color: {$color1}; } #bizproc_done img { background-color: {$color1}; } /*закругление элементов*/ ';
if ($radiusMenu == 1) {
    $new_css .= ' #mainmenu ul li { background: rgba({$color2rgb}, {$size_opacity}); border-radius: 7px 7px 0 0; } .fields__table img { border-radius: 7px; } #mainmenu ul li a:hover, #mainmenu ul li a:focus { background-color: {$color3}; border-radius: 7px 7px 0 0; } #submenu ul li a:hover, #submenu ul li strong a { border-radius: 7px 7px 0 0; } .filters ul li a { background-color: #fff; border-radius: 7px; color: #000; text-decoration: none; display: block; padding: 3px 8px 4px 8px; margin-top: 3px; } #legend_full { margin: 20px 0 40px; background-color: #fff; border: 1px solid #E1DFDF; border-radius: 7px; padding: 5px 0 10px; } div.display_notification { border-radius: 7px; } .notifySearch { border-radius: 7px; } .one_item_add { border-radius: 7px; } .colred.but a { border-radius: 7px; } a.but2_ul_btn { border-radius: 7px; } .but2_li_first_btn { border-radius: 7px 7px 0 0; } .but2_li_last_btn { border-radius: 0 0 7px 7px; } #mainmenu ul li.active, #mainmenu ul li.active a { background-color: {$color3}; border-radius: 7px 7px 0 0; } .subtabmenu_active{ border-radius: 7px 7px 0 0; } .inner { border-radius: 7px; } .note_group { border-radius: 7px; } ';
} else {
    $new_css .= '#mainmenu ul li { background: rgba({$color2rgb}, {$size_opacity}); } #mainmenu ul li a:hover, #mainmenu ul li a:focus { background-color: {$color3}; } #submenu ul li a:hover, #submenu ul li strong a { font-size: 14px; background-color: #fff; color: #000; padding: 3px 8px 4px 8px; } .filters ul li a { background-color: #fff; color: #000; text-decoration: none; display: block; padding: 3px 8px 4px 8px; margin-top: 3px; } #legend_full { margin: 20px 0 40px; background-color: #fff; border: 1px solid #E1DFDF; padding: 5px 0 10px; } #mainmenu ul li.active, #mainmenu ul li.active a { background-color: {$color3}; } .inner { border-radius: 0; }';
}
$new_css .= '/*самый светлый цвет*/ .top_line { background-color:{$color3}; } .top_line1 { background-color:{$color3}; } .back_tr { background-color: {$color3}; } #mainmenu ul li a.plus { color: {$color3}; font-weight: bold; } #submenu { background-color: {$color3}; } #submenu ul li a.plus:hover { background-color: {$color3}; } .filter { color: {$color3}; } ';
if ($gradient_stat == 1) {
    $new_css .= '.head_bgr { background: url(../images/backgroundImages/{$bgImgSelect}), {$color1}; background: url(../images/backgroundImages/{$bgImgSelect}), linear-gradient(to right, {$color1} 0%, {$color4} 100%); z-index: 5000; }';
} else {
    $new_css .= '.head_bgr { background: url(../images/backgroundImages/{$bgImgSelect}), {$color1}; }';
}
if ($font_select == 'Arial') {
    $new_css .= '.drop_down_icon_search { margin-top: 0px; } .drop_down_icon_search_hover { margin-top: 0px; } .drop_down_icon__searchdisabled { margin-top: 0px; }';
} else {
    $new_css .= ' .drop_down_icon_search { margin-top: -3px; } .drop_down_icon_search_hover { margin-top: -3px; } .drop_down_icon__searchdisabled { margin-top: -3px; } ';
}
$new_css .= 'body { font-family: {$font_select}; } li, td, a, form, p { font-family: {$font_select}; } textarea, input, select { font-family: {$font_select}; } .filter { font-family: {$font_select}; } .button { font-family: {$font_select}; } .datepicker { font-family: {$font_select}; } .datepicker_span { font-family: {$font_select}; } .datepicker_span input { font-family: {$font_select}; } .select_border_btn { font-family: {$font_select}; } .cust_select { font-family: {$font_select}; } .rights_group_headr { font-family: {$font_select}; } #demo_create_acc_btn_inp { font-family: {$font_select}; } div.display_notification { font-family: {$font_select}; } .d_button { font-family: {$font_select}; } .al_button { font-family: {$font_select}; } div#tips_window_controls td { font-family: {$font_select}; } input.superpuper { font-family: {$font_select}; } #wiz a { font-family: {$font_select}; } div#message_window_drafting textarea { font-family: {$font_select}; }';
$test = $new_css;
$test = str_replace('{$add_button_color}',$add_button_color,$test);
$test = str_replace('{$color1}',$color1,$test);
$test = str_replace('{$color2}',$color2,$test);
$test = str_replace('{$color3}',$color3,$test);
$test = str_replace('{$color2rgb}',$color2rgb,$test);
$test = str_replace('{$color3rgb}',$color3rgb,$test);
$test = str_replace('{$color4}',$color4,$test);
$test = str_replace('{$gradient_stat}',$gradient_stat,$test);
$test = str_replace('{$size_opacity}',$size_opacity,$test);
$test = str_replace('{$bgImgSelect}',$bgImgSelect,$test);
$test = str_replace('{$tableBg}',$tableBg,$test);
$test = str_replace('{$radiusMenu}',$radiusMenu,$test);
$test = str_replace('{$font_select}',$font_select,$test);
$new_css = $test;
$cur_css_name = $config['site_path'] . '/cache/cur_scheme_' . $config['css_id'] . '_' . $config['css_id_sheme'] . '.css';
@$cur_css = file_get_contents($cur_css_name);
if ($cur_css != $new_css) {
    // Удаляем текущий файл css
    @unlink($config['site_path'] . '/cache/cur_scheme_' . $config['css_id'] . '_' . $config['css_id_sheme'] . '.css');
    @unlink($config['site_path'] . '/cache/cur_scheme__1.css');

    // Увеличиваем номер последнего обновления
    $config['css_id_sheme']++;
    $sqlQuery = "UPDATE " . $config['table_prefix'] . "config SET value='" . $config['css_id_sheme'] . "' WHERE name='css_id_sheme'";
    $result = sql_query($sqlQuery);
    $smarty->assign('config', $config);

    // Пишем новый файл
    @$f = fopen($config['site_path'] . '/cache/cur_scheme_' . $config['css_id'] . '_' . $config['css_id_sheme'] . '.css',
        'w');
    if ($f) {#000000
        fwrite($f, $new_css);
        fclose($f);
    }
}