2010-01-28 01:21:53 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once('ComicPressStoryline.inc');
|
|
|
|
|
|
|
|
class ComicPressTagBuilderFactory {
|
|
|
|
private $storyline, $dbi;
|
|
|
|
|
|
|
|
public function __construct($dbi = null) {
|
|
|
|
$this->storyline = new ComicPressStoryline();
|
|
|
|
$this->storyline->read_from_options();
|
|
|
|
|
|
|
|
if (is_null($dbi)) {
|
|
|
|
$this->dbi = ComicPressDBInterface::get_instance();
|
|
|
|
} else {
|
|
|
|
$this->dbi = $dbi;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __call($method, $arguments) {
|
|
|
|
global $post;
|
|
|
|
|
|
|
|
$a = new ComicPressTagBuilder($post, $this->storyline, $this->dbi);
|
|
|
|
return call_user_func_array(array($a, $method), $arguments);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ComicPressTagBuilder {
|
|
|
|
public $categories, $restrictions, $storyline, $dbi, $parent_post, $post;
|
|
|
|
|
|
|
|
public function __construct($parent_post, $storyline, $dbi) {
|
|
|
|
$this->restrictions = array();
|
|
|
|
$this->storyline = $storyline;
|
|
|
|
$this->dbi = $dbi;
|
|
|
|
$this->parent_post = $parent_post;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __call($method, $arguments) {
|
2010-01-30 16:11:07 +00:00
|
|
|
$ok = false;
|
|
|
|
|
2010-01-28 01:21:53 +00:00
|
|
|
switch ($method) {
|
|
|
|
case 'next':
|
|
|
|
case 'previous':
|
|
|
|
case 'first':
|
|
|
|
case 'last':
|
|
|
|
$this->post = call_user_func(array($this->dbi, "get_${method}_post"), $this->storyline->build_from_restrictions($this->restrictions), $this->parent_post);
|
2010-01-30 16:11:07 +00:00
|
|
|
$ok = true;
|
2010-01-28 01:21:53 +00:00
|
|
|
break;
|
2010-01-28 03:05:29 +00:00
|
|
|
case 'in':
|
|
|
|
if (!isset($arguments[0])) { throw new Exception('Need to specify a category'); }
|
|
|
|
$this->restrictions[] = array('child_of', $arguments[0]);
|
2010-01-30 16:11:07 +00:00
|
|
|
$ok = true;
|
2010-01-28 03:05:29 +00:00
|
|
|
break;
|
2010-01-30 15:54:23 +00:00
|
|
|
case 'id':
|
|
|
|
return $this->post->ID;
|
|
|
|
case 'title':
|
|
|
|
return $this->post->post_title;
|
|
|
|
case 'timestamp':
|
|
|
|
return strtotime($this->post->post_date);
|
2010-01-30 16:11:07 +00:00
|
|
|
case 'date':
|
|
|
|
if (isset($arguments[0])) {
|
|
|
|
return date($arguments[0], strtotime($this->post->post_date));
|
|
|
|
}
|
|
|
|
break;
|
2010-01-30 15:54:23 +00:00
|
|
|
case 'permalink':
|
|
|
|
return get_permalink($this->post->ID);
|
2010-01-30 15:55:39 +00:00
|
|
|
case 'post':
|
|
|
|
return $this->post;
|
2010-01-30 15:54:23 +00:00
|
|
|
default:
|
|
|
|
$methods = $this->parse_method($method);
|
|
|
|
if (!empty($methods)) {
|
|
|
|
foreach ($methods as $method_info) {
|
|
|
|
$new_method = array_shift($method_info);
|
|
|
|
call_user_func_array(array($this, $new_method), $method_info);
|
|
|
|
}
|
2010-01-30 16:11:07 +00:00
|
|
|
$ok = true;
|
2010-01-30 15:54:23 +00:00
|
|
|
}
|
|
|
|
break;
|
2010-01-28 01:21:53 +00:00
|
|
|
}
|
|
|
|
|
2010-01-30 16:11:07 +00:00
|
|
|
if ($ok) {
|
|
|
|
return $this;
|
|
|
|
} else {
|
|
|
|
throw new ComicPressException("${method} isn't allowed at this point");
|
|
|
|
}
|
2010-01-28 01:21:53 +00:00
|
|
|
}
|
2010-01-30 15:54:23 +00:00
|
|
|
|
2010-01-30 16:25:49 +00:00
|
|
|
const START_PARSE = 'start parse';
|
|
|
|
const HAS_POST_METHOD = 'has post method';
|
|
|
|
const HAS_EXTRACT_METHOD = 'has extract method';
|
|
|
|
|
2010-01-30 15:54:23 +00:00
|
|
|
public function parse_method($method_name) {
|
|
|
|
$methods = array();
|
|
|
|
|
|
|
|
$parts = explode('_', $method_name);
|
|
|
|
|
2010-01-30 16:25:49 +00:00
|
|
|
$state = self::START_PARSE;
|
|
|
|
|
2010-01-30 15:54:23 +00:00
|
|
|
$post_method = null;
|
2010-01-30 16:25:49 +00:00
|
|
|
$extract_method = null;
|
2010-01-30 15:54:23 +00:00
|
|
|
|
|
|
|
while (!empty($parts)) {
|
|
|
|
$current = strtolower(array_shift($parts));
|
|
|
|
|
2010-01-30 16:25:49 +00:00
|
|
|
if (in_array($state, array(self::HAS_POST_METHOD, self::HAS_EXTRACT_METHOD))) {
|
2010-01-30 15:54:23 +00:00
|
|
|
if ($current == "in") {
|
2010-01-30 16:25:49 +00:00
|
|
|
if (empty($parts)) {
|
|
|
|
throw new ComicPressException("No category specified in tag ${method_name}");
|
|
|
|
}
|
2010-01-30 15:54:23 +00:00
|
|
|
$is_id = false;
|
|
|
|
if (count($parts) == 1) {
|
|
|
|
if (is_numeric($parts[0])) {
|
|
|
|
$methods[] = array('in', $parts[0]);
|
|
|
|
$is_id = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$is_id) {
|
|
|
|
$methods[] = array('in', implode('-', $parts));
|
|
|
|
}
|
2010-01-30 16:25:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_array($current, array('id', 'permalink', 'title', 'timestamp', 'date'))) {
|
|
|
|
if ($state == self::HAS_EXTRACT_METHOD) {
|
|
|
|
throw new ComicPressException('Only one extract method can be specified');
|
|
|
|
}
|
|
|
|
$extract_method = $current;
|
|
|
|
$state = self::HAS_EXTRACT_METHOD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($state == self::START_PARSE) {
|
|
|
|
if (in_array($current, array('first', 'previous', 'next', 'last'))) {
|
|
|
|
$post_method = $current;
|
|
|
|
$state = self::HAS_POST_METHOD;
|
|
|
|
} else {
|
|
|
|
throw new ComicPressException("${current} isn't allowed at this point");
|
2010-01-30 15:54:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-30 16:25:49 +00:00
|
|
|
foreach (array('post', 'extract') as $prefix) {
|
|
|
|
if (!is_null(${"${prefix}_method"})) {
|
|
|
|
$methods[] = array(${"${prefix}_method"});
|
|
|
|
}
|
2010-01-30 15:54:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $methods;
|
|
|
|
}
|
2010-01-28 01:21:53 +00:00
|
|
|
}
|