From 20dfe42954a2848c12f858ab878b250d43d25461 Mon Sep 17 00:00:00 2001 From: Makis Tracend Date: Thu, 4 Jul 2013 06:45:03 -0700 Subject: [PATCH] #105 ordering menu items by reading index in tags --- app/helpers/section.php | 42 ++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/app/helpers/section.php b/app/helpers/section.php index c126609..bba7333 100644 --- a/app/helpers/section.php +++ b/app/helpers/section.php @@ -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; }