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

	#--------------------------------------------------------------------
	# $Id: properties.php3,v 1.33 2000/12/14 19:13:58 shevett Exp $
	#--------------------------------------------------------------------
	
	# properties
	
	$wclause="where dtable='$proptable'";
	if ($sys_bugtrack && ($proptable == 'slips')) {
		$wclause = "where dtable='slips' or dtable='slipver'";
	}

	LogIt("DEBUG","properties.php3","Launching properties, f_func is '$f_func' and f_process is '$f_process'");
	if ($f_func=="props") {
		?>
		<tr bgcolor=#202020>
			<td>
				<form method="post" action="nbrowse.php3">
				<input type=hidden name=f_process value="props">
				<input type=hidden name=proptable value="<? echo $proptable ?>"
				<table width=100% bgcolor=#808080 cellpadding=1 cellspacing=3 border=0>
				<tr valign=top>
					<th bgcolor=#A0A0A0>Main <? echo $proptable ?> view</td>
					<th bgcolor=#A0A0A0>General Options</td>
				</tr>
				<tr valign=top>
					<td bgcolor=#A0A0A0>
						<center><font size=2>
						Check the columns you want to appear in the normal <? echo $proptable?>
						view. <br>Enter a sequence	number if desired.</center><br>
						<font size=3>
						<?

							#
							# I swore to myself I'd never do this, but...  There is a difference in how
							# PostGres and MySQL handle outer joins.  Rolf Ebert has supplied the PostGres
							# version of this complex select statement, so I must, sadly, put both in here
							# with an if structure around 'em.  *sigh*
							#

							switch ("$server") {

								case "mysql" : {
									$sq = db_query("select ".
														"dtable,dcolumn,dlabel,p_dataset,p_column,p_order,p_uid,p_active ".
														"from dictionary ".
														"left join propdisp ".
														"on dtable=p_dataset and dcolumn=p_column and p_uid='$curruser' ".
														"$wclause");
									break;;
								}
								case "postgresql" : {
									$sq = db_query("select ".
														"      D.dtable, D.dcolumn, D.dlabel, P.p_dataset, ".
														"      P.p_column, P.p_order, P.p_uid, P.p_active ".
														" from dictionary D, propdisp P ".
														"where D.dtable=P.p_dataset ".
														"  and D.dcolumn=P.p_column ".
														"  and P.p_uid='$curruser'".
														"  and D.dtable='$proptable'");
									break;;
								}
								case "oracle" : {
									$sq = db_query("select ".
										"D.dtable, D.dcolumn, D.dlabel, P.p_dataset, ".
										"P.p_column, P.p_order, P.p_uid, P.p_active ".
										" from dictionary D, propdisp P ".
										"where D.dtable=P.p_dataset(+) ".
										"  and D.dcolumn=P.p_column(+) ".
										"  and P.p_uid(+)='$curruser'".
										"  and D.dtable='$proptable'");
									break;;
								}
							}
	
							# Loop through the structure definitions, populate
							# 'op_columnname' with 'order preference'
							#	'sp_columnname' with 'selected preference' (check to display)
							# eg 'slips' column 'spid' will generate the form vars:
							#		op_spid and sp_spid
							while ($sd = db_fetch_array($sq)) {
								$ostring = ($sd[p_order]==0) ? "" : "$sd[p_order]" ;
								echo "<input type=text name=op_$sd[dcolumn] size=2 value=\"$ostring\">";
								echo "<input type=checkbox name=sp_$sd[dcolumn] ";
								echo $sd[p_active] ? "checked" : "";
								echo ">$sd[dlabel] <br>\n";
							}
						?>
					</td>
					<td bgcolor=#A0A0A0>
						<?
							# Load up the old options.
							$optq = db_query("select * from preferences where pr_uid='$curruser' and pr_table='$proptable'");
							while ($optd = db_fetch_array($optq)) {
								$prefs[$optd[pr_tag]] = $optd[pr_value];
							}
							LogIt("DEBUG","properties.php3","Proptable is $proptable");
							switch ($proptable) {
								case "slips" : {
									echo "<table border=0 cellpadding=0 cellspacing=0>";
									echo "<tr><td>Send mail to Open Tech by default</td>\n";
									$checked = ($prefs[opt_mopen]) ? "checked" : "";
									echo "<td><input type=checkbox name='opt_mopen' $checked ></td></tr> ";
									echo "<tr><td>Send mail to Contact by default</td>\n";
									$checked = ($prefs[opt_mcontact]) ? "checked" : "";
									echo "<td><input type=checkbox name='opt_mcontact' $checked > </td></tr>";
									echo "<tr><td>Send mail to Current Tech by default</td>\n";
									$checked = ($prefs[opt_mtech]) ? "checked" : "";
									echo "<td><input type=checkbox name='opt_mtech' $checked ></td></tr> ";
									echo "<tr><td>Other address to send to </td>\n";
									$checked = ($prefs[opt_mailme]) ? "checked" : "";
									# FIXME: needs maxsize based on database query
									echo "<td><input type=text name='opt_mother' value='$prefs[opt_mother]'></td></tr> ";
									echo "<tr><td>No. of slips per page?</td>\n";
									echo "<td><input type=text name='opt_mxslips' value='$prefs[opt_mxslips]'></td></tr> ";
									echo "</table>";
									$checked = ($prefs[opt_mailme]) ? "checked" : "";
									echo "<input type=checkbox name='opt_mailme' $checked > Mail me if activity happens on my slips?<br>\n";
									$checked = ($prefs[opt_pricols]) ? "checked" : "";
									echo "<input type=checkbox name='opt_pricols' $checked > Color slips by priority?<br>\n";
									$checked = ($prefs[opt_zoomignore]) ? "checked" : "";
									echo "<input type=checkbox name=\"opt_zoomignore\" $checked > Ignore selection criteria when zoomed?<br>\n";
									$checked = ($prefs[opt_filtpick]) ? "checked" : "";
									echo "<input type=checkbox name=\"opt_filtpick\" $checked> Show Filter picker? <br>\n";
									$checked = ($prefs[opt_cwritein]) ? "checked" : "";
									echo "<input type=checkbox name=\"opt_cwritein\" $checked> Use writein field for Contacts when adding? <br>\n";
									if ($sys_groupstatus) {
										$checked = ($prefs[opt_viewgroup]) ? "checked" : "";
										echo "<input type=checkbox name=\"opt_viewgroup\" $checked> View group slips? <br>\n";
									}
									break;;
								}
								case "contacts" : {
									echo "<table border=0 cellpadding=0 cellspacing=0>";
									echo "<tr><td>Contacts per page?</td>\n";
									echo "<td><input type=text name='opt_mxconts' value='$prefs[opt_mxconts]'></td></tr> ";
									$checked = ($prefs[opt_filtpick]) ? "checked" : "";
									echo "<input type=checkbox name=\"opt_filtpick\" $checked> Show Filter picker? <br>\n";
									break;;
								}
								case "resources" : {
									echo "<table border=0 cellpadding=0 cellspacing=0>";
									echo "<tr><td>Resources per page?</td>\n";
									echo "<td><input type=text name='opt_mxress' value='$prefs[opt_mxress]'></td></tr> ";
									$checked = ($prefs[opt_filtpick]) ? "checked" : "";
									echo "<input type=checkbox name=\"opt_filtpick\" $checked> Show Filter picker? <br>\n";
									break;;
								}
							}
						?>
						<hr>
						<table width=100% border=0 cellpadding=0 cellspacing=0>
						<tr>
							<td>
								Sort <? echo $proptable ?> first by...
							</td>
							<td>
								<select name="opt_prisort">
								<option value="">None
								<?
									$cn = db_query("select dcolumn,dlabel from dictionary where dtable='$proptable' order by dlabel");
									while ($cvals = db_fetch_array($cn)) {
										$sel = ($cvals[dcolumn] == $prefs[opt_prisort]) ? "selected " : "";
										echo "<option value=\"$cvals[dcolumn]\" $sel >$cvals[dlabel] \n";
									}
								?>
								</select>
							</td>
							<td>
								<?
									$checked = ($prefs[opt_pridesc]) ? "checked" : "";
									echo "<input type=checkbox name='opt_pridesc' $checked > Descending?";
								?>
							</td>
						</tr>
						<tr>
							<td>
								And then by...
							</td>
							<td>
								<select name="opt_secsort">
								<option value="">None
								<?
									$cn = db_query("select dcolumn,dlabel from dictionary where dtable='$proptable' order by dlabel");
									while ($cvals = db_fetch_array($cn)) {
										$sel = ($cvals[dcolumn] == $prefs[opt_secsort]) ? "selected " : "";
										echo "<option value=\"$cvals[dcolumn]\" $sel>$cvals[dlabel] \n";
									}
								?>
								</select>
							</td>
							<td>
								<?
									$checked = ($prefs[opt_secdesc]) ? "checked" : "";
									echo "<input type=checkbox name='opt_secdesc' $checked > Descending?";
								?>
							</td>
						</tr>
						</table>
						<hr>
						<center><font size=4>Selection Criteria</font></center><p>
						<table width=100% cellpadding=0 cellspacing=0>
						<?
							$selq = db_query("select dcolumn,dlabel,dtype from dictionary where dtable='$proptable'");
							while ($seld = db_fetch_array($selq)) {
								echo "<input type=hidden name=opt_t_$seld[dcolumn] value=$seld[dtype]>\n";
								echo "<tr><td>$seld[dlabel]</td>";
								echo "<td>";
								echo "<select name=\"opt_r_$seld[dcolumn]\">\n";
								echo "<option value=\"\">";
								$vname = "opt_r_$seld[dcolumn]";
								echo "<option value=\"=\" " ;
								echo ($prefs[$vname] == '=') ? "selected" : "" ;
								echo " >Equal to\n";
								echo "<option value=\"<\" " ;
								echo ($prefs[$vname] == '<') ? "selected" : "" ;
								echo " >Less than\n";
								echo "<option value=\">\" " ;
								echo ($prefs[$vname] == '>') ? "selected" : "" ;
								echo " >Greater than\n";
								echo "<option value=\"!=\" " ;
								echo ($prefs[$vname] == '!=') ? "selected" : "" ;
								echo " >Not equal to\n";
								echo "<option value=\"IN\" " ;
								echo ($prefs[$vname] == 'IN') ? "selected" : "" ;
								echo " >IN (val, val, val)\n";
								echo "<option value=\"Not IN\" " ;
								echo ($prefs[$vname] == 'NOT IN') ? "selected" : "" ;
								echo " >Not IN ('val', 'val', 'val')\n";
								echo "</select>";
								echo "</td>\n";
								echo "<td>";
								$vname = "opt_v_$seld[dcolumn]";
								# FIXME: needs maxsize based on database query
								echo "<input name=\"opt_v_$seld[dcolumn]\" value=\"$prefs[$vname]\">";
								echo "</td>";
								echo "</tr>";
							}
						?>
						</table>
					</td>
				</tr>
				<tr bgcolor=#202020><td colspan=2>
				<?
					PostBar("Post Changes");
				?>
			</td>
		</tr>
	<?
	} else {
		if ($f_process == "props") {

			# Handle display properties...

			$pq = db_query("delete from propdisp where p_dataset='$proptable' and p_uid='$curruser'");
			$pq = db_query("select dtable,dcolumn,dflags from dictionary $wclause");
			while ($pd = db_fetch_array($pq)) {
				$varname = "op_$pd[dcolumn]";
				eval("\$oval=\"\$$varname\";");
				$varname = "sp_$pd[dcolumn]";
				eval("\$sval=\"\$$varname\";");
				if (empty($oval)) { 
					$oval=0; 
				} 
				$active = $sval ? "1" : "0";
				$subtable = ($pd[dtable] == $proptable) ? '' : $pd[dtable] ;
				$iq = db_query( "insert into propdisp ".
												"(p_uid,p_flags,p_dataset,p_column,p_order,p_active,p_subtable) ".
												"values ".
												"('$curruser','$pd[dflags]','$proptable','$pd[dcolumn]',$oval,$active,'$subtable')");
			}

			# now handle user preferences.
			$optq = db_query("delete from preferences where pr_uid='$curruser' and pr_table='$proptable'");
			while (list($vname,$value) = each($GLOBALS)) {
				if ((substr($vname,0,4) == 'opt_') && ($value)) {
					$optq = db_query("insert into preferences (pr_uid,pr_tag,pr_value,pr_table) values ('$curruser','$vname','$value','$proptable')");
				}
			}

			# things are updated.  Reload for current pageview.
			unset($prefs);
			$optq = db_query("select * from preferences where pr_uid='$curruser' and pr_table='$proptable'");
			while ($optd = db_fetch_array($optq)) {
				$prefs[$optd[pr_tag]] = $optd[pr_value];
			}
		}
	}
?>
