chipping away at comic image ordering
This commit is contained in:
parent
eb5d2c945f
commit
3e1a2de3dc
@ -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();
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -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();
|
||||
|
@ -1,92 +1,61 @@
|
||||
<p><em>
|
||||
<?php _e('Drag and drop the comic files below to change the order in which they\'ll appear.', 'comicpress') ?>
|
||||
<?php
|
||||
printf(
|
||||
__('To upload new images, use the %s.', 'comicpress'),
|
||||
'<a href="' . $image_upload_iframe_src . '" class="thickbox" onclick="return false;">' . __('Image Uploader', 'comicpress') . '</a>'
|
||||
)
|
||||
?>
|
||||
<?php _e('Click the Refesh button underneath the zoom slider if you\'ve changed the images attached to this post.', 'comicpress') ?>
|
||||
<?php _e('Drag and drop the comic files below to change the order in which they\'ll appear.', 'comicpress') ?>
|
||||
<?php
|
||||
printf(
|
||||
__('To upload new images, use the %s.', 'comicpress'),
|
||||
'<a href="' . $image_upload_iframe_src . '" class="thickbox" onclick="return false;">' . __('Image Uploader', 'comicpress') . '</a>'
|
||||
)
|
||||
?>
|
||||
<?php _e('Click the Refesh button underneath the zoom slider if you\'ve changed the images attached to this post.', 'comicpress') ?>
|
||||
|
||||
</em></p>
|
||||
<?php if (!empty($ordering)) { ?>
|
||||
<div class="comic-ordering">
|
||||
<div id="comic-ordering">
|
||||
<?php foreach ($ordering as $id => $info) {
|
||||
$result = ComicPressBackend::generate_from_id($id);
|
||||
if (!empty($result)) {
|
||||
$info = $result->get_info(); ?>
|
||||
<div class="cp-comic-attachment" id="attachment_<?php echo $id ?>">
|
||||
<img src="<?php echo $result->url() ?>" border="0" height="<?php echo $zoom_level ?>" />
|
||||
<div>
|
||||
<?php if (isset($info['file'])) { ?>
|
||||
<p><strong><?php echo basename($info['file']) ?></strong></p>
|
||||
<?php } ?>
|
||||
<?php if (isset($info['width']) && isset($info['height'])) { ?>
|
||||
<p>
|
||||
<?php _e('Size:', 'comicpress') ?>
|
||||
<?php printf('%dx%d', $info['width'], $info['height'] ) ?>
|
||||
</p>
|
||||
<?php } ?>
|
||||
</div>
|
||||
<br style="clear: both" />
|
||||
</div>
|
||||
$info = $result->get_info(); ?>
|
||||
<div class="cp-comic-attachment" id="attachment_<?php echo $id ?>">
|
||||
<img src="<?php echo $result->url() ?>" border="0" height="<?php echo $zoom_level ?>" />
|
||||
<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'])) { ?>
|
||||
<p><strong><?php echo $result->source_name ?>:</strong> <?php echo basename($info['file']) ?></p>
|
||||
<?php } ?>
|
||||
<?php if (isset($info['width']) && isset($info['height'])) { ?>
|
||||
<p>
|
||||
<strong><?php _e('Size:', 'comicpress') ?></strong>
|
||||
<?php printf('%dx%d', $info['width'], $info['height'] ) ?>
|
||||
</p>
|
||||
<?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>
|
||||
<br style="clear: both" />
|
||||
</div>
|
||||
<?php } ?>
|
||||
<?php } ?>
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
<script type="text/javascript">
|
||||
(function() {
|
||||
new Control.Slider('ordering-zoom-handle', 'ordering-zoom-slider', {
|
||||
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); });
|
||||
}
|
||||
});
|
||||
ComicImageOrdering.slider_value = <?php echo 190 - $zoom_level ?>;
|
||||
ComicImageOrdering.available_attachments = <?php echo $this->_json_encode($available_attachments) ?>;
|
||||
|
||||
var get_ordering = function() {
|
||||
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();
|
||||
}());
|
||||
ComicImageOrdering.setup();
|
||||
</script>
|
||||
|
@ -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;
|
||||
}
|
@ -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); });
|
||||
}
|
||||
});
|
||||
});
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
@ -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(
|
||||
|
Loading…
Reference in New Issue
Block a user