# ==================================================================
# Plugins::HandlePage - Auto Generated Program Module
#
#   Plugins::HandlePage
#   Author  : Gossamer Threads Inc. (Virginia Lo)
#   Version : 1.0
#   Updated : Tue Jun  7 15:32:59 2011
#
# ==================================================================
#

package Plugins::HandlePage;
# ==================================================================

use strict;
use GT::Base;
use GT::Plugins qw/STOP CONTINUE/;
use Links qw/:objects/;
use Links::SiteHTML;

# Inherit from base class for debug and error methods
@Plugins::HandlePage::ISA = qw(GT::Base);

# Your code begins here.


# PLUGIN HOOKS
# ===================================================================


sub pre_handle_page {
# -----------------------------------------------------------------------------
# This subroutine will be called whenever the hook 'handle_page' is run.  You
# should call $PLG->action(STOP) if you don't want the regular
# 'handle_page' code to run, otherwise the code will continue as normal.
#
    my (@args) = @_;

# Do something useful here
    if (my $page2 = $IN->param('page2')) {
        $page2 =~ /.+_(\d+).html/;
        my $id = $1;
        my $linksdb = $DB->table('Links','CatLinks');
        my $link = $linksdb->select( { LinkID => $id })->fetchrow_hashref;
        if ($link) {
            $link = Links::SiteHTML::tags('link',$link, $link->{CategoryID});
            $link->{detailed_url} =~ /$CFG->{build_root_url}\/(.+)$/;
            my $match = $1;
            if ($page2 ne $match) {
                print $IN->redirect( -url => $link->{detailed_url}, -permanent => 1 );
                #print $IN->header . $page2 . "<br />";
                #print $link->{detailed_url} . " ($page2) should be redirected.";
            }
        }
        else {
            print "Status: 404" . $GT::CGI::EOL;
            print $IN->header();
            print Links::SiteHTML::display('error', { error => Links::language('PAGE_INVALIDDETAIL',$id) });
        }
    }

    return @args;
}

# Always end with a 1.
1;
