Created
February 28, 2018 00:30
-
-
Save eonarik/8de6376fab439d0bee06740a697e5a2e to your computer and use it in GitHub Desktop.
modx snippet finalParent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (!isset($modx)) return ''; | |
if (!function_exists('getParentIds')) | |
{ | |
function getParentIds($id = 0, $depth = 9, $finalWhere = array('parent' => 0), $alias = 'modResource') | |
{ | |
global $modx; | |
$ids = array(); | |
$curRes = $modx->getObject($alias, $id); | |
$parent = $curRes->parent; | |
if ( | |
$parent | |
&& $parent > 0 | |
&& $depth > 0 | |
&& !$modx->getCount($alias, array_merge(array('id' => $id), $finalWhere)) | |
) | |
{ | |
$ids[] = $parent; | |
$ids = array_merge($ids, getParentIds($parent, $depth--, $finalWhere, $alias)); | |
} | |
return $ids; | |
} | |
} | |
$id = $modx->getOption('id', $scriptProperties, $modx->resource->id); | |
$depth = $modx->getOption('depth', $scriptProperties, 9); | |
$finalWhere = $modx->getOption('finalWhere', $scriptProperties, array('parent' => 0)); | |
if (!is_array($where)) | |
{ | |
$where = json_decode($where, true); | |
} | |
$ids = getParentIds($id, $depth, $finalWhere); | |
if (!count($ids)) | |
{ | |
return 0; | |
} | |
return $ids[count($ids) - 1]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment