From 3e1a2de3dca61622fb3de581197c0914b58b5fbf Mon Sep 17 00:00:00 2001 From: John Bintz Date: Fri, 13 Nov 2009 16:15:36 -0500 Subject: [PATCH] chipping away at comic image ordering --- classes/ComicPressAdmin.inc | 26 +++- classes/ComicPressComicPost.inc | 39 ++++-- .../backends/ComicPressBackendAttachment.inc | 1 + .../_comic-image-ordering-sorters.inc | 123 +++++++----------- css/cp-admin.css | 16 +++ js/ComicImageOrdering.js | 57 ++++++++ test/ComicPressAdminTest.php | 20 +++ test/ComicPressComicPostTest.php | 37 ++++-- 8 files changed, 224 insertions(+), 95 deletions(-) diff --git a/classes/ComicPressAdmin.inc b/classes/ComicPressAdmin.inc index a3d809d..5106c90 100644 --- a/classes/ComicPressAdmin.inc +++ b/classes/ComicPressAdmin.inc @@ -174,6 +174,18 @@ class ComicPressAdmin { $media_upload_iframe_src = "media-upload.php?post_id=$uploading_iframe_ID"; $image_upload_iframe_src = apply_filters('image_upload_iframe_src', "$media_upload_iframe_src&type=image&TB_iframe=true"); + $comicpress = ComicPress::get_instance(); + + $available_attachments = array(); + foreach ($this->get_editable_attachment_list($ordering) as $id => $info) { + $result = ComicPressBackend::generate_from_id($id); + if (!empty($result)) { + $attachment_info = $result->get_info(); + + $available_attachments[] = array('id' => $id, 'name' => basename($attachment_info['file']), 'type' => $result->source_name, 'attachment' => $attachment_info, 'ordering' => $info); + } + } + if ($is_ajax === true) { include(dirname(__FILE__) . '/partials/_comic-image-ordering-sorters.inc'); } else { @@ -182,6 +194,18 @@ class ComicPressAdmin { } // @codeCoverageIgnoreEnd + function get_editable_attachment_list($ordering) { + foreach ($ordering as $id => $info) { + if (isset($info['children'])) { + foreach (array_values($info['children']) as $new_id) { + $ordering[$new_id] = array('enabled' => true); + } + } + } + + return $ordering; + } + /** * Create a dimension selector. * @param string $root The field name root. @@ -301,7 +325,7 @@ class ComicPressAdmin { function _json_encode($data) { if (function_exists('json_encode')) { - return json_decode($data); + return json_encode($data); } else { require_once(ABSPATH."/wp-includes/js/tinymce/plugins/spellchecker/classes/utils/JSON.php"); $j = new Moxiecode_JSON(); diff --git a/classes/ComicPressComicPost.inc b/classes/ComicPressComicPost.inc index 9159c39..02a2638 100644 --- a/classes/ComicPressComicPost.inc +++ b/classes/ComicPressComicPost.inc @@ -36,9 +36,7 @@ class ComicPressComicPost { foreach ($current_ordering as $key => $properties) { $all_current_ids[$key] = true; if (isset($properties['children'])) { - foreach ($properties['children'] as $type => $kids) { - $all_current_ids = array_merge($all_current_ids, $kids); - } + foreach (array_values($properties['children']) as $kid) { $all_current_ids[$kid] = true; } } } $all_current_ids = array_keys($all_current_ids); @@ -59,12 +57,9 @@ class ComicPressComicPost { } else { foreach ($current_ordering as $key => $properties) { if (isset($properties['children'])) { - foreach ($properties['children'] as $type => $kids) { - if (isset($kids[$attachment_id])) { unset($kids[$attachment_id]); } - if (empty($kids)) { - unset($properties['children'][$type]); - } else { - $properties['children'][$type] = $kids; + foreach ($properties['children'] as $type => $kid) { + if (!in_array($kid, $attachment_ids)) { + unset($properties['children'][$type]); } } if (empty($properties['children'])) { @@ -162,6 +157,32 @@ class ComicPressComicPost { return $parents; } + + function update_post_media_data($info) { + $ordering = array(); + foreach ($info as $image) { + if (isset($image['id'])) { + $data = array( + 'enabled' => false + ); + foreach ($image as $field => $value) { + switch ($field) { + case 'enabled': $data['enabled'] = true; break; + case 'children': + $any_entered = false; + foreach ($value as $type => $attached_id) { + if (!empty($attached_id)) { $any_entred = true; break; } + } + if ($any_entered) { $data['children'] = $value; } + break; + } + } + $ordering[$image['id']] = $data; + } + } + update_post_meta($this->post->ID, 'image-ordering', $ordering); + return $ordering; + } } ?> \ No newline at end of file diff --git a/classes/backends/ComicPressBackendAttachment.inc b/classes/backends/ComicPressBackendAttachment.inc index 8b0e8d9..f8aa0f7 100644 --- a/classes/backends/ComicPressBackendAttachment.inc +++ b/classes/backends/ComicPressBackendAttachment.inc @@ -4,6 +4,7 @@ require_once(dirname(__FILE__) . '/../ComicPressBackend.inc'); class ComicPressBackendAttachment extends ComicPressBackend { var $root_id = 'attachment'; + var $source_name = "Post attachment"; function generate_from_post($post) { $result = array(); diff --git a/classes/partials/_comic-image-ordering-sorters.inc b/classes/partials/_comic-image-ordering-sorters.inc index ad74df3..1f6d61c 100644 --- a/classes/partials/_comic-image-ordering-sorters.inc +++ b/classes/partials/_comic-image-ordering-sorters.inc @@ -1,92 +1,61 @@

- - ' . __('Image Uploader', 'comicpress') . '' - ) - ?> - + + ' . __('Image Uploader', 'comicpress') . '' + ) + ?> +

-
+
$info) { $result = ComicPressBackend::generate_from_id($id); if (!empty($result)) { - $info = $result->get_info(); ?> -
- -
- -

- - -

- - -

- -
-
-
+ $info = $result->get_info(); ?> +
+ +
+

+ +

+ +

source_name ?>:

+ + +

+ + +

+ + comicpress_options['image_types']) > 1) { ?> + +
+
    + comicpress_options['image_types'] as $type => $info) { ?> + +
  • + : + +
  • + + +
+
+ +
+
+
diff --git a/css/cp-admin.css b/css/cp-admin.css index 7007171..da23b24 100644 --- a/css/cp-admin.css +++ b/css/cp-admin.css @@ -106,4 +106,20 @@ line-height: 16px; display: block; margin-bottom: 10px; +} + +.comic-ordering-show-associations { + font-size: 14px; + color: #777 +} + +.cp-comic-info { + float: left; + display: inline; + margin-bottom: 5px; +} + +.comic-ordering-associations { + border: solid #aaa 1px; + padding: 5px; } \ No newline at end of file diff --git a/js/ComicImageOrdering.js b/js/ComicImageOrdering.js index e69de29..e60315f 100644 --- a/js/ComicImageOrdering.js +++ b/js/ComicImageOrdering.js @@ -0,0 +1,57 @@ +var ComicImageOrdering = {}; + +ComicImageOrdering.get_ordering = function() { + var ordering = {}; + $('cp-comic-order').value = Object.toJSON(ordering); +}; + +ComicImageOrdering.build_dropdowns = function() { + $$('#comic-ordering select').each(function(sel) { + sel.innerHTML = ''; + sel.appendChild(new Element('option', { value: '' }).update('-- default --')); + ComicImageOrdering.available_attachments.each(function(attachment) { + sel.appendChild(new Element('option', { value: attachment.id }).update(attachment.name)); + }); + }); +}; + +ComicImageOrdering.build_response = function() { + var output = []; + $('comic-ordering').select('.cp-comic-attachment').each(function(att) { + }); +}; + +ComicImageOrdering.setup = function() { + Sortable.create($('comic-ordering'), { + tag: 'div', + handle: 'div', + onUpdate: ComicImageOrdering.get_ordering + }); + + ComicImageOrdering.get_ordering(); + ComicImageOrdering.build_dropdowns(); +}; + +Event.observe(window, 'load', function() { + new Control.Slider('ordering-zoom-handle', 'ordering-zoom-slider', { + axis: 'vertical', + range: $R(40, 150), + sliderValue: ComicImageOrdering.slider_value, + onChange: function(v) { + v = 190 - v; + new Ajax.Request(ComicPressAdmin.ajax_uri, { + method: 'post', + parameters: { + 'cp[_nonce]': ComicPressAdmin.nonce, + 'cp[action]': 'zoom-slider', + 'cp[zoom_level]': v + } + }); + }, + onSlide: function(v) { + v = 190 - v; + v = Math.floor(v); + $$('#comic-ordering-holder img').each(function(i) { i.setAttribute('height', v); }); + } + }); +}); \ No newline at end of file diff --git a/test/ComicPressAdminTest.php b/test/ComicPressAdminTest.php index 7c9a2d4..4da6c47 100644 --- a/test/ComicPressAdminTest.php +++ b/test/ComicPressAdminTest.php @@ -288,6 +288,26 @@ class ComicPressAdminTest extends PHPUnit_Framework_TestCase { $this->admin->handle_update(); } + + function providerTestGetEditableAttachmentList() { + return array( + array( + array('attachment-1' => array('enabled' => false), 'attachment-2' => array('enabled' => true)), + array('attachment-1' => array('enabled' => false), 'attachment-2' => array('enabled' => true)), + ), + array( + array('attachment-1' => array('enabled' => false, 'children' => array('rss' => 'attachment-3')), 'attachment-2' => array('enabled' => true)), + array('attachment-1' => array('enabled' => false, 'children' => array('rss' => 'attachment-3')), 'attachment-2' => array('enabled' => true), 'attachment-3' => array('enabled' => true)), + ), + ); + } + + /** + * @dataProvider providerTestGetEditableAttachmentList + */ + function testGetEditableAttachmentList($list, $expected_result) { + $this->assertEquals($expected_result, $this->admin->get_editable_attachment_list($list)); + } } ?> diff --git a/test/ComicPressComicPostTest.php b/test/ComicPressComicPostTest.php index 526fb09..bb692ac 100644 --- a/test/ComicPressComicPostTest.php +++ b/test/ComicPressComicPostTest.php @@ -29,18 +29,13 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase { ), array( array('attachment-1'), - array('attachment-1' => array('enabled' => true, 'children' => array('rss' => array('attachment-2' => true)))), + array('attachment-1' => array('enabled' => true, 'children' => array('rss' => 'attachment-2'))), array('attachment-1' => array('enabled' => true)) ), - array( - array('attachment-1', 'attachment-2'), - array('attachment-1' => array('enabled' => true, 'children' => array('rss' => array('attachment-2' => true, 'attachment-3' => true)))), - array('attachment-1' => array('enabled' => true, 'children' => array('rss' => array('attachment-2' => true)))) - ), array( array('attachment-1', 'attachment-2', 'attachment-3'), - array('attachment-1' => array('enabled' => false, 'children' => array('rss' => array('attachment-2' => true)))), - array('attachment-1' => array('enabled' => false, 'children' => array('rss' => array('attachment-2' => true))), 'attachment-3' => array('enabled' => true)) + array('attachment-1' => array('enabled' => false, 'children' => array('rss' => 'attachment-2'))), + array('attachment-1' => array('enabled' => false, 'children' => array('rss' => 'attachment-2')), 'attachment-3' => array('enabled' => true)) ), ); } @@ -114,6 +109,32 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase { $this->assertEquals($expected_result, get_post_meta(1, 'comic_ordering', true)); } + function providerTestUpdatePostMediaData() { + return array( + array( + array( + array( + 'id' => 'attachment-1', + 'enabled' => 'yes', + 'children' => array('rss' => '', 'archive' => '') + ) + ), + array( + 'attachment-1' => array('enabled' => true) + ) + ) + ); + } + + /** + * @dataProvider providerTestUpdatePostMediaData + */ + function testUpdatePostMediaData($updated_ordering, $expected_meta) { + $this->p->post = (object)array('ID' => 1); + $this->assertEquals($expected_meta, $this->p->update_post_media_data($updated_ordering)); + $this->assertEquals($expected_meta, get_post_meta(1, 'image-ordering', true)); + } + function providerTestFindParents() { return array( array(