<?
session_start();
require_once("../../config.php");

$sql=mysql_connect($sugar_config['dbconfig']['db_host_name'],$sugar_config['dbconfig']['db_user_name'],$sugar_config['dbconfig']['db_password']);
mysql_select_db($sugar_config['dbconfig']['db_name']);
mysql_query("set names utf8");
mysql_query("set charaecterset utf8");

if($_POST['search'])
{
	if($_POST['name'])$clause[0]="name like '%".$_POST['name']."%'";
	if($_POST['version'])$clause[1]="version like '%".$_POST['version']."%'";
	if($_POST['name'] && $_POST['version'])$where="where ".$clause[0]." and ".$clause[1];
	if($_POST['name'] && !$_POST['version'])$where="where ".$clause[0];
	if(!$_POST['name'] && $_POST['version'])$where="where ".$clause[1];
	if(!$_POST['name'] && !$_POST['version'])$where="";
	$z="select * from ecmfiles_versions ".$where." order by name asc,version desc";
	$w=mysql_query($z);
	//print $z;
	$tabs=array("ecmcontracts","ecmdocs","ecmcorrespondenceins","ecmcorrespondenceouts","ecminvoiceins","ecminvoiceouts");
	while($r=mysql_fetch_array($w))
	{
		foreach($tabs as $tab)
		{
			$ww=mysql_query("select id,name from ".$tab." where file_id='".$r['file_id']."'");
			$rr=mysql_fetch_array($ww);
			$idn=$rr['id'];
			$namen=$rr['name'];
			$n[]=array(
				'i' => mysql_num_rows($ww),
				'module' => $tab,
				'id' => $idn,
				'name' => $namen,
				);
		}
		rsort($n);
		$list[$i]=array(
			'id' => $r['id'], 
			'name' => $r['name'], 
			'version' => $r['version'], 
			'module' => $n[0]['module'],
			'file_id' => $r['file_id'],
			'module_id' => $n[0]['id'],
			'module_name' => $n[0]['name'],
			);
		$i++;
	} 
}
else
{
	$z="select * from ecmfiles_versions order by name asc,version desc";
	$w=mysql_query($z);
	//print $z;
	$tabs=array("ecmcontracts","ecmdocs","ecmcorrespondenceins","ecmcorrespondenceouts","ecminvoiceins","ecminvoiceouts");
	while($r=mysql_fetch_array($w))
	{
		foreach($tabs as $tab)
		{
			$ww=mysql_query("select id,name from ".$tab." where file_id='".$r['file_id']."'");
			$rr=mysql_fetch_array($ww);
			$idn=$rr['id'];
			$namen=$rr['name'];
			$n[]=array(
				'i' => mysql_num_rows($ww),
				'module' => $tab,
				'id' => $idn,
				'name' => $namen,
				);
		}
		rsort($n);
		$list[$i]=array(
			'id' => $r['id'], 
			'name' => $r['name'], 
			'version' => $r['version'], 
			'module' => $n[0]['module'],
			'file_id' => $r['file_id'],
			'module_id' => $n[0]['id'],
			'module_name' => $n[0]['name'],
			);
		$i++;
	} 
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2" />
<title>Select File</title>
<style type="text/css">@import url("../../themes/Sugar/style.css?s=4.5.0g&c="); </style><link href="../../themes/Sugar/colors.sugar.css?s=4.5.0g&c=" rel="stylesheet" type="text/css" title="sugar" />
</head>

<body>

<table width="100%" cellpadding="0" cellspacing="0" border="0" class="h3Row"><tr><td nowrap><h3>File Search</h3></td><td width='100%'><IMG height='1' width='1' src='../../include/images/blank.gif' alt=''></td>
	  </tr>
</table>

<table cellpadding="0" cellspacing="0" border="0" width="100%" class="tabForm">

<tr>
<td>
<form action="SelectFile.php?target=<?print $_GET['target'];?>" method="post" name="popup_query_form" id="popup_query_form">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="dataLabel" nowrap="nowrap">Name:</td>
<td class="dataField" nowrap="nowrap"><input type="text" size="20" name="name" class="dataField" value="" /></td>
<td class="dataLabel" nowrap="nowrap">Version:</td>
<td class="dataField" nowrap="nowrap"><input name="version" type="text" class="dataField" id="version" value="" size="20" /></td>
</tr>
<tr>
<td valign="top" nowrap="nowrap" class="dataLabel">Module:</td>
<td class="dataField" nowrap="nowrap">
<select name="module[]" size="6" multiple="multiple">
<option value='ecmdocs'>Files</option>
<option value='ecmcontracts'>Contracts</option>
<option value='ecmcorrespondenceins'>Correspondence In</option>
<option value='ecmcorrespondenceouts'>Correspondence Out</option>
<option value='ecminvoiceins'>Invoice In</option>
<option value='ecminvoiceouts'>Invoice Out</option>
</select></td>
<td>

</td>
<td width="20%" align="right">
<input type="submit" name="search" class="button"
	title="Search [Alt+Q]"
	accessKey="Q"
	value="Search" />

</td>
</tr>
</table>
</form>

</td>
</tr>
</table>
<table width="100%" cellpadding="0" cellspacing="0" border="0" class="h3Row"><tr><td nowrap><h3>Files List</h3></td><td colspan='10' width='100%'><IMG height='1' width='1' src='../../include/images/blank.gif' alt=''></td>

</tr><tr>
<td align='left' valign='middle' nowrap style='padding-bottom: 2px;'>&nbsp;</td>
<td width='100%'><IMG height='1' width='1' src='../../include/images/blank.gif' alt=''></td>
	  </tr>
</table>



<table width="100%" border="0" cellspacing="0" cellpadding="0" class="listView">
<tr>
<td COLSPAN="20" align="right">&nbsp;</td>
</tr>
<tr height="20">
<td scope="col" class="listViewThS1" NOWRAP><img src='../../include/images/blank.gif' width="1" height="1" alt="" /></td>
<td scope="col" width="45%" class="listViewThS1"><a href="/sugar45/index.php?module=Accounts&action=Popup&mode=single&create=true&metadata=undefined&Accounts_ACCOUNT_offset=&Accounts_ACCOUNT_ORDER_BY=name" class="listViewThLinkS1">Name&nbsp;<img border='0' src='../../themes/Sugar/images/arrow_down.gif' width='8' height='10' align='absmiddle' alt='Sort'></a></td>
<td scope="col" width="35%" class="listViewThS1"><a href="/sugar45/index.php?module=Accounts&action=Popup&mode=single&create=true&metadata=undefined&Accounts_ACCOUNT_offset=&Accounts_ACCOUNT_ORDER_BY=BILLING_ADDRESS_CITY" class="listViewThLinkS1">Version&nbsp;<img border='0' src='../../themes/Sugar/images/arrow.gif' width='8' height='10' align='absmiddle' alt='Sort'></a></td>
<td scope="col" width="30%" class="listViewThS1"><a href="/sugar45/index.php?module=Accounts&action=Popup&mode=single&create=true&metadata=undefined&Accounts_ACCOUNT_offset=&Accounts_ACCOUNT_ORDER_BY=PHONE_OFFICE" class="listViewThLinkS1">Module:&nbsp;<img border='0' src='../../themes/Sugar/images/arrow.gif' width='8' height='10' align='absmiddle' alt='Sort'></a></td>
<td scope="col" width="30%" class="listViewThS1"><a href="/sugar45/index.php?module=Accounts&action=Popup&mode=single&create=true&metadata=undefined&Accounts_ACCOUNT_offset=&Accounts_ACCOUNT_ORDER_BY=PHONE_OFFICE" class="listViewThLinkS1">Document:&nbsp;<img border='0' src='../../themes/Sugar/images/arrow.gif' width='8' height='10' align='absmiddle' alt='Sort'></a></td>
</tr>
<?
if(count($list)>0)
{
foreach($list as $l)
{
if(count($_REQUEST['module'])>0)
{
foreach($_REQUEST['module'] as $mod)
{
	if(mysql_num_rows(mysql_query("select id from ".$mod." where file_id='".$l['file_id']."'"))>0)
	{
		$numi=1;
		break;
	}
	else $numi=0;
}
}
else $numi=1;
if($numi==1)
{
print '
<tr valign="top" height="20">
<td class="oddListRowS1" bgcolor="#ffffff" valign="top"></td>
<td scope="row" valign="top" class="oddListRowS1" bgcolor="#ffffff"><A href="#" onclick="parent.opener.document.getElementById(\'documentName'.$_GET['target'].'\').value=\''.$l['name'].'\';parent.opener.document.getElementById(\'documentId'.$_GET['target'].'\').value=\''.$l['id'].'\';window.close();" class="listViewTdLinkS1">'.$l['name'].'</A></td>

<td valign="top" class="oddListRowS1" bgcolor="#ffffff">'.$l['version'].'</td>
<td valign="top" class="oddListRowS1" bgcolor="#ffffff">';
if($l['module']=="ecmcontracts")print "Contracts";
if($l['module']=="ecmcorrespondenceins")print "Correspondence In";
if($l['module']=="ecmcorrespondenceouts")print "Correspondence Out";
if($l['module']=="ecminvoiceins")print "Invoice In";
if($l['module']=="ecminvoiceouts")print "Invoice Out";
if($l['module']=="ecmdocs")print "Files";

print '</td>
<td valign="top" class="oddListRowS1" bgcolor="#ffffff"><a href="index.php?module=';

if($l['module']=="ecmcontracts")print "EcmContracts";
if($l['module']=="ecmcorrespondenceins")print "EcmCorrespondenceIns";
if($l['module']=="ecmcorrespondenceouts")print "EcmCorrespondenceOuts";
if($l['module']=="ecminvoiceins")print "EcmInvoiceIns";
if($l['module']=="ecminvoiceouts")print "EcmInvoiceOuts";
if($l['module']=="ecmdocs")print "EcmDocs";
print '&action=DetailView&record='.$l['module_id'].'">'.$l['module_name'].'</a></td>
</tr>';
}
}
}
?>
</table>
</body>
</html>
<?
mysql_close($sql);
?>