Last active
April 25, 2023 12:08
-
-
Save viplexa/721ba928ab9600a06c3aa44fe8c32c16 to your computer and use it in GitHub Desktop.
MODX Revolution: Сниппет для разбивки элементов на группы для pdoResources
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 | |
/* | |
* Сниппет для разбивки элементов на группы для 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