MODX RevoТикеты по алфавиту в MODX Revo

26 января, 2017
1
Вывод тикетов по алфавиту в MODX Revo

Нужен вывод заголовков материалов по принципу словаря?

Пожалуйста!

Сниппет alpha.php

<?php
$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 = array('published'=>'1','deleted'=>'0');
$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]);
        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();
 
$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;
    }
}

Вызов

      {$_modx->runSnippet('@FILE PATH_TO_THEME_ELEMENTS/snippets/alpha.php', [
        'element' => 'getTickets',
        'parents' => $_modx->resource.id,
        'limit' => 0,
        'sortby' => 'pagetitle',
        'sortdir' => 'ASC',
        'tplLetter' => '@INLINE <h3>{$letter}</h3><div class="multiColumn">{$letters}</div>',
        'tplLetters' => '@INLINE <div class="term"><a href="/{$uri}">{$pagetitle}</a></div>',
        'tplLetterWrap' => '@INLINE <div class="terms clearfix">{$output}</div>',
        'colLetter' => 1,
        'showLog' => 0,
      ])}

Найдено в сети, автор не я.

Проверено, работает.

Поделиться