Last active
April 17, 2018 04:31
-
-
Save eonarik/1a6a0359742700e9b194a2ad706166b6 to your computer and use it in GitHub Desktop.
modx snippet migxParse
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 | |
/** | |
* snippet migxParse v.1.1.0 | |
* &input - входящая JSON строка, обязательный параметр | |
* &processFields - добавляет источники файлов для полей | |
* (ввод через запятую, двойной знак равно. первый параметр - поле, второй - название источника файлов). например src==Images,img==Images | |
* &tpl - шаблон строкового представления, для @INLINE (и для специального синтаксиса тегов {{+field:phx=^params^}} ) | |
* &where - условие в JSON формате, {"active":1} | |
* &limit - лимит | |
* &one - если нужно получить одно поле, ввод названия поля, параметр tpl не нужен (&one=`image`) | |
* &num - если нужно получить одно поле конкретной строки, работает в паре с one (&one=`image`&num=`2`), специальное значение rand - случайная строка | |
* &debug | |
**/ | |
if (!empty($debug)) | |
{ | |
ini_set('display_errors', true); | |
} | |
if (!$input) { | |
return; | |
} | |
$processFields = $modx->getOption('processFields', $scriptProperties, false); | |
$where = $modx->getOption('where', $scriptProperties, ''); | |
$limit = $modx->getOption('limit', $scriptProperties, 0); | |
$tpl = str_replace(array('^', '{{', '}}'), array('`', '[[', ']]'), $tpl); | |
$out = array(); | |
if (!function_exists('setMediaSource')) | |
{ | |
function setMediaSource($value, $source) | |
{ | |
global $modx; | |
if ( | |
strpos($value, '/') !== 0 | |
&& strpos($value, 'http') !== 0 | |
) | |
{ | |
if ($source = $modx->getObject('modFileMediaSource', array('name' => $source))) | |
{ | |
$source = $source->toArray(); | |
$source = $source['properties']['baseUrl']['value']; | |
$source = str_replace( | |
array('[[++assets_url]]') | |
, array($modx->getOption('assets_url')) | |
, $source | |
); | |
return $source . $value; | |
} | |
return $value; | |
} | |
return $value; | |
} | |
} | |
if (!is_array($input)) | |
{ | |
$input = $modx->fromJSON($input); | |
} | |
if (!empty($where) && !is_array($where)) | |
{ | |
$where = $modx->fromJSON($where); | |
} | |
if (!empty($processFields)) | |
{ | |
$arMediaFields = array(); | |
foreach (explode(',', $processFields) as $f) | |
{ | |
list($f1, $f2) = explode('==', $f); | |
$arMediaFields[$f1] = $f2; | |
} | |
unset($processFields); | |
} | |
if (!empty($one)) | |
{ | |
$in = ''; | |
if ($num) | |
{ | |
if ($num == 'rand') | |
{ | |
$num = rand(0, count($input) - 1); | |
} | |
$in = $input[intval($num) % count($input)][$one]; | |
} | |
else | |
{ | |
$in = current($input); | |
$in = $in[$one]; | |
} | |
if (!empty($arMediaFields) && !empty($arMediaFields[$one])) | |
{ | |
$in = setMediaSource($in, $arMediaFields[$one]); | |
} | |
$out[] = $in; | |
} | |
else | |
{ | |
$i = 0; | |
if (preg_match('/^@INLINE/i', $tpl)) | |
{ | |
$tpl = preg_replace('/^@INLINE/i', '', $tpl); | |
$chunk = $modx->newObject('modChunk'); | |
$chunk->set('snippet', $tpl); | |
} | |
else | |
{ | |
$chunk = $modx->getObject('modChunk', array('name' => $tpl)); | |
} | |
$chunk->setCacheable(false); | |
foreach ($input as $in) | |
{ | |
if (!empty($where)) | |
{ | |
$flag = 1; | |
foreach ($in as $k => $it) | |
{ | |
if ($where[$k] && $where[$k] != $it) | |
{ | |
$flag = 0; | |
break; | |
} | |
} | |
if (!$flag) | |
{ | |
continue; | |
} | |
} | |
if (!empty($arMediaFields)) | |
{ | |
foreach ($arMediaFields as $k => $source) | |
{ | |
if (!empty($in[$k])) | |
{ | |
$in[$k] = setMediaSource($in[$k], $source); | |
} | |
} | |
} | |
$in['idx'] = $i; | |
$out[] = $chunk->process($in); | |
$i++; | |
if ($limit > 0 && $i == $limit) { | |
break; | |
} | |
} | |
} | |
if (!empty($debug)) | |
{ | |
print '<pre>'; | |
print_r($out); | |
print '</pre>'; | |
} | |
else | |
{ | |
return implode("\n", $out); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment