package MT::Plugin::AnotherLocationPlugin; ######################################################## # CHANGE THE LINE BELOW TO POINT TO YOUR LOCATION FILE # ######################################################## my $LOCATION_FILE = "/home/yuri/domains/freewisdom.org/mt/loc_db.txt"; ######################################################## # CHANGE THE LINE ABOVE TO POINT TO YOUR LOCATION FILE # ######################################################## use MT::Template::Context; ######################################################## # Associate tags with functions ######################################################## MT::Template::Context->add_tag(Location => \&location); MT::Template::Context->add_tag(LocationMetaTags => \&location_meta); MT::Template::Context->add_conditional_tag(IfLocation => \&if_location); ######################################################## # Load a location hash from a file ######################################################## my %loc_hash = &get_loc_hash(); sub get_loc_hash { my $record; my %loc_hash = (); open (LOC, $LOCATION_FILE) or die "Can't open file: " . $LOCATION_FILE; while () { chomp; my ($loc, $name, $latitude, $longitude) = split(/\t/); $loc_hash{$loc} = [$name, $latitude, $longitude]; } return %loc_hash; } ######################################################## # The function for MTIfLocation ######################################################## sub if_location { my $ctx = shift; return $ctx->stash('entry')->keywords? 1:0; } ######################################################## # The function for MtLocation ######################################################## sub location { my $ctx = shift; my $args = shift; return "" unless $ctx->stash('entry')->keywords; my @keywords = split (/ /, $ctx->stash('entry')->keywords); foreach (@keywords) { if (/^loc/) { my ($name, $latitude, $longitude) = @{$loc_hash{$_}}; if ($args->{geourl} && $latitude && $longitude) { return "$name"; } else { return $name; } } } return "Somewhere, SW"; } ######################################################## # The function for MtLocationMetaTag ######################################################## sub location_meta { my $ctx = shift; return "" unless $ctx->stash('entry')->keywords; my @keywords = split (/ /, $ctx->stash('entry')->keywords); foreach (@keywords) { if (/^loc/) { my ($name, $latitude, $longitude) = @{$loc_hash{$_}}; if ($latitude && $longitude) { my $title = $ctx->stash('entry')->title(); return "\n" ."\n" } else { return ""; } } } return "Whoops"; } 1; # stupid perl