drag and drop category sorting working
This commit is contained in:
parent
b96082826a
commit
18e74f459c
@ -10,7 +10,7 @@ class ComicPressAddonCore extends ComicPressAddon {
|
||||
* @param ComicPress $comicpress The master ComicPress object.
|
||||
*/
|
||||
function init() {
|
||||
$this->comicpress = ComicPress::get_instance();
|
||||
$this->comicpress = &ComicPress::get_instance();
|
||||
|
||||
add_action('admin_menu', array(&$this, 'setup_admin_interface'));
|
||||
add_filter('attachment_fields_to_edit', array(&$this, 'setup_comic_metadata_buttons'), 10, 2);
|
||||
@ -300,6 +300,7 @@ class ComicPressAddonCore extends ComicPressAddon {
|
||||
$nonce = wp_create_nonce('comicpress');
|
||||
$root_categories = $this->get_root_categories();
|
||||
$storyline = new ComicPressStoryline();
|
||||
$storyline->normalize();
|
||||
$storyline->read_from_options();
|
||||
|
||||
include(dirname(__FILE__) . '/partials/options-admin.inc');
|
||||
@ -489,7 +490,7 @@ class ComicPressAddonCore extends ComicPressAddon {
|
||||
break;
|
||||
case 'storyline_order':
|
||||
$storyline = new ComicPressStoryline();
|
||||
var_dump($storyline->set_flattened_storyline_order($info[$option]));
|
||||
$storyline->normalize($info[$option]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -30,14 +30,14 @@ class ComicPress {
|
||||
var $partial_paths = array();
|
||||
var $layouts = null;
|
||||
|
||||
function get_instance() {
|
||||
function &get_instance() {
|
||||
static $instance;
|
||||
|
||||
if (empty($instance)) {
|
||||
$instance = &new ComicPress();
|
||||
if (!$instance) {
|
||||
$instance = array(new ComicPress());
|
||||
}
|
||||
|
||||
return $instance;
|
||||
return $instance[0];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -7,18 +7,16 @@ class ComicPressStoryline {
|
||||
var $_structure, $root_category;
|
||||
|
||||
function read_from_options() {
|
||||
$comicpress = ComicPress::get_instance();
|
||||
var_dump($comicpress);
|
||||
$this->create_structure($comicpress->comicpress_options['storyline_order']);
|
||||
$this->create_structure($this->get_flattened_storyline());
|
||||
}
|
||||
|
||||
function get_flattened_storyline() {
|
||||
$comicpress = ComicPress::get_instance();
|
||||
$comicpress = &ComicPress::get_instance();
|
||||
return $comicpress->comicpress_options['storyline_order'];
|
||||
}
|
||||
|
||||
function set_flattened_storyline($storyline) {
|
||||
$comicpress = ComicPress::get_instance();
|
||||
$comicpress = &ComicPress::get_instance();
|
||||
$comicpress->comicpress_options['storyline_order'] = $storyline;
|
||||
$comicpress->save();
|
||||
}
|
||||
@ -44,7 +42,7 @@ class ComicPressStoryline {
|
||||
* @return boolean True if the structure was valid.
|
||||
*/
|
||||
function create_structure($structure) {
|
||||
$new_structure = array();
|
||||
$new_structure = array();
|
||||
$parent = null;
|
||||
$all_leaves = array();
|
||||
$this->root_category = false;
|
||||
@ -188,6 +186,10 @@ class ComicPressStoryline {
|
||||
return $structure;
|
||||
}
|
||||
|
||||
function get_category_flattened($parent) {
|
||||
return $this->flatten_simple_storyline($this->get_category_simple_structure($parent));
|
||||
}
|
||||
|
||||
function _merge_simple_storyline($simple_storyline) {
|
||||
while (count($simple_storyline) > 0) {
|
||||
$merge_found = false;
|
||||
@ -219,6 +221,40 @@ class ComicPressStoryline {
|
||||
return $simple_storyline;
|
||||
}
|
||||
|
||||
/**
|
||||
* Integrates a bunch of other things.
|
||||
*/
|
||||
function normalize($flattened_storyline = null, $set = true) {
|
||||
$comicpress = ComicPress::get_instance();
|
||||
if (is_null($flattened_storyline)) {
|
||||
$flattened_storyline = $this->get_flattened_storyline();
|
||||
}
|
||||
$all_categories_flattened = $this->get_category_flattened($comicpress->comicpress_options['comic_category_id']);
|
||||
|
||||
$result = $this->normalize_flattened_storyline($flattened_storyline, $all_categories_flattened);
|
||||
if ($set) {
|
||||
$this->set_flattened_storyline($result);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
function _length_sort($parts) {
|
||||
$new = array();
|
||||
foreach ($parts as $part) {
|
||||
$p = explode('/', $part);
|
||||
if (!isset($new[count($p)])) {
|
||||
$new[count($p)] = array();
|
||||
}
|
||||
$new[count($p)][] = $part;
|
||||
}
|
||||
ksort($new);
|
||||
$output = array();
|
||||
foreach (array_values($new) as $values) {
|
||||
$output = array_merge($output, $values);
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
function normalize_flattened_storyline($storyline, $comic_categories) {
|
||||
$storyline_nodes = explode(",", $storyline);
|
||||
$category_nodes = explode(",", $comic_categories);
|
||||
@ -227,6 +263,7 @@ class ComicPressStoryline {
|
||||
$extra_in_storyline = array_diff($storyline_nodes, $category_nodes);
|
||||
|
||||
if (!empty($missing_from_storyline)) {
|
||||
$missing_from_storyline = $this->_length_sort($missing_from_storyline);
|
||||
foreach ($missing_from_storyline as $node) {
|
||||
$parent_pattern = implode('/', array_slice(explode('/', $node), 0, -1));
|
||||
$last = null;
|
||||
@ -237,6 +274,8 @@ class ComicPressStoryline {
|
||||
}
|
||||
if (!is_null($last)) {
|
||||
array_splice($storyline_nodes, $last + 1, 0, array($node));
|
||||
} else {
|
||||
$storyline_nodes[] = $node;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ Storyline.setup = function() {
|
||||
placeholder: 'placeholder',
|
||||
forcePlaceholderSize: true,
|
||||
opacity: 0.4,
|
||||
change: function(e, ui) {
|
||||
stop: function(e, ui) {
|
||||
Storyline.get_order();
|
||||
}
|
||||
});
|
||||
|
@ -258,6 +258,7 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
|
||||
array('0/1,0/1/2,0/1/2/4', '0/1,0/1/2,0/1/2/4,0/1/2/3'),
|
||||
array('0/1,0/1/2,0/1/2/4,0/1/2/3,0/1/5', '0/1,0/1/2,0/1/2/4,0/1/2/3'),
|
||||
array('0/1,0/1/2,0/1/2/3,0/1/5', '0/1,0/1/2,0/1/2/3,0/1/2/4'),
|
||||
array('', '0/1,0/1/2,0/1/2/3,0/1/2/4'),
|
||||
);
|
||||
}
|
||||
|
||||
@ -285,6 +286,18 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
function testLengthSort() {
|
||||
$data = array(
|
||||
'0/1', '0/1/3', '0/1/3/6', '0/1/3/7', '0/1/4', '0/1/4/2', '0/1/4/3'
|
||||
);
|
||||
|
||||
$expected_result = array(
|
||||
'0/1', '0/1/3', '0/1/4', '0/1/3/6', '0/1/3/7', '0/1/4/2', '0/1/4/3'
|
||||
);
|
||||
|
||||
$this->assertEquals($expected_result, $this->css->_length_sort($data));
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
Reference in New Issue
Block a user