Сниппет для вывода ресурсов кириллицей в алфавитном порядке с разбивкой на 3 колонки

может кому пригодится немного модифицированный pdoAtoZ

вызов сниппета

$_modx->runSnippet("@FILE snippets/pdoatoz.php", [
'select' => 'id,pagetitle,uri,parent',
'limit' => '0',
'depth' => $cat_depth,
'parents' => $parents,
'tpl' => '@INLINE <a href="{$uri}" class="abc-item-list">{$pagetitle}</a>',
'tplDelimiter' => '</div><div class="col-lg-4 col-md-12 col-sm-12 col-sx-12">',
'ClassRow' => 'row col-12 abc-list pb-5',
'ClassCol' => 'col-lg-4 col-md-12 col-sm-12 col-sx-12',
'ClassIndex' => 'abc-index text-center',
'indexDelimiter' => ' '
])

сам сниппет

<?php
/**
 * pdoAtoZ
 */
 
$parents = $modx->getOption('parents', $scriptProperties, 0);
$select = $modx->getOption('select', $scriptProperties, 0);
$includeTVs = $modx->getOption('includeTVs', $scriptProperties, '');
$processTVs = $modx->getOption('processTVs', $scriptProperties, '');
$limit = $modx->getOption('limit', $scriptProperties, '10');
$depth = $modx->getOption('depth', $scriptProperties, '10');
$sortby = $modx->getOption('sortby', $scriptProperties, 'pagetitle');
$itemTpl = $modx->getOption('tpl', $scriptProperties, false);
$ClassRow = $modx->getOption('ClassRow', $scriptProperties, 'row');
$ClassCol = $modx->getOption('ClassCol', $scriptProperties, 'col');
$ClassIndex = $modx->getOption('ClassIndex', $scriptProperties, 'alpha-index');
$tplDelimiter = $modx->getOption('tplDelimiter', $scriptProperties, '');
$indexDelimiter = $modx->getOption('indexDelimiter', $scriptProperties, ' | ');

if (!$itemTpl) return;

$pdo = $modx->getService('pdoFetch');

//Get collection of resources
$items = $pdo->getCollection('modResource', array(
    'published' => true,
    'deleted' => false
), array(
    'parents' => $parents,
    'select' => $select,
    'includeTVs' => $includeTVs,
    'processTVs' => $processTVs,
    'limit' => $limit,
    'sortby' => $sortby,
    'sortdir' => $sortdir,
    'depth' => $depth
));

$total=count($items);
$column2=intval($total/3);
$column3=intval($total/3*2);
$counter=0;

//create an array keyed with alphabet letters

$alphas = array();
foreach (range(chr(0xC0),chr(0xDF)) as $v)
  $alphas[$v] = iconv('CP1251','UTF-8',$v);

$alphaGroups = array();
foreach($alphas as $key=>$value){
    $alphaGroups[$value] = array();
}

// organize the resources by first letter
foreach ($items as $item) {
$firstLetter = mb_strtoupper(mb_substr($item['pagetitle'], 0, 1));
$item['url'] = $modx->makeUrl($item['id']);
// check to see if first letter is a letter, if not don't do anything with it
if (!preg_match('/[А-ЯЁ]/u', $firstLetter)) continue;

    array_push($alphaGroups[$firstLetter], $item);
}

$alphaIndex = array();
$output = '';

$id = $modx->getOption('id', $scriptProperties, $modx->resource->get('id'));
$url = $modx->makeUrl($id);

foreach ($alphaGroups as $alpha => $alphaGroup) {
    if (count($alphaGroup) == 0) {
        
    } else {

        array_push($alphaIndex, '<a href="' . $url . '#' . $alpha . '">' . $alpha . '</a>');
        $formattedGroup = '';
        // to separate collumns    
        if ($counter >= $column2 && $col2 != 1 && $col2 != 1) { $formattedGroup .= $tplDelimiter; $col2 = 1;} 
        if ($counter >= $column3 && $col2 == 1 && $col3 != 1) { $formattedGroup .= $tplDelimiter; $col3 = 1;}
        $formattedGroup .= '<h3 id="' . $alpha . '" class="">' . $alpha . '</h3>'; 
        foreach ($alphaGroup as $item) {
            $formattedGroup .= $pdo->getChunk($itemTpl, $item);
            // uncomment the following line to see the item printed out in json format
            //$formattedGroup .= json_encode($item, JSON_PRETTY_PRINT);
        }
        $counter = $counter + count($alphaGroup);
        $output .= $formattedGroup;
    }
}

//return $alphaIndex + this list of resources grouped by letter;
return '<div class="'. $ClassIndex .'">' . implode($indexDelimiter, $alphaIndex) . '</div>' . '<div class="' . $ClassRow . '"><div class="' . $ClassCol . '">'. $output .'</div></div>';
SYAN
19 января 2024, 17:51
modx.pro
1
319
0

Комментарии: 3

Артур Шевченко
19 января 2024, 18:34
0
А почему бы просто не использовать pdoResources с сортировкой по pagetitle? Колонки можно сделать с помощью CSS.
    SYAN
    21 января 2024, 16:15
    0
    Это сниппет для создания списка с алфавитным указателем
    SYAN
    22 января 2024, 13:49
    0
    стоит добавить в сниппете 'isfolder' => 0
    $items = $pdo->getCollection('modResource', array(
        'published' => true,
        'deleted' => false,
        'isfolder' => 0
      Авторизуйтесь или зарегистрируйтесь, чтобы оставлять комментарии.
      3