From 3fda650af741b0e724185a053f1f891165b5960e Mon Sep 17 00:00:00 2001 From: John Bintz Date: Thu, 5 Nov 2009 22:29:29 -0500 Subject: [PATCH] more storyline handling stuff --- classes/ComicPressStoryline.inc | 62 ++++++++++++++++ test/ComicPressStorylineTest.php | 120 ++++++++++++++++++++++++++----- 2 files changed, 163 insertions(+), 19 deletions(-) diff --git a/classes/ComicPressStoryline.inc b/classes/ComicPressStoryline.inc index 5916141..2a4a64a 100644 --- a/classes/ComicPressStoryline.inc +++ b/classes/ComicPressStoryline.inc @@ -5,6 +5,7 @@ require_once('ComicPressDBInterface.inc'); class ComicPressStoryline { var $_structure, $root_category; + var $_category_search; function read_from_options() { $this->create_structure($this->get_flattened_storyline()); @@ -78,6 +79,8 @@ class ComicPressStoryline { $leaf = end($parts); $all_leaves[] = $leaf; + $data['level'] = count($parts) - 1; + if (count($parts) > 2) { $parent = $parts[count($parts) - 2]; @@ -351,6 +354,65 @@ class ComicPressStoryline { } return $output; } + + function &include_all() { + $this->_category_search = array_keys($this->_structure); + return $this; + } + + function &exclude_all() { + $this->_category_search = array(); + return $this; + } + + function _find_children($parent) { + $children = array($parent); + do { + $found_children = false; + foreach ($this->_structure as $category_id => $info) { + if (!in_array($category_id, $children)) { + if (in_array($info['parent'], $children)) { + $children[] = $category_id; + $found_children = true; + } + } + } + } while ($found_children); + + return $children; + } + + function &include_children($parent = null) { + $this->_category_search = array_unique(array_merge($this->_category_search, $this->_find_children($parent))); + sort($this->_category_search); + return $this; + } + + function &exclude_children($parent = null) { + $this->_category_search = array_diff($this->_category_search, $this->_find_children($parent)); + sort($this->_category_search); + return $this; + } + + function _find_level_or_above($level = null) { + $found = array(); + foreach ($this->_structure as $category_id => $info) { + if ($info['level'] <= $level) { $found[] = $category_id; } + } + return $found; + } + + function &include_level_or_above($level = null) { + $this->_category_search = array_unique(array_merge($this->_category_search, $this->_find_level_or_above($level))); + sort($this->_category_search); + return $this; + } + + function &exclude_level_or_above($level = null) { + $this->_category_search = array_diff($this->_category_search, $this->_find_level_or_above($level)); + sort($this->_category_search); + return $this; + } } ?> \ No newline at end of file diff --git a/test/ComicPressStorylineTest.php b/test/ComicPressStorylineTest.php index 612cdb0..c412a3e 100644 --- a/test/ComicPressStorylineTest.php +++ b/test/ComicPressStorylineTest.php @@ -35,46 +35,46 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { ), array( array('0/1'), - array('1' => array()), + array('1' => array('level' => 1)), 1 ), array( array('0/1', '0/1/2'), - array('1' => array('next' => 2), '2' => array('parent' => 1, 'previous' => 1)), + array('1' => array('next' => 2, 'level' => 1), '2' => array('parent' => 1, 'previous' => 1, 'level' => 2)), 1 ), array( array('0/1', '0/1/2', '0/1/3'), array( - '1' => array('next' => 2), - '2' => array('parent' => 1, 'previous' => 1, 'next' => 3), - '3' => array('parent' => 1, 'previous' => 2), + '1' => array('next' => 2, 'level' => 1), + '2' => array('parent' => 1, 'previous' => 1, 'next' => 3, 'level' => 2), + '3' => array('parent' => 1, 'previous' => 2, 'level' => 2), ), 1 ), array( array('0/1', '0/1/2', '0/1/2/3', '0/1/2/4', '0/1/5'), array( - '1' => array('next' => 2), - '2' => array('parent' => 1, 'next' => 3, 'previous' => 1), - '3' => array('parent' => 2, 'next' => 4, 'previous' => 2), - '4' => array('parent' => 2, 'next' => 5, 'previous' => 3), - '5' => array('parent' => 1, 'previous' => 4), + '1' => array('next' => 2, 'level' => 1), + '2' => array('parent' => 1, 'next' => 3, 'previous' => 1, 'level' => 2), + '3' => array('parent' => 2, 'next' => 4, 'previous' => 2, 'level' => 3), + '4' => array('parent' => 2, 'next' => 5, 'previous' => 3, 'level' => 3), + '5' => array('parent' => 1, 'previous' => 4, 'level' => 2), ), 1 ), array( array('0/1', '0/1/2', '0/1/2/3', '0/1/2/4', '0/1/5', '0/1/5/6', '0/1/5/7', '0/1/5/8', '0/1/9'), array( - '1' => array('next' => 2), - '2' => array('parent' => 1, 'next' => 3, 'previous' => 1), - '3' => array('parent' => 2, 'next' => 4, 'previous' => 2), - '4' => array('parent' => 2, 'next' => 5, 'previous' => 3), - '5' => array('parent' => 1, 'next' => 6, 'previous' => 4), - '6' => array('parent' => 5, 'next' => 7, 'previous' => 5), - '7' => array('parent' => 5, 'next' => 8, 'previous' => 6), - '8' => array('parent' => 5, 'next' => 9, 'previous' => 7), - '9' => array('parent' => 1, 'previous' => 8), + '1' => array('next' => 2, 'level' => 1), + '2' => array('parent' => 1, 'next' => 3, 'previous' => 1, 'level' => 2), + '3' => array('parent' => 2, 'next' => 4, 'previous' => 2, 'level' => 3), + '4' => array('parent' => 2, 'next' => 5, 'previous' => 3, 'level' => 3), + '5' => array('parent' => 1, 'next' => 6, 'previous' => 4, 'level' => 2), + '6' => array('parent' => 5, 'next' => 7, 'previous' => 5, 'level' => 3), + '7' => array('parent' => 5, 'next' => 8, 'previous' => 6, 'level' => 3), + '8' => array('parent' => 5, 'next' => 9, 'previous' => 7, 'level' => 3), + '9' => array('parent' => 1, 'previous' => 8, 'level' => 2), ), 1 ), @@ -311,6 +311,88 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { $this->assertEquals($expected_result, $this->css->_length_sort($data)); } + + function testIncludeAll() { + $this->css->_structure = array( + '1' => array('next' => 2), + '2' => array('parent' => 1, 'previous' => 1, 'next' => 3), + '3' => array('parent' => 2, 'next' => 4, 'previous' => 2), + '4' => array('parent' => 2, 'previous' => 3) + ); + + $this->assertEquals($this->css, $this->css->include_all()); + $this->assertEquals(array(1,2,3,4), $this->css->_category_search); + } + + function testExcludeAll() { + $this->css->_category_search = array(1,2,3,4); + + $this->assertEquals($this->css, $this->css->exclude_all()); + $this->assertEquals(array(), $this->css->_category_search); + } + + function testFindChildren() { + $this->css->_structure = array( + '1' => array('next' => 2), + '2' => array('parent' => 1, 'previous' => 1, 'next' => 3), + '3' => array('parent' => 2, 'next' => 4, 'previous' => 2), + '4' => array('parent' => 2, 'previous' => 3) + ); + + $this->assertEquals(array(2,3,4), $this->css->_find_children(2)); + } + + function testIncludeChildren() { + $css = $this->getMock('ComicPressStoryline', array('_find_children')); + $css->expects($this->once())->method('_find_children')->will($this->returnValue(array(2,3,4))); + $css->_category_search = array(4,5); + + $css->include_children(2); + + $this->assertEquals(array(2,3,4,5), $css->_category_search); + } + + function testExcludeChildren() { + $css = $this->getMock('ComicPressStoryline', array('_find_children')); + $css->expects($this->once())->method('_find_children')->will($this->returnValue(array(2,3,4))); + $css->_category_search = array(4, 5, 6); + + $css->exclude_children(2); + + $this->assertEquals(array(5, 6), $css->_category_search); + } + + function testFindLevelOrAbove() { + $this->css->_structure = array( + '1' => array('next' => 2, 'level' => 1), + '2' => array('parent' => 1, 'previous' => 1, 'next' => 3, 'level' => 2), + '3' => array('parent' => 2, 'next' => 4, 'previous' => 2, 'level' => 3), + '4' => array('parent' => 2, 'previous' => 3, 'level' => 3) + ); + + $this->assertEquals(array(1, 2), $this->css->_find_level_or_above(2)); + } + + function testIncludeLevelOrAbove() { + $css = $this->getMock('ComicPressStoryline', array('_find_level_or_above')); + $css->expects($this->once())->method('_find_level_or_above')->will($this->returnValue(array(2,3,4))); + $css->_category_search = array(4,5); + + $css->include_level_or_above(2); + + $this->assertEquals(array(2,3,4,5), $css->_category_search); + } + + function testExcludeLevelOrAbove() { + $css = $this->getMock('ComicPressStoryline', array('_find_level_or_above')); + $css->expects($this->once())->method('_find_level_or_above')->will($this->returnValue(array(2,3,4))); + $css->_category_search = array(4, 5, 6); + + $css->exclude_level_or_above(2); + + $this->assertEquals(array(5, 6), $css->_category_search); + } + } ?> \ No newline at end of file