Skip to content

Instantly share code, notes, and snippets.

@eonarik
Last active November 19, 2016 15:28
Show Gist options
  • Save eonarik/b1139200a72888ab263f88acf93fe986 to your computer and use it in GitHub Desktop.
Save eonarik/b1139200a72888ab263f88acf93fe986 to your computer and use it in GitHub Desktop.
modx snippet migxDBParse
<?php
$out = array();
if(
!empty($loadModels)
&& !empty($class)
){
$loadModels = explode(',',$loadModels);
$class = explode(',',$class);
foreach($loadModels as $i=>$package){
$className = $class[$i];
$modx->addPackage($package,MODX_CORE_PATH . "components/{$package}/model/");
$q = $modx->newQuery($className);
if(!empty($where)){
$q->where(json_decode($where,1));
}
if(!empty($sortby)){
foreach(json_decode($sortby,1) as $sort=>$dir){
$q->sortby($sort,$dir);
}
}
if(!empty($limit)){
$q->limit($limit, !empty($offset) ? $offset : 0);
}
$q->select(array(
"{$className}.*"
));
if($q->prepare() && $q->stmt->execute()){
$result = $q->stmt->fetchAll(2);
$tpl = str_replace(array('{{','}}','^'),array('[[',']]','`'),$tpl);
if(preg_match('/^@INLINE.*/is',$tpl)){
$tpl = trim(str_replace('@INLINE','',$tpl));
if(empty($chunk)){
$chunk = $modx->newObject('modChunk');
$chunk->setCacheable(false);
}
$chunk->set('snippet',$tpl);
foreach($result as $props){
$out[] = $chunk->process($props);
}
} else {
foreach($result as $props){
$out[] = $modx->getChunk($tpl,$props);
}
}
} else {
$modx->log(modX::LOG_LEVEL_ERROR, "migxDBParse: [{$package},{$className}] !prepare or !execute");
}
}
}
return implode("\n",$out);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment