# ==================================================================
# Gossamer List - enhanced mailing list management system
#
#   Website  : http://gossamer-threads.com/
#   Support  : http://gossamer-threads.com/scripts/support/
#   CVS Info :                          
#   Revision : $Id: GUI.pm,v 1.5 2004/08/24 19:28:37 bao Exp $
#
# Copyright (c) 2004 Gossamer Threads Inc.  All Rights Reserved.
# Redistribution in part or in whole strictly prohibited.  Please
# see LICENSE file for full details.
# ==================================================================
#

package GList::GUI;
# ==================================================================

use strict;
use GList qw/:objects/;

sub gui_profile_form {
# -------------------------------------------------------------------
    require GT::SQL::Display::HTML;
    require GT::SQL::Display::HTML::Table;

    my $opts    = {@_};
    my $user_tb = $DB->table('Users');

    $opts->{cols} ||= [ grep(/^pro_/, $user_tb->ordered_columns) ];
    $opts->{tr}   ||= 'class="body"';
    $opts->{td_l} ||= 'class="body" width="40%" align="right"';
    $opts->{td_r} ||= 'class="body" align="left"';
    $opts->{cols} ||= [];
    $opts->{mode} ||= 'edit';
    $opts->{required} ||= ($opts->{mode} eq 'search') ? '' : '*';

    my $tags    = GT::Template->tags;
    my $cols    = $user_tb->cols;
    my $disp    = $DB->html($user_tb, GT::Template->tags);
    my $html    = '';
    my $prefix  = $opts->{prefix} || '';

    if ( $opts->{mode} eq 'hidden' ) {

# Preserve all columns that relate to the Users database
        my $cols        = $user_tb->cols;
        my $hidden_html = '';
        foreach my $col ( keys %$cols ) {
            foreach my $name ( map { "$col$_" } ( '', qw( -opt -gt -lt -le -ge -ne )) ) {
                my $v = $tags->{$name};
                next unless defined $v;
                my $input_html = gui_form_control({
                    form_type => 'hidden',
                    value     => $v,
                    name      => $name
                });
                $html .= $$input_html;
            }
        }
        return \$html;
    }

    my %search_defs = (
        string  => { names => [qw( LIKE <> = )] },
        number  => { names => [qw( = <> < <= > >= )] },
        date    => { names => [ '', qw( = <> < <= > >= )] },
        radio   => { names => [qw( = <> )] },
        minimal => { names => [qw( = )] }
    );

    foreach my $col (@{$opts->{cols}}) {
        my $control_opts        = {%{$cols->{$col}||{}}};
        $control_opts->{name}   = $col;
        $control_opts->{value}  = $tags->{$col};
        my $title               = GList::language( $cols->{$col}{form_display} );
        my $input_html = gui_form_control({
            name => "$prefix$col",
            value=> ($opts->{mode} eq 'search') ? '' : $tags->{"$prefix$col"},
            def  => $control_opts
        });
        $html .= ( $cols->{$col}->{not_null} ) ?
            "<tr $opts->{tr}><td $opts->{td_l}><ul/>$title</td><td $opts->{td_r}>$$input_html $opts->{required}</td></tr>" :
            "<tr $opts->{tr}><td $opts->{td_l}><ul/>$title</td><td $opts->{td_r}>$$input_html</td></tr>";
    }
    return \$html;
}

sub gui_form_control {
# -------------------------------------------------------------------
    require GT::SQL::Display::HTML;
    require GT::SQL::Display::HTML::Table;
    require GT::Template;

    my $opts      = ref $_[0] eq 'HASH' ? shift : {@_};

    my $user_tb   = $DB->table('Users');
    my $tags      = GT::Template->tags || {};
    my $disp      = $DB->html($user_tb, $tags);

    my $form_type  = lc $opts->{def}->{form_type};
    exists $opts->{blank} or $opts->{blank} = $form_type eq 'select' ? 1 : 0;

    $opts->{def}->{class} = 'object' if ($form_type !~ /radio|checkbox/);
    my $input_html =  'radio'    eq $form_type ? $disp->radio( $opts ) :
                      'checkbox' eq $form_type ? $disp->checkbox( $opts ) :
                      'select'   eq $form_type ? $disp->select( $opts ) :
                      'hidden'   eq $form_type ? $disp->hidden( $opts ) :
                      'multiple' eq $form_type ? $disp->multiple( $opts ) :
                      'textarea' eq $form_type ? $disp->textarea( $opts ) :
                      'file'     eq $form_type ? "File type not supported." :
                      'date'     eq $form_type ? do {
                            require GT::Date;
                            my ($sel_year, $sel_mon, $sel_day) = split /\-/, GT::CGI::html_escape($opts->{value});
                            $sel_year ||= 1970;
                            $sel_mon  ||= 1;
                            $sel_day  ||= 1;
                            my $month_sel = $disp->select({
                                name   => "$opts->{name}-mon",
                                value  => $sel_mon,
                                values => { map { sprintf("%02d", $_) => $GT::Date::LANGUAGE->{short_month_names}->[$_ - 1] } (1 .. 12) },
                                sort   => [ map { sprintf("%02d", $_) } (1 .. 12) ],
                                blank  => 0
                            });
                            my $day_sel = $disp->select({
                                name => "$opts->{name}-day",
                                value => $sel_day,
                                values => { map { sprintf("%02d", $_) => $_ } (1 .. 31) },
                                sort   => [ map { sprintf("%02d", $_) } (1 .. 31) ],
                                blank => 0
                            });
                            qq~
                                $day_sel /
                                $month_sel /
                                <input type="text" name="$opts->{name}-year" value="$sel_year" size="4" maxlength="4">
                            ~;
                        } :
                        $disp->text($opts);

    return \$input_html;
}

