pepebe
7/3/2013 - 4:24 AM

MODX Snippet: Insert a header on top of each new month and year.

MODX Snippet: Insert a header on top of each new month and year.

[[!getResources?
  &comment=`Get a list of resources sorted by a date TV`
  &parents=`[[*id]]` 
  &tpl=`events_getResources_tpl`
  &includeTVs=`1` &processTVs=`1` &tvPrefix=`` &tvPrefix=``
  &includeContent=`1`
  &limit=`999`
  &sortbyTV=`event_start`
  &sortdirTV=`ASC`
]]
<?php
/*
v.0.1
info@pepebe.de

Insert a header on top of each new month and year.

2013
Januar
...
Februar
...
March
...

Use it at the beginning of a getResources tpl chunk:

<div class="event">
  [[!eventHeader? &date=`[[+event_start:strtotime:date=`%d.%m.%Y`]]`]]
  [[+event_start:strtotime:date=`%d.%m.%Y`]]<br/>
  [[+pagetitle]]<br/>
  [[+content]]
</div>
<!-- /.event -->

2do: Make snippet more customizable. Use placeholders and chunk for output.

*/

$months = array(
   '01' => 'Januar'
	,'02' => 'Februar'
	,'03' => 'März'
	,'04' => 'April'
	,'05' => 'Mai'
	,'06' => 'Juni'
	,'07' => 'Juli'
	,'08' => 'August'
	,'09' => 'September'
	,'10' => 'Oktober'
	,'11' => 'November'
	,'12' => 'Dezember'
);

$output = "";

$date = explode(".",$date);
$d = $date[0];
$m = $date[1];
$y = $date[2];

$ph_d = $modx->getPlaceholder('d');
$ph_m = $modx->getPlaceholder('m');
$ph_y = $modx->getPlaceholder('y');

if($d > $ph_d OR $m > $ph_m OR $y > $ph_y){
	$modx->setPlaceholder('d',$d);
	$new_d = true;
}

if($m > $ph_m OR $y > $ph_y){
	$modx->setPlaceholder('m',$m);
	$new_m = true;
}

if($y > $ph_y){
	$modx->setPlaceholder('y',$y);
	$new_y = true;
}

if($new_y === true){
	$output .= "<h2 class='year'>" . $y . "</h2>";
}
if($new_m === true){
	$output .= "<h3 class='month'>" . $months[$m] . "</h3>";
}

return $output;