comic reordering working

This commit is contained in:
John Bintz 2009-08-02 20:32:08 -04:00
parent fa6d62e9d0
commit de57ff0444
2 changed files with 79 additions and 17 deletions

View File

@ -105,10 +105,20 @@ class ComicPressComicPost {
return $orderings; 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() { function normalize_comic_image_ordering() {
if (is_array($this->get_comic_image_attachments())) { if (is_array($this->get_comic_image_attachments())) {
$ordering_by_type = array(); $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); } if (!empty($ordering_types)) { $ordering_types = $this->breakdown_comic_ordering_string($ordering_types); }
$comic_image_ordering = array(); $comic_image_ordering = array();
@ -148,14 +158,8 @@ class ComicPressComicPost {
} }
$result = array_merge($comic_image_ordering, $remaining_posts_to_sort); $result = array_merge($comic_image_ordering, $remaining_posts_to_sort);
$result_string_parts = array();
foreach ($result as $key => $values) { update_post_meta($this->post->ID, 'comic_ordering', $this->build_comic_ordering_string($result));
$result_string_parts[] = "${key}:" . implode(",", $values);
}
sort($result_string_parts);
update_post_meta($post_id, 'comic_ordering', implode(";", $result_string_parts));
return $result; return $result;
} }
} }
@ -163,6 +167,41 @@ class ComicPressComicPost {
function sort_remaining_comic_images($a, $b) { function sort_remaining_comic_images($a, $b) {
return strtotime($a->post_date) - strtotime($b->post_date); 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));
}
}
} }
?> ?>

View File

@ -153,27 +153,50 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase {
wp_insert_post((object)array('ID' => 1)); wp_insert_post((object)array('ID' => 1));
update_post_meta(1, 'comic_ordering', "comic:3,2"); 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(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)); $this->assertEquals('comic:3,2;rss:5,4', get_post_meta(1, 'comic_ordering', true));
} }
function providerTestChangeComicImageOrdering() { 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 * @dataProvider providerTestChangeComicImageOrdering
*/ */
function testChangeComicImageOrdering($current_ordering, $revised_ordering, $expected_result) { 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( $this->p->post = (object)array('ID' => 1);
'comic' => array( $this->p->change_comic_image_ordering($revised_ordering);
'3' => 1, '2' => 3, '1' => 2
) $this->assertEquals($expected_result, get_post_meta(1, 'comic_ordering', true));
));
} }
} }