This question has been answered by supanick. See the first response.
<?php $tvname = $modx->getOption('tvname', $scriptProperties, ''); $groupLimit = $modx->getOption('groupLimit', $scriptProperties, '0'); $outputvalue = $modx->getOption('value', $scriptProperties, ''); $docid = $modx->getOption('docid', $scriptProperties, (isset($modx->resource) ? $modx->resource->get('id') : 1)); if (!empty($tvname)) { if ($tv = $modx->getObject('modTemplateVar', array('name' => $tvname))) { $outputvalue = empty($outputvalue) ? $tv->renderOutput($docid) : $outputvalue; } } $items = $modx->fromJSON($outputvalue); $output = array(); $groupidx = 1; $count = 1; $total = count($items); foreach($items as $item){ $item['_groupidx'] = $groupidx; $item['_groupopen'] = $groupidx==1? '1' : '0'; $item['_groupclose'] = $count==$total ? '1' : '0'; if (!empty($grouplimit) && $groupidx == $grouplimit){ $item['_groupclose'] = '1'; $groupidx = 0; } $output[]=$item; $groupidx++; $count++; } $properties = $scriptProperties; $properties['value'] = $modx->toJson($output); return $modx->runSnippet('getImageList',$properties); /* [[groupJson? &groupLimit=`5` &tvname=`myMigxTv` &tpl=`imageTpl`]] imageTpl: [[+_groupopen:is=`1`:then=`<div>`:else=``]] <img src="[[+image]]" /> [[+_groupclose:is=`1`:then=`</div>`:else=``]] */
<?php $tvname = $modx->getOption('tvname', $scriptProperties, ''); $tvValue = $modx->getOption('value', $scriptProperties, ''); $groupLimit = $modx->getOption('groupLimit', $scriptProperties, ''); $docid = $modx->getOption('docid', $sciprtProperties, (isset($modx->resource) ? $modx->resource->get('id') : 1)); if (!empty($tvname)) { if ($tv = $modx->getObject('modTemplateVar', array('name' => $tvname))) { $tvValue = empty($tvValue) ? $tv->renderOutput($docid) : $tvValue; } } $images = $modx->fromJSON($tvValue); $output = array(); $groupidx = 1; $count = 1; $total = count($images); foreach ($images as $image) { $image['groupidx'] = $groupidx; $image['groupopen'] = $groupidx==1 ? '1' : '0'; $image['groupclose'] = '0'; //groupclose value wasn't being set correctly - so this is where the main change is if(!empty($groupLimit) && $groupidx == $groupLimit) { $image['groupclose'] = '1'; $groupidx = '0'; } elseif ($count == $total) { $image['groupclose'] = '1'; } $output[] = $image; $groupidx++; $count++; } $properties = $scriptProperties; $properties['value'] = $modx->toJson($output); echo $modx->runSnippet('getImageList', $properties); ?>
$item['_groupclose'] = $count==$total ? '1' : '0'; if (!empty($grouplimit) && $groupidx == $grouplimit){ $item['_groupclose'] = '1'; $groupidx = 0; }
if(!empty($groupLimit) && $groupidx == $groupLimit) { $image['groupclose'] = '1'; $groupidx = '0'; } elseif ($count == $total) { $image['groupclose'] = '1'; }
return $modx->runSnippet('getImageList', $properties);
<?php /** * @description MODx MIGX getImageList wrapper snippet to allow grouping of output items (basically to make columns on the fly) * * @author exside * @licence free beer or do whatever you like with it licence * @credits * - Bruno Perner, the creator of the amazing MIGX addon * - http://forums.modx.com/thread/76778/migx---grouping-or-paging-results * * @compatibility Tested with MODx Revolution 2.2.6pl * * @version 1.0.0pl * @modified 06.04.2013 * * @changelog * - 1.0.0pl * - refactoring code from forum post * * @todo * * @param groups optional int The amount of groups that should be created from the migx grid items * * @usage [[!getImageListGrouped? &tvname=`migx.tv.name` &groups=`2` &tpl=`chunk.name` &docid=`100`]] * in your &tpl chunk you then will have the placeholders * [[+groupidx]] = the group the item belongs to (int) * [[+groupopen]] = equals 1 if the item is the first in it's group, usage like [[+groupopen:is=`1`:then=`<div class="wrapper">`:else=``]] * [[+groupclose]] = equals 1 if the item is the last in it's group, usage like [[+groupclose:is=`1`:then=`</div>`:else=``]] * * @package migx **/ // packageparams $version = '1.0.0pl'; $packagename = 'getImageListGrouped'; // enable debugging via &debug=`1` in the snippet params or directly by setting it to true here if ( $debug = isset($debug) ? $debug : false ) { $modx->setLogLevel(modX::LOG_LEVEL_INFO); } // addon options/settings that can be set via snippet params and make them accessible with $settings['param'] $settings =& $scriptProperties; $docid = isset($settings['docid']) ? $settings['docid'] : ( isset($modx->resource) ? $modx->resource->get('id') : 1 ); // Resource id $tvname = isset($settings['tvname']) ? $settings['tvname'] : ''; // name of the migx tv to read $tvvalue = isset($settings['value']) ? $settings['value'] : ''; // if a value is passed in the snippet params, take it $groups = isset($settings['groups']) ? $settings['groups'] : 1; // sets the amount of groups to create if ( !empty($tvname) ) { if ( $tv = $modx->getObject('modTemplateVar', array('name' => $tvname)) ) { $tvvalue = empty($tvvalue) ? $tv->renderOutput($docid) : $tvvalue; } else { if ( $debug ) { $modx->log(modX::LOG_LEVEL_INFO, '[' . $packagename . '] The TV you specified could not be found: ' . $tvname); } } } $items = $modx->fromJSON($tvvalue); $output = array(); $groupidx = 1; $groupcount = 1; $totalcount = 1; $totalitems = count($items); $grouplimit = round($totalitems / $groups); foreach ($items as $item) { $item['groupidx'] = $groupidx; $item['groupopen'] = $groupcount == 1 ? '1' : '0'; $item['groupclose'] = $groupcount == $grouplimit ? '1' : ( $totalcount == $totalitems ? '1' : '0' ); if ( $item['groupclose'] ) { ++$groupidx; $groupcount = 1; ++$totalcount; } else { ++$groupcount; ++$totalcount; } $output[] = $item; } $properties = $scriptProperties; $properties['value'] = $modx->toJson($output); // disable debugging if ( $debug ) { $modx->setLogLevel(modX::LOG_LEVEL_ERROR); } return $modx->runSnippet('getImageList', $properties);
hmm... I can't see any functional differencies by looking to the code between
$item['_groupclose'] = $count==$total ? '1' : '0'; if (!empty($grouplimit) && $groupidx == $grouplimit){ $item['_groupclose'] = '1'; $groupidx = 0; }
(...)