#!/usr/local/bin/perl -wI.
##!/home/bradford/bin/perl -wT
##!/usr/bin/perl -wT
#
#
# This script was modified from the original attach code to add a growing
# modifiable verification table to the page.
#
# Shawn Bradford 20011010 Initial design
#
# TWiki WikiClone (see TWiki.pm for $wikiversion and other info)
#
# Copyright (C) 1999-2001 Peter Thoeny, peter@thoeny.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details, published at 
# http://www.gnu.org/copyleft/gpl.html

# Modified to support Table editing support
# Shawn Bradford 2001-11-12

use CGI::Carp qw( fatalsToBrowser );
use CGI;
use lib ( '.' );
use lib ( '../lib' );
use TWiki;

use strict;

#open(STDERR,'>&STDOUT'); # redirect error to browser
$| = 1;                  # no buffering

&main();


# =========================
sub main
{
    my $query = new CGI;
    my $thePathInfo = $query->path_info(); 
    my $theRemoteUser = $query->remote_user();
    my $theTopic = $query->param( 'topic' );
    my $theUrl = $query->url;
    
    my( $topic, $webName, $dummy, $userName ) = 
	&TWiki::initialize( $thePathInfo, $theRemoteUser, $theTopic, $theUrl, $query );
    $dummy = "";  # to suppress warning

    my $tmpl = "";
    my $text = "";
    my $meta = "";
    my $atext = "";
	my $template = $query->param( 'template' ) || "";
	my $tableName = $query->param( 'tablename' ) || "";
	my $entry = $query->param( 'entry' ) || "";

	
    my $fileUser = "";
    my $wikiUserName = &TWiki::userToWikiName( $userName );

    if( ! &TWiki::Store::webExists( $webName ) ) {
        my $url = &TWiki::getOopsUrl( $webName, $topic, "oopsnoweb" );
        TWiki::redirect( $query, $url );
        return;
    }

    my( $mirrorSiteName, $mirrorViewURL ) = &TWiki::readOnlyMirrorWeb( $webName );
    if( $mirrorSiteName ) {
        my $url = &TWiki::getOopsUrl( $webName, $topic, "oopsmirror", $mirrorSiteName, $mirrorViewURL );
        TWiki::redirect( $query, $url );
        return;
    }

    # check access permission
    if( ! &TWiki::Access::checkAccessPermission( "change", $wikiUserName, "", $topic, $webName ) ) {
        my $url = &TWiki::getOopsUrl( $webName, $topic, "oopsaccesschange" );
        TWiki::redirect( $query, $url );
        return;
    }

    if( &TWiki::Store::topicExists( $webName, $topic ) ) {
        ( $meta, $text ) = &TWiki::Store::readTopic( $webName, $topic );
    } else {
        my $url = &TWiki::getOopsUrl( $webName, $topic, "oopsnoweb" );
        TWiki::redirect( $query, $url );
        return;
    }

    # why log attach before post is called?
    # FIXME: Move down, log only if successful (or with error msg?)
    # Attach is a read function, only has potential for a change
    if( $TWiki::doLogTopicAttach ) {
        # write log entry
        &TWiki::Store::writeLog( "editTable", "$webName.$topic", $tableName );
    }

	$tmpl = &TWiki::Store::readTemplate( "editnewTable" );

	# This loads the table that you want
    $tmpl =~ s/%TEMPLATE%/$template/go;
	$tmpl =~ s/%TABLENAME%/$tableName/go;

	# This renders the editable fields
	my @fieldDefs = &TWiki::Form::getFormDef( $webName, $template );

	# If we are editing an existing Form add meta fields
	if ($entry ne "") {
		my %table = $meta->findOne( "TABLE", TWiki::Plugins::TablePlugin::stringConvert($entry) );
		if ( $table{"name"} eq "" ) { #This was added to support the original style (Should eventually take out)
			%table = $meta->findOne( "TABLE", $entry );
		}
		foreach my $fieldDefP ( @fieldDefs ) {
			my @fieldDef = @$fieldDefP;
			my $entryName = shift @fieldDef;
			my $title = shift @fieldDef;
			my $value = $table{$entryName};
			my @tmpArgs = (
				"name" => $entryName,
				"title" => $title,
				"value" => TWiki::Plugins::TablePlugin::carriageReturnConvert( $value )
				);
			$meta->put("FIELD",@tmpArgs);
		}
		#$entry = TWiki::Plugins::TablePlugin::stringConvert($entry);
		$tmpl =~ s/%ENTRY%/$entry/go;
	} else {
		my $id = time;
		$tmpl =~ s/%ENTRY%/$id/go;
	}
	
    my $formText = &TWiki::Form::renderForEdit( $webName, $topic, $template, $meta, $query, 0, @fieldDefs );

    $tmpl =~ s/%ATTACHTABLE%/$atext/go;
    $tmpl = &TWiki::handleCommonTags( $tmpl, $topic );
    $tmpl = &TWiki::handleMetaTags( $webName, $topic, $tmpl, $meta );
    $tmpl = &TWiki::Render::getRenderedVersion( $tmpl );
	$tmpl =~ s/%TABLEFIELDS%/$formText/go; #Moved after getRenderedVersion so that TWiki Syntax does not expand    
    TWiki::writeHeader( $query );
    print $tmpl;
}

# EOF

