#!/usr/bin/perl
#------------------------------------------------------------------------------
# Copyright (C) 2001 Thomax Kaulmann
# 
# 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, 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.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


use DBI;
use CGI;
use OMA;
use NEWASSET;
use XSQL;

local (%in, @row, $st, , $trans);
undef(%in);
undef(@row);
undef($trans);


                   ###            ##
                    ##            ##
 ##  ## ## ###   #####   ####    #####    ####
 ##  ##  ##  ## ##  ##      ##    ##     ##  ##
 ##  ##  ##  ## ##  ##   #####    ##     ######
 ##  ##  #####  ##  ##  ##  ##    ## ##  ##
  #####  ##      ### ##  ### ##    ###    ####
        ####
sub update {
	local @deps;
	local (%hashrow, %hashrow2);
	local ($st, $nst);
	local @row;

	foreach $k (keys %in) {
		$in{$k} =~ s/'/\'/g;
		$in{$k} =~ s/"/&quot;/g;
	}

	print "<pre>";
	XSQL::transact("
		UPDATE	newasset_page
		SET		title = '$in{'title'}',
				expire = $in{'expire'}
		WHERE	id=$in{'ID'}
		AND location='$NEWASSET::HOST'");

		
	$st = $db->prepare("
		SELECT	*
		FROM	newasset_conn
		WHERE	newasset_conn.newasset_page=$in{'ID'}
		AND		newasset_conn.location='$NEWASSET::HOST'
	");
	$st->execute();
	$st->bind_columns(\(@hashrow{@{$st->{NAME_lc}}}));


	@deps = split(/\000/,$in{'department'});

	while(@row = $st->fetchrow()) {
		XSQL::transact("
			UPDATE	item
			SET		description = '$in{'title'}'
			WHERE	type != 'meta'
			AND		(sid=$hashrow{item_sid}
			OR		link_item=$hashrow{item_sid})
		");
		XSQL::transact("
			UPDATE	item
			SET		node_sid = $deps[0]
			WHERE	type != 'meta'
			AND		(sid=$hashrow{item_sid}
			AND		link_item=0)
		");
	}
	$st->finish();

	print "DONE.\n";
}





  ####   ####    ##  ##   ####
 ##         ##   ##  ##  ##  ##
  ####   #####   ##  ##  ######
     ## ##  ##    ####   ##
 #####   ### ##    ##     ####

sub save {
	local @deps, $ID, $i;
	undef(@deps);

	$ID = time;

	mkdir("$NEWASSET::NEWASSET_PATH/$ID",0775) or die "Can't create desired directory";
	system("mv \"$NEWASSET::DOWNLOAD/$ENV{'REMOTE_USER'}/$in{'identifier'}\" $NEWASSET::NEWASSET_PATH/$ID") if($in{'identifier'});

	foreach $k (keys %in) {
		$in{$k} =~ s/'/\'/g;
		$in{$k} =~ s/"/&quot;/g;
	}

	$in{'expire'} = 0 if(!$in{'expire'});
	
	XSQL::transact("
		INSERT INTO newasset_page
		VALUES (	$ID,
					'$NEWASSET::HOST',
					'$ENV{'REMOTE_USER'}',
					'$in{'title'}',
					'$in{'identifier'}',
					$in{'expire'},
					'$NEWASSET::NEWASSETDATA')");

	@deps = split(/\000/,$in{'department'});
	for($i=0;$i<=$#deps;$i++) {
		my $newsid = OMA::nextsid("item_sid");
		XSQL::transact("INSERT INTO item VALUES ($newsid,$deps[$i],'$in{'title'}','newasset',0,0)");
		XSQL::transact("
			INSERT INTO newasset_conn
			VALUES ($newsid,$ID,'$NEWASSET::HOST',0,$newsid)");
	}
	print "DONE.\n";
}



   ###            ###
    ##             ##
 #####    ####     ##
##  ##   ##  ##    ##
##  ##   ######    ##
##  ##   ##        ##
 ### ##   ####    ####

sub del {
	print "delete all related with $in{'ID'}\n";
			if(-d "$NEWASSET::NEWASSET_PATH/$in{'ID'}") {
		system("rm -rf $NEWASSET::NEWASSET_PATH/$in{'ID'}");	
	}

	$st = $db->prepare("
		SELECT	*
		FROM	newasset_conn
		WHERE	newasset_conn.newasset_page=$in{'ID'}
		AND		newasset_conn.location='$NEWASSET::HOST'");
	$st->execute();

	while(@row=$st->fetchrow()) {
		XSQL::transact("
				DELETE FROM item
				WHERE	(item.sid = $row[0]
				OR		item.link_item = $row[0])
				AND item.type='newasset'
		");
	}
	$st->finish();

	XSQL::transact("
		DELETE FROM newasset_conn
		WHERE newasset_conn.newasset_page=$in{'ID'}
		AND newasset_conn.location='$NEWASSET::HOST'");

	XSQL::transact("
		DELETE FROM newasset_page
		WHERE id=$in{'ID'}
		AND location='$NEWASSET::HOST'");
}



                        ###                     ###
                         ##                      ##
  #####   ####           #####   ####     ####   ##  ##
 ##  ##  ##  ##          ##  ##     ##   ##  ##  ## ##
 ##  ##  ##  ##          ##  ##  #####   ##      ####
  #####  ##  ##          ##  ## ##  ##   ##  ##  ## ##
     ##   ####          ## ###   ### ##   ####  ###  ##
 #####          ########
sub go_back {
	print "You did not enter something in the field <b>$_[0]</b>.
Please <a href=javascript:this.back()>switch back</a> to correct this.

";
}



                                   ###
                                  ## ##
## ###  ## ###    ####    ####    ##
 ##  ##  ### ##  ##  ##  ##  ##  ####
 ##  ##  ##      ##  ##  ##  ##   ##
 #####   ##      ##  ##  ##  ##   ##
 ##     ####      ####    ####   ####
####
sub proof {
	local (@deps, $d="", $i, $op);
	undef(@deps);

	print "$NEWASSET::NEWASSET_BODY";
	print "<b>[newasset]</b>";

	if((!$in{'identifier'}
	||	!$in{'title'}
	||	!$in{'department'})
	&&	($in{'op2'} ne "delete")) {
		print "SORRY! YOU FORGET TO SELECT MEDIA FILE OR ENTER A TITLE OR SELECT A NODE\n";
		exit;
	}
	print "<table><tr><td><form method=post>";
	@deps = split(/\000/,$in{'department'});
	for($i=0;$i<=$#deps;$i++) {
		$d .= OMA::getpath($deps[$i]). ", ";
#		print "<input type=hidden name=department value=\"$deps[$i]\">";
	}

	print "<input type=hidden name=department value=\"$in{'department'}\">";
	$op = "<input type=hidden name=op value=save>";
	$op = "<input type=hidden name=op value=update> <input type=hidden name=ID value=$in{'ID'}>" if($in{'op2'} eq "update");
	$op = "<input type=hidden name=op value=delete> <input type=hidden name=ID value=$in{'ID'}>" if($in{'op2'} eq "delete");

$in{'abstract'} =~ s/"/&quot;/g;

	print "
<pre>
Your entry is:

       title : $in{'title'}
days to live : $in{'expire'}
   locations : $d

$in{'abstract'}

</pre>
";

	foreach $k (keys %in) {
		$in{$k} =~ s/'/\\'/g;
		$in{$k} =~ s/"/&quot;/g;
	}

	print <<"EOT";
<input type=hidden name=title value="$in{'title'}">
<input type=hidden name=identifier value="$in{'identifier'}">
<input type=hidden name=expire value="$in{'expire'}">

$op
EOT

	go_back("identifier") if(!$in{'identifier'});
	go_back("title") if(!$in{'title'});

	print <<"EOT";
<input type=submit value=\"I am responsible.\">
</td></tr></table></form>
EOT
}



                                ###
                                 ##
  ####   ## ##   ####     ####   ##  ##
 ##  ## #######     ##   ##      ## ##
 ###### ## # ##  #####    ####   ####
 ##     ## # ## ##  ##       ##  ## ##
  ####  ##   ##  ### ##  #####  ###  ##

sub emask {
	local ($files, $deps, @row);

	$file = $deps = "";

	$st=$db->prepare("SELECT * FROM treemap ORDER BY node");
	$st->execute();
	while(@row=$st->fetchrow()) {
		$deps .=  "<option value=$row[0]>$row[1]";
	}
	$st->finish();


	open(P,"/bin/ls $NEWASSET::DOWNLOAD/$ENV{'REMOTE_USER'}/* 2> /dev/null |");
	while(<P>)
	{
	    $_ =~ s/$NEWASSET::DOWNLOAD\/$ENV{'REMOTE_USER'}\///g;
	    $_ = "<OPTION> $_";
	    $files .= $_;
	}
	close(P);

	print <<"EOT";
$NEWASSET::NEWASSET_BODY

<b>[newasset]</b>


<form method=post>
<pre>
title : <input name=title type=text size=30>

newasset File:
<select name=identifier size=6>
$files
</select>

department :
<select name=department size=6>$deps</select>

days to live: <input type=text size=3 name=expire value=0>

<input type=hidden name=op value=proof>
<input type=submit value="I have the right to publish this material.">

</pre>
</form>
EOT
}




                                                  ##
                                                  ##
  ####    ####  ## ###  ## ###    ####    ####   #####
 ##  ##  ##  ##  ### ##  ### ##  ##  ##  ##  ##   ##
 ##      ##  ##  ##      ##      ######  ##       ##
 ##  ##  ##  ##  ##      ##      ##      ##  ##   ## ##
  ####    ####  ####    ####      ####    ####     ###

sub correct {
	local $deps;
	local @row;
	local $abstract;
	local %deps_s;
	local %hashrow;

	undef(@row);
	undef(%deps_s);
	undef(%hashrow);
	undef(@abstract);
	$deps = "";

	$st = $db->prepare("
				SELECT	*
				FROM	newasset_page
				WHERE	id=$in{'ID'}
				AND		location='$in{'base'}'
	");
	$st->execute();
	$st->bind_columns(\(@hashrow{@{$st->{NAME_lc}}}));
	$st->fetchrow();
	$st->finish();

	$deps="";
	$st = $db->prepare("
			SELECT	newasset_conn.newasset_page,item.node_sid
			FROM	newasset_conn, item
 			WHERE	newasset_conn.item_sid = item.sid
		 	AND		newasset_conn.newasset_page=$in{'ID'}
			AND		newasset_conn.location='$in{'base'}'
			AND		item.link_item=0
			AND		item.type != 'meta'
	");
	$st->execute();
	while(@row=$st->fetchrow()) {
	 	$deps_s{$row[1]} = $row[0];
 	}
 	$st->finish();

	$st = $db->prepare("SELECT * FROM treemap ORDER BY node");
	$st->execute();
	while(@row=$st->fetchrow()) {
		if($deps_s{$row[0]}) {
			$deps .= "<option value=$row[0] selected>$row[1]\n";
		}
		else {
			$deps .= "<option value=$row[0]>$row[1]\n";
		}
	}


	print <<"EOT";
$NEWASSET::NEWASSET_BODY
<b>[newasset]</b>


<form method=post>
<table>
<tr>
<td>
<pre>
          title : <input name=title type=text value="$hashrow{title}" size=30>
</td>
<td>
<pre>

newasset File : $hashrow{identifier}
<input type=hidden name=identifier value="$hashrow{identifier}">
</td>
</tr>
<tr>
<td>
<pre>

     department : <select name=department size=6>
$deps
</select>
</td>
<td>
</td>
</tr>
<tr>
<td colspan=2>
<pre>
   days to live : <input type=text size=3 name=expire value="$hashrow{expire}">


<input type=submit value="I have the right to publish this material.">
</pre>
<input type=hidden name=op value=proof>
<input type=hidden name=op2 value=update>
<input type=hidden name=ID value=$in{'ID'}>
</form>

<form method=post>
<input type=hidden name=op value=proof>
<input type=hidden name=op2 value=delete>
<input type=hidden name=ID value=$in{'ID'}>
<input type=submit value="I have the right to delete this material.">
</form>
</td>
</tr>
</table>
EOT
}


##   ##            ##            ######         ##   ##
### ###           ####             ##           ###  ##
#######          ##  ##            ##           #### ##
#######          ##  ##            ##           ## ####
## # ##          ######            ##           ##  ###
##   ##          ##  ##            ##           ##   ##
##   ##          ##  ##          ######         ##   ##

CGI::ReadParse();
$db = NEWASSET::opendb();
if($msg=OMA::opallowed($in{'ID'},$in{'base'},'newasset')) {
    print $msg;
    exit;
}

XSQL::transopen();

subtitles_save() if($in{'op'} eq "subtitles_save");
del() if($in{'op'} eq "delete");
update() if($in{'op'} eq "update");
save() if($in{'op'} eq "save");
proof() if($in{'op'} eq "proof");
subtitles() if($in{'op'} eq "subtitles");
correct() if($in{'op'} eq "correct");
emask() if($in{'op'} eq "" || !defined($in{'op'}));

XSQL::transclose();

