]> git.decadent.org.uk Git - memories.git/commitdiff
HIGHLY EXPERIMENTAL - first stage of getting Tagtools to do what it was made for
authorSimon Cozens <simon@simon-cozens.org>
Wed, 11 Apr 2007 21:08:04 +0000 (21:08 +0000)
committerSimon Cozens <simon@simon-cozens.org>
Wed, 11 Apr 2007 21:08:04 +0000 (21:08 +0000)
git-svn-id: http://svn.simon-cozens.org/memories/trunk@64 041978f6-d955-411f-a9d7-1d8545c9c3c7

Memories.pm
Memories/Tag.pm
Tagtools.pm

index 1a0b994edd3a96dc15f0a68b23443608421b5be8..43f9a3e003e2d9de31c1a6cef320661e8b7264b0 100644 (file)
@@ -4,6 +4,7 @@ our $VERSION = "1.2";
 use Maypole::Application qw(Authentication::UserSessionCookie);
 use HTML::TagCloud;
 use URI;
+use Tagtools;
 use Memories::Config;
 use Memories::DBI;
 use Memories::Photo;
@@ -15,12 +16,12 @@ use Memories::Album;
 use URI::Escape;
 use Calendar::Simple;
 use XML::RSS;
-use Tagtools;
 
 Memories->config->auth->{ user_field } = "name";
 Memories->config->model("Maypole::Model::CDBI::Plain");
 Memories->setup([qw/ Memories::Photo Memories::User Memories::Tag
 Memories::Album Memories::SystemTag/]);
+Memories->setup_tagging("photo");
 
 sub message {
     my ($self, $message) = @_;
index 744a98a089e49af7da5fff6192c99f4cf7865a7f..3354489cc661d3a15c9738dddb0687a7fecd669a 100644 (file)
@@ -74,22 +74,8 @@ sub list_js :Exported {
 
 package Memories::Tagging;
 use base qw(Memories::DBI);
-use Class::DBI::Pager;
-__PACKAGE__->columns(TEMP => qw/count/);
-__PACKAGE__->columns(Essential => qw/id tag photo/);
-__PACKAGE__->set_sql(summary => qq/
-SELECT  id, tag, count(*) AS count
-FROM tagging
-GROUP BY tag
-ORDER BY count DESC
-LIMIT 75
-    /);
-__PACKAGE__->set_sql(all => qq/
-SELECT  id, tag, count(*) AS count
-FROM tagging
-GROUP BY tag
-ORDER BY count DESC
-    /);
+
+
 __PACKAGE__->set_sql(user_summary => qq/
 SELECT  tagging.id id, tag, count(*) AS count
 FROM tagging, photo
@@ -98,12 +84,4 @@ GROUP BY tag
 ORDER BY count DESC
 /);
 
-Memories::Tagging->has_a("photo" => "Memories::Photo");
-Memories::Tagging->has_a("tag" => "Memories::Tag");
-
-Memories::Photo->has_many(tags => ["Memories::Tagging" => "tag"]);
-Memories::Photo->has_many(taggings => "Memories::Tagging");
-Memories::Tag->has_many(photos => ["Memories::Tagging" => "photo"] );
-Memories::Tag->has_many(taggings => "Memories::Tagging");
-
 1;
index f161c475b4e9bcf9edd4090cfe1ffbfd697d0078..04e2f63e50e042e9ae6d50a35e6cd206cf03e392 100644 (file)
@@ -1,4 +1,5 @@
 package Tagtools;
+use Lingua::EN::Inflect::Number qw(to_PL);
 use URI::Escape;
 use HTML::TagCloud;
 use Carp;
@@ -6,6 +7,7 @@ use Cache::FileCache;
 use Storable qw(freeze); use MIME::Base64;
 use Calendar::Simple;
 use Text::Balanced qw(extract_multiple extract_quotelike);
+
 sub import {
     my $whence = caller;
     my ($class) = @_;
@@ -55,7 +57,53 @@ sub import {
     for my $thing (qw(tagcloud calendar)) {
         *{$whence."::$thing"} = sub { shift->do_cached(\&{$whence."::_".$thing}, @_) }
     }
+    *{$whence."::setup_tagging"} = \&Tagtools::_setup_tagging;
+}
+
+sub _setup_tagging {
+    my ($maypole_class, $target_table, $tag_table_name) = @_;
+    my $class_for = sub {
+        $maypole_class->config->model->class_of($maypole_class, shift)
+    };
+    $tag_table_name ||= "tag";
+    my $target = $class_for->($target_table) 
+        || die "Couldn't find a class representing $target_table";
+    my $via_table = $tag_table_name . "ging";
+
+    # Does the tag table exist?
+    # If not create it or at least moan
+    # If so configure it as a new class
+
+    # At this point, the $via_table should now be able to be named as...
+    my $tag_class = $class_for->($tag_table_name);
+    my $via_table = $tag_table_name."ging";
+    my $via = $tag_class."ging";
 
+    # Set up the class
+    @{$via."::ISA"} = @{$tag_class."::ISA"};
+    $via->columns(TEMP => qw/count/);
+    $via->columns(Essential => "id", $tag_table_name, $target_table);
+    # Set up the auxilliary methods
+    $via->set_sql(summary => qq/
+    SELECT  id, $tag_table_name, count(*) AS count
+    FROM $via_table
+    GROUP BY $tag_table_name
+    ORDER BY count DESC
+    LIMIT 50
+    /);
+    $via->set_sql(all => qq/
+    SELECT  id, $tag_table_name, count(*) AS count
+    FROM $via_table
+    GROUP BY $tag_table_name
+    ORDER BY count DESC
+        /);
+    # Set up the has_many relations
+    $via->has_a($target_table => $target);
+    $via->has_a($tag_table_name => $tag_class);
+    $target->has_many(to_PL($tag_table_name) => [ $via => $tag_table_name ]);
+    $target->has_many(to_PL($via) => $via);
+    $tag_class->has_many(to_PL($target_table) => [ $via => $target_table ]);
+    $tag_class->has_many(to_PL($via_table) => $via);
 }
 
 sub separate_tags {