Last active
January 22, 2017 18:25
-
-
Save gvozdb/854fc1ec16416a2845af2fcb6be15305 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Плагин для MODX Revolution, предназначен для переключения контекстов в зависимости от домена. | |
* Для работы плагина необходимо в каждом контексте-домене создать настройки: | |
* http_host - domain.ru | |
* site_name - Название сайта | |
* site_start - id ресурса, который является главной страницей | |
* site_url - http://domain.ru/ | |
*/ | |
if ($modx->event->name != 'OnMODXInit' || $modx->context->key == 'mgr'/* || !$modx->getOption('friendly_urls')*/) { | |
return; | |
} | |
// Получаем текущий домен | |
$host = str_replace('www.', '', $_SERVER['HTTP_HOST'] ?: $_SERVER['SERVER_NAME']); | |
if ($host) { | |
// Выбираем контексты с настройкой http_host | |
$q = $modx->newQuery('modContextSetting', array('key' => 'http_host', 'value:!=' => '')); | |
$q->select('context_key as ctx, value as host'); | |
$tstart = microtime(true); | |
if ($q->prepare() && $q->stmt->execute()) { | |
// Учитываем наш запрос в БД | |
$modx->queryTime += microtime(true) - $tstart; | |
$modx->executedQueries++; | |
while ($row = $q->stmt->fetch(PDO::FETCH_ASSOC)) { | |
// Если нашли нужный контекст - переключаем на него | |
if ($row['host'] == $host) { | |
$modx->switchContext($row['ctx']); | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment