#!/usr/bin/perl
# ==================================================================
# Links SQL - enhanced directory management system
#
#   Website  : http://gossamer-threads.com/
#   Support  : http://gossamer-threads.com/scripts/support/
#   CVS Info : 087,070,087,092,085 
#   Revision : $Id: showpicture.cgi,v 1.6 2006/12/29 15:37:20 aki 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.
# ==================================================================

    use strict;
    use lib 'admin';
    use Links qw/$DB $IN $USER $CFG/;
    use Links::Plugins;

    Links::init('admin');
    Links::init_user();

    local $SIG{__DIE__} = \&Links::fatal;


    main();

sub main {
# -------------------------------------------------------------------
# display an html page for a picture frame    
#

    my $tbl  = $DB->table('Links');
    my $id = $IN->param('ID');
    my $rec  = $tbl->get( $id ) || {};
    my $col  = $IN->param('v');

# go through and find out what columsn have images
    my $conf       = Links::Plugins::get_plugin_user_cfg( 'SlideShow' );
    my @image_cols = grep $_, map { s,^(\s*),,; s,(\s*)$,,; $_ } split /,/, $conf->{seq_image_cols};

    $col ||= $image_cols[ int( $IN->param( 'index' ) || 0 ) || 0];
    $col ||= $image_cols[0];

    my $cached     = eval $rec->{SlideShowCache};
    my $changed    = 1;
    my ( $linksdb, $linkscols, %image_lookup );

    require Plugins::SlideShow;
    my $paths = Plugins::SlideShow::generate_paths( $id, $rec );
    @$rec{ keys %$paths } = values %$paths;

# find out which columns have files (assume images) populated
# create 
#       - @images order array with column names 
#       - %images hash with the index lookup
    my ( @images, %images, $index ); 
    foreach my $colname ( @image_cols ) {
        $rec->{$colname} or next;
        push @images, $colname;
        $images{$colname} = $index++;
    }

# find out where we are in the list
    my ( $Image_Previous, $Image_Next, $Image_First, $Image_Last, 
        $Image_Count, $Image_Tool, $Image_Current );
    $Image_Count = scalar( @images );
    $index = $images{ $col }; # convert from column name to index #

# get the next and previous
    ( $index > 0 ) and $Image_Previous = $images[$index-1];
    ( $index < $Image_Count ) and $Image_Next = $images[$index+1];

# get the first and last
    $Image_Current          = $index + 1;
    $index and $Image_First = $images[0];
    $index+1 < $Image_Count and $Image_Last = $images[$Image_Count-1];

# for the toolbar
    $index = 1;
    foreach my $colname ( @images ) {
        push @{$Image_Tool}, {
                                Image_Colname => $colname,
                                Image_Index => $index++,
                                Image_Selected => ( $colname eq $col ? 1 : 0 ),
                             };
    }


    print $IN->header();
    print Links::user_page( 
        'pictureframe.html', 
        { 
            %{$USER || {} }, 
            %{$IN->get_hash}, 
            %$rec,
            %{$image_lookup{$col}||{}},
            Image_Previous     => $Image_Previous,
            Image_First        => $Image_First,
            Image_Last         => $Image_Last,
            Image_Next         => $Image_Next,
            Image_Count        => $Image_Count,
            Image_Current      => $Image_Current,
            Image_Description  => $rec->{$col."_description"},
            Image_Tool         => $Image_Tool,
            image_url_path     => sub {
                my $col  = join( "_", @_ ) or return;
                my $tags = GT::Template->tags;
                return $tags->{$col.'_path'};
            }
        } 
    );

}



