2007-09-25 21:28:09 +00:00
|
|
|
<?php
|
2008-04-11 06:12:07 +00:00
|
|
|
/**
|
|
|
|
* Name: Tagger
|
|
|
|
* Description: Advanced Tagging v2
|
|
|
|
* Author: Artanis (Erik Youngren) <artanis.00@gmail.com>
|
|
|
|
* Do not remove this notice.
|
|
|
|
*/
|
2007-09-28 05:10:44 +00:00
|
|
|
|
2008-08-23 12:08:19 +00:00
|
|
|
class Tagger implements Extension {
|
2007-09-25 21:28:09 +00:00
|
|
|
var $theme;
|
|
|
|
|
2008-12-13 04:14:30 +00:00
|
|
|
public function receive_event ($event) {
|
2007-09-25 21:28:09 +00:00
|
|
|
if(is_null($this->theme))
|
2008-09-06 16:59:02 +00:00
|
|
|
$this->theme = get_theme_object($this);
|
2007-09-28 05:10:44 +00:00
|
|
|
|
2008-08-22 09:41:30 +00:00
|
|
|
if($event instanceof DisplayingImageEvent) {
|
2007-10-23 22:08:22 +00:00
|
|
|
global $page, $config, $user;
|
|
|
|
|
2007-10-31 21:30:41 +00:00
|
|
|
if($config->get_bool("tag_edit_anon")
|
|
|
|
|| ($user->id != $config->get_int("anon_id"))
|
|
|
|
&& $config->get_bool("ext_tagger_enabled"))
|
|
|
|
{
|
2007-10-23 22:08:22 +00:00
|
|
|
$this->theme->build_tagger($page,$event);
|
2007-10-31 21:30:41 +00:00
|
|
|
}
|
|
|
|
}
|
2008-08-22 09:41:30 +00:00
|
|
|
|
|
|
|
if($event instanceof SetupBuildingEvent) {
|
2007-10-31 21:30:41 +00:00
|
|
|
$sb = new SetupBlock("Tagger");
|
|
|
|
$sb->add_bool_option("ext_tagger_enabled","Enable Tagger");
|
|
|
|
$sb->add_int_option("ext_tagger_search_delay","<br/>Delay queries by ");
|
|
|
|
$sb->add_label(" milliseconds.");
|
|
|
|
$sb->add_label("<br/>Limit queries returning more than ");
|
|
|
|
$sb->add_int_option("ext_tagger_tag_max");
|
|
|
|
$sb->add_label(" tags to ");
|
|
|
|
$sb->add_int_option("ext_tagger_limit");
|
|
|
|
$event->panel->add_block($sb);
|
2007-09-28 05:10:44 +00:00
|
|
|
}
|
2007-10-23 22:08:22 +00:00
|
|
|
}
|
2008-08-22 09:41:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
add_event_listener(new Tagger());
|
2007-10-23 22:08:22 +00:00
|
|
|
|
|
|
|
// Tagger AJAX back-end
|
2008-08-23 12:08:19 +00:00
|
|
|
class TaggerXML implements Extension {
|
|
|
|
public function receive_event(Event $event) {
|
2008-12-13 04:14:30 +00:00
|
|
|
if(($event instanceof PageRequestEvent)
|
|
|
|
&& $event->page_name == "tagger"
|
|
|
|
&& $event->get_arg(0) == "tags")
|
|
|
|
{
|
2007-09-25 21:28:09 +00:00
|
|
|
global $page;
|
2007-09-26 07:23:16 +00:00
|
|
|
|
2007-10-23 22:08:22 +00:00
|
|
|
//$match_tags = null;
|
|
|
|
//$image_tags = null;
|
|
|
|
$tags=null;
|
|
|
|
if (isset($_GET['s'])) { // tagger/tags[/...]?s=$string
|
|
|
|
// return matching tags in XML form
|
|
|
|
$tags = $this->match_tag_list($_GET['s']);
|
2008-12-13 04:14:30 +00:00
|
|
|
} else if($event->get_arg(1)) { // tagger/tags/$int
|
2007-10-23 22:08:22 +00:00
|
|
|
// return arg[1] AS image_id's tag list in XML form
|
2008-12-13 04:14:30 +00:00
|
|
|
$tags = $this->image_tag_list($event->get_arg(1));
|
2007-10-02 22:27:09 +00:00
|
|
|
}
|
2007-10-23 22:08:22 +00:00
|
|
|
|
|
|
|
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".
|
2007-10-31 21:30:41 +00:00
|
|
|
"<tags>".
|
2007-10-23 22:08:22 +00:00
|
|
|
$tags.
|
|
|
|
"</tags>";
|
|
|
|
|
|
|
|
$page->set_mode("data");
|
|
|
|
$page->set_type("text/xml");
|
|
|
|
$page->set_data($xml);
|
2007-09-26 07:23:16 +00:00
|
|
|
}
|
2007-10-23 22:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function match_tag_list ($s) {
|
|
|
|
global $database, $config, $event;
|
2007-09-26 07:23:16 +00:00
|
|
|
|
2007-10-31 21:30:41 +00:00
|
|
|
$max_rows = $config->get_int("ext_tagger_tag_max",30);
|
|
|
|
$limit_rows = $config->get_int("ext_tagger_limit",30);
|
|
|
|
|
2007-10-23 22:08:22 +00:00
|
|
|
$values = array();
|
|
|
|
|
|
|
|
// Match
|
2007-10-31 21:48:10 +00:00
|
|
|
$p = strlen($s) == 1? " ":"\_";
|
|
|
|
$sq = "%".$p.mysql_real_escape_string($s)."%";
|
2007-10-23 22:08:22 +00:00
|
|
|
$match = "concat(?,tag) LIKE ?";
|
|
|
|
array_push($values,$p,$sq);
|
|
|
|
// Exclude
|
|
|
|
// $exclude = $event->get_arg(1)? "AND NOT IN ".$this->image_tags($event->get_arg(1)) : null;
|
|
|
|
|
|
|
|
// Hidden Tags
|
|
|
|
$hidden = $config->get_string('ext-tagger_show-hidden','N')=='N' ?
|
|
|
|
"AND substring(tag,1,1) != '.'" : null;
|
|
|
|
|
|
|
|
$q_where = "WHERE {$match} {$hidden} AND count > 0";
|
|
|
|
|
|
|
|
// FROM based on return count
|
|
|
|
$q_from = null;
|
|
|
|
$count = $this->count($q_where,$values);
|
2007-10-31 21:30:41 +00:00
|
|
|
if ($count > $max_rows) {
|
2007-10-23 22:08:22 +00:00
|
|
|
$q_from = "FROM (SELECT * FROM `tags` {$q_where} ".
|
2007-10-31 21:30:41 +00:00
|
|
|
"ORDER BY count DESC LIMIT 0, {$limit_rows}) AS `c_tags`";
|
2007-10-23 22:08:22 +00:00
|
|
|
$q_where = null;
|
|
|
|
$count = array("max"=>$count);
|
|
|
|
} else {
|
|
|
|
$q_from = "FROM `tags`";
|
|
|
|
$count = null;
|
2007-10-02 18:44:29 +00:00
|
|
|
}
|
|
|
|
|
2007-10-23 22:08:22 +00:00
|
|
|
$tags = $database->Execute("
|
|
|
|
SELECT *
|
|
|
|
{$q_from}
|
|
|
|
{$q_where}
|
|
|
|
ORDER BY tag",
|
|
|
|
$values);
|
|
|
|
|
|
|
|
return $this->list_to_xml($tags,"search",$s,$count);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function image_tag_list ($image_id) {
|
|
|
|
global $database;
|
|
|
|
$tags = $database->Execute("
|
|
|
|
SELECT tags.*
|
|
|
|
FROM image_tags JOIN tags ON image_tags.tag_id = tags.id
|
|
|
|
WHERE image_id=? ORDER BY tag", array($image_id));
|
|
|
|
return $this->list_to_xml($tags,"image",$image_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function list_to_xml ($tags,$type,$query,$misc=null) {
|
|
|
|
$r = $tags->_numOfRows;
|
|
|
|
|
|
|
|
$s_misc = "";
|
|
|
|
if(!is_null($misc))
|
|
|
|
foreach($misc as $attr => $val) $s_misc .= " ".$attr."=\"".$val."\"";
|
|
|
|
|
|
|
|
$result = "<list id=\"$type\" query=\"$query\" rows=\"$r\"{$s_misc}>";
|
|
|
|
foreach($tags as $tag) {
|
|
|
|
$result .= $this->tag_to_xml($tag);
|
|
|
|
}
|
|
|
|
return $result."</list>";
|
|
|
|
}
|
|
|
|
|
|
|
|
private function tag_to_xml ($tag) {
|
|
|
|
return
|
|
|
|
"<tag ".
|
|
|
|
"id=\"".$tag['id']."\" ".
|
|
|
|
"count=\"".$tag['count']."\">".
|
|
|
|
html_escape($tag['tag']).
|
|
|
|
"</tag>";
|
|
|
|
}
|
|
|
|
|
|
|
|
private function count($query,$values) {
|
|
|
|
global $database;
|
|
|
|
return $database->Execute(
|
|
|
|
"SELECT COUNT(*) FROM `tags` $query",$values)->fields['COUNT(*)'];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function image_tags ($image_id) {
|
|
|
|
global $database;
|
|
|
|
$list = "(";
|
|
|
|
$i_tags = $database->Execute(
|
|
|
|
"SELECT tag_id FROM `image_tags` WHERE image_id=?",
|
|
|
|
array($image_id));
|
|
|
|
|
|
|
|
$b = false;
|
|
|
|
foreach($i_tags as $tag) {
|
|
|
|
if($b)
|
|
|
|
$list .= ",";
|
|
|
|
$b = true;
|
|
|
|
$list .= $tag['tag_id'];
|
|
|
|
|
2007-09-25 21:28:09 +00:00
|
|
|
}
|
2007-10-23 22:08:22 +00:00
|
|
|
$list .= ")";
|
2007-09-28 05:10:44 +00:00
|
|
|
|
2007-10-23 22:08:22 +00:00
|
|
|
return $list;
|
2007-09-25 21:28:09 +00:00
|
|
|
}
|
2007-10-23 22:08:22 +00:00
|
|
|
} add_event_listener( new taggerXML(),10);
|
2007-09-25 10:16:26 +00:00
|
|
|
?>
|