# ================================================================== # 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|{tr}>{td} width='30%'>{col_font}>$display_name{td} width='70%'>{val_font}>|; $ret .= $self->$disp({ name => $field_name, def => $opts, value => (defined $value ? $value : '')}); $ret .= qq|\n{tr}>{td} width="30%">{col_font}>Original $display_name{td} width="70%">{val_font}>|; if ($opts->{form_type} eq 'TEXT') { $ret .= qq||; } else { my ($cols, $rows) = ref $opts->{form_size} ? (@{$opts->{form_size}}) : ($opts->{form_size} || 20, 4); $ret .= qq|| } $ret .= "\n"; }; COL: for my $col (keys %$current) { next if !$cols->{$col}->{form_type} or ($cols->{$col}->{form_type} ne 'TEXT' and $cols->{$col}->{form_type} ne 'TEXTAREA'); # Skip hidden fields for (@{$opts->{hide}}) { next COL if $_ eq $col; } if ((not defined $opts->{values}->{$col} or $current->{$col} ne $opts->{values}->{$col}) and not $opts->{code}->{$col}) { $opts->{code}->{$col} = $textarea; } } } # Display the form. my $out = $self->SUPER::form($opts); # Display the category select box. if ($opts->{mode} and $opts->{mode} =~ /$SHOW_CAT_FORM/o) { my $name = $opts->{multiple} ? "$opts->{multiple}-CatLinks.CategoryID" : 'CatLinks.CategoryID'; my $id = $opts->{values}->{$name} || $self->{input}->{$name}; $id = (ref $id eq 'ARRAY') ? $id : $id ? [$id] : []; my $font = $self->{font}; my ($output, $h); # Add javascript to display the original categories my $cats_modified; if ($opts->{show_diff} and @$id and $link_id) { my $ccl = $DB->table('Category', 'CatLinks'); $ccl->select_options("ORDER BY CategoryID"); my $sth = $ccl->select('CategoryID', 'Full_Name', { LinkID => $link_id }); my (@cid, @cats); while (my $cat = $sth->fetchrow_hashref) { push @cid, $cat->{CategoryID}; push @cats, $cat->{Full_Name}; } if (@$id == @cid) { my @sorted = sort { $a > $b } @$id; for (my $i = 0; $i < @cid; $i++) { if ($cid[$i] != $sorted[$i]) { $cats_modified = join "\n", sort @cats; last; } } } else { $cats_modified = join "\n", sort @cats; } } # Display the category using treecats if ($CFG->{db_gen_category_list} == 2) { my $name = $opts->{multiple} ? "$opts->{multiple}-CatLinks.CategoryID" : 'CatLinks.CategoryID'; my $jsname = $opts->{multiple} ? "tc$opts->{multiple}" : 'tc'; if (!@$id and $link_id) { $h = $self->{db}->get_categories($link_id); for (keys %$h) { push @$id, $_; } } $out .= qq~

~ unless exists $opts->{extra_table} and $opts->{extra_table} == 0; $out .= qq~
~; $out .= qq|| if $cats_modified; $out .= "Categories"; $out .= qq|| if $cats_modified; for (@$id) { $out .= qq||; } $out .= qq~
~; $out .= qq~

~ unless exists $opts->{extra_table} and $opts->{extra_table} == 0; } # Display category as a select box. elsif ($CFG->{db_gen_category_list}) { if (!@$id and $link_id) { $h = $self->{db}->get_categories($link_id); $output = $self->get_categories_with_html([keys %$h], $name); } else { $output = $self->get_categories_with_html($id, $name); } $out .= "

"; $out .= qq~
~ unless exists $opts->{extra_table} and $opts->{extra_table} == 0; $out .= qq~
~; $out .= qq|| if $cats_modified; $out .= "Categories"; $out .= qq|| if $cats_modified; $out .= qq~ $output
~; $out .= qq~
~ unless exists $opts->{extra_table} and $opts->{extra_table} == 0; } # Display category as a textarea box. else { my ($vals); if (@$id) { my $db = $DB->table('Category'); foreach (@$id) { if (/^\d+$/) { $vals .= $db->get_name_from_id($_) . "\n"; } else { $vals .= $_ . "\n"; } } } elsif ($link_id) { $h = $self->{db}->get_categories($link_id); $vals = join("\n", sort values %$h); } else { $vals = ''; } $out .= "

"; $out .= qq~
~ unless exists $opts->{extra_table} and $opts->{extra_table} == 0; $out .= qq~
~; $out .= qq|| if $cats_modified; $out .= "Categories"; $out .= qq|| if $cats_modified; $out .= qq~
One per line
~; $out .= qq~
~ unless exists $opts->{extra_table} and $opts->{extra_table} == 0; } if ($cats_modified) { $out .= qq~ ~; } } 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~ $col->{form_display}$val edit ~; } 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|$name|; if ($val == UNLIMITED) { $out .= "Never"; } elsif ($val == UNPAID) { $out .= "Awaiting Payment"; } elsif ($val == FREE) { $out .= "No Payment Required (free)"; if ($rec->{LinkExpired}) { require GT::Date; $out .= " - Payment Expired " . GT::Date::date_get($rec->{LinkExpired}, '%yyyy%/%m%/%d% %HH%:%MM%:%ss%'); } $out .= ""; } elsif ($val == 0) { $out .= "Invalid Date (0)!"; } else { require GT::Date; $out .= GT::Date::date_get($val, '%yyyy%/%m%/%d% %HH%:%MM%:%ss%'); } $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;