Skip to content

Instantly share code, notes, and snippets.

@pmacswebteam
Created September 22, 2021 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmacswebteam/98bd059bf759242d0c1fe4a77879c5bc to your computer and use it in GitHub Desktop.
Save pmacswebteam/98bd059bf759242d0c1fe4a77879c5bc to your computer and use it in GitHub Desktop.
For MODX CMS, produces an A to Z listing of selected resources using pdoTools
<?php
/**
* pdoAtoZ
*
* DESCRIPTION
*
* Produces an A to Z listing of selected resources using pdoTools
* See https://www.med.upenn.edu/modx-blog/a-to-z-list.html for more info
*
* REQUIRES pdoTools PACKAGE
*
* SELECTION PROPERTIES:
*
* available pdoTools properties
* (see pdoResources doc for more info: https://docs.modx.pro/en/components/pdotools/snippets/pdoresources)
*
* &parents integer optional. Default: 0
* &select string optional. Default: ''
* &includeTVs string optional. Default: ''
* &processTVs string optional. Default: ''
* &limit integer optional. Default: 10
* &sortby string optional. Default: 'pagetitle'
*
* TEMPLATE PROPERTIES:
*
* &itemTpl string, the name of the chunk to use as a template for each resource in the list, required.
*
* USAGE:
*
* Create a chunk to use for &itemTpl, a-to-z-tpl, for example:
*
* <p>
<a target="_blank" href="[[~[[+id]]]]">[[+pagetitle]]</a>
<br>
[[+introtext:!empty=`[[+introtext:nl2br]]<br>`]]
<a href="[[~[[+id]]]]">More Info</a>
</p>
*
* [[pdoAtoZ?
&select=`{"modResource":"id,pagetitle,introtext"}`
&itemTpl=`a-to-z-tpl`
&limit=`0`
]]
*
*/
$parents = $modx->getOption('parents', $scriptProperties, 0);
$select = $modx->getOption('select', $scriptProperties, 0);
$includeTVs = $modx->getOption('includeTVs', $scriptProperties, '');
$processTVs = $modx->getOption('processTVs', $scriptProperties, '');
$limit = $modx->getOption('limit', $scriptProperties, '10');
$sortby = $modx->getOption('sortby', $scriptProperties, 'pagetitle');
$itemTpl = $modx->getOption('itemTpl', $scriptProperties, false);
if (!$itemTpl) return;
$pdo = $modx->getService('pdoFetch');
//Get collection of resources
$items = $pdo->getCollection('modResource', array(
'published' => true,
'deleted' => false
), array(
'parents' => $parents,
'select' => $select,
'includeTVs' => $includeTVs,
'processTVs' => $processTVs,
'limit' => $limit,
'sortby' => $sortby,
'sortdir' => $sortdir,
));
//create an array keyed with alphabet letters
$alphas = range('A', 'Z');
$alphaGroups = array();
foreach($alphas as $key=>$value){
$alphaGroups[$value] = array();
}
// organize the resources by first letter
foreach ($items as $item) {
$firstLetter = strtoupper(substr ( $item['pagetitle'] , 0, 1));
$item['url'] = $modx->makeUrl($item['id']);
// check to see if first letter is a letter, if not don't do anything with it
if (!preg_match('/[A-Z]/', $firstLetter)) continue;
array_push($alphaGroups[$firstLetter], $item);
}
$alphaIndex = array();
$output = '';
foreach ($alphaGroups as $alpha => $alphaGroup) {
if (count($alphaGroup) == 0) {
array_push($alphaIndex, $alpha);
} else {
array_push($alphaIndex, '<a href="a-to-z-list.html#' . $alpha . '">' . $alpha . '</a>');
$formattedGroup = '';
$formattedGroup .= '<h3 id="' . $alpha . '">' . $alpha . '</h3>';
foreach ($alphaGroup as $item) {
$formattedGroup .= $pdo->getChunk($itemTpl, $item);
// uncomment the following line to see the item printed out in json format
//$formattedGroup .= json_encode($item, JSON_PRETTY_PRINT);
}
$output .= '<hr>' . $formattedGroup;
}
}
//return $alphaIndex + this list of resources grouped by letter;
return '<p class="alpha-index">' . implode(' | ', $alphaIndex) . '</p>' . $output;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment