3 use Lingua::EN::Inflect::Number qw(to_PL);
8 use Storable qw(freeze); use MIME::Base64;
10 use Text::Balanced qw(extract_multiple extract_quotelike);
15 my %cache_options = ( 'namespace' => $whence.'TagTools',
16 'default_expires_in' => 600 );
18 new Cache::FileCache( \%cache_options ) or
19 croak( "Couldn't instantiate FileCache" );
20 *{$whence."::zap_cache"} = sub { $cache->Clear };
21 *{$whence."::do_cached"} = sub {
22 my ($self, $codeblock,$arg) = @_;
23 my $key = 0+$codeblock; if ($arg) { $key .=":".encode_base64(freeze(\$arg)); }
24 my $c = $cache->get($key); return @$c if $c;
25 my @stuff = $codeblock->($arg);
26 $cache->set($key, [ @stuff ]);
29 *{$whence."::_tagcloud"} = sub {
30 my $cloud = HTML::TagCloud->new();
31 my $base = $whence->config->uri_base."tag/view/";
32 for my $tagging (($whence."::Tagging")->search_summary) {
33 my $name = $tagging->tag->name;
34 $cloud->add($name, $base.uri_escape($name), $tagging->{count})
38 *{$whence."::_calendar"} = sub {
41 my ($y, $m) = split /-/, ($arg || Time::Piece->new->ymd);
42 my @m = Calendar::Simple::calendar($m, $y);
44 foreach my $week (@m) {
46 foreach my $day (@$week) {
47 my $d = { day => $day };
49 my $tag = "date:$y-$m-".sprintf("%02d", $day);
50 my ($x) = ($whence."::SystemTag")->search(name => $tag);
51 if ($x) { $d->{tag} = "/system_tag/view/$tag" }
55 push(@month, \@weekdays);
59 for my $thing (qw(tagcloud calendar)) {
60 *{$whence."::$thing"} = sub { shift->do_cached(\&{$whence."::_".$thing}, @_) }
62 *{$whence."::setup_tagging"} = \&Tagtools::_setup_tagging;
66 my ($maypole_class, $target_table, $tag_table_name) = @_;
68 $maypole_class->config->model->class_of($maypole_class, shift)
70 $tag_table_name ||= "tag";
71 my $target = $class_for->($target_table)
72 || die "Couldn't find a class representing $target_table";
73 my $via_table = $tag_table_name . "ging";
75 # Does the tag table exist?
76 # If not create it or at least moan
77 # If so configure it as a new class
79 # At this point, the $via_table should now be able to be named as...
80 my $tag_class = $class_for->($tag_table_name);
81 my $via = $tag_class."ging";
84 @{$via."::ISA"} = @{$tag_class."::ISA"};
85 $via->table($via_table);
86 $via->columns(TEMP => qw/count/);
87 $via->columns(Essential => "id", $tag_table_name, $target_table);
88 # Set up the auxilliary methods
89 $via->set_sql(summary => qq/
90 SELECT id, $tag_table_name, count(*) AS count
92 GROUP BY $tag_table_name
96 $via->set_sql(all => qq/
97 SELECT id, $tag_table_name, count(*) AS count
99 GROUP BY $tag_table_name
102 # Set up the has_many relations
103 $via->has_a($target_table => $target);
104 $via->has_a($tag_table_name => $tag_class);
105 $target->has_many(to_PL($tag_table_name) => [ $via => $tag_table_name ]);
106 $target->has_many(to_PL($via) => $via);
107 $tag_class->has_many(to_PL($target_table) => [ $via => $target_table ]);
108 $tag_class->has_many(to_PL($via_table) => $via);
112 map { s/^"|"$//g; $_}
123 sub Time::Piece::next_month {
125 my $month = $tp + ONE_MONTH;
126 return if $month > Time::Piece->new;
129 sub Time::Piece::prev_month {
131 my $month = $tp - ONE_MONTH;