From 9f963e1fbd87897f572101bdc255eb8852176219 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Mon, 27 Jul 2009 20:16:40 -0400 Subject: [PATCH] previous next categories finding worked out --- classes/ComicPress.inc | 31 +++++++++++++++++++++++++++---- test/ComicPressTest.php | 15 +++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/classes/ComicPress.inc b/classes/ComicPress.inc index a94e42e..cb2e567 100644 --- a/classes/ComicPress.inc +++ b/classes/ComicPress.inc @@ -379,12 +379,35 @@ class ComicPress { function get_previous_next_categories($category_id) { $prev_next_categories = array(); - - for ($i = 0, $il < count($this->category_tree); $i < $il; ++$i) { - $parts = explode("/", $this->category_tree); + + for ($i = 0, $il = count($this->category_tree); $i < $il; ++$i) { + $parts = explode("/", $this->category_tree[$i]); if (count($parts) > 2) { if (end($parts) == $category_id) { - + while (count($parts) > 2) { + foreach (array( + 'previous' => -1, + 'next' => 1 + ) as $key => $direction) { + $index = $i; + while (isset($this->category_tree[$index])) { + $index += $direction; + if (isset($this->category_tree[$index])) { + $compare_parts = explode("/", $this->category_tree[$index]); + if (count($compare_parts) == count($parts)) { + $target_category = array_pop($compare_parts); + $parent_category = array_pop($compare_parts); + + if (!isset($prev_next_categories[$parent_category])) { + $prev_next_categories[$parent_category] = array(); + } + $prev_next_categories[$parent_category][$key] = $target_category; + } + } + } + } + array_pop($parts); + } } } } diff --git a/test/ComicPressTest.php b/test/ComicPressTest.php index da187a5..3812479 100644 --- a/test/ComicPressTest.php +++ b/test/ComicPressTest.php @@ -200,6 +200,21 @@ class ComicPressTest extends PHPUnit_Framework_TestCase { '1' => array('next' => 3) ) ), + array( + array( + '0/1', + '0/1/6', + '0/1/2', + '0/1/2/4', + '0/1/2/5', + '0/1/3', + ), + 5, + array( + '2' => array('previous' => 4), + '1' => array('previous' => 6, 'next' => 3), + ) + ), ); }