Last active
July 11, 2017 14:32
-
-
Save ezersky/101817dfd936dfefd12609ebb8d56358 to your computer and use it in GitHub Desktop.
alphabetically
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
$pdo = $modx->getService('pdoFetch'); | |
$pdo->addTime('pdoTools загружен'); | |
$class = 'modResource'; | |
if (empty($outputSeparator)) {$outputSeparator = "\n";} | |
if (empty($colLetter)) {$colLetter = 1;} | |
if (isset($parents) && $parents === '') { | |
$parents = $modx->resource->id; | |
} | |
$parents = explode(',', $parents); | |
// where | |
$where = array(); | |
$where['modResource.parent:IN'] = $parents; | |
// join | |
$leftJoin = array( | |
); | |
// select | |
$select = array( | |
'modResource' => $modx->getSelectColumns('modResource', 'modResource') . ', SUBSTRING(modResource.pagetitle,1,1) as letter' | |
); | |
// Add custom parameters | |
foreach (array('where') as $v) { | |
if (!empty($scriptProperties[$v])) { | |
$tmp = $modx->fromJSON($scriptProperties[$v]); | |
//PHP 7.1 | |
if (!is_array($$v)) { $$v = array();} | |
// | |
if (is_array($tmp)) { | |
$$v = array_merge($$v, $tmp); | |
} | |
} | |
unset($scriptProperties[$v]); | |
} | |
$default = array( | |
'class' => $class, | |
'leftJoin' => $modx->toJSON($leftJoin), | |
'where' => $modx->toJSON($where), | |
'select' => $modx->toJSON($select), | |
'groupby' => $class.'.id', | |
'sortby' => 'letter', | |
'sortdir' => 'ASC', | |
'fastMode' => false, | |
'return' => !empty($returnIds) ? 'ids' : 'data', | |
'nestedChunkPrefix' => 'alpha_', | |
'disableConditions' => true, | |
); | |
// Merge all properties and run! | |
$pdo->addTime('Query parameters ready'); | |
$pdo->setConfig(array_merge($default, $scriptProperties), false); | |
$rows = $pdo->run(); | |
//echo "<pre>"; | |
//print_r($rows);die; | |
$letters = $letter = $output = ''; | |
if (!empty($rows) && is_array($rows)) { | |
foreach ($rows as $k => $row) { | |
$row['idx'] = $pdo->idx++; | |
$letters[$row['letter']]['letter'] = $row['letter']; | |
$letters[$row['letter']]['letters'][] = empty($tplLetters) | |
? $pdo->getChunk('', $row) | |
: $pdo->getChunk($tplLetters, $row, $pdo->config['fastMode']); | |
} | |
$pdo->idx = 1; | |
foreach ($letters as $k => $row) { | |
$row['idx'] = $pdo->idx++; | |
$row['letters'] = is_array($row['letters']) ? implode($outputSeparator, $row['letters']) : $row['letters']; | |
$letter .= empty($tplLetters) | |
? $pdo->getChunk('', $row) | |
: $pdo->getChunk($tplLetter, $row, $pdo->config['fastMode']); | |
if ((($row['idx'] % $colLetter) == 0) && !empty($tplLetterWrap)) { | |
$output .= $pdo->getChunk($tplLetterWrap, array('output' => $letter), $pdo->config['fastMode']); | |
$letter = ''; | |
} | |
} | |
} | |
else { | |
$output = empty($tplEmpty) | |
? $pdo->getChunk('', array()) | |
: $pdo->getChunk($tplEmpty, array(), $pdo->config['fastMode']); | |
} | |
// log | |
if ($modx->user->hasSessionContext('mgr') && !empty($showLog)) { | |
$log .= '<pre class="Log">' . print_r($pdo->getTime(), 1) . '</pre>'; | |
$output .= $log; | |
} | |
// Return output | |
if (!empty($returnIds)) { | |
$modx->setPlaceholder('alpha.log', $log); | |
return $output; | |
} | |
else { | |
$output .= $log; | |
if (!empty($tplWrapper) && (!empty($wrapIfEmpty) || !empty($output))) { | |
$output = $pdo->getChunk($tplWrapper, array('output' => $output), $pdo->config['fastMode']); | |
} | |
if (!empty($toPlaceholder)) { | |
$modx->setPlaceholder($toPlaceholder, $output); | |
} | |
else { | |
return $output; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment