From b104fb8b12c22a8d740489fbf150e355fa0864e2 Mon Sep 17 00:00:00 2001 From: Gabriel Dunne Date: Sat, 8 Sep 2012 14:11:22 -0700 Subject: [PATCH] Added sorting function in data.php. --- lib/data.php | 91 ++++++++++++++++++++-------------------------------- 1 file changed, 34 insertions(+), 57 deletions(-) diff --git a/lib/data.php b/lib/data.php index b2eb09f..9e92627 100644 --- a/lib/data.php +++ b/lib/data.php @@ -39,45 +39,23 @@ function get_entries( $path = "", $args = array() ) $entries = array(); if (isset($dir_iterator)) { - foreach ($dir_iterator as $file => $info) { - if (!$info->isDir() && $info->getFilename() != CONFIG_FILE) { - // skip dot files - if (substr($info->getFilename(), 0, 1) == '.') - continue; - $entry = parse_entry($info); - if (!isset($tag)) { - $entries[] = $entry; - } - else if ($tag && in_array($tag, isset($entry->tags) && is_array($entry->tags) ? $entry->tags : array())) { - $entries[] = $entry; + foreach ($dir_iterator as $file => $info) { + if (!$info->isDir() && $info->getFilename() != CONFIG_FILE) { + // skip dot files + if (substr($info->getFilename(), 0, 1) == '.') + continue; + $entry = parse_entry($info); + if (!isset($tag)) { + $entries[] = $entry; + } + else if ($tag && in_array($tag, isset($entry->tags) && is_array($entry->tags) ? $entry->tags : array())) { + $entries[] = $entry; + } } } } - } - - switch ($order_by) - { - case 'alpha' : - - foreach ($entries as $key => $row) - $alpha[$key] = $row->title; - - if (isset($alpha)) - array_multisort($alpha, $order, $entries); - - break; - - - case 'date' : - default : - - foreach ($entries as $key => $row) - $time[$key] = $row->timestamp; - - if (isset($time)) - array_multisort($time, $order, $entries); - } + $entries = sort_entries($entries, $order_by, $order); if ($create_datafile) { if (!is_dir($data_path)) { @@ -107,33 +85,32 @@ function get_entries_and_dirs( $path = "", $args = array() ) $entries = get_entries($path, $args); $dirs = get_dirs($path, $args); - $res = array_merge($entries, $dirs); + $res = sort_entries($res, $order_by, $order); - switch ($order_by) - { - case 'alpha' : - - foreach ($res as $key => $row) - $alpha[$key] = $row->title; - - if (isset($alpha)) - array_multisort($alpha, $order, $res); - - break; - - - case 'date' : - default : + return $res; +} - foreach ($res as $key => $row) - $time[$key] = $row->timestamp; - if (isset($time)) - array_multisort($time, $order, $res); - } +function sort_entries($entries, $order_by, $order = SORT_DESC) +{ + switch ($order_by) + { + case 'alpha' : + foreach ($entries as $key => $row) + $alpha[$key] = $row->title; + if (isset($alpha)) + array_multisort($alpha, $order, $entries); + return $entries; - return $res; + case 'date' : + default : + foreach ($entries as $key => $row) + $time[$key] = $row->timestamp; + if (isset($time)) + array_multisort($time, $order, $entries); + return $entries; + } } -- 2.34.1