Created
June 4, 2020 22:55
-
-
Save andronex/9d816a4935fcabe70e3776d5024b531d to your computer and use it in GitHub Desktop.
Вывод всех городов региональных поддоменов со ссылками на соответствующий поддомен (с разбивкой по алфавиту)
This file contains 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->runSnippet('@FILE snippets/get_all_cities.php', ['split' => 'А-Б,Г-Д,Е-Ж,З-И,Ш-Щ,Ф-Х'])} | |
* split - объединение букв для вывода городов | |
* шаблон вывода вшит в скрипт (при желании можно сделать параметром) | |
* название настройки контекста, содержащее название города, вшито в скрипт (при желании можно сделать параметром) | |
*/ | |
$query = $modx->newQuery('modContext'); | |
$query->leftJoin('modContextSetting', 'ContextSettings'); | |
$query->where(array( | |
'modContext.key:!=' => 'mgr', | |
'ContextSettings.key' => 'subdomain_region', //название настройки контекста, содержащее название города | |
)); | |
$query->select(array( | |
'modContext.key', | |
'ContextSettings.key as setting', | |
'ContextSettings.value as city' | |
)); | |
$ctxs = $modx->getIterator('modContext', $query); | |
$split = isset($scriptProperties['split'])?$scriptProperties['split']:''; | |
$split = $split?array_map('trim', explode(',', $split)):[]; | |
$tplRowOuter = '<div> | |
<div class="letter">[[+capital]]</div> | |
<ul> | |
[[+out]] | |
</ul> | |
</div>'; | |
$tplRow = '<li><a href="[[+uri]]">[[+name]]</a></li>'; | |
$out = []; | |
foreach($ctxs as $ctx){ | |
$capital = mb_substr($ctx->city,0,1); | |
if(count($split) > 0){ | |
$replace_capital = preg_grep("/[{$capital}]/ui", $split); | |
$replace_capital = array_values($replace_capital); | |
if(isset($replace_capital[0])){ | |
$capital = $replace_capital[0]; | |
} | |
} | |
$out[ $capital ][$ctx->key] = $ctx->city; | |
asort($out[ $capital ]); | |
} | |
ksort($out); | |
$old_context = $modx->context->key; | |
$modx->switchContext('web'); | |
$scheme = $modx->getOption('server_protocol') . '://'; | |
$http_host = $modx->getOption('http_host') . '/'; | |
$modx->switchContext($old_context); | |
if($modx->resource->id != $modx->getOption('site_start')){ | |
$http_host = $http_host . $modx->resource->uri; | |
} | |
$output = ''; | |
foreach($out as $capital => $items){ | |
$inner = ''; | |
foreach($items as $key => $name){ | |
$uri = ($key == 'web')?$scheme . $http_host:$scheme . $key . '.' . $http_host; | |
$inner .= str_replace(['[[+uri]]', '[[+name]]'], [$uri, $name], $tplRow); | |
} | |
$output .= str_replace(['[[+out]]', '[[+capital]]'], [$inner, $capital], $tplRowOuter); | |
} | |
return $output; |
This file contains 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
<!-- полный код модального окна в качестве примера --> | |
<div id="dist-cities" tabindex="-1" role="dialog" aria-labelledby="dist-citiesLabel" aria-hidden="true" class="modal fade"> | |
<div class="modal-dialog modal-lg"> | |
<div class="modal-content"> | |
<div class="modal-header"> | |
<p class="h3 modal-title">Мы работаем в городах России</p> | |
<button class="close" type="button" data-dismiss="modal" aria-label="Закрыть"> | |
<svg> | |
<use xlink:href="img/sprite.svg#cross"></use> | |
</svg> | |
</button> | |
</div> | |
<div class="modal-body"> | |
<div class="dist-cities-columns"> | |
{$_modx->runSnippet('@FILE snippets/get_all_cities.php', ['split' => 'А-Б,Г-Д,Е-Ж,З-И,Ш-Щ,Ф-Х'])} | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment