pepebe
7/25/2013 - 11:29 AM

Merge several MIGX into one JSON block and put the result into a placeholder. Use getImageList to filter and sort the output.

Merge several MIGX into one JSON block and put the result into a placeholder. Use getImageList to filter and sort the output.

<ul class="nav nav-pills nav-stacked">
[[!mergeJSON? 
&parents=`157,224` 
&tvname=`veranstaltungen`
&ph=`json`
]]

[[!getImageList? 
&value=`[[+json]]`
&tpl=`veranstaltungen_uebersicht_tpl`
&tpl_=`veranstaltungen_details_tpl`
&limit=`5`
&where=`{ "veranstaltung_bis:>" : "[[!today? &format=`Y-m-d H:i:s`]]" , "published:is":"1"}`
&sort=`[{ "sortby" : "veranstaltung_von" , "sortdir" : "ASC" }]`
]]
</ul>
<?php

/*
Work in progress. 

The example below (include children of several parents) works. 
Searching for a list of resources should also work, but was NOT tested.

&parents=`1,2,3` A comma seperated list of resources whose children TVs will be combined.
&resources=`1,2,3` A comma seperated list of resources whose TVs are combined.
&tvname=`galery` Name of the TV that will be combined
&ph=`json` Name of the placeholder that will contain the combined json File
*/

$id   = $modx->resource->get('id');
$resources  = !empty($resources) 	? $resources 	: "";
$parents	= !empty($parents) 	? $parents 	: "";
$tvname = !empty($tvname)   ? $tvname 	: "";
$ph     = !empty($ph)       ? $ph 	    : "json";


if(empty($tvname)) return "";

if(!empty($parents)){
	$parents	= explode(',',$parents);
	foreach($parents as $key => $value){
		if(!isset($childIds)){
			$childIds 	= $modx->getChildIds($value,1);
		}
		else{
			$childIds	= array_merge($childIds, $modx->getChildIds($value,1) );
		}
	}

}
elseif(!empty($resources)){
	$childIds	= implode(",", $resources );
}
else{
	$childIds	= implode(",", $id );
}

$i = 0;

if(is_array($childIds)){
  foreach($childIds as $key => $childId){

	  $child 		= $modx->getObject('modResource',array('id'=>$childId));

	  $TVValue 	= $child->getTVValue($tvname);

	  if(!empty($TVValue)){

		  $TVValue 	= $modx->fromJSON($TVValue);

  		  // Add id of current resource to array
		  if(is_array($TVValue)){
			  foreach($TVValue as $key => $array){
				  $TVValue[$key]['docid'] = $childId;
			  }
		  }

		  if(!isset($output)){
			  $output = $TVValue;

		  }
		  elseif(isset($output) AND !empty($TVValue)) {
			  $output = array_merge($output,$TVValue);
		  }
	  }
  }
}

// Update MIGX_id
foreach($output as $key => $value){
	$output[$key]['MIGX_id'] = $i;
	$i++;
}

$output = print_r($modx->toJson($output),true);

$modx->toPlaceholder($ph,$output);

return;