<h2>Raport Niedopasowanych Emaili</h2>
<table width="100%">
<tr><td width="33%">Tytuł</td><td width="14%">Nadawca</td><td width="22%">Odbiorca</td><td width="10%">Typ</td><td width="10%">Przypisane do</td><td width="10%">Data</td></tr>
<?php
mb_internal_encoding("UTF-8");
include_once("../../config.php");
function get_emails ($str)
{
$emails = array();
preg_match_all("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $str, $output);
foreach($output[0] as $email) array_push ($emails, strtolower($email));
if (count ($emails) >= 1) return $emails;
else return false;
}
global $sugar_config;
$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 utf-8');

// dopasuj nowy kontakty do pojedynczych odbiorców
$r=mysql_query("select emails.id,rel.address_type,txt.user_id,txt.to_addrs,txt.from_addr,emails.type,emails.name,us.first_name,us.last_name,emails.date_sent from emails as emails
inner join emails_email_addr_rel as rel on emails.id=rel.email_id
inner join users as us on us.id=emails.created_by
inner join emails_text as txt on txt.email_id=emails.id
where txt.gid is not null group by rel.email_id having count(rel.email_id)<2;");
//echo "Znaleziono: ".mysql_num_rows($r)." nie dopasowanych emaili<br>";

while($dane=mysql_fetch_array($r)){
	if($dane['type']=='out'){
		$typ='Wychodząca';
	} else {
		$typ='Przychodząca';
	}
	$emails = get_emails ($dane['from_addr']);
	$emails = get_emails ($dane['to_addrs']);
	echo "<tr><td><a href='index.php?action=DetailView&module=Emails&record=".$dane['id']."'>".$dane['name']."</a></td><td>".$emails[0]."</td><td>".implode(",", $emails)."</td><td>".$typ."</td><td><a href='index.php?module=Users&action=DetailView&record=".$dane['user_id']."'>".$dane['first_name']." ".$dane['last_name']."</a></td><td>".$dane['date_sent']."</td></tr>";
}
// dopasuj nowe kontakty do wiadomosci z instniejacymi juz odbiorcami

$q=mysql_query("select emails.tid,emails.id,rel.email_id,rel.address_type,txt.user_id,txt.to_addrs,txt.from_addr,emails.type,emails.name,us.first_name,us.last_name,emails.date_sent from emails as emails
inner join emails_email_addr_rel as rel on emails.id=rel.email_id
inner join emails_text as txt on txt.email_id=emails.id
inner join users as us on us.id=emails.created_by
where emails.tid is not null and rel.address_type='to' group by rel.email_id having count(rel.email_id)<emails.tid;");
while($dane=mysql_fetch_array($q)){
$emails = get_emails ($dane['from_addr']);
	$emails = get_emails ($dane['to_addrs']);
	echo "<tr><td><a href='index.php?action=DetailView&module=Emails&record=".$dane['id']."'>".$dane['name']."</a></td><td>".$emails[0]."</td><td>".implode(",", $emails)."</td><td>".$typ."</td><td><a href='index.php?module=Users&action=DetailView&record=".$dane['user_id']."'>".$dane['first_name']." ".$dane['last_name']."</a></td><td>".$dane['date_sent']."</td></tr>";
}
//var_dump($mail->getAttachments());


?>
</table>