Last active
October 4, 2017 13:29
-
-
Save eonarik/649cbce7a6a2cb39859923e79072bb8f to your computer and use it in GitHub Desktop.
modx snippet link
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 | |
// v.1.1.0 | |
/** | |
* string link(id = $modx->resource->id [, get, implode, rm, type, request_uri]) - генерация произвольного URL с параметрами GET | |
* @param id - ид ресурса, для которого генерируется урл, по умолчанию, текущий ресурс | |
* @param type = full, необязательно | |
* @param get - список GET параметров в виде json строки | |
* @param merge = false|1|2 - следует ли объединять дополнительные GET с текущими, 1 - с перезаписью, 2 - добавляет дополнительный элемент массива | |
* @param rm - удалить ненужный GET параметр из строки. может быть использовано несколько параметров через запятую, либо json если понадобится удалить конкретное значение | |
* @param request_uri = false|true - флаг, вместо makeUrl использовать SERVER[REQUEST_URI] | |
**/ | |
$type = $modx->getOption('type', $scriptProperties, ''); | |
$id = $modx->getOption('id', $scriptProperties, $modx->resource->id); | |
$get = $modx->getOption('get', $scriptProperties, false); | |
$merge = $modx->getOption('merge', $scriptProperties, false); | |
$rm = $modx->getOption('rm', $scriptProperties, false); | |
$request_uri = $modx->getOption('request_uri', $scriptProperties, false); | |
$get_url = $_GET; | |
unset($get_url['id']); | |
unset($get_url['q']); | |
unset($get_url['PHPSESSID']); | |
if ($get) | |
{ | |
if (!is_array($get)) | |
{ | |
$get = json_decode($get, true); | |
} | |
if (!empty($get_url) && $merge) | |
{ | |
switch ($merge) | |
{ | |
case 1: | |
$get_url = array_merge($get_url, $get); | |
break; | |
case 2: | |
foreach ($get_url as $key => $value) | |
{ | |
foreach ($get as $k => $it) | |
{ | |
if (is_array($it)) | |
{ | |
$get_url[$key] = array_merge($get_url[$key], $it); | |
} | |
else | |
{ | |
$get_url[$key][] = $it; | |
} | |
} | |
} | |
break; | |
default: | |
} | |
} | |
else | |
{ | |
$get_url = $get; | |
} | |
} | |
if ($rm) | |
{ | |
if (!is_array($rm)) | |
{ | |
if (strpos($rm, '{') === 0) | |
{ | |
$rm = json_decode($rm, true); | |
foreach ($get_url as $k => & $it) | |
{ | |
foreach ($rm as $itm) | |
{ | |
$it = array_diff($it, $itm); | |
} | |
} | |
} | |
else | |
{ | |
foreach (explode(',', $rm) as $it) | |
{ | |
unset($get_url[$it]); | |
} | |
} | |
} | |
} | |
if ($request_uri) | |
{ | |
$output = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) .'?'. http_build_query($get_url); | |
} | |
else | |
{ | |
if (!empty($get_url)) | |
{ | |
$output = $modx->makeUrl($id, null, $get_url, $type); | |
} else { | |
$output = $modx->makeUrl($id); | |
} | |
} | |
return $output; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment