# ==================================================================
# Plugins::OverrideModDate - Auto Generated Program Module
#
#   Plugins::OverrideModDate
#   Author  : Gossamer Threads Inc.
#   Version : 1.0
#   Updated : Mon Sep 24 14:34:23 2007
#
# ==================================================================
#

package Plugins::OverrideModDate;
# ==================================================================

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

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

# Your code begins here.


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

sub post_modify_link {
# -------------------------------------------------------------------
# Modify a single link.
#
    my $ret = shift;
    return $ret if (!$ret);

    my $new = {};
    my $update = 0;
    Links::init_date();
    if ($IN->param('Add_Date') =~ /^\d\d\d\d-\d\d-\d\d$/) {
        $new->{Add_Date} = $IN->param('Add_Date');
        $update = 1;
    }
    if ($IN->param('Mod_Date') =~ /^\d\d\d\d-\d\d-\d\d$/) {
        $new->{Mod_Date} = $IN->param('Mod_Date');
        $update = 1;
    }
    my $id = $IN->param('ID') || $IN->param('LinkID');
    if ($id and $update) {
        $DB->table('Links')->update({ %$new }, { ID => $id });
    }

    return $ret;
}

sub pre_handle {
# ---------------------------------------------------
# Determine what to do.
#
    my @args = @_;
    my $link_id = $IN->param('LinkID');
    if ($CFG->{user_required} and !$USER) {
        $PLG->action(STOP);
        print $IN->redirect(Links::redirect_login_url('modify'));
        return @args;
    }

# Perform the link modification
    if ($IN->param('modify')) {
        return @args;
    }
    elsif ($USER) {
# Display the link modify form (for a specific link)
        if ($IN->param('LinkID')) {
            $PLG->action(STOP);
            _modify_passed_in();
        }
    }
    return @args;
}

sub _modify_passed_in {
# --------------------------------------------------------
# Display link that was passed in.
#
    my $lid = $IN->param('LinkID');
    my $link_db = $DB->table('Links');
    my $mtl = Links::Build::build('title', Links::language('LINKS_MODIFY'), "$CFG->{db_cgi_url}/modify.cgi?LinkID=$lid");
    my $sth = $link_db->select({ ID => $lid, LinkOwner => $USER->{Username} }, VIEWABLE);
    if ($USER->{Status} eq 'Administrator') {
        $sth = $link_db->select({ ID => $lid }, VIEWABLE);
    }
    if ($sth->rows) {
        my $link = $sth->fetchrow_hashref;
        my @ids = $DB->table('CatLinks')->select('CategoryID', { LinkID => $link->{ID} })->fetchall_list;
        $IN->param('CatLinks.CategoryID', \@ids);

        $link->{Contact_Name}  ||= $USER->{Name} || $USER->{Username};
        $link->{Contact_Email} ||= $USER->{Email};

        my $category = {};
        if ($CFG->{db_gen_category_list} < 2) {
            require Links::Tools;
            $category = Links::Tools::category_list();
            $category->{Category} = sub { Links::Tools::category_list_html() };
        }
        print $IN->header();
        print Links::SiteHTML::display('modify', {
            main_title_loop => $mtl,
            %$link,
            %$category
        });
    }
    elsif (!$CFG->{user_required}) {
        require Links::User::Modify;
        Links::User::Modify::_modify_form();
    }
    else {
        print $IN->header();
        print Links::SiteHTML::display('error', { error => Links::language('MODIFY_NOTOWNER'), LinkID => $lid, main_title_loop => $mtl });
    }
}

sub post_add_link {
# -------------------------------------------------------------------
# Modify a single link.
#
    my $ret = shift;
    return $ret if (!$ret);

    my $new = {};
    my $update = 0;
    Links::init_date();
    if ($IN->param('Add_Date') =~ /^\d\d\d\d-\d\d-\d\d$/) {
        $new->{Add_Date} = $IN->param('Add_Date');
        $update = 1;
    }
    if ($IN->param('Mod_Date') =~ /^\d\d\d\d-\d\d-\d\d$/) {
        $new->{Mod_Date} = $IN->param('Mod_Date');
        $update = 1;
    }
    my $id = $ret->{ID};
    if ($id and $update) {
        $DB->table('Links')->update({ %$new }, { ID => $id });
    }

    return $ret;
}


# Always end with a 1.
1;
