From 7557e13d3b078cafb24508fe254396130c947ef6 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Tue, 20 Oct 2009 20:48:01 -0400 Subject: [PATCH] starting cleanup work for new navigation --- classes/ComicPressNavigation.inc | 43 ++++++++ classes/ComicPressStoryline.inc | 119 +++++++++++++++++++++++ test/widgets/GraphicalNavigationTest.php | 11 +++ 3 files changed, 173 insertions(+) create mode 100644 classes/ComicPressNavigation.inc create mode 100644 classes/ComicPressStoryline.inc create mode 100644 test/widgets/GraphicalNavigationTest.php diff --git a/classes/ComicPressNavigation.inc b/classes/ComicPressNavigation.inc new file mode 100644 index 0000000..fa37995 --- /dev/null +++ b/classes/ComicPressNavigation.inc @@ -0,0 +1,43 @@ +_storyline = $storyline; + $this->_dbi = ComicPressDBInterface::get_instance(); + } + + function get_post_nav($post) { + $nav = array(); + + // global previous/next + foreach (array('previous', 'next') as $field) { + $nav[$field] = $this->_dbi->{"get_${field}_comic"}(null, $post); + } + + // global first/last + if ($root_category = $this->_storyline->root_category) { + foreach (array('first', 'last') as $field) { + $nav[$field] = $this->_dbi->{"get_${field}_comic"}($root_category); + } + } + + if ($category = $this->_storyline->get_valid_post_category($post->ID)) { + // storyline previous/next + foreach (array('previous', 'next') as $field) { + $nav["storyline-${field}"] = $this->_dbi->{"get_${field}_comic"}($category, $post); + } + + // adjacent storyline nodes + if (is_array($valid = $this->_storyline->valid($category))) { + foreach ($valid as $field) { + $nav["storyline-chapter-${field}"] = $this->_dbi->get_first_comic($this->_storyline->{$field}($category)); + } + } + } + } +} + +?> \ No newline at end of file diff --git a/classes/ComicPressStoryline.inc b/classes/ComicPressStoryline.inc new file mode 100644 index 0000000..4acc4db --- /dev/null +++ b/classes/ComicPressStoryline.inc @@ -0,0 +1,119 @@ +root_category = false; + + $adjacents_by_parent = array(); + + if (is_array($structure)) { + $is_valid = true; + foreach ($structure as $branch) { + if (is_string($branch)) { + $parts = explode('/', $branch); + $valid = false; + if (count($parts) > 1) { + if ($parts[0] == '0') { $valid = true; } + } + if (!$valid) { + $is_valid = false; break; + } else { + $data = array(); + $leaf = end($parts); + $all_leaves[] = $leaf; + + if (count($parts) > 2) { + $parent = $parts[count($parts) - 2]; + + if (!isset($adjacents_by_parent[$parent])) { + $adjacents_by_parent[$parent] = array(); + } + $adjacents_by_parent[$parent][] = $leaf; + + $data['parent'] = $parent; + } else { + $this->root_category = $leaf; + } + + $new_structure[$leaf] = $data; + } + } else { + $is_valid = false; break; + } + } + if ($is_valid) { + for ($i = 0; $i < count($all_leaves); ++$i) { + foreach (array('previous' => -1, 'next' => 1) as $type => $dir) { + if (isset($all_leaves[$i + $dir])) { + $new_structure[$all_leaves[$i]][$type] = $all_leaves[$i + $dir]; + } + } + } + + $this->_structure = $new_structure; + } + } + return is_array($this->_structure); + } + + function _get_field($field, $id) { + if (isset($this->_structure)) { + if (isset($this->_structure[$id])) { + if (isset($this->_structure[$id][$field])) { + return $this->_structure[$id][$field]; + } + } + } + return false; + } + + function parent($id) { return $this->_get_field('parent', $id); } + function previous($id) { return $this->_get_field('previous', $id); } + function next($id) { return $this->_get_field('next', $id); } + function valid($id) { + if (isset($this->_structure[$id])) { + return array_keys($this->_structure[$id]); + } + return false; + } + + /** + * Get the valid navigation directions for a particular post. + */ + function get_valid_nav($post_id) { + if (($category = $this->get_valid_post_category($post_id)) !== false) { + return $this->valid($category); + } + return false; + } + + /** + * Get the valid comic category for this post. + */ + function get_valid_post_category($post_id) { + $result = false; + + foreach (wp_get_post_categories($post_id) as $category) { + if ($this->valid($category)) { + if ($result) { return false; } + + $result = $category; + } + } + return $result; + } +} + +?> \ No newline at end of file diff --git a/test/widgets/GraphicalNavigationTest.php b/test/widgets/GraphicalNavigationTest.php new file mode 100644 index 0000000..21ca059 --- /dev/null +++ b/test/widgets/GraphicalNavigationTest.php @@ -0,0 +1,11 @@ + \ No newline at end of file