Skip to content

Instantly share code, notes, and snippets.

@viplexa
Last active April 25, 2023 12:08
Show Gist options
  • Save viplexa/721ba928ab9600a06c3aa44fe8c32c16 to your computer and use it in GitHub Desktop.
Save viplexa/721ba928ab9600a06c3aa44fe8c32c16 to your computer and use it in GitHub Desktop.
MODX Revolution: Сниппет для разбивки элементов на группы для pdoResources
<?php
/*
* Сниппет для разбивки элементов на группы для pdoResources
* Автор: Алексей Митин, viplexa@gmail.com
*/
// Количество элементов в группе, по умолчанию 2
$numberInGroup = $numberInGroup ? $numberInGroup : 2;
// Snippet для обработки
$snippet = $snippet ? $snippet : 'pdoResources';
// Class для обработки
$class = $class ? $class : 'modResource';
// Чанк обертка
if (empty($tplWrapper)) return;
// Чанк элемента
if (empty($tpl)) return;
$scriptProperties['returnIds'] = 1;
$pdo = $modx->getService('pdoTools');
$output = '';
$itemGroupTpl = '';
$itemCount = 0;
$ids = $modx->runSnippet($snippet, $scriptProperties);
$ids = explode(",", $ids);
if ($items = $modx->getCollection($class, ["id:IN" => $ids]))
{
$allCount = count($items) - 1;
$i=0;
foreach ($items as $item) {
if ($itemCount < $numberInGroup)
{
$itemGroupTpl .= $pdo->getChunk($tpl, array_merge($item->toArray(), ['idx' => $i]));
$itemCount++;
if ($itemCount == $numberInGroup || $allCount == $i) {
$output .= $pdo->getChunk($tplWrapper, ['wrapper' => $itemGroupTpl]);
$itemGroupTpl = '';
$itemCount = 0;
}
}
$i++;
}
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment