#!/usr/bin/perl -wT
#
# TWiki WikiClone (see wiki.pm for $wikiversion and other info)
#
# Copyright (C) 1999-2002 Peter Thoeny, peter@thoeny.com
# Copyright (C) 2002 Motorola - all rights reserved
#
# 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

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

use strict;

&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 $breakLock = $query->param( 'breaklock' ) || "";
  my $onlyWikiName = $query->param( 'onlywikiname' ) || "";
  my $text = "";
  my $meta = "";
  my $extra = "";
  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 );
    print $query->redirect( $url );
    return;
  }

  # read topic and check access permission
  if( &TWiki::Store::topicExists( $webName, $topic ) ) {
    ( $meta, $text ) = &TWiki::Store::readTopic( $webName, $topic );
  }
  my $actionEditAllowed = &TWiki::Access::checkActionChangePermission( $wikiUserName, $text, $topic, $webName );
  my $accessAllowed = &TWiki::Access::checkAccessPermission( "change", $wikiUserName, $text, $topic, $webName );
  #TARZAN
  if( ! $accessAllowed  &&  ! $actionEditAllowed ) {
    # user has not permission to change the topic
    my $url = &TWiki::getOopsUrl( $webName, $topic, "oopsaccesschange" );
    TWiki::redirect( $query, $url );
    return;
  }

  my( $lockUser, $lockTime ) = &TWiki::Store::topicIsLockedBy( $webName, $topic );
  if( ( ! $breakLock ) && ( $lockUser ) ) {
    # warn user that other person is editing this topic
    $lockUser = &TWiki::userToWikiName( $lockUser );
    use integer;
    $lockTime = ( $lockTime / 60 ) + 1; # convert to minutes
    my $editLock = $TWiki::editLockTime / 60;
    # PTh 20 Jun 2000: changed to getOopsUrl
    my $url = &TWiki::getOopsUrl( $webName, $topic, "oopslocked", $lockUser, $editLock, $lockTime );
    TWiki::redirect( $query, $url );
    return;
  }
  &TWiki::Store::lockTopic( $topic );

  # get edit template, standard or a different skin
  my $skin = $query->param( "skin" ) || &TWiki::Prefs::getPreferencesValue( "SKIN" );
  my $tmpl = &TWiki::Store::readTemplate( "editaction", $skin );

  if( $TWiki::doLogTopicEdit ) {
      # write log entry
    &TWiki::Store::writeLog( "editaction", "$webName.$topic" );
  }

  # Most of the script above this point is identical to 'edit'
  # Now we do the action-specific stuff
  # Find the Nth action

  my $tmplForm = TWiki::Func::readTemplate( "actionform", "");
  my $date = TWiki::getGmDate();
  $tmplForm =~ s/%DATE%/$date/go;
  my $user = TWiki::Func::getWikiUserName();
  $tmplForm =~ s/%WIKIUSERNAME%/$user/go;
  $tmplForm = TWiki::Func::expandCommonVariables( $tmplForm, $topic, $webName );
  $tmplForm = TWiki::Func::renderText( $tmplForm, $webName );

  # The 'command' parameter is used to signal to the afterEditHandler and
  # the beforeSaveHandler that they have to handle the fields of the
  # edit differently
  my $fields = $query->hidden( -name=>'closeactioneditor', -value=>1 );
  $fields .= $query->hidden( -name=>'cmd', -value=>"" );

  # Throw away $_[0] and re-read the topic, extracting meta-data.
  # Oh, how I wish the topic reading/writing was smarter! Or even
  # that already extracted meta-data was passed in here!
  my $oldText = TWiki::Func::readTopicText( $_[2], $_[1]);
  $text = "";
  foreach my $line ( split( /\r?\n/, $oldText ) ) {
    if( $line =~ /^%META:([^{]+){([^}]*)}%/ ) {
      my $type = $1;
      my $args = $2;
      if ( $type eq "FIELD" ) {
	      my $name = "UNKNOWN";
	      my $value = "";
	      if ( $args =~ m/\s*name=\"([^\"]*)\"/io ) {
	        $name = $1;
	      }
	      if ( $args =~ m/\s*value=\"([^\"]*)\"/io ) {
	        $value = $1;
	      }
	      $fields .= $query->hidden( -name=>$name, -value=>$value );
      }
    } else {
      $text .= "$line\n";
    }
  }

  # Find the action. This re-reads the topic, but the cost doesn't seem
  # to be too high.
  my $uid = $query->param( "action" ) || $query->param( "last" ) || $query->param( "parent" );
  my $parent = $query->param( "parent" ) || "";
  my $attrs = "";
  my ( $action, $pretext, $posttext ) = ActionTrackerPlugin::Action::findActionByUID( $webName, $topic, $text, $uid );

  if ( defined( $query->param( "remove" ) ) ) {
    my $cachedActions = TWiki::Plugins::ActionTrackerPlugin::readActionFile();
    $pretext =~ s/ {3}/\t/go;
    $posttext =~ s/ {3}/\t/go;
    my $error = &TWiki::Store::saveTopic( $webName, $topic, "$pretext$posttext", $meta, "", "on", "checked" );
    if ($error) {
      my $url = &TWiki::getOopsUrl( $webName, $topic, "oopssaveerr", $error );
      TWiki::redirect( $query, $url );
    }
    else {
      #TWiki::Plugins::ActionTrackerPlugin::writeActionFile( $cachedActions, $uid );
      my $url = &TWiki::getViewUrl( $webName, $topic );
      TWiki::redirect( $query, $url."?unlock=on" );
    }
    return;
  }
    
  if ( defined( $query->param( "add" ) ) ) {
    $pretext .= $action->toString()."\n";
    if ( $parent ne "" ) { $attrs = "parent=$parent" };
    $action = new ActionTrackerPlugin::Action( $topic, $webName, 0, $attrs, "" );
    $action->populateNewActionMissingFields();
    $uid = $action->{uid};
    $fields .= $query->hidden( -name=>'add', -value=>"on" );
  }
  my $from = $query->param( "actionfrom" ) || "";
  $fields .= $query->hidden( -name=>'actionfrom', -value=>$from );
  $fields .= $query->hidden( -name=>'pretext', -value=>$pretext );
  $fields .= $query->hidden( -name=>'posttext', -value=>$posttext );

  $tmplForm =~ s/%UID%/$uid/go;
  
  my $useNewWindow = ( _getPref( "USENEWWINDOW", 0 ) == 1 );
  
