diff --git a/addons/Core/Core.inc b/addons/Core/Core.inc index f8257fb..c88c9fe 100644 --- a/addons/Core/Core.inc +++ b/addons/Core/Core.inc @@ -10,7 +10,7 @@ class ComicPressAddonCore extends ComicPressAddon { * @param ComicPress $comicpress The master ComicPress object. */ function init() { - $this->comicpress = ComicPress::get_instance(); + $this->comicpress = &ComicPress::get_instance(); add_action('admin_menu', array(&$this, 'setup_admin_interface')); add_filter('attachment_fields_to_edit', array(&$this, 'setup_comic_metadata_buttons'), 10, 2); @@ -300,6 +300,7 @@ class ComicPressAddonCore extends ComicPressAddon { $nonce = wp_create_nonce('comicpress'); $root_categories = $this->get_root_categories(); $storyline = new ComicPressStoryline(); + $storyline->normalize(); $storyline->read_from_options(); include(dirname(__FILE__) . '/partials/options-admin.inc'); @@ -489,7 +490,7 @@ class ComicPressAddonCore extends ComicPressAddon { break; case 'storyline_order': $storyline = new ComicPressStoryline(); - var_dump($storyline->set_flattened_storyline_order($info[$option])); + $storyline->normalize($info[$option]); break; } } diff --git a/classes/ComicPress.inc b/classes/ComicPress.inc index e093be2..5720a25 100644 --- a/classes/ComicPress.inc +++ b/classes/ComicPress.inc @@ -30,14 +30,14 @@ class ComicPress { var $partial_paths = array(); var $layouts = null; - function get_instance() { + function &get_instance() { static $instance; - if (empty($instance)) { - $instance = &new ComicPress(); + if (!$instance) { + $instance = array(new ComicPress()); } - return $instance; + return $instance[0]; } /** diff --git a/classes/ComicPressStoryline.inc b/classes/ComicPressStoryline.inc index cac22e1..c2cd453 100644 --- a/classes/ComicPressStoryline.inc +++ b/classes/ComicPressStoryline.inc @@ -7,18 +7,16 @@ class ComicPressStoryline { var $_structure, $root_category; function read_from_options() { - $comicpress = ComicPress::get_instance(); - var_dump($comicpress); - $this->create_structure($comicpress->comicpress_options['storyline_order']); + $this->create_structure($this->get_flattened_storyline()); } function get_flattened_storyline() { - $comicpress = ComicPress::get_instance(); + $comicpress = &ComicPress::get_instance(); return $comicpress->comicpress_options['storyline_order']; } function set_flattened_storyline($storyline) { - $comicpress = ComicPress::get_instance(); + $comicpress = &ComicPress::get_instance(); $comicpress->comicpress_options['storyline_order'] = $storyline; $comicpress->save(); } @@ -44,7 +42,7 @@ class ComicPressStoryline { * @return boolean True if the structure was valid. */ function create_structure($structure) { - $new_structure = array(); + $new_structure = array(); $parent = null; $all_leaves = array(); $this->root_category = false; @@ -187,6 +185,10 @@ class ComicPressStoryline { } return $structure; } + + function get_category_flattened($parent) { + return $this->flatten_simple_storyline($this->get_category_simple_structure($parent)); + } function _merge_simple_storyline($simple_storyline) { while (count($simple_storyline) > 0) { @@ -219,6 +221,40 @@ class ComicPressStoryline { return $simple_storyline; } + /** + * Integrates a bunch of other things. + */ + function normalize($flattened_storyline = null, $set = true) { + $comicpress = ComicPress::get_instance(); + if (is_null($flattened_storyline)) { + $flattened_storyline = $this->get_flattened_storyline(); + } + $all_categories_flattened = $this->get_category_flattened($comicpress->comicpress_options['comic_category_id']); + + $result = $this->normalize_flattened_storyline($flattened_storyline, $all_categories_flattened); + if ($set) { + $this->set_flattened_storyline($result); + } + return $result; + } + + function _length_sort($parts) { + $new = array(); + foreach ($parts as $part) { + $p = explode('/', $part); + if (!isset($new[count($p)])) { + $new[count($p)] = array(); + } + $new[count($p)][] = $part; + } + ksort($new); + $output = array(); + foreach (array_values($new) as $values) { + $output = array_merge($output, $values); + } + return $output; + } + function normalize_flattened_storyline($storyline, $comic_categories) { $storyline_nodes = explode(",", $storyline); $category_nodes = explode(",", $comic_categories); @@ -227,6 +263,7 @@ class ComicPressStoryline { $extra_in_storyline = array_diff($storyline_nodes, $category_nodes); if (!empty($missing_from_storyline)) { + $missing_from_storyline = $this->_length_sort($missing_from_storyline); foreach ($missing_from_storyline as $node) { $parent_pattern = implode('/', array_slice(explode('/', $node), 0, -1)); $last = null; @@ -237,6 +274,8 @@ class ComicPressStoryline { } if (!is_null($last)) { array_splice($storyline_nodes, $last + 1, 0, array($node)); + } else { + $storyline_nodes[] = $node; } } } diff --git a/js/Storyline.js b/js/Storyline.js index 0757689..47e27c0 100644 --- a/js/Storyline.js +++ b/js/Storyline.js @@ -16,7 +16,7 @@ Storyline.setup = function() { placeholder: 'placeholder', forcePlaceholderSize: true, opacity: 0.4, - change: function(e, ui) { + stop: function(e, ui) { Storyline.get_order(); } }); diff --git a/test/ComicPressStorylineTest.php b/test/ComicPressStorylineTest.php index a10cc16..c8c4695 100644 --- a/test/ComicPressStorylineTest.php +++ b/test/ComicPressStorylineTest.php @@ -258,6 +258,7 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { array('0/1,0/1/2,0/1/2/4', '0/1,0/1/2,0/1/2/4,0/1/2/3'), array('0/1,0/1/2,0/1/2/4,0/1/2/3,0/1/5', '0/1,0/1/2,0/1/2/4,0/1/2/3'), array('0/1,0/1/2,0/1/2/3,0/1/5', '0/1,0/1/2,0/1/2/3,0/1/2/4'), + array('', '0/1,0/1/2,0/1/2/3,0/1/2/4'), ); } @@ -285,6 +286,18 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { ) )); } + + function testLengthSort() { + $data = array( + '0/1', '0/1/3', '0/1/3/6', '0/1/3/7', '0/1/4', '0/1/4/2', '0/1/4/3' + ); + + $expected_result = array( + '0/1', '0/1/3', '0/1/4', '0/1/3/6', '0/1/3/7', '0/1/4/2', '0/1/4/3' + ); + + $this->assertEquals($expected_result, $this->css->_length_sort($data)); + } } ?> \ No newline at end of file