From de57ff044405924d62422e2def22e1275437da64 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sun, 2 Aug 2009 20:32:08 -0400 Subject: [PATCH] comic reordering working --- classes/ComicPressComicPost.inc | 57 +++++++++++++++++++++++++++----- test/ComicPressComicPostTest.php | 39 +++++++++++++++++----- 2 files changed, 79 insertions(+), 17 deletions(-) diff --git a/classes/ComicPressComicPost.inc b/classes/ComicPressComicPost.inc index afb6428..11f1568 100644 --- a/classes/ComicPressComicPost.inc +++ b/classes/ComicPressComicPost.inc @@ -105,10 +105,20 @@ class ComicPressComicPost { return $orderings; } + function build_comic_ordering_string($order) { + $result_string_parts = array(); + + foreach ($order as $key => $values) { + $result_string_parts[] = "${key}:" . implode(",", $values); + } + sort($result_string_parts); + return implode(";", $result_string_parts); + } + function normalize_comic_image_ordering() { if (is_array($this->get_comic_image_attachments())) { $ordering_by_type = array(); - $ordering_types = get_post_meta($this->post->id, 'comic_ordering', true); + $ordering_types = get_post_meta($this->post->ID, 'comic_ordering', true); if (!empty($ordering_types)) { $ordering_types = $this->breakdown_comic_ordering_string($ordering_types); } $comic_image_ordering = array(); @@ -148,14 +158,8 @@ class ComicPressComicPost { } $result = array_merge($comic_image_ordering, $remaining_posts_to_sort); - $result_string_parts = array(); - - foreach ($result as $key => $values) { - $result_string_parts[] = "${key}:" . implode(",", $values); - } - sort($result_string_parts); - - update_post_meta($post_id, 'comic_ordering', implode(";", $result_string_parts)); + + update_post_meta($this->post->ID, 'comic_ordering', $this->build_comic_ordering_string($result)); return $result; } } @@ -163,6 +167,41 @@ class ComicPressComicPost { function sort_remaining_comic_images($a, $b) { return strtotime($a->post_date) - strtotime($b->post_date); } + + function change_comic_image_ordering($requested_new_order) { + if (($order_string = get_post_meta($this->post->ID, 'comic_ordering', true)) !== false) { + $orderings = $this->breakdown_comic_ordering_string($order_string); + + $new_order = array(); + + foreach ($orderings as $type => $current_order) { + $new_order[$type] = array(); + $sort_by_position = array(); + foreach ($requested_new_order[$type] as $id => $position) { + if (!isset($sort_by_position[$position])) { + $sort_by_position[$position] = array(); + } + $sort_by_position[$position][] = $id; + } + ksort($sort_by_position); + $requested_order = array(); + foreach ($sort_by_position as $position => $ids) { + sort($ids); + $requested_order = array_merge($requested_order, $ids); + } + + $requested_order = array_merge($requested_order, array_diff($current_order, $requested_order)); + + foreach ($requested_order as $requested_comic) { + if (in_array($requested_comic, $current_order)) { + $new_order[$type][] = $requested_comic; + } + } + } + + update_post_meta($this->post->ID, 'comic_ordering', $this->build_comic_ordering_string($new_order)); + } + } } ?> \ No newline at end of file diff --git a/test/ComicPressComicPostTest.php b/test/ComicPressComicPostTest.php index 49bb249..286ca56 100644 --- a/test/ComicPressComicPostTest.php +++ b/test/ComicPressComicPostTest.php @@ -153,27 +153,50 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase { wp_insert_post((object)array('ID' => 1)); update_post_meta(1, 'comic_ordering', "comic:3,2"); - $result = $p->normalize_comic_image_ordering(1); + $p->post = (object)array('ID' => 1); + + $result = $p->normalize_comic_image_ordering(); $this->assertEquals(array('comic' => array(3,2), 'rss' => array(5,4)), $result); $this->assertEquals('comic:3,2;rss:5,4', get_post_meta(1, 'comic_ordering', true)); } function providerTestChangeComicImageOrdering() { - + return array( + array( + 'comic:1,2,3', + array( + 'comic' => array('1' => 3, '2' => 1, '3' => 2) + ), + 'comic:2,3,1' + ), + array( + 'comic:1,2,3', + array( + 'comic' => array('1' => 2, '2' => 2, '3' => 1) + ), + 'comic:3,1,2' + ), + array( + 'comic:1,2,3', + array( + 'comic' => array('1' => 1, '2' => 2) + ), + 'comic:1,2,3' + ), + ); } /** * @dataProvider providerTestChangeComicImageOrdering */ function testChangeComicImageOrdering($current_ordering, $revised_ordering, $expected_result) { - update_post_meta(1, 'comic_ordering', array('comic:1,2,3')); + update_post_meta(1, 'comic_ordering', $current_ordering); - $this->p->change_comic_image_ordering(array( - 'comic' => array( - '3' => 1, '2' => 3, '1' => 2 - ) - )); + $this->p->post = (object)array('ID' => 1); + $this->p->change_comic_image_ordering($revised_ordering); + + $this->assertEquals($expected_result, get_post_meta(1, 'comic_ordering', true)); } }