<?php
include_once "../../../../tools/inc.php";
include("../../../common.php");
$module_name='campaigns';
$sub_module='unsubscribe_stats';
$table_u='stat_unsubscribes'.$config["table_prefix"];
$table_s='subscribers'.$config["table_prefix"];
$n_id=intval($n_id);
$sort_d=(isset($sort_d) ? $sort_d : 0);
$sort=(in_array($sort, array('mail', 'name', 'date_created', 'ip', 'browser', 'os')) ? $sort : "date_created");
$n=$settings["rec_count"];
$link->add('n_id');
$link->add('log_type');
$offset_link=new Link($link->get());
$offset_link->add("sort");
$offset_link->add("sort_d");

$page = new Template("templates/statistics/{$module_name}/{$sub_module}_log.tpl");
$page->dictionary("common/dict.ru");

$where="where u.newsletter_id={$n_id} and u.status='{$log_type}'";
$sql="select count(*) from $table_u u $where";
$count=$db->count($sql, true);
$i=ceil($count/$n)-1;
$offset=(($offset>$i) ? $i : ($offset<0 ? 0 : $offset));
$sql="select s.id as s_id, s.mail, s.name, u.* from {$table_u} u
left join {$table_s} s on s.id=u.subscriber_id
{$where}
order by {$sort} ".($sort_d ? " DESC" : "")."
limit ".($offset*$n). ", $n";
$rs = $db->execute($sql);
while ($row=$db->get_row($rs)) {
	$row['date_created']=date($settings["date_format"]." [H:i]", $row["date_created"]);
	$page->addrow("LOG_LINE", $row);
}


$page->assign(array(
	'PATH'			=> $PATH,
	'n_id'			=> $n_id,
	'offset'		=> get_new_offsets($count, $n, 'logData.php'.$offset_link->get(), $offset),

	'sort_mail_img'		=> ($sort=="mail" ? ($sort_d ? "_asc" : "_desc") : ""),
	'sort_name_img'		=> ($sort=="name" ? ($sort_d ? "_asc" : "_desc") : ""),
	'sort_date_created_img'	=> ($sort=="date_created" ? ($sort_d ? "_asc" : "_desc") : ""),
	'sort_ip_img'		=> ($sort=="ip" ? ($sort_d ? "_asc" : "_desc") : ""),
	'sort_browser_img'	=> ($sort=="browser" ? ($sort_d ? "_asc" : "_desc") : ""),
	'sort_os_img'		=> ($sort=="os" ? ($sort_d ? "_asc" : "_desc") : ""),
	'log_count'		=> $count,
));
$db->close();
print $page->evaluate();
exit;
?>