# ==================================================================
# GList::Plugins::SubscribersMod - Auto Generated Program Module
#
#   GList::Plugins::SubscribersMod
#   Author  : Virginia Lo
#   Version : 1
#   Updated : Wed Jun  4 12:24:28 2008
#
# ==================================================================
#

package GList::Plugins::SubscribersMod;
# ==================================================================

use strict;
use GT::Base;
use GT::Plugins qw/STOP CONTINUE/;
use GList qw/$IN $DB $CFG/;

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

# Your code begins here.


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


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

# Do something useful here
    GT::Plugins->action(STOP);

    my $sub_id   = $IN->param('subid');
    my $old_data = $DB->table('Lists', 'Subscribers')->select({ sub_id => $sub_id }, [ 'lst_title', 'sub_email as new_email', 'sub_name as new_name', 'sub_validated as new_validated', 'sub_bounced as new_bounced', 'sub_list_id_fk', 'Subscribers.*'])->fetchrow_hashref;
    return lst_subscribers(GList::language('LST_INVALID')) if (!$old_data);

    my $cols = $DB->table('Subscribers')->cols;
    foreach (keys %$cols) {
        next if ($_ eq 'sub_created' or $_ eq 'sub_id' or $_ eq 'sub_user_id_fk' or $_ eq 'sub_list_id_fk' or $_ eq 'sub_val_code');
        my $key = $_;
        $key =~ s/sub_/new_/g;
        $old_data->{$key} ||= $old_data->{$_};
        delete $old_data->{$_};
    }

    my $info = GList::check_owner('Lists', 'lst', $old_data->{sub_list_id_fk});
    return lst_subscribers($info) if (ref $info ne 'HASH');

    return ('lst_sub_modify.html', $old_data) if ($IN->param('form'));

    my $new_email = $IN->param('new_email');
    my $name      = $IN->param('new_name');
    my $validated = ($IN->param('new_validated')) ? '1' : '0';
    my $bounced   = $IN->param('new_bounced') || 0;

    if ($new_email !~ /^(?:(?:.+\@.+\..+)|\s*)$/ or $new_email =~ /\s/) { # check email address
        return ('lst_sub_modify.html', { msg => GList::language('LST_IPT_INVALID_EMAIL'), %$info });
    }

    require GT::SQL::Condition;
    if ($DB->table('Subscribers')->count( GT::SQL::Condition->new(
            sub_email      => '=' => $new_email,
            sub_list_id_fk => '=' => $old_data->{sub_list_id_fk},
            sub_id         => '<>'=> $sub_id,
        )) == 1 ) {
        return ('lst_sub_modify.html', { msg => GList::language('LST_IPT_DUPLICATE_EMAIL'), %$info });
    }
    else {
        my $update = {
            sub_email       => $new_email,
            sub_name        => $name,
            sub_validated   => $validated,
            sub_bounced     => $bounced,
        };
        foreach (keys %$cols) {
            my $key = $_;
            $key =~ s/sub_/new_/g;
            if ($IN->param($key)) {
                $update->{$_} ||= $IN->param($key);
            }
        }
#use Data::Dumper; print $IN->header . "<pre>".Dumper($old_data,$update)."</pre>";
        $DB->table('Subscribers')->update({
            %$update
        }, { sub_id => $sub_id });
    }
    require GList::List;
    return GList::List::lst_subscribers(GList::language('LST_SUB_MODIFIED', $old_data->{new_email}));


    return @args;
}

# Always end with a 1.
1;
