2009-10-18 21:05:10 +00:00
|
|
|
<?php
|
|
|
|
|
2009-11-01 18:15:59 +00:00
|
|
|
require_once('ComicPress.inc');
|
2009-10-20 02:41:37 +00:00
|
|
|
require_once('ComicPressDBInterface.inc');
|
|
|
|
|
2009-10-18 21:05:10 +00:00
|
|
|
class ComicPressStoryline {
|
2009-10-21 00:40:16 +00:00
|
|
|
var $_structure, $root_category;
|
2009-11-01 18:15:59 +00:00
|
|
|
|
|
|
|
function read_from_options() {
|
2009-11-01 19:04:15 +00:00
|
|
|
$this->create_structure($this->get_flattened_storyline());
|
2009-11-01 18:15:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_flattened_storyline() {
|
2009-11-01 19:04:15 +00:00
|
|
|
$comicpress = &ComicPress::get_instance();
|
2009-11-01 18:15:59 +00:00
|
|
|
return $comicpress->comicpress_options['storyline_order'];
|
|
|
|
}
|
|
|
|
|
|
|
|
function set_flattened_storyline($storyline) {
|
2009-11-01 19:04:15 +00:00
|
|
|
$comicpress = &ComicPress::get_instance();
|
2009-11-01 18:15:59 +00:00
|
|
|
$comicpress->comicpress_options['storyline_order'] = $storyline;
|
|
|
|
$comicpress->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
function set_order_via_flattened_storyline($order) {
|
|
|
|
$nodes = explode(',', $order);
|
|
|
|
$original_nodes = explode(',', $this->get_flattened_storyline());
|
|
|
|
|
|
|
|
$missing_good_nodes = array_diff($original_nodes, $nodes);
|
|
|
|
$any_bad_nodes = array_diff($nodes, $original_nodes);
|
|
|
|
|
|
|
|
if (empty($missing_good_nodes) && empty($any_bad_nodes)) {
|
|
|
|
$this->set_flattened_storyline($order);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-18 21:59:40 +00:00
|
|
|
/**
|
|
|
|
* Create a searchable structure from a node list.
|
|
|
|
* @param array $structure The structure to process.
|
|
|
|
* @return boolean True if the structure was valid.
|
|
|
|
*/
|
2009-10-18 21:05:10 +00:00
|
|
|
function create_structure($structure) {
|
2009-11-01 19:04:15 +00:00
|
|
|
$new_structure = array();
|
2009-10-18 21:05:10 +00:00
|
|
|
$parent = null;
|
2009-10-18 21:59:40 +00:00
|
|
|
$all_leaves = array();
|
2009-10-21 00:40:16 +00:00
|
|
|
$this->root_category = false;
|
2009-10-18 21:05:10 +00:00
|
|
|
|
|
|
|
$adjacents_by_parent = array();
|
|
|
|
|
2009-10-22 10:56:53 +00:00
|
|
|
if (is_string($structure)) {
|
|
|
|
$structure = explode(',', $structure);
|
|
|
|
}
|
2009-10-22 02:41:35 +00:00
|
|
|
|
2009-10-18 21:05:10 +00:00
|
|
|
if (is_array($structure)) {
|
|
|
|
$is_valid = true;
|
|
|
|
foreach ($structure as $branch) {
|
|
|
|
if (is_string($branch)) {
|
|
|
|
$parts = explode('/', $branch);
|
|
|
|
$valid = false;
|
|
|
|
if (count($parts) > 1) {
|
|
|
|
if ($parts[0] == '0') { $valid = true; }
|
|
|
|
}
|
|
|
|
if (!$valid) {
|
|
|
|
$is_valid = false; break;
|
|
|
|
} else {
|
|
|
|
$data = array();
|
|
|
|
$leaf = end($parts);
|
2009-10-18 21:59:40 +00:00
|
|
|
$all_leaves[] = $leaf;
|
2009-10-18 21:05:10 +00:00
|
|
|
|
|
|
|
if (count($parts) > 2) {
|
|
|
|
$parent = $parts[count($parts) - 2];
|
|
|
|
|
|
|
|
if (!isset($adjacents_by_parent[$parent])) {
|
|
|
|
$adjacents_by_parent[$parent] = array();
|
|
|
|
}
|
|
|
|
$adjacents_by_parent[$parent][] = $leaf;
|
|
|
|
|
|
|
|
$data['parent'] = $parent;
|
2009-10-21 00:40:16 +00:00
|
|
|
} else {
|
|
|
|
$this->root_category = $leaf;
|
2009-10-18 21:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$new_structure[$leaf] = $data;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$is_valid = false; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($is_valid) {
|
2009-10-18 21:59:40 +00:00
|
|
|
for ($i = 0; $i < count($all_leaves); ++$i) {
|
2009-10-21 00:40:16 +00:00
|
|
|
foreach (array('previous' => -1, 'next' => 1) as $type => $dir) {
|
2009-10-18 21:59:40 +00:00
|
|
|
if (isset($all_leaves[$i + $dir])) {
|
|
|
|
$new_structure[$all_leaves[$i]][$type] = $all_leaves[$i + $dir];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-10-22 10:56:53 +00:00
|
|
|
|
2009-10-18 21:05:10 +00:00
|
|
|
$this->_structure = $new_structure;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return is_array($this->_structure);
|
|
|
|
}
|
2009-10-20 02:41:37 +00:00
|
|
|
|
2009-10-18 21:59:40 +00:00
|
|
|
function _get_field($field, $id) {
|
|
|
|
if (isset($this->_structure)) {
|
|
|
|
if (isset($this->_structure[$id])) {
|
|
|
|
if (isset($this->_structure[$id][$field])) {
|
|
|
|
return $this->_structure[$id][$field];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function parent($id) { return $this->_get_field('parent', $id); }
|
|
|
|
function previous($id) { return $this->_get_field('previous', $id); }
|
|
|
|
function next($id) { return $this->_get_field('next', $id); }
|
|
|
|
function valid($id) {
|
|
|
|
if (isset($this->_structure[$id])) {
|
2009-10-31 20:04:29 +00:00
|
|
|
return array_keys($this->_structure[$id]);
|
2009-10-18 21:59:40 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-10-20 02:41:37 +00:00
|
|
|
/**
|
|
|
|
* Get the valid navigation directions for a particular post.
|
|
|
|
*/
|
2009-10-20 00:56:32 +00:00
|
|
|
function get_valid_nav($post_id) {
|
2009-10-20 02:41:37 +00:00
|
|
|
if (($category = $this->get_valid_post_category($post_id)) !== false) {
|
|
|
|
return $this->valid($category);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-10-18 21:59:40 +00:00
|
|
|
|
2009-10-20 02:41:37 +00:00
|
|
|
/**
|
|
|
|
* Get the valid comic category for this post.
|
|
|
|
*/
|
|
|
|
function get_valid_post_category($post_id) {
|
|
|
|
$result = false;
|
|
|
|
|
2009-10-18 21:59:40 +00:00
|
|
|
foreach (wp_get_post_categories($post_id) as $category) {
|
2009-10-20 02:41:37 +00:00
|
|
|
if ($this->valid($category)) {
|
|
|
|
if ($result) { return false; }
|
2009-10-18 21:59:40 +00:00
|
|
|
|
2009-10-20 02:41:37 +00:00
|
|
|
$result = $category;
|
2009-10-18 21:59:40 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-20 02:41:37 +00:00
|
|
|
return $result;
|
|
|
|
}
|
2009-10-22 02:41:35 +00:00
|
|
|
|
|
|
|
function get_comic_categories() {
|
|
|
|
return array_keys($this->_structure);
|
|
|
|
}
|
2009-10-31 20:04:29 +00:00
|
|
|
|
|
|
|
function get_simple_storyline() {
|
|
|
|
$simple_storyline = array('0' => array());
|
|
|
|
foreach ($this->_structure as $category_id => $adjacents) {
|
|
|
|
$parent = 0;
|
|
|
|
if (isset($adjacents['parent'])) { $parent = $adjacents['parent']; }
|
|
|
|
if (!isset($simple_storyline[$parent])) {
|
|
|
|
$simple_storyline[$parent] = array();
|
|
|
|
}
|
|
|
|
$simple_storyline[$parent][$category_id] = true;
|
|
|
|
}
|
|
|
|
|
2009-11-01 18:15:59 +00:00
|
|
|
return $this->_merge_simple_storyline($simple_storyline);
|
|
|
|
}
|
|
|
|
|
2009-11-06 02:15:12 +00:00
|
|
|
function get_category_simple_structure($parent = null) {
|
2009-11-01 18:15:59 +00:00
|
|
|
$structure = array();
|
|
|
|
foreach (get_all_category_ids() as $category_id) {
|
|
|
|
$category = get_category($category_id);
|
|
|
|
if (!isset($structure[$category->parent])) {
|
|
|
|
$structure[$category->parent] = array();
|
|
|
|
}
|
|
|
|
$structure[$category->parent][$category_id] = true;
|
|
|
|
}
|
|
|
|
$structure = $this->_merge_simple_storyline($structure);
|
2009-11-06 02:15:12 +00:00
|
|
|
if (!empty($parent)) {
|
|
|
|
if (isset($structure[0])) {
|
|
|
|
foreach ($structure[0] as $key => $children) {
|
|
|
|
if ($key != $parent) { unset($structure[0][$key]); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-11-01 18:15:59 +00:00
|
|
|
return $structure;
|
|
|
|
}
|
2009-11-01 19:04:15 +00:00
|
|
|
|
2009-11-06 02:15:12 +00:00
|
|
|
function get_category_flattened($parent = null) {
|
2009-11-01 19:04:15 +00:00
|
|
|
return $this->flatten_simple_storyline($this->get_category_simple_structure($parent));
|
|
|
|
}
|
2009-11-01 18:15:59 +00:00
|
|
|
|
|
|
|
function _merge_simple_storyline($simple_storyline) {
|
2009-10-31 20:04:29 +00:00
|
|
|
while (count($simple_storyline) > 0) {
|
|
|
|
$merge_found = false;
|
|
|
|
foreach ($simple_storyline as $parent => $children) {
|
|
|
|
$has_no_descendents = true;
|
|
|
|
foreach (array_keys($children) as $child) {
|
|
|
|
if (is_numeric($child)) {
|
|
|
|
if (isset($simple_storyline[$child])) {
|
|
|
|
$has_no_descendents = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($has_no_descendents) {
|
|
|
|
$merge_found = $parent; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($merge_found !== false) {
|
|
|
|
foreach ($simple_storyline as $parent => $children) {
|
|
|
|
if (isset($children[$merge_found])) {
|
|
|
|
$simple_storyline[$parent][$merge_found] = $simple_storyline[$merge_found];
|
|
|
|
unset($simple_storyline[$merge_found]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$merge_found) { break; }
|
|
|
|
}
|
|
|
|
return $simple_storyline;
|
|
|
|
}
|
2009-11-01 18:15:59 +00:00
|
|
|
|
2009-11-01 19:04:15 +00:00
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
2009-11-06 02:15:12 +00:00
|
|
|
$all_categories_flattened = $this->get_category_flattened();
|
2009-11-01 19:04:15 +00:00
|
|
|
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2009-11-01 18:15:59 +00:00
|
|
|
function normalize_flattened_storyline($storyline, $comic_categories) {
|
|
|
|
$storyline_nodes = explode(",", $storyline);
|
|
|
|
$category_nodes = explode(",", $comic_categories);
|
2009-11-06 02:15:12 +00:00
|
|
|
|
2009-11-01 18:15:59 +00:00
|
|
|
$missing_from_storyline = array_diff($category_nodes, $storyline_nodes);
|
|
|
|
$extra_in_storyline = array_diff($storyline_nodes, $category_nodes);
|
|
|
|
|
|
|
|
if (!empty($missing_from_storyline)) {
|
2009-11-01 19:04:15 +00:00
|
|
|
$missing_from_storyline = $this->_length_sort($missing_from_storyline);
|
2009-11-01 18:15:59 +00:00
|
|
|
foreach ($missing_from_storyline as $node) {
|
|
|
|
$parent_pattern = implode('/', array_slice(explode('/', $node), 0, -1));
|
|
|
|
$last = null;
|
|
|
|
for ($i = 0, $il = count($storyline_nodes); $i < $il; ++$i) {
|
|
|
|
if (strpos($storyline_nodes[$i], $parent_pattern) === 0) {
|
|
|
|
$last = $i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!is_null($last)) {
|
|
|
|
array_splice($storyline_nodes, $last + 1, 0, array($node));
|
2009-11-01 19:04:15 +00:00
|
|
|
} else {
|
|
|
|
$storyline_nodes[] = $node;
|
2009-11-01 18:15:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($extra_in_storyline)) {
|
|
|
|
$new = array();
|
|
|
|
foreach ($storyline_nodes as $node) {
|
|
|
|
if (!in_array($node, $extra_in_storyline)) {
|
|
|
|
$new[] = $node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$storyline_nodes = $new;
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode(',', $storyline_nodes);
|
|
|
|
}
|
|
|
|
|
|
|
|
function flatten_simple_storyline($storyline) {
|
|
|
|
return implode(',', $this->_follow_simple_storyline($storyline));
|
|
|
|
}
|
|
|
|
|
|
|
|
function _follow_simple_storyline($storyline, $parent = null) {
|
|
|
|
$output = array();
|
|
|
|
foreach ($storyline as $key => $children) {
|
|
|
|
if (is_null($parent)) {
|
|
|
|
$new_parent = $key;
|
|
|
|
} else {
|
|
|
|
$new_parent = $parent . '/' . $key;
|
|
|
|
$output[] = $new_parent;
|
|
|
|
}
|
|
|
|
if (is_array($children)) {
|
|
|
|
$output = array_merge($output, $this->_follow_simple_storyline($children, $new_parent));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $output;
|
|
|
|
}
|
2009-10-18 21:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|