РАЗНОЕ / Модальное окно выбора города

Заметка создана: 09 февраля 2025 г.

Показываем модальное окно и сохраняем в сессию сделанный выбор.

Загружаем в корень сайта файл citychng.php

<?php
$bShowPopup = !(isset($_GET['citychng']) || isset($_COOKIE['citychng']));
if (isset($_GET['citychng'])) @setcookie('citychng', 1, time()+18000, '/',  $_SERVER['HTTP_HOST']);

if ($bShowPopup){
    ?>
    <div class="city-popup-overlay">
        <div class="city-popup">
            <div class="city-popup-body">
                <h3>Выберите свой город</h3>
                <div class="cities">
                    <a href="http://unity-school.com<?php
                   $oURL = $_SERVER['REQUEST_URI'];
                   $oURL = $oURL;
                   if ($oURL == '') echo '';
                        else echo $oURL;
                   ?>?citychng">Москва</a>
                   <a href="http://khv.unity-school.com<?php
                   $oURL = $_SERVER['REQUEST_URI'];
                   $oURL = $oURL;
                   if ($oURL == '') echo '';
                       else echo $oURL;
                   ?>?citychng">Хабаровск</a>
                </div>
            </div>
        </div>
    </div>
    <script>
    function close_popup(){
        $('.city-popup-overlay').fadeOut();
        return false;
    }
    $('.city-popup-overlay').bind('click', close_popup);
    $('.city-popup').bind('click', function(event){event.stopPropagation();});

    setTimeout(function(){$('.city-popup-overlay').fadeIn()}, 4000);
    </script>
    <?php
}
?>

Подключаем файл php в макет сайта

<?php require_once(CMS_FOLDER.'citychng.php'); ?>

В файл bootstrap.php в корне сайта добавляем код

//Cities
$cookie_expire = 60 * 60 * 24 * 30; // 30 дней (в секундах)
$cities = array('msk', 'khv');
$city = 'msk';
if (!empty($_GET['city']) && in_array($_GET['city'], $cities))
{
setcookie("city", $_GET['city'], time()+$cookie_expire, '/');
$city = $_GET['city'];
}
else
{
if (isset($_COOKIE['city'])) $city = $_COOKIE['city'];
}
define('MY_CITY', $city);

Стили CSS . Стили для модального окна

/*city popup*/
.city-popup-overlay{position: fixed; top: 0; bottom: 0; left: 0; right: 0; background: rgba(0,0,0,0.5); z-index: 999; display: none;}
.city-popup{position: absolute; left: 50%; width: 300px; margin-left: -150px; box-shadow: 0 0 10px 1px rgba(0,0,0,0.5); background: #008ed8;border-radius: 2px; top: 100px;}
.city-popup-body{padding: 20px 20px 30px; text-align: center;}
.city-popup-body h3{color: #fff;}
.city-popup-body .cities a {border: 1px solid #7c0045; display: inline-block; background: white; font-size: 14px; color: #7c0045; text-decoration: none; padding: 6px 25px; position: relative;}
.cities a:first-child {color: white; background: #d61781; -webkit-border-top-left-radius: 2px; -webkit-border-bottom-left-radius: 2px; -moz-border-radius-topleft: 2px; -moz-border-radius-bottomleft: 2px; border-top-left-radius: 2px; border-bottom-left-radius: 2px; border-right: none; margin-right:-4px;}
.city-popup-body .cities a:last-child {-webkit-border-top-right-radius: 2px; -webkit-border-bottom-right-radius: 2px; -moz-border-radius-topright: 2px; -moz-border-radius-bottomright: 2px; border-top-right-radius: 2px; border-bottom-right-radius: 2px; border-left: none;}
/*~city popup*/