Skip to content

Commit

Permalink
#105 ordering menu items by reading index in tags
Browse files Browse the repository at this point in the history
  • Loading branch information
tracend committed Jul 4, 2013
1 parent 44774d8 commit 20dfe42
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions app/helpers/section.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,43 @@ private function getItems($data=false){
while ($v = $results->fetch(PDO::FETCH_ASSOC)) {
// pick only first level pages
$path = explode("/", $v['path'] );

if(count($path) > 1){
$items[$path[0]] = array( 'url' => url( $path[0] ), 'title' => ucwords($path[0]) );
// add seleted class if uri matches path
$items[$path[0]]['selected'] = ($uri == $v['path']) ? "selected" : null;
// get the position of the item, if any
preg_match("/".$tag."[a-z\-]*-(\d+)/", $v['tags'], $order );
$order = array_pop( $order );
$k = ( is_null( $order ) ) ? false : (int) $order -1;
$selected = ($uri == $v['path']) ? "selected" : null;

$p = (count($path) > 1) ? $path[0] : $v['path'];
$title = (count($path) > 1) ? ucwords($path[0]) : $v['title'];

$item = array( 'url' => url( $p ), 'title' => $title );
// add seleted class if uri matches path
$item['selected'] = $selected;
$item['order'] = $k;
// position in their right order
if( $k ){
if( array_key_exists($k, $items) ){
$i = $items[$k];
$items[$k] = $item;
$items[] = $i;
} else {
$items[$k] = $item;
}
} else {
$items[$v['path']] = array( 'url' => url( $v['path'] ), 'title' => $v['title'] );
// add seleted class if uri matches path
$items[$v['path']]['selected'] = ($uri == $v['path']) ? "selected" : null;
$placed = false;
for( $j = 0; $j < count( $items ); $j++){
if( !array_key_exists($j, $items) ){
$items[$j] = $item;
$placed = true;
}
}
// place at the end if not placed
if( !$placed ) $items[] = $item;
}
}
}

// sort elements by key
ksort($items);
return $items;
}

Expand Down

0 comments on commit 20dfe42

Please sign in to comment.