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

	#----------------------------------------------------------------------
	# $Id: genmail.php3,v 1.37 2000/12/14 19:13:48 shevett Exp $
	#----------------------------------------------------------------------
	
	#----------------------------------------------------------------------

	#make sure we have the status codes handy...

	Function LookupSlipInfo($key, $long, $table, $slipvalue) {
		
		$mq = db_query("select $key,$long from $table where $key=$slipvalue");
		$ms = db_fetch_array($mq);

		return "$ms[$key] (" . stripslashes($ms[$long]).")";
	}

	if ($f_process == "sendmail") {
		$mq = db_query("select * from slips where sid=$sid");
		$ms = db_fetch_array($mq);

		$lookuptemp = "";
	
		$subject	= "Re: [$sys_email_cookie$sid] ".stripslashes($ms[problem]);
	
		$body = "---------------------------------------------------------------------------\n";
		$body .= "Slip number -----: " . LookupSlipInfo("sid", "problem", "slips", $ms[sid]) . "\n";
		if ($ms[spid] > 0) {
			$body .= "Child of --------: " . LookupSlipInfo("sid", "problem", "slips", $ms[spid]) . "\n";
		}
		if ($ms[ctag]) {
			$body .= "Contact ---------: " . LookupSlipInfo("tag", "name", "contacts", "'$ms[ctag]'") . "\n";
		} else {
			$body .= "Contact ---------: None\n";
		}
		$body .= "Problem ---------: ".stripslashes($ms[problem])."\n";
		$body .= "Opened by -------: " . LookupSlipInfo("login", "fullname", "people", "'$ms[o_tech]'") . " on " . date("$sys_datefmt",$ms[o_date]) . "\n";
		if ($ms[c_date]) {
			$body .= "Closed by -------: " . LookupSlipInfo("login", "fullname", "people", "'$ms[c_tech]'") . " on " . date("$sys_datefmt",$ms[c_date]) . "\n";
		}
		if ($ms[currtech]) {
			$body .= "Assigned To -----: " . LookupSlipInfo("login", "fullname", "people", "'$ms[currtech]'") . "\n";
		} else {
			$body .= "Assigned To -----: None\n";
		}
		$body	.= "Current Status --: " . LookupSlipInfo("flag", "label", "status", "'$ms[status]'") . "\n";
		$body .= "---------------------------------------------------------------------------\n";
		$body .= "Summary:\n";
		$body .= stripslashes("$ms[summary]\n");
		# Try yer darndest to wrap the text so it makes sense in generated mail.
		# the performance hit here is abysmal.  Removed.
#		$summarytmp = stripslashes($ms[summary]);
#		$count = 10;
#		while (ereg('[^[:space:]]', $summarytmp) && $count) {
#			ereg('^(.{1,76})([[:space:]]+.*|$)', $summarytmp,
#				$summarymatch) ||
#				ereg('^([^[:space:]]+)([[:space:]]+.*|$)',
#				$summarytmp, $summarymatch);
#			$body .= $summarymatch[1] . "\n";
#			$summarytmp = ereg_replace('^[[:space:]]*', "",
#			$summarymatch[2]);
#			$count--;
#		}

		$body .= "---------------------------------------------------------------------------\n";

		if ($m_justlast) {
			LogIt("DEBUG","genmail.php3","Justlast requested ");
			$highseq = CalcHighSeq($sid);
			if ("$sys_mailfollorder" == "desc") {
				$targseq = $highseq;
			} else {
				$targseq = 1;
			}
			$selstring = "and f_seq=$targseq";
			LogIt("DEBUG","genmail.php3","selstring is $selstring");
		}

		$qstring = "select * from followups where f_sid=$sid ";

		if ($m_incpriv) {
			$fq = db_query("$qstring $selstring order by f_seq $sys_mailfollorder");
		} else {
			$fq = db_query("$qstring $selstring and f_public=1 order by f_seq $sys_mailfollorder");
		}
		if (db_numrows($fq)> 0) {
			$body .= "History:\n";
		}
		while ($fr = db_fetch_array($fq)) {
			$body .= date("$sys_datefmt",$fr[f_date]);
			$body .= " by " . $fr[f_tech];
			if ($fr[f_time]) {
				$body .= " (Time Spent: " . $fr[f_time] . ")";
			}
			$body .= ":\n";
			$body .= stripslashes("$fr[f_text]\n");
			$body .= "---------------------------------------------------------------------------\n";
		}
		$body .= "\n\n          Full information on this slip is available at:\n";
		if ($sys_internalmail) {
			$body	.=	"${sys_url}nbrowse.php3?f_expand=1&f_sid=$sid&f_stflag=M&f_slipzoom=$sid" ;
		} else {
			$body	.=	"${sys_url}visitor.php3?sid=$sid&v_func=zoom\n";
		}

		#
		# Okay, message formatted, lets send...
		#
		
		$resmsg = "Sent mail to: ";

		# Check the preferences for the o_tech and the currtech on whether or not they
		# want e-mail. If so, pretend the buttons were pressed. And don't mail yourself.

		# we don't want to mail ourselves, do we?
		if ($ms[o_tech] != $curruser) {
			$o_tech_mail =  db_query("select * from preferences where (pr_uid='$ms[o_tech]' and pr_tag='opt_mailme')");
			$o_tech_mail_array = db_fetch_array($o_tech_mail);
			if (count($o_tech_mail_array)) {
				if ($o_tech_mail_array[pr_value] == "on") {
					$m_open="1";
					LogIt("DEBUG","genmail.php3","o_tech $ms[o_tech] has preference to be emailed, adding to list.");
				}
			}
		}

		if ($ms[currtech] != $curruser) {
			# lets check if they equal each other so we don't get weird dupes.

			if ($ms[o_tech] != $ms[currtech]) {
				$currtech_mail =  db_query("select * from preferences where (pr_uid='$ms[currtech]' and pr_tag='opt_mailme')");
				$currtech_mail_array = db_fetch_array($currtech_mail);

				if (db_numrows($currtech_mail) > 0) {
					if ($currtech_mail_array[pr_value] == "on") {
						$m_currtech="1";
						LogIt("DEBUG","genmail.php3","currtech $ms[currtech] has preference to be emailed, adding to list.");
					}
				}
			}
		}


		if ($m_open) {
			if (eregi("@",$ms[o_tech])) {
				$em = "$ms[o_tech]";
			} else {
				$gt = db_query("select email from people where login='$ms[o_tech]'");
				$em = db_result($gt,0,"email");
			}
			$resmsg  .= "$em ";
			Send_Mail($em,$subject,$body);
		}

		if ($m_contact) {
			$gt = db_query("select email from contacts where tag='$ms[ctag]'");
			if (db_numrows($gt) > 0) {
				$em = db_result($gt,0,"email");
				if (! $em) {
					$em = "None Available";
				} else {
					Send_Mail($em,$subject,$body);
				}
			} else {
				$em = "None Available";
			}
			$resmsg .= "$em ";
		}

		if ($m_currtech) {
			if ($ms[currtech]) {
				$gt = db_query("select email from people where login='$ms[currtech]'");
				$em = db_result($gt,0,"email");
				Send_Mail($em,$subject,$body);
				$resmsg  .= "$em ";
			}
		}

		if ($ms[grp]) {
			$gt = db_query("select g_id,g_email from groups where g_id='$ms[grp]'");
			$em = db_fetch_array($gt);
			if ($em[g_email]) {
				Send_Mail($em[g_email],$subject,$body);
				$resmsg .= "group-$em[g_id] ";
			}
		}

		if ($oldslip[grp] && ($oldslip[grp] != $ms[grp])) {
			$gt = db_query("select g_id,g_email from groups where g_id='$oldslip[grp]'");
			$em = db_fetch_array($gt);
			if ($em[g_email]) {
				Send_Mail($em[g_email],$subject,$body);
				$resmsg .= "group-$em[g_id] ";
			}
		}
		
		if ($m_other) {
			Send_Mail($m_other,$subject,$body);
			$resmsg .= "$m_other ";
		}

		if ($sys_always_mail) {
			Send_Mail($sys_always_mail,$subject,$body);
		}
	
		LogIt("DEBUG","genmail.php3","Resmsg is '$resmsg'");

		if ("$resmsg" <> "Sent mail to: ") {
			$lastresult[0] = 0;
			$lastresult[1] = "$resmsg";
			# Record that this mail was sent out in the followups...
			# (this code was lifted right from 'postfoll.php3')

			$nextseq = CalcHighSeq($sid) + 1;
			$now = time();
			$flist = "f_sid,f_date,f_tech,f_text,f_seq,f_public,f_time";
			$vlist	=	"$sid,$now,'$curruser','$resmsg',$nextseq,0,0";
			$result = db_query("insert into followups ($flist) values ($vlist)");
			$result = db_query("update slips set l_activity=$now, l_tech='$curruser' where sid=$sid");
			RecalcSlip($sid);
		}

	} else {
		if ($f_func == "sendmail") {
			?>
				<tr bgcolor=#202020>
					<td align=center>
					<?
						ShowStatusBar($sid);
					?>
					</td>
				</tr>
				<tr bgcolor=#202020><td align=center>
				<table bgcolor=#808080 cellpadding=5>
				<tr bgcolor=#BBBBBB valign=center>
				<td align=center>
					<b>Select target(s) for Email</b><br>
				</td>
				</tr>
				<tr bgcolor=#808080><td>
				<form method="post" action="nbrowse.php3">
				<input type=hidden name=sid value=<? echo $sid ?>>
				<input type=hidden name=f_process value="sendmail">
				<? MailOptions(); ?>
				</td>
				</tr>
				<tr bgcolor=#000000><td>
				<? 
					PostBar("Send Mail"); 
				?>
				</form>
				</tr></table></td></tr>
			<?
		}
	}
		

?>
	
