<?
	#---------------------------------------------------------------------#
	# This source code is (c)2000 White Pajamas, Inc                      #
	# see http://www.stonekeep.com/license.html for licensing information #
	#---------------------------------------------------------------------#

	#----------------------------------------------------------------------------
	# $Id: postslip.php3,v 1.59 2000/12/29 05:46:42 shevett Exp $
	#----------------------------------------------------------------------------
	
	#----------------------------------------------------------------------------
	# Post an added or modified slip.  (f_flag will tell 'mod' or 'add')
	#----------------------------------------------------------------------------

	$now=time();
	
	$a_problem = addslashes($a_problem);
	$a_summary = addslashes($a_summary);
	$scargs = explode("/",$a_sc_date);
	$soargs = explode("/",$a_so_date);
	LogIt("DEBUG","postslip.php3","scargs[2] is $scargs[2]");
	if ($scargs[2] < 1900) {
		$scargs[2] += 2000;
	}
	if ($soargs[2] < 1900) {
		$soargs[2] += 2000;
	}
	LogIt("DEBUG","postslip.php3","scargs[2] is now $scargs[2]");
	$na_sc_date	=	$a_sc_date > 0 ? mktime(0,0,0,$scargs[0],$scargs[1],$scargs[2]) : 0;
	$na_so_date	=	$a_so_date > 0 ? mktime(0,0,0,$soargs[0],$soargs[1],$soargs[2]) : 0;



	$flist	= "spid,depend,o_date,o_tech,c_date,c_tech,currtech,status,priority,grp,ctag,problem,summary,is_public,l_activity,l_tech,sc_date,so_date,spolicy,caddlabel,cphonelabel,timespent,resource";
	$vlist	= "$a_spid,$a_depend,$now,'$curruser',";
	# Was the slip both opened and closed in one-shot?
	if ($a_status == "$sys_closedflag") {
		$vlist .= "$now,'$curruser',";
	} else {
		$vlist .= "0,'',";
	}
	$vlist  .= "'$a_currtech','$a_status',$a_priority,'$a_grp','$a_ctag','$a_problem','$a_summary','$a_public',$now,'$curruser',$na_sc_date,$na_so_date,$a_spolicy,'$a_cadd','$a_cphone',$a_timespent,'$a_resource'";

	# Build up the bugtrack info if appropriate...
	if ($sys_bugtrack) {
		$bflist = "v_id,v_prod,v_mod,v_plat,v_foundin,v_target,v_verify,v_resolve";
		$bvlist = "'$a_v_prod','$a_v_mod','$a_v_plat','$a_v_foundin','$a_v_target','$a_v_verify','$a_v_resolve'";
	}
	
	if ($f_flag == "add") {

		$result	=	db_query("insert into slips ($flist) values ($vlist)");
		$a_sid = db_insertid($result);
		
		RecalcSlip($a_spid);

		if ($sys_bugtrack) {
			$result = db_query("insert into slipver ($bflist) values($a_sid,$bvlist)");
		}

		$lastresult[0] = 0;
		$lastresult[1] = "Slip added successfully, and assigned sid $a_sid.";
		LogIt("INFO","postslip.php3","new slip added by $curruser with sid $a_sid");
		$sid=$a_sid;
		
	}

	# $lookup is an array that has {table,key,name}
	# this is kind of an ugly interface, but I can't think of a better way to do this.
	Function CheckChanged($new,$old,$name,$lookup, $type) {
		Global $chstring, $oldslip;

		if ($new <> $old) {
			if ($lookup && $type) {
				$oldres = db_query("select $lookup[2] from $lookup[0] where $lookup[1]='$old'");
				$oldresarray = db_fetch_array($oldres);
				$newres = db_query("select $lookup[2] from $lookup[0] where $lookup[1]='$new'");
				$newresarray = db_fetch_array($newres);
				if ($type == "simple") {
					$old = ($old) ? $oldresarray[$lookup[2]] : "";
					$new = ($new) ? $newresarray[$lookup[2]] : "";
				} else if ($type == "mixed") {
					$old = ($old) ? $oldresarray[$lookup[2]] . " ($old)" : "";
					$new = ($new) ? $newresarray[$lookup[2]] . " ($new)" : "";
				}
			} else if ($type == "public") {
					$old = ($old) ? "Public" : "Private";
					$new = ($new) ? "Public" : "Private";
			}
			$chstring .= $name;
			if (strlen($new) > 60 || strlen($old) > 60) {
				$sep = "\n\n";
			} else {
				$sep = " ";
			}
			if ($new && $old) {
				$chstring .= " changed to".$sep."\"".$new."\"";
			} else if ($new && !$old) {
				$chstring .= " set to".$sep."\"".$new."\"";
			} else {
				$chstring .= " cleared";
			}
			$chstring .= ($old) ? $sep."from".$sep."\"".$old."\"\n" : "\n";
		}
	}
	
	if ($f_flag == "mod") {
		#
		# find out what changed.
		#
		
		$oldres = db_query("select * from slips where sid=$sid");
		$oldslip = db_fetch_array($oldres) ;
		$chstring = "";
		CheckChanged($a_depend, $oldslip[depend], "Dependency", array("slips", "sid", "problem"), "mixed");
		CheckChanged($a_spid, $oldslip[spid], "Parent slip", array("slips", "sid", "problem"), "mixed");
		# XXX - the next two are untested, however modslip should never change them.
		CheckChanged(Date("$sys_datefmt","$a_o_date"),Date("$sys_datefmt","$oldslip[o_date]"), "Open date", "", "");
		CheckChanged(Date("$sys_datefmt","$a_c_date"),Date("$sys_datefmt","$oldslip[c_date]"), "Closed date", "", "");
		CheckChanged($a_o_tech, $oldslip[o_tech], "Open tech", array("people", "login", "fullname"), "mixed");
		CheckChanged($a_c_tech, $oldslip[c_tech], "Close tech", array("people", "login", "fullname"), "mixed");
		CheckChanged($a_currtech, $oldslip[currtech], "Current tech", array("people", "login", "fullname"), "mixed");
		CheckChanged($a_status, $oldslip[status], "Status", array("status", "flag", "label"), "simple");
		CheckChanged($a_priority, $oldslip[priority], "Priority", "", "");
		CheckChanged($a_grp, $oldslip[grp], "Group", "", "");
		CheckChanged($a_ctag, $oldslip[ctag], "Contact", array("contacts", "tag", "name"), "mixed");
		CheckChanged(stripslashes($a_problem), $oldslip[problem], "Problem", "", "");
		CheckChanged(stripslashes($a_summary), $oldslip[summary], "Summary", "", "");
		CheckChanged($na_sc_date, $oldslip[sc_date], "Scheduled Close Date", "", "");
		CheckChanged($na_so_date, $oldslip[so_date], "Scheduled Open Date", "", "");
		CheckChanged($a_public, $oldslip[is_public], "Public setting", "", "public");
		CheckChanged($a_spolicy, $oldslip[spolicy], "Policy", "", "");
		CheckChanged($a_cadd, $oldslip[caddlabel], "Contact Address", "", "");
		CheckChanged($a_cphone, $oldslip[cphonelabel], "Contact Phone", "", "");
		CheckChanged($a_timespent, $oldslip[timespent], "Time Spent", "", "");
		CheckChanged($a_resource, $oldslip[resource], "Resource", "", "");

		if ("$sys_promptmod") {
			if ("$a_comment") {
				$chstring = "$chstring \n-----\nComment from $curruser:\n$a_comment";
			}
		}

		#
		# Pick a followup number and post the data.
		#

		$highseq = CalcHighSeq($sid) + 1;
		
		if(!empty($chstring)) {
			$chstring	=	addslashes($chstring);
			$flist	= "f_sid,f_date,f_tech,f_text,f_seq,f_public,f_usergen,f_time";
			$now		= time();
			$vlist	=	"$sid,$now,'$curruser','$chstring',$highseq";
			LogIt("DEBUG","postslip.php3","sys_switch_followpub is $sys_switch_followpub");
			switch ($sys_switch_followpub) {
				case 0 : { 
					$vlist="$vlist,0";
					break;;
				}
				case 1 : {
					$vlist="$vlist,1";
					break;;
				}
				case 2 : {
					$vlist="$vlist,$a_public";
					break;;
				}
			}
			$vlist="$vlist,0,NULL";
			LogIt("DEBUG","postslip.php3","vlist ended up being $vlist");
			$result = db_query("insert into followups ($flist) values ($vlist)");
			$result = db_query("update slips set spid=$a_spid, depend=$a_depend, o_tech='$a_o_tech', currtech='$a_currtech', status='$a_status', priority=$a_priority, grp='$a_grp', ctag='$a_ctag', problem='$a_problem', summary='$a_summary', is_public='$a_public', l_activity=$now, l_tech='$curruser', sc_date=$na_sc_date, so_date=$na_so_date, spolicy=$a_spolicy, caddlabel='$a_cadd', cphonelabel='$a_cphone', timespent=$a_timespent, resource='$a_resource' where sid=$sid");
			$lastresult[0] = 0;
			$lastresult[1] = "Slip $sid updated successfully.";
		}
		
		# make sure counters are reset
		RecalcSlip($a_spid);
		RecalcSlip($sid);
		RecalcSlip($oldslip[spid]);
	}

	#
	# Generate mail if someone asks... 
	#

	if (($chstring) || ($f_flag=="add")) {
		$f_process="sendmail";
		include "./genmail.php3";
	}

	# this -should- work for adds or mods...  test! test! test! okay!
	#
	# Delete data within bulk for this row...
	#
		
	
	$sid = ($a_sid) ? $a_sid : $sid ;
	$counter=0;
	$toss = db_query("delete from ebulk where btable='slips' and browref=$sid");
	$counter = InsertExtras ("slips",$sid);

?>

