MODx pagenator

MODxのスニペットでページネーターを作りました。
Pre Next
というリンクが生成されます。前後がサブディレクリ構造でも追います。
([の後のスペースを取ってください。please remove after '[' space)

<?php
/*
::::::::::::::::::::::::::::::::::::::::
 Snippet name: pagenator
 Short Desc: page Navigation
 Version: 0.5
 Authors:
    Tetsuya Fujieda (tonttu.net)
 Date: 2010-05-01
::::::::::::::::::::::::::::::::::::::::
Snipett Call
[ [pagenator] ]
or
[ [pagenator? &rootid=`6` &notop=1 &topstr=`High` &prestr=`Preview` &prestr=`Nextview` &delimitor=`&nbsp;` ] ]
::::::::::::::::::::::::::::::::::::::::
*/

//Config
$rootid = isset($rootid) ? $rootid : 0; //rootid
$notop = isset($notop) ? $notop : 1; //Disable Top Template
$topstr = isset($topstr) ? $topstr : 'Top'; //Top link text
$prestr = isset($prestr) ? $prestr : 'Pre'; //Preview link text
$nextstr = isset($nextstr) ? $nextstr : 'Next'; //Next link text
$delimitor = isset($delimitor) ? $delimitor : '&nbsp;'; //link between string
//Please Custamaiz HTML
$toptplsrc = "<a href='[ +pgn.topurl+ ]'>[ +pgn.topstr+ ]</a>";
$pretplsrc = "<a href='[ +pgn.preurl+ ]'>[ +pgn.prestr+ ]</a>";
$nexttplsrc = "<a href='[ +pgn.nexturl+ ]'>[ +pgn.nextstr+ ]</a>";

//Do not Edit
$myid = null; //自分のid
$parentid = null; //自分の親のid
$mylebels = null; //自分のレベル
$parentlebels = null; //自分の親のレベル

$myid = $modx->documentIdentifier;
$parent = $modx->getParent( $myid );
if( $parent ) {
  $parentid = $parent['id'];
  $mylebels = $modx->getActiveChildren( $parentid ); //自分のレベル取得
  if( $parentid == $rootid ) {
    $parentid = null;
  } else {
    $parentparent = $modx->getParent( $parentid );
    if( $parentparent ) {
      $parentparentid = $parentparent['id'];
      $parentlebels = $modx->getActiveChildren( $parentparentid ); //自分の親のレベル取得
    } else {
      $parentlebels = $modx->getActiveChildren( $rootid ); //rootのレベル取得
    }
  }
} else {
  $mylebels = $modx->getActiveChildren( $rootid ); //自分のレベル取得
}
// -------------------------------------------------------------------------------------
$preid = null; //自分の前
$nextid = null; //自分の後
$ppreid = null; //親の前
$pnextid = null; //親の後
$flg = -1;

//自分のレベル
foreach( $mylebels as $id ) {
  if( $flg == 1 )  {
    $nextid = $id['id'];
    break;
  } elseif( $id['id'] == $myid ) {
   $flg = 1;
  } else {
    $preid = $id['id'];
  }
}

//親のレベル
if($parentlebels) {
  $flg = -1;
  foreach( $parentlebels as $id ) {
    if( $flg == 1 ) {
      $pnextid = $id['id'];
      break;
    } elseif( $id['id'] == $parentid ) {
      $flg = 1;
    } else {
      $ppreid = $id['id'];
    }
  }
}
// -------------------------------------------------------------------------------------

if( $preid ) {
  $prechildlebels = $modx->getActiveChildren( $preid ); //自分の前の子のレベル取得
  if( $prechildlebels ) {
    $preid = $prechildlebels[ count($prechildlebels)-1 ]['id']; //自分の前の子の末尾を前idにする
  }
} elseif( $parentid ) {
  $preid = $parentid; //親を前idにする
} elseif( $mylebels ) {
  $preid = $mylebels[ count( $mylebels )-1 ]['id']; //自分のレベルの末尾を前idにする
}
$mychildlebels = $modx->getActiveChildren( $myid ); //自分の子のレベル取得
if( $mychildlebels ) {
  $nextid = $mychildlebels[ 0 ]['id']; //自分の子の先頭を次idにする
} elseif( !$nextid ) {
  if( $pnextid ) {
    $nextid = $pnextid; //親の次を次idにする
  } elseif( $mylebels ) {
    $nextid = $mylebels[ 0 ]['id']; //自分のレベルの先頭を次idにする
  }
}
// -------------------------------------------------------------------------------------
if( $parentid ) {
  $toptplout = str_replace( "[ +pgn.topurl+ ]", $modx->makeUrl( $parentid ), $toptplsrc );
  $toptplout = str_replace( "[ +pgn.topstr+ ]", $topstr, $toptplout );
} elseif( $rootid ) {
  $toptplout = str_replace( "[ +pgn.topurl+ ]", $modx->makeUrl( $rootid ), $toptplsrc );
  $toptplout = str_replace( "[ +pgn.topstr+ ]", $topstr, $toptplout );
} else {
  $toptplout = "";
}
if( $preid ) {
  $pretplout = str_replace( "[ +pgn.preurl+ ]", $modx->makeUrl( $preid ), $pretplsrc );
  $pretplout = str_replace( "[ +pgn.prestr+ ]", $prestr, $pretplout );
} else {
  $pretplout = "";
}
if( $nextid ) {
  $nexttplout = str_replace( "[ +pgn.nexturl+ ]", $modx->makeUrl( $nextid ), $nexttplsrc );
  $nexttplout = str_replace( "[ +pgn.nextstr+ ]", $nextstr, $nexttplout );
} else {
  $nexttplout = "";
}

if( $notop==1 ) {
  $output = $pretplout.$delimitor.$nexttplout;
} else {
  $output = $pretplout.$delimitor.$toptplout.$delimitor.$nexttplout;
}
print $output;
?>