From 46f17463376ae7d47a744f0181a16ea4e502aa66 Mon Sep 17 00:00:00 2001 From: atomicdryad Date: Wed, 22 Dec 2010 17:10:24 -0600 Subject: [PATCH 1/3] Extension: tag_editcloud: Add or remove tags to the editor via clicking --- contrib/tag_editcloud/main.php | 107 ++++++++++++++++++++++++++++++++ contrib/tag_editcloud/script.js | 46 ++++++++++++++ contrib/tag_editcloud/style.css | 20 ++++++ 3 files changed, 173 insertions(+) create mode 100644 contrib/tag_editcloud/main.php create mode 100644 contrib/tag_editcloud/script.js create mode 100644 contrib/tag_editcloud/style.css diff --git a/contrib/tag_editcloud/main.php b/contrib/tag_editcloud/main.php new file mode 100644 index 00000000..a75cb4fe --- /dev/null +++ b/contrib/tag_editcloud/main.php @@ -0,0 +1,107 @@ +board prefs + * toggle sorting method via javascript || usepref(todo2: port userpref) + * colorize used tags in cloud || always show used tags in front of cloud + * theme junk + */ +class TagEditCloud implements Extension { + var $theme; + + public function receive_event(Event $event) { + global $config, $database, $page, $user; + //if(is_null($this->theme)) $this->theme = get_theme_object($this); + + if($event instanceof ImageInfoBoxBuildingEvent) { + if($this->can_tag($event->image)) { + $cfgstub_sortbyname=false; // FIXME + $cfgstub_showtop=40; // Derp + $cfgstub_minuse=2; // Derp + if($cfgstub_sortbyname) { + $event->add_part($this->build_tag_map($event->image,$cfgstub_minuse,false),40); + } else { + $event->add_part($this->build_tag_map($event->image,$cfgstub_showtop,4096),40); + } + } + } + } + + private function tag_link($tag) { + $u_tag = url_escape($tag); + return make_link("post/list/$u_tag/1"); + } +///// build_tag_map: output cloud of clickable tags +// build_tag_map($image|false, $defcount, $maxcount|false) -- taglist sorted by usage, displaying $defcount by default, up to $maxcount via toggle. +// build_tag_map($image|false, $minusage|false) -- taglist sorted by alpha, only showing tags with usage >= $minusage + + private function build_tag_map($image,$defcount,$maxcount) { // + + global $database; + $html="";$cloud="";$precloud=""; + $itags=Array(); + $tags_min=1; + $alphasort=false; + if(!is_int($defcount)) $defcount=20; + if(!is_int($maxcount)) { // Derp this is pretty cheesy. + $maxcount=4096; // Hurrrr + $tags_min=$defcount; + $alphasort=true; + } + + if ((gettype($image) == 'object') && (isset($image->tag_array)) && ($itags=$image->tag_array)) $itags=array_fill_keys(array_values($itags),true); + + $result = $database->execute(" SELECT tag, FLOOR(LOG(2.7, LOG(2.7, count - ? + 1)+1)*1.5*100)/100 AS scaled, count + FROM tags WHERE count >= ? ORDER BY ". + (!$alphasort ? "count DESC":"tag"). + " limit $maxcount", + array($tags_min,$tags_min) + ); + + + + $tag_data = $result->GetArray(); + $counter=1; + foreach($tag_data as $row) { + if((!$alphasort)&&($counter==$defcount)) $cloud .= "
"; + $h_tag = html_escape($row['tag']); + $size = sprintf("%.2f", (float)$row['scaled']/2); + $usecount=$row['count']; + $link = $this->tag_link($row['tag']); + if($size<0.5) $size = 0.5; + if(isset($itags[$row['tag']])) { +// if($size<0.75) $size = 0.75; + $precloud .= " $h_tag \n"; + } else { + $counter++; + $cloud .= " $h_tag \n"; + } + } + if ($precloud != '') $html .= "
$precloud
"; + $html .="
$cloud
"; + $rem=count($tag_data)-$defcount; +// $script = ""; +// $html.=$script; + if((!$alphasort)&&($counter>=$defcount)) $html .= "

[show $rem more tags]"; +// $html.='
'.var_export($itags,true).'
'; + return "
$html
"; // FIXME: stupidasallhell + } + + + private function can_tag($image) { + global $config, $user; + return ( + ($config->get_bool("tag_edit_anon") || !$user->is_anonymous()) && + ($user->is_admin() || !$image->is_locked()) + ); + } + +} +add_event_listener(new TagEditCloud()); +?> diff --git a/contrib/tag_editcloud/script.js b/contrib/tag_editcloud/script.js new file mode 100644 index 00000000..b0384a88 --- /dev/null +++ b/contrib/tag_editcloud/script.js @@ -0,0 +1,46 @@ +Array.prototype.editcloud_contains = function (ele) { + for (var i = 0; i < this.length; i++) { + if (this[i] == ele) { + return true; + } + } + return false; +}; +Array.prototype.editcloud_remove = function (ele) { + var arr = new Array(); + var count = 0; + for (var i = 0; i < this.length; i++) { + if (this[i] != ele) { + arr[count] = this[i]; + count++; + } + } + return arr; +}; + +function tageditcloud_toggle_extra(obj,hide) { + var el = document.getElementById(obj); + el.style.display = (el.style.display != 'none' ? 'none' : '' ); + hide.innerHTML=(el.style.display != 'none' ? 'show less tags' : 'show more tags' ); +} + +function tageditcloud_toggle_tag(ele) { + var thisTag = ele.innerHTML; + var taglist = document.getElementById('tag_editor'); + var tags = taglist.value.split(' '); + + // If tag is already listed, remove it + if (tags.editcloud_contains(thisTag)) { + tags = tags.editcloud_remove(thisTag); + ele.className = 'tag-unselected'; + + // Otherwise add it + } else { + tags.splice(0, 0, thisTag); + ele.className = 'tag-selected'; + } + + taglist.value = tags.join(' '); + + document.getElementById('tags').focus(); +} diff --git a/contrib/tag_editcloud/style.css b/contrib/tag_editcloud/style.css new file mode 100644 index 00000000..0c5b9566 --- /dev/null +++ b/contrib/tag_editcloud/style.css @@ -0,0 +1,20 @@ +span.tag-selected { + background:#88EE88; +} + +.tageditcloud { + color: #0000FF; +} + +.tageditcloud div#tagcloud_unset { + color: #0000FF; +} + +.tageditcloud div#tagcloud_set { + color: #0000FF; + font-style:italic; +} + +.tageditcloud div#tagcloud_extra { + color: #0000FF; +} From 323f0953bc4d836118de081457fce46cab9ea348 Mon Sep 17 00:00:00 2001 From: atomicdryad Date: Wed, 22 Dec 2010 18:59:44 -0600 Subject: [PATCH 2/3] TagEditCloud: Now with options. --- contrib/tag_editcloud/main.php | 62 ++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 14 deletions(-) diff --git a/contrib/tag_editcloud/main.php b/contrib/tag_editcloud/main.php index a75cb4fe..93803c18 100644 --- a/contrib/tag_editcloud/main.php +++ b/contrib/tag_editcloud/main.php @@ -20,17 +20,47 @@ class TagEditCloud implements Extension { //if(is_null($this->theme)) $this->theme = get_theme_object($this); if($event instanceof ImageInfoBoxBuildingEvent) { - if($this->can_tag($event->image)) { - $cfgstub_sortbyname=false; // FIXME - $cfgstub_showtop=40; // Derp - $cfgstub_minuse=2; // Derp - if($cfgstub_sortbyname) { - $event->add_part($this->build_tag_map($event->image,$cfgstub_minuse,false),40); - } else { - $event->add_part($this->build_tag_map($event->image,$cfgstub_showtop,4096),40); + if(!$config->get_bool("tageditcloud_disable")) { + if($this->can_tag($event->image)) { + if(!$cfg_minusage=$config->get_int("tageditcloud_minusage")) $cfg_minusage=2; + if(!$cfg_defcount=$config->get_int("tageditcloud_defcount")) $cfg_defcount=40; + if(!$cfg_maxcount=$config->get_int("tageditcloud_maxcount")) $cfg_maxcount=4096; + if($config->get_string("tageditcloud_sort") != "p") { + $event->add_part($this->build_tag_map($event->image,$cfg_minusage,false),40); + } else { + $event->add_part($this->build_tag_map($event->image,$cfg_defcount,$cfg_maxcount),40); + } } } } + + if($event instanceof InitExtEvent) { + $config->set_default_bool("tageditcloud_disable",false); + $config->set_default_bool("tageditcloud_usedfirst",true); + $config->set_default_string("tageditcloud_sort",'a'); + $config->set_default_int("tageditcloud_minusage",2); + $config->set_default_int("tageditcloud_defcount",40); + $config->set_default_int("tageditcloud_maxcount",4096); + } + + if($event instanceof SetupBuildingEvent) { + $sort_by = array('Alphabetical'=>'a','Popularity'=>'p'); + + $sb = new SetupBlock("Tag Edit Cloud"); + $sb->add_bool_option("tageditcloud_disable", "Disable Tag Selection Cloud: "); + $sb->add_choice_option("tageditcloud_sort", $sort_by, "
Sort the tags by:"); + $sb->add_bool_option("tageditcloud_usedfirst","
Always show used tags first: "); + $sb->add_label("
Alpha sort:
Only show tags used at least "); + $sb->add_int_option("tageditcloud_minusage"); + $sb->add_label(" times.
Popularity sort:
Show tags used "); + $sb->add_int_option("tageditcloud_defcount"); + $sb->add_label(" times by default.
Show a maximum of "); + $sb->add_int_option("tageditcloud_maxcount"); + $sb->add_label(" tags."); + + $event->panel->add_block($sb); + } + } private function tag_link($tag) { @@ -43,18 +73,19 @@ class TagEditCloud implements Extension { private function build_tag_map($image,$defcount,$maxcount) { // - global $database; + global $database,$config; $html="";$cloud="";$precloud=""; $itags=Array(); $tags_min=1; $alphasort=false; + $usedfirst=$config->get_bool("tageditcloud_usedfirst"); + if(!is_int($defcount)) $defcount=20; if(!is_int($maxcount)) { // Derp this is pretty cheesy. $maxcount=4096; // Hurrrr $tags_min=$defcount; $alphasort=true; } - if ((gettype($image) == 'object') && (isset($image->tag_array)) && ($itags=$image->tag_array)) $itags=array_fill_keys(array_values($itags),true); $result = $database->execute(" SELECT tag, FLOOR(LOG(2.7, LOG(2.7, count - ? + 1)+1)*1.5*100)/100 AS scaled, count @@ -75,9 +106,14 @@ class TagEditCloud implements Extension { $usecount=$row['count']; $link = $this->tag_link($row['tag']); if($size<0.5) $size = 0.5; + if(isset($itags[$row['tag']])) { -// if($size<0.75) $size = 0.75; - $precloud .= " $h_tag \n"; + if($usedfirst) { + $precloud .= " $h_tag \n"; + } else { + $counter++; + $cloud .= " $h_tag \n"; + } } else { $counter++; $cloud .= " $h_tag \n"; @@ -86,8 +122,6 @@ class TagEditCloud implements Extension { if ($precloud != '') $html .= "
$precloud
"; $html .="
$cloud
"; $rem=count($tag_data)-$defcount; -// $script = ""; -// $html.=$script; if((!$alphasort)&&($counter>=$defcount)) $html .= "
[show $rem more tags]"; // $html.='
'.var_export($itags,true).'
'; return "
$html
"; // FIXME: stupidasallhell From 70abb804291b3041b4740833107745ad2f4422d0 Mon Sep 17 00:00:00 2001 From: atomicdryad Date: Wed, 22 Dec 2010 19:04:52 -0600 Subject: [PATCH 3/3] TagEditCloud: Typos are not nice. --- contrib/tag_editcloud/main.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contrib/tag_editcloud/main.php b/contrib/tag_editcloud/main.php index 93803c18..d66fc957 100644 --- a/contrib/tag_editcloud/main.php +++ b/contrib/tag_editcloud/main.php @@ -52,9 +52,9 @@ class TagEditCloud implements Extension { $sb->add_bool_option("tageditcloud_usedfirst","
Always show used tags first: "); $sb->add_label("
Alpha sort:
Only show tags used at least "); $sb->add_int_option("tageditcloud_minusage"); - $sb->add_label(" times.
Popularity sort:
Show tags used "); + $sb->add_label(" times.
Popularity sort:
Show "); $sb->add_int_option("tageditcloud_defcount"); - $sb->add_label(" times by default.
Show a maximum of "); + $sb->add_label(" tags by default.
Show a maximum of "); $sb->add_int_option("tageditcloud_maxcount"); $sb->add_label(" tags.");