<?
/*
The Contacts tab provides a list of contacts ( Users with ProMan accounts) for the selected project. 
<P>You can send email to a project member by clicking on their email address in the listing. 
<P>You can send email to the entire project team by clicking on Contacts at the top of the listing. 
 Users in the contact list may update their personal information via the MyProfile tab. 
<p>
<img src=../images/Contacts.jpg>
*/
global $rID;
global $rstring;
global $ffn;
global $ffn2;
global $cact;

$url=$_SERVER[~REQUEST_URI~];
//echo ~url=$url<br>~;
$pos = strpos($url, `&cact`);
if ($pos>0) $url=substr($url,0,$pos-1);

$url=str_replace(`/ProjectManagement/`,``,$url);

$cact=$_REQUEST[`cact`];
//echo ~cact=$cact<br>~;


//$_REQUEST[`clean`]=1;

$mSubj=`Contacts`;

$rID=$_REQUEST[~RolesAvailable~];
if ($rID == ~~)
	$rID=999;

if ($rID==999)
	$rstring=~ ~;
else
	$rstring=~ and RoleId=~ . $rID . ~ ~;

//echo ~RolesAvailable=~ , $rID;

$NOSORT=1;

if ($cact==-10) AllVCF();
elseif ($cact>0) UserVCF($cact);

$sql=~select LastName as Last,~.
		~concat(`<a href=javascript:DoPostBack(`,UserId,`); title=Export.contact.info.for.`,FirstName,`.`,LastName,`>`,FirstName,`</a>`) as First,~. 
		~TelePhone as Phone,~ .
		~concat(`<a href=mailto:`,email,` title=Send.Email.to.`,FirstName,`.`,LastName,`>`,email,`</a>`) as Email ,~ .
                ~Street as Street_or_PO_Box_No, City, Region, PostalCode as Zip,~.
                ~UserName, concat(`<a href=http://`, uUrl, ` target=_BLANK_>`, uUrl,~.
                ~`</a>`) as Url, Unit as Org ~ . 
		~from Users where isDeleted=0 and UserID in ~ .
		~(select UserID from ProjectMembers ~ .
		~where projectid=~ . $_SESSION[~ProjectId~] . ~) ~ .
		$rstring .
		~order by LastName, FirstName~;

//echo ~sql:~ , $sql , ~~;

function UserVCF(&$user)
{
	global $cact;

	$ffn=$_SESSION[`PMROOT`].~/~.$_SESSION[`RootDir`].~/~.`temp.vcf`;
	$ffn2=~../upload/~.$_SESSION[`RootDir`].~/~.`temp.vcf`;
	
	$usernam=ShowQueryResultScalar(~select concat(FirstName,` `,LastName) from Users where UserId=`$user`~);
	$UserId=$_SESSION[`UserId`];
	$useremail=ShowQueryResultScalar(~select Email from Users where UserId=`$UserId`~);


	$tstr=ExportVCF($user);
	$fp = fopen($ffn,`w`);
	fwrite($fp, $tstr);
	fclose($fp);
	
	MailSend($useremail, $useremail, ~Contact info for $usernam~, $tstr, $ffn);
	
	$cact=0;
	$_REQUEST[`cact`]=0;
}

function AllVCF()
{
	global $crlf;
	
	$ffn=$_SESSION[`PMROOT`].~/~.$_SESSION[`RootDir`].~/~.`contacts.vcf`;
	$ffn2=~../upload/~.$_SESSION[`RootDir`].~/~.`contacts.vcf`;
	
	$sql = ~select UserId from Users where isDeleted=0 and UserID in ~ .
		~(select UserID from ProjectMembers ~ .
		~where projectid=~ . $_SESSION[~ProjectId~] . ~) ~ .
		~ order by LastName, FirstName~;

	$str=``;
	$users=ShowRowsAsArray($sql);
	
	foreach ($users as &$user) 
	   {
		$tstr=ExportVCF($user);
		$str=$str.$tstr.$crlf;
//echo ~user=$user<br>~;
	   }
	unset($user);
	
	$fp = fopen($ffn,`w`);
	fwrite($fp, $str);
	fclose($fp);
	
	echo $crlf,`<script language=javascript>
//		alert(~trying to popup~);
		popitup(~`,$ffn2,`~,100,100,100,100);
		DoPostBack(0);
//		alert(~after popup~);
	function popitup(url,t,l,h,w)
	{
	//alert(~footer popitup URL=~+url+~ top=~+t+~ left=~+l+~ height=~+h+~ width=~+w);
	    ps=~top=~ + t + ~,left=~ + l + ~,height=~ + h + ~,width=~ + w;
	    var d = new Date();
		var n = d.getSeconds();
	    wnam=~pop_~+n;
		newwindow=window.open(url,wnam,ps);
//	newwindow.body.background = ~`,$_SESSION[`PM_CONTENT_BG`],`~;
		newwindow.focus();
//	return false;
	}

	      </script>`,$crlf;	
}

function ToAll()
{
	global $rstring;

	$sql = ~select email from Users where isDeleted=0 and UserID in ~ .
		~(select UserID from ProjectMembers ~ .
		~where projectid=~ . $_SESSION[~ProjectId~] . ~) ~ .
		$rstring .
		~order by LastName, FirstName~;

	$res=GetResultset($sql);

    if(!$row = mysql_fetch_assoc($res))
    {
        return false;
    }

	$tComma=~~;
    do
    {
 		$tList= $tList . $tComma . $row[~email~];
 		$tComma=~;~;
    }
    while ($row = mysql_fetch_assoc($res));

	DisConnect();

	return $tList;
}

function loadRoles()
{
	global $rID;

	echo ~<option value=999>All Roles</option>~;

	$sql = ~Select RoleId,RoleName from Roles where isDisabled=0 order by RoleName~;

	$res=GetResultset($sql);

    if(!$row = mysql_fetch_assoc($res))
    {
        return false;
    }

    do
    {
		if ($row[~RoleId~] == $rID)
			echo `<option selected=~yes~ value=~` , $row[~RoleId~] , `~>` ,   $row[~RoleName~], `</option>`;
		else
			echo `<option value=~` , $row[~RoleId~] , `~>` ,  $row[~RoleName~], `</option>`;
    }
    while ($row = mysql_fetch_assoc($res));

	DisConnect();
}
echo `<table class=tdblueS width=100%><tr><td>`;
echo `<table border=0 width=100%><tr><td align=middle>`;

echo `<a href=mailto:`, ToAll(), ` title=~Send email to everyone~><font size=3>Email to Contacts</font></a>`;
echo `</td><td>`;
echo `<a href=~javascript: DoPostBack(-10);~ title=~Export contact info for all users~><font size=3>Export Contacts</font></a></td>`;
echo `<td align=left>`;
echo `<select name=~RolesAvailable~ class=~NormalTextBox~ style=~width:180px;~ onchange=~ReLoadPage();~>`;

loadRoles();

echo `</select>`;
echo `</td>`;
echo `</tr></table>`;
$CELLPADDING=10;
$CELLSPACING=0;
ShowQueryResult($sql);
echo `<br><font size=1>(Only portal account holders appear in the above list)</font>`;
echo `</td></tr></table>`;
echo `<p>`;

echo `</center>`;

echo `
<script language=javascript>
function DoPostBack(userid)
{
//alert(~DoPostBack userid=~+userid);
   url=~`,$url,`&cact=~+userid;
//alert(~DoPostBack url=~+url);
   window.document.Help.action=url;
   window.document.Help.submit(true);
}
</script>
`;

?>