pepebe
2/14/2017 - 5:40 PM

Return id of a sibling document. Useful to build carouse navigations.

Return id of a sibling document. Useful to build carouse navigations.

<?php
/*
  ppbCarousel returns ids of specific sibling resources.
  
  It can be used to build prev next links.
  
  If the current Resource is at the beginning or the end of the list, 
  prev and next will circle to the first or last element.

  [[ppbCarousel? 
    &var=`prev` 
    &sortby=`menuindex`
    &sortdir=`asc
  ]]

  [[+prev]] - the one before or, if you are at the beginning, the last one
  [[+next]] - the next one, or if you are at the end, the first one
  [[+currentIndex]] - the current position in relation to all siblings
  [[+current]] - the id of the current resource (should be obvious)
  [[+total]] - the total number of siblings including the current ressource. 
  
  -------------------------
  
  Example: 
  [[ppbCarousel? 
    &var=`prev` 
    &sortby=`menuindex`
    &sortdir=`asc
  ]]  
  <a href="[[~[[+prev]]]]">Vor</a>

  -------------------------

  Example using fastfield to get the pagetitle of the previous resource.
  <a href="[[~[[+prev]]]]">[[#[[+prev]].pagetitle]]</a>
  
  Hint: If you don't add var, you'll get a debug message with all available variables.

*/
    
    if(!function_exists('debug')){
        function debug($msg){
            return "<pre>".print_r($msg,true)."</pre>";
        }
    }
    
    $debug = !empty($debug) ? $debug : false;
    
    $start  = microtime(true);
    $id     = !empty($id)        ? $id     : $modx->resource->get('id');
    $parent = !empty($parent)    ? $parent : $modx->resource->get('parent');
    
    $sortby = !empty($sortby) ? $sortby : 'menuindex';
    $sortdir = !empty($sortdir) ? $sortdir : 'asc';
    
    $var    = !empty($var)       ? $var : false;
    
    $output['id'] = $id;
    $output['parent'] = $parent;

    $where = array(
         'parent' => $parent
        ,'published' => 1
        ,'deleted' => 0
    );
    
    $c = $modx->newQuery('modResource');
    $c->where($where);
    $c->sortby($sortby,$sortdir);
    
    $parent = $modx->getObject('modResource', $parent);
    $children = $parent->getMany('Children',$c);
    
    /* ******************* */
  
    if (empty($children)) {return;}
  
    foreach($children as $child){
        $ids[] = $child->get('id');
    }    
    $output['ids'] = implode(",",$ids);           

    $count = count($ids);
    $output['count'] = $count;    

    if($count > 1) {
    
        $firstIndex = 0;
        $currentIndex = array_search($id,$ids);
        $lastIndex = $count -1;
        
        /* Impossible?
        if(!$currentIndex) return "heureka";
        */
        
        if($currentIndex == 0){
            $nextIndex = $currentIndex + 1;
            $prevIndex = $lastIndex;
        }
        elseif($currentIndex == $lastIndex){
            $nextIndex = $firstIndex;
            $prevIndex = $currentIndex - 1;
        }
        else{
            $nextIndex = $currentIndex + 1;
            $prevIndex = $currentIndex - 1;
        }
        
        $output['prev'] = $ids[$prevIndex];
        $output['next'] = $ids[$nextIndex];

        $output['currentIndex'] = $currentIndex;
        $output['current'] = $ids[$currentIndex];
        $output['total'] = $count;
    }
    
    
    if(!$var) {
        $output['time'] = round(microtime(true) - $start, 3);
        return debug($output);
    }
    else {
        return $output[$var];
    }