sub gui_toolbar {
    my %input = @_;
    my $tags = GT::Template->tags;
    $input{first}          ||= q|<img src="$image_url/icons/first.gif" border="0" width="17" height="11" alt="First page">|;
    $input{first_grey}     ||= q|<img src="$image_url/icons/first_grey.gif" border="0" width="17" height="11" alt="First page">|;
    $input{prev}           ||= q|<img src="$image_url/icons/prev.gif" border="0" width="10" height="11" alt="Previous page">|;
    $input{prev_grey}      ||= q|<img src="$image_url/icons/prev_grey.gif" border="0" width="10" height="11" alt="Previous page">|;
    $input{next}           ||= q|<img src="$image_url/icons/next.gif" border="0" width="10" height="11" alt="Next page">|;
    $input{next_grey}      ||= q|<img src="$image_url/icons/next_grey.gif" border="0" width="10" height="11" alt="Next page">|;
    $input{last}           ||= q|<img src="$image_url/icons/last.gif" border="0" width="17" height="11" alt="Last page">|;
    $input{last_grey}      ||= q|<img src="$image_url/icons/last_grey.gif" border="0" width="17" height="11" alt="Last page">|;
    $input{view_all}       ||= q|View All|;
    $input{pages}          ||= 9;
    $input{'...'}          ||= '...';
    $input{'first_...'}    ||= 1;
    $input{'last_...'}     ||= 1;
    $input{before_page}    ||= q||;
    $input{after_page}     ||= q||;
    $input{before_current} ||= q|<b>|;
    $input{after_current}  ||= q|</b>|;
    $input{'glist.cgi'}    ||= 'glist.cgi';

    for (keys %input) {
        $input{$_} =~ s/\$image_url/$tags->{image_url}/g;
    }

    my $hidden_query = ${$tags->{hidden_query} || \''};

    my $num_page_items = ref $tags->{num_page_items} eq 'SCALAR' ? ${$tags->{num_page_items}} : $tags->{num_page_items};
    my $paging = GList::paging($num_page_items, @$tags{qw/mh nh/}, @input{qw/pages last_.../});
    ($paging, my ($top_page, $ddd)) = @$paging{'paging', 'top_page', 'dotdotdot'};
    my $return = '';
    my $search = '';
    if ($tags->{toolbar_table}) {
        my $cols = $DB->table($tags->{toolbar_table})->cols;
        foreach my $c (keys %{$cols}) {
            next unless $tags->{$c};
            $search .= qq|$c=$tags->{$c};|;
            if ($tags->{"$c-opt"}) { $search .= qq|$c-opt=$tags->{"$c-opt"};|; }
        }
    }

    my $link = sub {
        my ($page, $disp) = @_;
        $return .= qq|<a href="$input{'glist.cgi'}?do=$tags->{do};|;
        if ($tags->{toolbar_query}) {
            my $query = ref $tags->{toolbar_query} ? ${$tags->{toolbar_query}} : $tags->{toolbar_query};
            $return .= qq|$query;|;
        }
        if ($search) {
            $return .= qq|$search|;
        }
        $return .= qq|nh=$page;|;
        if ($tags->{users})     { $return .= qq|users=1;| }
        if ($tags->{show_user}) { $return .= qq|show_user=1;| }
        if ($tags->{fd})        { $return .= qq|fd=$tags->{fd};| }
        if ($tags->{sb})        { $return .= qq|sb=$tags->{sb};| }
        if ($tags->{so})        { $return .= qq|so=$tags->{so};| }
        if ($tags->{mh})        { $return .= qq|mh=$tags->{mh};| }
        if ($tags->{id})        { $return .= qq|id=$tags->{id};| }
        if ($tags->{cs})        { $return .= qq|cs=$tags->{cs};| }
        if ($tags->{first})     { $return .= qq|first=$tags->{first};| }
        if ($tags->{mn_disable}){ $return .= qq|mn_disable=1;| }

        $return .= qq|$hidden_query">$disp</a>\n|;
    };

    unless ($top_page == 1) {
        if ($tags->{nh} == 1) {
            $return .= $input{first_grey} . "\n";
            $return .= $input{prev_grey} . "\n";
        }
        else {
            my $prev = ($tags->{nh} == -1) ? 1 : ($tags->{nh} - 1);
            $link->(1, $input{first});
            $link->($prev, $input{prev});
        }

        if (@$paging and $paging->[0]->{page_num} > 1 and $input{'first_...'}) {
            $link->(1, qq|$input{before_page}1$input{after_page}|);
            $return .= "$input{before_page}$input{'...'}" . ($input{after_page} || " ") if $paging->[0]->{page_num} > 2;
        }
        for (@$paging) {
            if ($_->{is_current_page}) {
                $return .= qq|$input{before_current}$_->{page_num}$input{after_current}\n|;
            }
            else {
                $link->($_->{page_num}, qq|$input{before_page}$_->{page_num}$input{after_page}|);
            }
        }
        if ($ddd) {
            $return .= "$input{before_page}$input{'...'}" . ($input{after_page} || " ");
            $link->($top_page, "$input{before_page}$top_page$input{after_page}");
        }

        if ($tags->{nh} >= $top_page) {
            $return .= $input{next_grey} . "\n";
            $return .= $input{last_grey} . "\n";
        }
        else {
            my $next = ($tags->{nh} == -1) ? 1 : ($tags->{nh} + 1);
            $link->($next, $input{next});
            $link->($top_page, $input{last});
        }
    }
    return \$return;
}
1;
