<?php
// The Resource ID to test
$id = (int) $modx->getOption('id', $scriptProperties, $modx->resource->get('id'), true);
// Option to return early if Current Resource matches $id
$matchCurrent = $modx->getOption('matchCurrent', $scriptProperties, true);
// IDs of Resources deemed as "sections", against which to test
$sectionIds = array_filter(array_map('trim', explode(',', $modx->getOption('sectionIds', $scriptProperties, ''))));
// IDs of parent Resources, the direct children of which will be added to $sectionIds
$sectionParentIds = array_filter(array_map('trim', explode(',', $modx->getOption('sectionParentIds', $scriptProperties, ''))));
// If the test passes, meaning the Resource has a $sectionId as an ancestor, output this
$inSectionOutput = $modx->getOption('inSectionOutput', $scriptProperties, 'active');
// Otherwise output this
$defaultOutput = $modx->getOption('defaultOutput', $scriptProperties, '');
// Handy
$toPlaceholder = $modx->getOption('toPlaceholder', $scriptProperties, '');
// Current Resource
$current = (int) $modx->resource->get('id');
// Return early if matching enabled and matched
if ($matchCurrent && ($current === $id)) return $inSectionOutput;
// Loop through sectionParentIds
foreach ($sectionParentIds as $spi) {
$sectionIds = array_merge($sectionIds, $modx->getChildIds($spi, 1));
}
$sectionIds = array_unique($sectionIds);
// We need the parents of the current Resource
$pids = $modx->getParentIds($current);
// Store interecting values between the parent IDs and section IDs
$matches = array_intersect($sectionIds, $pids);
//var_dump($id, $current, $pids, $sections, $matches);
// If ID to test is in the array of the intersection of sectionIds and parentIds, we're a match
// Set output value
$output = (in_array($id, $matches)) ? $inSectionOutput : $defaultOutput;
// Output
if (empty($toPlaceholder)) return $output;
$modx->setPlaceholder($toPlaceholder, $output);