Add a tag cloud.
[pr0n] / perl / Sesse / pr0n / Listing.pm
index 6f47355f4596631f9d20693dc48c48d5ba8c961a..f97a643414677f9ad177a3b5ab0f44ce1586684e 100644 (file)
@@ -3,6 +3,7 @@ use strict;
 use warnings;
 
 use Sesse::pr0n::Common qw(error dberror);
+use HTML::TagCloud;
 
 sub handler {
        my $r = shift;
@@ -27,34 +28,61 @@ sub handler {
        if ((my $rc = $r->meets_conditions) != Apache2::Const::OK) {
                return $rc;
        }
-
-#      my $q = $dbh->prepare('SELECT t1.id,t1.date,t1.name FROM events t1 LEFT JOIN images t2 ON t1.id=t2.event WHERE t1.vhost=? GROUP BY t1.id,t1.date,t1.name ORDER BY COALESCE(MAX(t2.date),\'1970-01-01 00:00:00\'),t1.id') or
-#              dberror($r, "Couldn't list events");
-       my $q = $dbh->prepare('SELECT event,date,name FROM events e JOIN last_picture_cache c USING (vhost,event) WHERE vhost=? ORDER BY last_picture DESC')
-               or dberror($r, "Couldn't list events");
-       $q->execute($r->get_server_name)
-               or dberror($r, "Couldn't get events");
-       
-       Sesse::pr0n::Common::header($r, Sesse::pr0n::Templates::fetch_template($r, 'event-listing'));
-       my $allcaption = Sesse::pr0n::Templates::fetch_template($r, 'all-event-title');
-       $r->print("    <ul>\n");
-       $r->print("      <li><a href=\"+all/\">$allcaption</a></li>\n");
-       $r->print("    </ul>\n");
        
-       $r->print("    <ul>\n");
+       if ($r->uri =~ /^\/\+tags\/?/) {
+               # Tag cloud
+               my $q = $dbh->prepare('SELECT tag,COUNT(*) AS frequency FROM tags t JOIN images i ON t.image=i.id WHERE vhost=? GROUP BY tag ORDER BY COUNT(*) DESC LIMIT 75')
+                       or dberror($r, "Couldn't list events");
+               $q->execute($r->get_server_name)
+                       or dberror($r, "Couldn't get events");
+               
+               Sesse::pr0n::Common::header($r, Sesse::pr0n::Templates::fetch_template($r, 'tag-listing'));
+
+               my $cloud = HTML::TagCloud->new;
+
+               while (my $ref = $q->fetchrow_hashref()) {
+                       my $tag = Encode::decode_utf8($ref->{'tag'});
+                       my $html = HTML::Entities::encode_entities($tag);    # is this right?
+                       my $uri = Sesse::pr0n::Common::pretty_escape($tag);  # and this?
+
+                       $cloud->add($html, "/+tags/$uri/", $ref->{'frequency'});
+               }
 
-       while (my $ref = $q->fetchrow_hashref()) {
-               my $id = $ref->{'event'};
-               my $date = HTML::Entities::encode_entities(Encode::decode_utf8($ref->{'date'}));
-               my $name = HTML::Entities::encode_entities(Encode::decode_utf8($ref->{'name'}));
+               $r->print($cloud->html_and_css());
+               Sesse::pr0n::Common::footer($r);
+
+               $q->finish();
+       } else {
+               # main listing
+#              my $q = $dbh->prepare('SELECT t1.id,t1.date,t1.name FROM events t1 LEFT JOIN images t2 ON t1.id=t2.event WHERE t1.vhost=? GROUP BY t1.id,t1.date,t1.name ORDER BY COALESCE(MAX(t2.date),\'1970-01-01 00:00:00\'),t1.id') or
+#                      dberror($r, "Couldn't list events");
+               my $q = $dbh->prepare('SELECT event,date,name FROM events e JOIN last_picture_cache c USING (vhost,event) WHERE vhost=? ORDER BY last_picture DESC')
+                       or dberror($r, "Couldn't list events");
+               $q->execute($r->get_server_name)
+                       or dberror($r, "Couldn't get events");
                
-               $r->print("      <li><a href=\"$id/\">$name</a> ($date)</li>\n");
-       }
+               Sesse::pr0n::Common::header($r, Sesse::pr0n::Templates::fetch_template($r, 'event-listing'));
+               my $allcaption = Sesse::pr0n::Templates::fetch_template($r, 'all-event-title');
+               $r->print("    <ul>\n");
+               $r->print("      <li><a href=\"+all/\">$allcaption</a></li>\n");
+               $r->print("    </ul>\n");
+               
+               $r->print("    <ul>\n");
 
-       $r->print("    </ul>\n");
-       Sesse::pr0n::Common::footer($r);
+               while (my $ref = $q->fetchrow_hashref()) {
+                       my $id = $ref->{'event'};
+                       my $date = HTML::Entities::encode_entities(Encode::decode_utf8($ref->{'date'}));
+                       my $name = HTML::Entities::encode_entities(Encode::decode_utf8($ref->{'name'}));
+                       
+                       $r->print("      <li><a href=\"$id/\">$name</a> ($date)</li>\n");
+               }
+
+               $r->print("    </ul>\n");
+               Sesse::pr0n::Common::footer($r);
+
+               $q->finish();
+       }
 
-       $q->finish();
        return Apache2::Const::OK;
 }