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

	#----------------------------------------------------------------------
	# $Id: sliputils.php3,v 1.57 2000/12/29 05:46:43 shevett Exp $
	#----------------------------------------------------------------------
	

	Function GenWhere() {
		Global $state, $sys_justparents, $prefs;

		#
		# This function generates the necessary 'where' clause when viewing
		# slips.  It takes into acounts system settings and user preferences.
		#

		#if ($sys_justparents) {
		#	$wclause = "where spid=0";
		#}

		if (sizeof($prefs)) {
			while (list($vname,$value) = each($prefs)) {
				if ((substr($vname,0,6) == 'opt_v_') && ($value)) {
					$cname = ereg_replace("opt_v_","","$vname");
					$c = $prefs["opt_r_$cname"];
					$v = $prefs["opt_v_$cname"];
					if ("$c" && "$v") {
						$wstring = ("$wclause") ? "and " : "where ";
						if (($prefs["opt_t_$cname"] == 'C') && (substr($v,0,1) != '(')) {
							$wclause = "$wclause $wstring $cname $c '$v'";
						} else {
							$wclause = "$wclause $wstring $cname $c $v";
						}
					}
				}
			}
		}
		return $wclause;
	}

	#----------------------------------------------------------------------

	Function LoadColInfo () {
		Global $Colheader,$curruser,$cscol,$csflags;

		$hq = db_query("select ".
			"p_column,p_active,p_flags,dcolumn,dheading,p_order,p_subtable ".
			"from propdisp,dictionary ".
			"where ".
			"(dtable=p_dataset or dtable=p_subtable) ".
			"and ".
			"p_column=dcolumn and p_uid='$curruser' and p_active=1 ".
			"order by p_order");
#
#			p_uid,p_dataset,p_column,p_subtable,dtable,dcolumn from propdisp,dictionary where (dtable=p_dataset or dtable=p_subtable) and p_column=dcolumn and p_uid='shevett' and p_active=1 ;    
#		$hq = db_query("select p_column,p_active,p_flags,dcolumn,dheading,p_order,p_subtable ".
#									"from ".
#									"propdisp,dictionary ".
#									"where ".
#									"p_dataset=dtable ".
#									"and (dtable='slips' or dtable='slipver') ".
#									"and p_column=dcolumn ".
#									"and p_uid='$curruser' ".
#									"and p_active=1 ".
#									"order by p_order");
		$Colheader="<td bgcolor=\"#bbbbbb\">&nbsp;</td>";
		if (db_numrows($hq)) {
			while ($hinfo = db_fetch_array($hq)) {
				$stname = ($hinfo[p_subtable]) ? $hinfo[p_subtable] : "";
				$cscol[] = "$hinfo[p_column]";
				$csflags[] = "$hinfo[p_flags]";
				$Colheader="$Colheader <td bgcolor=\"#bbbbbb\"><b><font size=2>$hinfo[dheading]</font></td>\n";
			}
		} else {

			# There are no rows in the preferences table describing how things should
			# be shown.  Here are the defaults.

			$cscol[]="problem";	$csflags[]="";	$Colheader="$Colheader <td bgcolor=\"#bbbbbb\"><b><font size=2>Problem</td>";
			$cscol[]="ctag";		$csflags[]="";	$Colheader="$Colheader <td bgcolor=\"#bbbbbb\"><b><font size=2>Contact</td>";
			$cscol[]="o_date";		$csflags[]="D";	$Colheader="$Colheader <td bgcolor=\"#bbbbbb\"><b><font size=2>OpenDate</td>";
			$cscol[]="o_tech";		$csflags[]="";	$Colheader="$Colheader <td bgcolor=\"#bbbbbb\"><b><font size=2>OpenTech</td>";
			$cscol[]="c_date";		$csflags[]="D";	$Colheader="$Colheader <td bgcolor=\"#bbbbbb\"><b><font size=2>CloseDate</td>";
			$cscol[]="c_tech";		$csflags[]="";	$Colheader="$Colheader <td bgcolor=\"#bbbbbb\"><b><font size=2>CloseTech</td>";

		}
	}

	#----------------------------------------------------------------------

	Function ChooseParent($varname="a_spid",$selval="") {
		Global $f_func, $sys_selall, $currow, $sys_selfont, $sys_groupstatus;

		?> 
		
		<font size=<? echo $sys_selfont?>>
		<i>Child of... </i><br>
		<?
			echo "<select name=\"$varname\">\n";
			echo "<option value=\"0\">None.\n";
		
			# PARENT SID
			# ----------------------------------------------
			# Pull out a summary of other slips in the system
			# but make sure there's a blank one.
			#
			# If we are adding a slip on top of another one
			# make sure we make that the default.
#			if ($currow[spid] or $selval) {
#				if ($currow[spid]) {
#					$cq = db_query("select problem from slips where sid=$currow[spid]");
#					$ptext = db_result($cq,0,0);
#					echo "<option selected value=\"$currow[spid]\"> $currow[spid] - $ptext";
#				} else {
#					$cq = db_query("select problem from slips where sid=$selval");
#					$ptext = db_result($cq,0,0);
#					echo "<option selected value=\"$selval\"> $selval - $ptext";
#				}
			if ($selval) {
				$cq = db_query("select problem from slips where sid=$selval");
				if (db_numrows($cq)) {
					$ptext = db_result($cq,0,0);
					echo "<option selected value=\"$selval\"> $selval - $ptext";
					echo "<option value=0> ---------------------";
				}
			}
		
			if ($sys_groupstatus) {
				$wclause = "where status='$sys_groupstatus'";
			} else {
				$wclause = ($sys_selall) ? "" : "where spid=0" ;
				$wclause .= ($f_func == 'addchild') ? " or sid=$selval" : "" ;
				# we can't be a child of ourself, try not to loop either
				if ($currow[sid] != 0) {
					$wclause .= " and sid<>" . $currow[sid];
					$wclause .= " and spid<>" . $currow[sid];
				}
			}
				
			# Query 
			$q = db_query("select sid,problem from slips $wclause order by slips.sid");
			while ($row=db_fetch_array($q)) {
				$txt = substr($row[problem],0,20);
				if (strlen($row[problem]) > 20) {
					$txt="$txt...";
				}
				$sel="";
				if ( ($row[sid] == $currow[spid]) || ($row[sid] == $selval) ) {
					$sel = "selected";
				}
				echo "<option $sel value=\"$row[sid]\"> $row[sid] - $txt\n";
			}
				
			?>
		
		</select>
		
		<?
	}

	#----------------------------------------------------------------------

	Function ChooseDependent() {
		Global $sys_selall, $currow, $sys_selfont, $sys_groupstatus;
		?>

			<font size=<? echo $sys_selfont?>>
			<i>Dependent on...</i><br>
				<select name="a_depend">
				<option value="0">None.
				<?
			
					#
					# Again, another slip list
					#
				
					# dummy where clause
					$wclause = "where sid<>0";
					if ($currow[sid] != 0) {
						# we can't be a dependent of ourself
						$wclause .= " and sid<>" . $currow[sid];
						# we can't have matching  dependents 
						$wclause .= " and depend<>" . $currow[sid];
					}
					if ($sys_groupstatus) {
						$wclause .= " and status<>'$sys_groupstatus'";
					}
					$wclause .= ($sys_selall) ? "" : " and spid=0" ;
					$q = db_query("select sid,problem from slips $wclause order by slips.sid");
					while ($row=db_fetch_array($q)) {
						$txt = substr($row[problem],0,20);
						if (strlen($row[problem]) > 20) {
							$txt="$txt...";
						}
						if ($row[sid] == $currow[depend]) {
							$sel = "selected";
						} else {
							$sel = "";
						}
						echo "<option value=\"$row[sid]\" $sel> $row[sid] - $txt\n";
					}
				?>
		
				</select>

		<?
	}

	#----------------------------------------------------------------------

	Function ChooseResource() {
		Global $currow, $parentinfo, $prefs, $sys_selfont;

		echo "<font size=$sys_selfont>";
		echo "<i>Resource...</i><br>";
		echo "<select name=\"a_resource\">";
		echo "<option value=\"\">None.";
		$orderclause = "order by r_tag";
		$q = db_query("select r_tag from resources $orderclause");
		while ($row=db_fetch_array($q)) {
			if ( ($row[r_tag] == $parentinfo[resource]) || ($row[r_tag] == $currow[resource]) ) {
				$sel = "selected";
			} else {
				$sel = "";
			}
			echo "<option value=\"$row[r_tag]\" $sel> $row[r_tag]\n";
		}
		echo "</select>";
	}

	#----------------------------------------------------------------------

	Function ChooseContact() {
		Global $currow, $parentinfo, $prefs, $sys_selfont;

		echo "<font size=$sys_selfont>";
		echo "<i>Contact...</i><br>";
		if ($prefs[opt_cwritein]) {
			echo "<input name=\"a_ctag\" size=15 value=\"$currow[ctag]\">";
		} else {
			echo "<select name=\"a_ctag\">";
			echo "<option value=\"\">&nbsp;";
			$orderclause = "order by name";
			$q = db_query("select tag,name from contacts $orderclause");
			while ($row=db_fetch_array($q)) {
				if ( ($row[tag] == $parentinfo[ctag]) || ($row[tag] == $currow[ctag]) ) {
					$sel = "selected";
				} else {
					$sel = "";
				}
				echo "<option value=\"$row[tag]\" $sel> $row[name] ($row[tag])\n";
			}
			echo "</select>";
		}
	}

	#----------------------------------------------------------------------

	Function ChooseCadd() {
		Global $currow, $parentinfo, $sys_selfont;
		?>
			<font size=<? echo $sys_selfont ?>>
			<i>Address...</i><br>
			<select name="a_cadd">
			<option value="">Use default
			<?
				$q = db_query("select label from addresses group by label");
				while ($row=db_fetch_array($q)) {
					if ($row[label] == $currow[caddlabel]) {
						$sel = "selected";
					} else {
						$sel = "";
					}
					echo "<option value=\"$row[label]\" $sel> $row[label]\n";
				}
			?>
			</select>
		<?
	}
	
	#----------------------------------------------------------------------

	Function ChooseCphone() {
		Global $currow, $parentinfo, $sys_selfont;
		?>
			<font size=<? echo $sys_selfont ?>>
			<i>Phone...</i><br>
			<select name="a_cphone">
			<option value="">Use default
			<?
				$q = db_query("select label from phones group by label");
				while ($row=db_fetch_array($q)) {
					if ($row[label] == $currow[cphonelabel]) {
						$sel = "selected";
					} else {
						$sel = "";
					}
					echo "<option value=\"$row[label]\" $sel> $row[label]\n";
				}
			?>
			</select>
		<?
	}

	#----------------------------------------------------------------------

	Function ChooseGroup() {
		Global $currow, $sys_selfont;

		?>
			<font size=<? echo $sys_selfont ?>>
			<i>Group...</i><br>
			<select name="a_grp">
			<?
				$gq=db_query("select g_id from groups where g_assign='1' or g_assign='Y'");
				if (db_numrows($gq) != 0) {
					echo "<option value=\"\" $sel> None.\n";
					while ($ginfo = db_fetch_array($gq)) {
						$sel = ($currow[grp] == $ginfo[g_id]) ? "selected" : "" ;
						echo "<option value=\"$ginfo[g_id]\" $sel> $ginfo[g_id]\n";
					}
				} else {
					echo "<option value=\"\">None available.";
				}
			?>
			</select>
		<?
	}
	
	#----------------------------------------------------------------------

	Function ChoosePolicy() {

		echo "<i>Policy</i><br>\n";
		echo "<select name=\"a_spolicy\">";
		ShowPolicies();
		echo "</select>\n";
	}


	#----------------------------------------------------------------------

	Function ChooseCurrtech($label=false,$varname=false,$defval=false) {
		Global $currow, $parentinfo, $parent, $f_func, $curruser, $sys_selfont;

		echo "<font size=$sys_selfont>";
		$ltext = ($label) ? "<i>$label...</i><br>" : "<i>Assigned to...</i><br>";
		$vtext = ($varname) ? "$varname" : "a_currtech" ;
		echo "$ltext";
		?>
			<select name="<? echo $vtext ?>">
			<option value=""> None.
			<?
				$wclause = "members.element=people.login and members.id='tech'";
				$oclause = "members.element";
				$mq = db_query("select ".
							   		 "members.id,members.element,people.login,people.fullname ".
							   		 "from members,people ".
							   		 "where $wclause ".
							   		 "order by $oclause ");
				if (db_numrows($mq)) {
					if ($f_func == "addslip" || $f_func == "addchild" || $f_func == "addlslip" || $f_func == "addres" ) {
						$seltarget = $curruser;
						if (IsSet($parent)) {
							$seltarget = $parentinfo[currtech];
						}
					}											
					
					while ($mdata = db_fetch_array($mq)) {
						if ($mdata[login] == $currow[currtech]) {
							$seltarget = $currow[currtech];
						}
						if ($mdata[login] == $currow[r_currtech]) {
							$seltarget = $currow[r_currtech];
						}
						$sel = ($mdata[login] == $seltarget) ? "selected" : "";
						echo "<option value=\"$mdata[login]\" $sel> $mdata[login] = $mdata[fullname]\n";
					}
				} else {
					echo "<option value=\"\"> No users in the 'tech' group.";
				}
			?>
			</select>
		<?
	}
		
	#----------------------------------------------------------------------

	Function ChooseStatus($varname="a_status",$selval=false) {
		Global $sys_lstatus, $f_func, $currow, $sys_selfont;
	
		?>
			<font size=<? echo $sys_selfont ?>>
			<i>Status...</i><br>
			<?
				echo "<select name=\"$varname\">";
				$stq = db_query("select flag,label from status");
				while ($row=db_fetch_array($stq)) {
					$sel = "";
					if (($selval) && ($row[flag] == $selval)) {
						$sel = "selected"; 
					} else {
						if ((($row[flag] == $sys_lstatus) && ($f_func != 'modslip')) || ($row[flag] == $currow[status])) {
							$sel = "selected";
						}
					}
					echo "<option value=\"$row[flag]\" $sel>$row[label]\n";
				}
			?>
			</select>
		<?
	}

	#----------------------------------------------------------------------

	Function ChoosePriority($varname="a_priority",$selval=0) {
		Global	$f_func, $currow, $sys_selfont, $sys_lowprio, $sys_highprio, $sys_defprio, $sys_prio_name;

		?>
			<font size=<? echo $sys_selfont?> >
			<i>Priority...</i><br>
			<?
				echo "<select name=\"$varname\">\n";
				for ($pri=$sys_highprio; $pri < $sys_lowprio + 1; $pri++) {
					$tag = "";
					$sel = "";
					if ($sys_prio_name[$pri]) { $tag = " - " . $sys_prio_name[$pri]; }
					$sel = ($pri == $selval) ? "selected" : "";
#					if ( (($pri == $sys_defprio) && (($f_func == 'addslip') || ($f_func == 'addchild'))) || ($currow[priority] == $pri) ) {
#						$sel = "selected";
#					}
					
					echo "<option value=\"$pri\" $sel> $pri $tag\n";
				}
			?>
			</select>
		<?
	}

	#----------------------------------------------------------------------

	Function ChoosePublic() {
		Global $f_func, $currow, $sys_defpublic;

		?>
			<i>Can this slip be viewed via the public 'view all slips' pages?</i><p>
			<?
				if (IsSet($currow[is_public])) {
					$pubcheck =  ($currow[is_public] == "1") ? "checked" : "" ;
					$privcheck = ($currow[is_public] == "0") ? "checked" : "" ;
				} else {
					if ("$sys_defpublic" == "1") {
						$pubcheck = "checked"; 
					} else {
						$privcheck = "checked";
					}
				}
			?>
			<input type="radio" name="a_public" value="1" <? echo "$pubcheck" ?>> Public <br>
			<input type="radio" name="a_public" value="0" <? echo "$privcheck" ?>> Private

		<?
	}

	#----------------------------------------------------------------------

	Function TimeSpent() {
		
		Global $currow;

		?>

			<i>Time Spent...</a><br>
			<input name="a_timespent" maxlength=4 size=5 value="<? echo ($currow[timespent]) ? $currow[timespent] : "0" ?>">

		<?
	}

	#----------------------------------------------------------------------
	Function ShowAttachRow($rowdata) {
		Global $sys_datefmt, $sys_attachdir;

		# Link in the info from the Library

		$lq = db_query("select * from library where li_fname='$rowdata[at_fname]'");
		$linfo = db_fetch_array($lq);
		echo "<tr bgcolor=#bbbbbb valign=top>";
		echo "<td align=center><a href=\"$sys_attachdir/$rowdata[at_fname]\">";
		echo "<img src=\"graphics/generic.gif\" border=0><br>$linfo[li_oname]</a></td>";
		echo "<td><i>Upload information</i><br>";
		echo "U/L on ".date("$sys_datefmt",$rowdata[at_date]);
		echo "<br>";
		echo "by ".$rowdata[at_tech]."</td>";
		echo "<td><i>Library information</i><br>";
		echo "$linfo[li_fsize] bytes</td>";
		echo "</tr>\n";
	}

	#----------------------------------------------------------------------
	Function ShowLibRow($rowdata) {
		Global $sys_datefmt, $sys_attachdir;

		echo "<tr bgcolor=#bbbbbb valign=top>";
                # 
                # 02 Nov 1999  [ROM]  changed from li_oname to li_title to be more informative for the user
                # 02 Nov 1999  [ROM]  Added description lines to display
                # 11 Nov 1999  [ROM]  moved upload info to far right cell, leaving only description in the center cell
		#
		# file download cell
		# FIXME: rename file @ upload to retain filename (and thus web server MIME typing) while avoiding name collision.  Yes, not handled in this function...
		echo "<td align=center><a href=\"attachments/$rowdata[li_fname]\">";
                echo "<img src=\"graphics/generic.gif\" border=0><br>$rowdata[li_title]</a><br>";
                echo "<font size=-1>[$rowdata[li_oname]]</font></td>";
		# description cell
                echo "<td><i>Description</i><br> ".$rowdata[li_descrip]."</td>";
		# upload info cell
                echo "<td><font size=-1><i>Upload information</i><br>";
                echo "U/L on ".date("$sys_datefmt",$rowdata[li_update]);
                echo "<br>";
                echo "by ".$rowdata[li_uptech]."<br>";
                echo "<i>Library information</i><br>";
                echo "$rowdata[li_fsize] bytes</font></td>";
                echo "</tr>\n";
	}


?>
