package MT::Plugin::Tags; use MT; use MT::Template::Context; use MT::Plugin; use MT::Category; use MT::Placement; @ISA = ('MT::Plugin'); sub name{ "Tagger"; } sub description{ "A keyword tagging plugin. Nothing to configure, ta."; } sub pre_save_callback { my ($cb, $entry) = @_; # # remove old categories # $_->remove() for MT::Placement->load({ blog_id => $entry->blog_id, entry_id => $entry->id }); # # get the keyword list and tag-i-fy it # my @tags = map{lc}split /\s+/, $entry->keywords; # # write the tags # foreach $tag(@tags){ my $cat = MT::Category->load({ label => $tag }); if (!$cat){ $cat = MT::Category->new; $cat->blog_id($entry->blog_id); $cat->label($tag); $cat->column('is_a_tag', 1); $cat->save or die $cat->errstr; } my $place = MT::Placement->new; $place->entry_id($entry->id); $place->blog_id($entry->blog_id); $place->category_id($cat->id); $place->is_primary(0); $place->save or die $place->errstr; } # # remove any 'tag' categories with no placements # for my $cat(MT::Category->load()){ $cat->remove() unless !$cat->is_a_tag || MT::Placement->count({ category_id => $cat->id }); } } ######################################### my $plugin = new MT::Plugin::Tags(); MT->add_plugin($plugin); MT::Entry->add_callback("pre_save", 1, $plugin, \&pre_save_callback); push @{$MT::Category::__properties->{columns}}, 'is_a_tag'; 1;