Skip to content

Instantly share code, notes, and snippets.

@gvozdb
Last active January 22, 2017 18:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gvozdb/854fc1ec16416a2845af2fcb6be15305 to your computer and use it in GitHub Desktop.
Save gvozdb/854fc1ec16416a2845af2fcb6be15305 to your computer and use it in GitHub Desktop.
<?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