chipping away at comic image ordering

This commit is contained in:
John Bintz 2009-11-13 16:15:36 -05:00
parent eb5d2c945f
commit 3e1a2de3dc
8 changed files with 224 additions and 95 deletions

View File

@ -174,6 +174,18 @@ class ComicPressAdmin {
$media_upload_iframe_src = "media-upload.php?post_id=$uploading_iframe_ID"; $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"); $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) { if ($is_ajax === true) {
include(dirname(__FILE__) . '/partials/_comic-image-ordering-sorters.inc'); include(dirname(__FILE__) . '/partials/_comic-image-ordering-sorters.inc');
} else { } else {
@ -182,6 +194,18 @@ class ComicPressAdmin {
} }
// @codeCoverageIgnoreEnd // @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. * Create a dimension selector.
* @param string $root The field name root. * @param string $root The field name root.
@ -301,7 +325,7 @@ class ComicPressAdmin {
function _json_encode($data) { function _json_encode($data) {
if (function_exists('json_encode')) { if (function_exists('json_encode')) {
return json_decode($data); return json_encode($data);
} else { } else {
require_once(ABSPATH."/wp-includes/js/tinymce/plugins/spellchecker/classes/utils/JSON.php"); require_once(ABSPATH."/wp-includes/js/tinymce/plugins/spellchecker/classes/utils/JSON.php");
$j = new Moxiecode_JSON(); $j = new Moxiecode_JSON();

View File

@ -36,9 +36,7 @@ class ComicPressComicPost {
foreach ($current_ordering as $key => $properties) { foreach ($current_ordering as $key => $properties) {
$all_current_ids[$key] = true; $all_current_ids[$key] = true;
if (isset($properties['children'])) { if (isset($properties['children'])) {
foreach ($properties['children'] as $type => $kids) { foreach (array_values($properties['children']) as $kid) { $all_current_ids[$kid] = true; }
$all_current_ids = array_merge($all_current_ids, $kids);
}
} }
} }
$all_current_ids = array_keys($all_current_ids); $all_current_ids = array_keys($all_current_ids);
@ -59,12 +57,9 @@ class ComicPressComicPost {
} else { } else {
foreach ($current_ordering as $key => $properties) { foreach ($current_ordering as $key => $properties) {
if (isset($properties['children'])) { if (isset($properties['children'])) {
foreach ($properties['children'] as $type => $kids) { foreach ($properties['children'] as $type => $kid) {
if (isset($kids[$attachment_id])) { unset($kids[$attachment_id]); } if (!in_array($kid, $attachment_ids)) {
if (empty($kids)) {
unset($properties['children'][$type]); unset($properties['children'][$type]);
} else {
$properties['children'][$type] = $kids;
} }
} }
if (empty($properties['children'])) { if (empty($properties['children'])) {
@ -162,6 +157,32 @@ class ComicPressComicPost {
return $parents; 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;
}
} }
?> ?>

View File

@ -4,6 +4,7 @@ require_once(dirname(__FILE__) . '/../ComicPressBackend.inc');
class ComicPressBackendAttachment extends ComicPressBackend { class ComicPressBackendAttachment extends ComicPressBackend {
var $root_id = 'attachment'; var $root_id = 'attachment';
var $source_name = "Post attachment";
function generate_from_post($post) { function generate_from_post($post) {
$result = array(); $result = array();

View File

@ -10,23 +10,41 @@
</em></p> </em></p>
<?php if (!empty($ordering)) { ?> <?php if (!empty($ordering)) { ?>
<div class="comic-ordering"> <div id="comic-ordering">
<?php foreach ($ordering as $id => $info) { <?php foreach ($ordering as $id => $info) {
$result = ComicPressBackend::generate_from_id($id); $result = ComicPressBackend::generate_from_id($id);
if (!empty($result)) { if (!empty($result)) {
$info = $result->get_info(); ?> $info = $result->get_info(); ?>
<div class="cp-comic-attachment" id="attachment_<?php echo $id ?>"> <div class="cp-comic-attachment" id="attachment_<?php echo $id ?>">
<img src="<?php echo $result->url() ?>" border="0" height="<?php echo $zoom_level ?>" /> <img src="<?php echo $result->url() ?>" border="0" height="<?php echo $zoom_level ?>" />
<div> <div class="cp-comic-info">
<p>
<input type="checkbox" name="cp[attachments][<?php echo $id ?>][visible]" value="yes" /> <?php _e('Allow this image to be shown', 'comicpress') ?>
</p>
<?php if (isset($info['file'])) { ?> <?php if (isset($info['file'])) { ?>
<p><strong><?php echo basename($info['file']) ?></strong></p> <p><strong><?php echo $result->source_name ?>:</strong> <?php echo basename($info['file']) ?></p>
<?php } ?> <?php } ?>
<?php if (isset($info['width']) && isset($info['height'])) { ?> <?php if (isset($info['width']) && isset($info['height'])) { ?>
<p> <p>
<?php _e('Size:', 'comicpress') ?> <strong><?php _e('Size:', 'comicpress') ?></strong>
<?php printf('%dx%d', $info['width'], $info['height'] ) ?> <?php printf('%dx%d', $info['width'], $info['height'] ) ?>
</p> </p>
<?php } ?> <?php } ?>
<?php if (count($comicpress->comicpress_options['image_types']) > 1) { ?>
<a class="comic-ordering-show-associations" href="#"><?php _e('Edit associations', 'comicpress') ?></a>
<div class="comic-ordering-associations">
<ul>
<?php foreach ($comicpress->comicpress_options['image_types'] as $type => $info) { ?>
<?php if (!$info['default']) { ?>
<li>
<strong><?php echo $info['name'] ?>:</strong>
<select name="cp[attachments][<?php echo $id ?>][<?php echo $type ?>]"></select>
</li>
<?php } ?>
<?php } ?>
</ul>
</div>
<?php } ?>
</div> </div>
<br style="clear: both" /> <br style="clear: both" />
</div> </div>
@ -36,57 +54,8 @@
<?php } ?> <?php } ?>
<script type="text/javascript"> <script type="text/javascript">
(function() { ComicImageOrdering.slider_value = <?php echo 190 - $zoom_level ?>;
new Control.Slider('ordering-zoom-handle', 'ordering-zoom-slider', { ComicImageOrdering.available_attachments = <?php echo $this->_json_encode($available_attachments) ?>;
axis: 'vertical',
range: $R(40, 150),
sliderValue: <?php echo 190 - $zoom_level ?>,
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); });
}
});
var get_ordering = function() { ComicImageOrdering.setup();
var ordering = {};
$$('.comic-ordering').each(function(co) {
var matches = co.id.match(/-([^-]+)$/);
if (matches) {
var type = matches[1];
ordering[type] = [];
co.select('.cp-comic-attachment').each(function(att) {
var matches = att.id.match(/_([0-9]+)$/);
if (matches) {
ordering[type].push(matches[1]);
}
});
}
});
$('cp-comic-order').value = Object.toJSON(ordering);
};
$$('.comic-ordering').each(function(ord) {
if (ord.select('.cp-comic-attachment').length > 1) {
Sortable.create(ord.id, {
tag: 'div',
handle: 'div',
onUpdate: get_ordering
});
}
});
get_ordering();
}());
</script> </script>

View File

@ -107,3 +107,19 @@
display: block; display: block;
margin-bottom: 10px; 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;
}

View File

@ -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); });
}
});
});

View File

@ -288,6 +288,26 @@ class ComicPressAdminTest extends PHPUnit_Framework_TestCase {
$this->admin->handle_update(); $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));
}
} }
?> ?>

View File

@ -29,18 +29,13 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase {
), ),
array( array(
array('attachment-1'), 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('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(
array('attachment-1', 'attachment-2', 'attachment-3'), 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' => 'attachment-2'))),
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')), '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)); $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() { function providerTestFindParents() {
return array( return array(
array( array(