# ==================================================================
# 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: Links.pm,v 1.25 2007/11/14 02:40:26 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::Links;
# ==================================================================
# Handles displaying of forms and HTML.
#
use strict;
use vars qw/@ISA/;
use GT::SQL::Display::HTML::Table;
use Links qw/:payment :objects/;
@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|validate)$';
my $FORM_HIDE_FIELDS = [qw/isNew isChanged isPopular Status Date_Checked/];
my $SHOW_CAT_LIST = '^(search_results|add_success|delete_search_results|modify_search_results|modify_success|modify_multi_search_results|modify_multi_results_norec)$';
my $SHOW_CAT_FORM = '^(search_form|add_form|delete_search_form|modify_form|modify_search_form|modify_multi_search_results|modify_multi_result_changed|modify_multi_results_err|validate)$';
sub display {
# -------------------------------------------------------------------
# Displays a link, but passes through the plugin system.
#
my $self = shift;
my $p = (ref $_[0] eq 'HASH') ? shift : {@_};
$PLG->dispatch('display_link', sub { return $self->_plg_display (@_); }, $p );
}
sub form {
# -------------------------------------------------------------------
# Displays a link form, but passes through the plugin system.
#
my $self = shift;
my $p = (ref $_[0] eq 'HASH') ? shift : {@_};
$PLG->dispatch('form_link', sub { return $self->_plg_form (@_); }, $p );
}
sub _plg_display {
# -------------------------------------------------------------------
# Displays a record.
#
my ($self, $opts) = @_;
$opts->{code}->{LinkOwner} ||= \&disp_username;
$opts->{code}->{ExpiryDate} ||= \&disp_expiry;
my $hidden = sub { '' };
$opts->{code}->{ExpiryCounted} ||= $hidden;
$opts->{code}->{ExpiryNotify} ||= $hidden;
$opts->{code}->{LinkExpired} ||= $hidden;
my $out = $self->SUPER::display($opts);
if ($opts->{mode} =~ /$SHOW_CAT_LIST/o) {
my $id = $opts->{values}->{ID};
if ($id) {
my $font = $self->{font};
my $output = $self->disp_categories($id);
$out .= qq~
Categories
$output
~;
}
}
return $out;
}
sub _plg_form {
# -------------------------------------------------------------------
# Displays a form.
#
my ($self, $opts) = @_;
my $link_id = $opts->{values}->{ID} || $self->{input}->{ID};
# Hide fields we don't want to show on add/modify forms.
if ($opts->{mode} and $opts->{mode} =~ /$FORM_HIDE/o) {
$opts->{hide} ||= [];
push @{$opts->{hide}}, @{$FORM_HIDE_FIELDS};
}
$opts->{code}->{ExpiryDate} ||= \&form_expiry;
# Add javascript to display the original values for text/textarea columns
if ($opts->{show_diff} and $link_id) {
my $current = $DB->table('Links')->select({ ID => $link_id })->fetchrow_hashref;
my $cols = $DB->table('Links')->cols;
my $textarea = sub {
my ($self, $opts, $values, $col) = @_;
my $field_name = $self->{multiple} ? "$self->{multiple}-$col" : $col;
my $display_name = (exists $self->{cols}->{$col}->{form_display} and length $self->{cols}->{$col}->{form_display})
? $self->{cols}->{$col}->{form_display} : $col;
my $value = $values->{$col};
my $disp = $opts->{form_type} eq 'TEXT' ? 'text' : 'textarea';
my $ret = qq|
~ unless exists $opts->{extra_table} and $opts->{extra_table} == 0;
}
if ($cats_modified) {
$out .= qq~
Original Categories
~;
}
}
return $out;
}
sub disp_username {
# -------------------------------------------------------------------
# Display the username with links to edit.
#
my ($self, $col, $rec) = @_;
my $val = $rec->{LinkOwner};
my $val_e = GT::CGI->escape($val);
my $font = $self->{font};
return qq~
~;
}
sub disp_categories {
# -------------------------------------------------------------------
# Displays a list of categories for the form.
#
my $self = shift;
my $id = shift;
my $cat = $self->{db}->get_categories ($id);
my $out = '';
foreach my $id (sort { lc $cat->{$a} cmp lc $cat->{$b} } keys %$cat) {
$out .= "$id: $cat->{$id} \n";
}
return $out;
}
sub disp_expiry {
# -------------------------------------------------------------------
#
my ($self, $col, $rec) = @_;
my $val = $rec->{ExpiryDate};
my $name = $col->{form_display};
my $font = $self->{font};
my $td = $self->{td};
my $out = qq|
|;
return $out;
}
sub form_expiry {
# -------------------------------------------------------------------
#
my ($self, $col, $rec) = @_;
require GT::Date;
my $val = $rec->{ExpiryDate};
my $name = $col->{form_display};
my $font = $self->{font};
my $td = $self->{td};
my $got_date = $val && $val < UNLIMITED && $val > 0;
($got_date and $val !~ m|^\d+$|) and $val = Links::date_to_time($val);
my $multiple = $self->{multiple} ? "$self->{multiple}-" : '';
$name .= ' Dates can be entered in the following formats: YYYY-MM-DD, YYYY/MM/DD, YYYY/MM/DD HH:MM:SS';
my $out = qq|
$name
|;
$out .= qq| Never Awaiting Payment No Payment Required (free)|;
$out .= qq| - Expired | . GT::Date::date_get($rec->{LinkExpired}, '%yyyy%/%m%/%d% %HH%:%MM%:%ss%') if $val and $val == FREE and $rec->{LinkExpired};
$out .= qq|
|;
if ($self->{mode} =~ /search/ or (exists $self->{input}->{action} and $self->{input}->{action} =~ /search/)) { # Hack to get this to show up on the Browser search
$out .= qq|
|;
}
return $out;
}
sub get_all_categories {
# -------------------------------------------------------------------
# Returns a select box of all categories.
#
my $self = shift;
my $id = shift;
my $name = shift || 'CatLinks.CategoryID';
my $mult = shift || 5;
my $db = $DB->table ('Category');
my $sth = $db->select ( ['ID', 'Full_Name'] );
my %res = ();
while (my ($id, $name) = $sth->fetchrow_array) {
$res{$id} = $name;
}
return $self->select ( { name => $name, values => \%res, value => $id, blank => 0, multiple => $mult, sort => sub { lc $_[0] cmp lc $_[1] } } );
}
sub get_categories_with_html {
# -------------------------------------------------------------------
# Returns select list, and adds which categories are selected as text.
#
my ($self, @param) = @_;
my $select = $self->get_all_categories(@param);
my $output = '';
my @vals = ref $param[0] ? @{$param[0]} : ($param[0]);
if (@vals) {
my $db = $DB->table ('Category');
foreach my $id (@vals) {
next unless ($id and $id =~ /^\d+$/);
my $name_r = $db->get ($id, 'ARRAY', ['Full_Name']);
$output .= $name_r->[0] . " ";
}
}
$output .= $select;
return $output;
}
1;