#  if ( $useNewWindow ) {
#    $cancelScript = "ONCLICK=\"window.close();\"";
#  }

  my $hdrs = _getPref( "EDITHEADER" );
  my $body = _getPref( "EDITFORMAT" );
  my $vert = _getPref( "EDITORIENT" );

  my $fmt = new ActionTrackerPlugin::Format( $hdrs, $body, $vert, "", "" );
  my $editable = $action->formatForEdit( $fmt );
  $tmplForm =~ s/%EDITFIELDS%/$editable/o;

  my $dfltH = TWiki::Func::getPreferencesValue( 'EDITBOXHEIGHT' );
  my $ebh = _getPref( "EDITBOXHEIGHT", $dfltH );
  $tmplForm =~ s/%EBH%/$ebh/go;
  
  my $dfltW = TWiki::Func::getPreferencesValue( 'EDITBOXWIDTH' );
  my $ebw = _getPref( "EDITBOXWIDTH", $dfltW );
  $tmplForm =~ s/%EBW%/$ebw/go;

  $text = $action->{text};
  # Process the text so it's nice to edit. This gets undone in Action.pm
  # when the action is saved.
  $text =~ s/^\t/   /gos;
  $text =~ s/<br( \/)?>/\n/gios;
  $text =~ s/<p( \/)?>/\n\n/gios;

  $tmplForm =~ s/%TEXT%/$text/go;
  $tmplForm =~ s/%HIDDENFIELDS%/$fields/go;

#    if ($actionEditAllowed == 2) {
#      $tmpl =~ s/%WHO%/<input type='hidden' name='who' value='$who'>$who/go;
#      $tmpl =~ s/%TASKMASTER%/<input type=hidden' name='taskmaster' value='$taskmaster'>$taskmaster/go;
#    	$tmpl =~ s/%DUE%/<input type='hidden' name='due' value='$due'>$due/go;
#	    $tmpl =~ s/%READONLY%/readonly/go;
#    }
#    else {
#      $tmpl =~ s/%WHO%/<input type='text' name='who' value='$who'>/go;
#      $tmpl =~ s/%TASKMASTER%/<input type='text' name='taskmaster' value='$taskmaster'>/go;
#	    $tmpl =~ s/%DUE%/<input type='text' name='due' value='$due'>/go;
#	    $tmpl =~ s/%READONLY%//go;
#    }
#    $tmpl =~ s/%OPENSELECT%/$openselect/go;
#    $tmpl =~ s/%CLOSEDSELECT%/$closedselect/go;
#    $tmpl =~ s/%ACTION_NUMBER%/$an/go;
#    $tmpl =~ s/%TEXT%/$text/go;
#    $tmpl =~ s/%COMMENTS%/$commenttopic/go;
#    $tmpl =~ s/%EBH%/$ebh/go;
#    $tmpl =~ s/%EBW%/$ebw/go;

  $tmpl = &TWiki::handleCommonTags( $tmpl, $topic );
  $tmpl = &TWiki::handleMetaTags( $webName, $topic, $tmpl, $meta );
  $tmpl = &TWiki::getRenderedVersion( $tmpl );
  $tmpl =~ s/%TEXT%/$tmplForm/;
  $tmpl =~ s/%ACTION_NUMBER%/$uid/ge;

  TWiki::writeHeader( $query );
  print $tmpl;
}

sub _getPref {
  my ( $vbl, $default ) = @_;
  my $val = TWiki::Func::getPreferencesValue( "ACTIONTRACKERPLUGIN_$vbl" );
  if ( !defined( $val ) || $val eq "" ) {
    $val = $default;
  }
  return $val;
}

1;
