# ==================================================================
# Gossamer Links - enhanced directory management system
#
#   Website  : http://gossamer-threads.com/
#   Support  : http://gossamer-threads.com/scripts/support/
#   CVS Info : 087,071,086,086,085      
#   Revision : $Id: Category.pm,v 1.14 2007/09/25 06:19:32 brewt Exp $
#
# Copyright (c) 2001 Gossamer Threads Inc.  All Rights Reserved.
# Redistribution in part or in whole strictly prohibited. Please
# see LICENSE file for full details.
# ==================================================================

package Links::HTML::Category;
# ==================================================================
# Handles displaying of forms and HTML.
#
use strict;
use vars  qw/@ISA/;
use Links qw/:payment :objects/;
use GT::SQL::Display::HTML::Table;

@ISA = qw/GT::SQL::Display::HTML::Table/;

my $FORM_HIDE = 'add_form|modify_form|modify_multi_search_results|modify_multi_result_changed|modify_multi_results_err';
my $FORM_HIDE_FIELDS = [qw/Full_Name Number_of_Links Direct_Links Has_New_Links Has_Changed_Links Newest_Link Timestmp/];

sub display {
# -------------------------------------------------------------------
# Displays a category, but passes through the plugin system.
#
    my $self = shift;
    my $p    = (ref $_[0] eq 'HASH') ? shift : {@_};

    $PLG->dispatch('display_category', sub { return $self->_plg_display(@_); }, $p);
}

sub form {
# -------------------------------------------------------------------
# Displays a category form, but passes through the plugin system.
#
    my $self = shift;
    my $p    = (ref $_[0] eq 'HASH') ? shift : {@_};

    $PLG->dispatch('form_category', sub { return $self->_plg_form(@_); }, $p);
}

sub _plg_display {
# -------------------------------------------------------------------
# Displays a record.
#
    my ($self, $opts) = @_;
    $opts->{hide} ||= [];

    if (!exists $opts->{code}->{FatherID} and !grep { $_ eq 'FatherID' } @{$opts->{hide}}) {
        $opts->{code}->{FatherID} = \&disp_fatherid_html
    }

    push @{$opts->{hide}}, qw/Full_Name/;
    $CFG->{payment}->{enabled} or push @{$opts->{hide}}, 'Payment_Mode';

    my $out = $self->SUPER::display($opts);

    my $id = $opts->{values}->{ID};
    if ($CFG->{payment}->{enabled} and $id and $opts->{values}->{Payment_Mode} >= OPTIONAL) {
        my $font = $self->{font};
        $out .= qq~
<p><table border=1 cellpadding=0 bgcolor="#FFFFFF" cellspacing=0 width="500"><tr><td>
    <table border=0 bgcolor="#FFFFFF" width="500"><tr>
        <td width="100%" valign="top" align="center"><font $font><a href="admin.cgi?do=page;page=payment_cat_price.html;ID=$id;not_global=1">Add/Update payment terms for this category</a></td>
    </tr></table>
</td></tr></table>
        ~;
    }
    return $out;
}

sub _plg_form {
# -------------------------------------------------------------------
# Displays a form.
#
    my ($self, $opts) = @_;
    $opts->{hide} ||= [];

    if ($opts->{mode} and $opts->{mode} =~ /$FORM_HIDE/o) {
        push @{$opts->{hide}}, @{$FORM_HIDE_FIELDS};
    }

    if (!exists $opts->{code}->{FatherID} and !grep { $_ eq 'FatherID' } @{$opts->{hide}}) {
        $opts->{code}->{FatherID} = \&disp_fatherid_form;
    }

    $CFG->{payment}->{enabled} or push @{$opts->{hide}}, 'Payment_Mode';

    return $self->SUPER::form($opts);
}

sub select {
# -------------------------------------------------------------------
# Override Payment_Mode select field in add form.
#
    my ($self, $opts) = @_;
    $CFG->{payment}->{enabled} and $opts->{name} eq 'Payment_Mode'
        and $self->{input}->{do} and ($self->{input}->{do} eq 'add_form' or $self->{input}->{do} eq 'modify_form')
        and $opts->{blank} = 0;
    return $self->SUPER::select($opts);
}

sub disp_fatherid_form {
# -------------------------------------------------------------------
# Display the list of subcategories as either a drop down list of a text box.
#
    my ($self, $col, $rec) = @_;
    my $font = $self->{font};
    my $out;
    my $form_name = $self->{multiple} ? "$self->{multiple}-FatherID" : 'FatherID';

    if ($CFG->{db_gen_category_list} == 2) {
        if ($rec->{FatherID}) {
            $out .= qq|<input type="hidden" name="FatherID" value="$rec->{FatherID}" />|;
        }
        $out .= qq|<script type="text/javascript" src="$CFG->{build_static_url}/treecats.js"></script>
<link type="text/css" rel="stylesheet" href="$CFG->{build_static_url}/admin/treecats.css" />
<input type="hidden" name="FatherID-opt" value="=" />
<tr><td valign="top"><font $font>Subcategory of</font></td><td><font $font><div id="treecats"></div></font></td></tr>
<script type="text/javascript">var tc = new treecats({ selectionRequired : false, inputName : 'FatherID', cgiURL : '$CFG->{db_cgi_url}', imageURL : '$CFG->{build_static_url}/admin' }, { noSelection : 'Root', rootText : 'Root' }); tc.load();</script>\n|;
    }
    elsif ($CFG->{db_gen_category_list}) {
        my $sth = $self->{db}->select(["DISTINCT Full_Name, ID"]);
        my %names;
        if ($sth) {
            while (my ($name, $id) = $sth->fetchrow_array) {
                $names{$id} = $name;
            }
        }
        $names{0} = '--Root--';
        my $select = $self->select({ name => $form_name, values => \%names, blank => 1, sort => sub { lc $_[0] cmp lc $_[1] }, value => defined $rec->{FatherID} ? $rec->{FatherID} : "" });
        $out = qq~
<tr><td valign=top><font $font>Subcategory of</font></td><td><font $font>$select<input type=hidden name="FatherID-opt" value="="></td></tr>
        ~;
    }
    else {
        my $value = $rec->{FatherID} || '';
        if ($value =~ /^\d+$/) {
            my $sth = $self->{db}->select('Full_Name', { ID => $value });
            if ($sth) {
                ($value) = $sth->fetchrow_array;
            }
        }
        $out = qq~
<tr><td><font $font>Full Sub Category<br><font size=1>Separated with /'s</font></font></td><td><input type=text size="40" name="$form_name" value="$value"></td></tr>
        ~;
    }
    return $out;
}

sub disp_fatherid_html {
# -------------------------------------------------------------------
# Display the father.
#
    my ($self, $col, $rec) = @_;
    my ($parent) = $rec->{Full_Name} =~ m,^(.*)/[^/]+$,;
    my $font = $self->{font};
    $parent ||= 'Root';
    return qq~
<tr><td><font $font>Subcategory of</font></td><td><font $font>$parent</td></tr>
    ~;
}

1;
