# ==================================================================
# 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: Users.pm,v 1.4 2007/03/22 22:05:44 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::Users;
# ==================================================================
# Handles displaying of forms and HTML.
#
use strict;
use vars  qw/@ISA/;
use Links qw/:objects/;
use GT::SQL::Display::HTML::Table;

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

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

    $PLG->dispatch('display_user', sub { $self->SUPER::display(@_) }, $p);
}

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

    $PLG->dispatch('form_user', sub { return $self->SUPER::form(@_) }, $p);
}

sub _display {
# -------------------------------------------------------------------
# Adds on a box with quick links to the users links.
#
    my ($self, $opts) = @_;
    my $user  = $opts->{values}->{Username};
    my $output = '';

# If we are modifying, then add a hidden field for the original record.
    if ($opts->{mode} eq 'modify_form') {
        $opts->{code}->{Username} ||= \&disp_username;
        my $user_q = GT::CGI->html_escape($user);
        $output .= qq~<input type="hidden" name="orig_username" value="$user_q">~;
    }
    else {
        delete $self->{code}->{Username};
    }
    $output  .= $self->SUPER::_display($opts);
    if ($user) {
        my $link_db = $DB->table('Links');
        my $count   = $link_db->count({ LinkOwner => $user });
        my $url     = GT::CGI->url({ query_string => 0 });
        my $user_q  = GT::CGI->escape($user);
        $output    .= <<HTML;
<p>
<table border=1 cellpadding=0 bgcolor="#FFFFFF" cellspacing=0 width="500"><tr><td>
  <table border=0 bgcolor="#FFFFFF" width="500"><tr>
    <td><font face="Tahoma,Arial,Helvetica" size="2">
      Links ($count): 
        <a href="$url?db=Links&do=search_results&LinkOwner=$user_q&ww=1">View</a> |
        <a href="$url?db=Links&do=modify_search_results&LinkOwner=$user_q&ww=1">Modify</a> |
        <a href="$url?db=Links&do=delete_search_results&LinkOwner=$user_q&ww=1">Delete</a>
    </font></td>
  </tr></table>
</td></tr></table>
HTML
    }
    return $output;
}

sub disp_username {
# -------------------------------------------------------------------
# Display the username with links to edit.
#
    my ($self, $col, $rec) = @_;
    my $val   = $rec->{Username};
    my $val_e = GT::CGI->html_escape($val);
    my $font  = $self->{font};
    return <<HTML;
  <tr>
    <td><font $font>Username</font></td>
    <td><font $font><input type="text" name="Username" value="$val_e" size="20"></font></td>
  </tr>
HTML
}

1;
