Help with snippet pdoAtoZ

This is a very nice snippet that outputs an A to Z list of resources. The benefit over SiteAtoZ is the use of templates for output and less html/css coded into the php.

See: Creating a Fast Loading A to Z List of Pages in MODX | PMACS Web Team MODX Blog | Perelman School of Medicine at the University of Pennsylvania

The problem with the snippet is they hard coded the page name in the php. I would like to explore making the snippet usable on any modx resource. Here is the section of the code that needs to be changed:

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;
	}
}

This is the line that needs editing:

array_push($alphaIndex, '<a href="a-to-z-list.html#' . $alpha . '">' . $alpha . '</a>');

I used

array_push($alphaIndex, '<a href="[[~[[*id]] ]]#' . $alpha . '">' . $alpha . '</a>');

Anybody have suggestions to change the this code and improve the code. Is there a better replacement for the link template tag.

Thanks

Roy

2 Likes

You could also use the function makeUrl:

$modx->makeUrl($modx->resource->get('id'))

Or if you want to be able to set the id as a snippet property, then something like this:

//[[pdoAtoZ? &id=`12` ...]]
$id = $modx->getOption('id', $scriptProperties, $modx->resource->get('id'));
$url = $modx->makeUrl($id);
1 Like

You might give this a try:

$page = "";
array_push($alphaIndex, '<a href="{$page}#' . $alpha . '">' . $alpha . '</a>');  

/* existing code */
return $output;

That should display the AtoZ list on any page with the snippet tag.

FWIW, as long as you’re messing with the code, I’d replace the line above with this:

$alphaIndex[] =  '<a href="{$page}#' . $alpha . '">' . $alpha . '</a>';  

It’s shorter code, and should be about 50% faster than calling a function over and over.

2 Likes

Thank you Bobray and HalftrainedHarry. Great suggestions. I hope to make some changes to this snippet. I am testing out your suggested modifications for my current project.

My long term goal is to create output as follows

    ... list of links by letter ...

Letter top link>

<ul class="list-Class">
          ... list of item displayed by template ...
<ul>

Above group repeated for each letter group

I have asked the Univ of Penn people If I may modify and share with MODX community.

Roy

1 Like

can you help me for cyrillic alphabet ?

In the code replace the line 75

$alphas = range('A', 'Z');

with

$alphas = array('А','Б','В','Г','Д','Е','Ё','Ж','З','И','Й','К','Л','М','Н','О','П','Р','С','Т','У','Ф','Х','Ц','Ч','Ш','Щ','Ъ','Ы','Ь','Э','Ю','Я');

(Maybe remove 'Ъ','Ы','Ь', from the list)


Also replace lines 83-86

$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;

with

$firstLetter = mb_strtoupper(mb_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('/[А-ЯЁ]/u', $firstLetter)) continue;