Created
December 14, 2017 19:17
-
-
Save Pathologic/a3b8bf29329f6d80f8c6bd4d88e7a630 to your computer and use it in GitHub Desktop.
Группировка документов по букве
This file contains hidden or 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 | |
/* Пример вызова: | |
[!GroupByLetter? | |
&parents=`24` | |
&depth=`3` | |
&showParent=`0` | |
&tpl=`@CODE:<a href="[+url+]">[+pagetitle+]</a><br>` | |
&wrapTpl=`@CODE:<h1>[+letter+]</h1>[+wrap+]` | |
&ownerTPL=`@CODE:[+dl.wrap+]` | |
!] | |
*/ | |
$tpl = isset($tpl) ? $tpl : ''; | |
$field = isset($field) ? $field : 'pagetitle'; //откуда брать буквы | |
$wrapTpl = isset($wrapTpl) ? $wrapTpl : '@CODE:[+letter+] [+wrap+]'; //вывод одной буквы | |
$ownerTPL = isset($ownerTPL) ? $ownerTPL : '@CODE:[+dl.wrap+]'; //вывод всего | |
$params['saveDLObject'] = '_DL'; | |
$params['tpl'] = ''; | |
$data = array(); | |
$out = ''; | |
$modx->runSnippet('DocLister', $params); | |
$DLTemplate = DLTemplate::getInstance($modx); | |
$DL = $modx->getPlaceholder('_DL'); | |
foreach ($DL->docsCollection() as $doc) { | |
if (isset($doc[$field])) { | |
//тут добавляем дополнительные всякие данные, если нужно | |
$doc['url'] = $modx->makeUrl($doc['id']); | |
$letter = mb_strtoupper(mb_substr($doc[$field],0,1)); | |
$data[$letter][] = $doc; | |
} | |
} | |
if (!empty($data)) { | |
ksort($data); | |
foreach ($data as $letter => $docs) { | |
$wrap = ''; | |
foreach ($docs as $doc) { | |
$wrap .= $DLTemplate->parseChunk($tpl, $doc); | |
} | |
$out .= $DLTemplate->parseChunk($wrapTpl, array('letter' => $letter, 'wrap' => $wrap)); | |
} | |
$out = $DLTemplate->parseChunk($ownerTPL, array('dl.wrap' => $out)); | |
} | |
return $out; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment