<?php
include_once "../../../tools/inc.php";
$page = include("../../common.php");
include("../tools/function.string.php");
include("../tools/function.sql.php");
$module_name='statistics_campaigns';
$n=$settings["rec_count"];
$table="newsletters".$config["table_prefix"];
$table_sg='subscribers_group'.$config["table_prefix"];
$access=check_level("Statistics");
$sort=(in_array($sort, array('name', 'subscribers', 'date_created', 'modify_time', 'last_sent')) ? $sort : 'name');
$sort_d=(isset($sort_d) ? 1 : 0);

$sort_link=new Link($link->get());
$link->add("sort");
$link->add("sort_d");
$filter_link=new Link($link->get());
$link->add("_search");
$sort_link->add("_search");
$offset_link=new Link($link->get());
$link->add("offset");

$message=new Message();
$error=new Message("error");
if (isset($_r)) $message->add($message_list[$_r]);
if (isset($access["view"]) or isset($access["admin"])) {
        $page->section(strtoupper($module_name));
	if (isset($user_permissions['newsletters_list'])) $where="where n.id in (".$user_permissions['newsletters_list'].")";
        $where=get_search_where("n.name", $_search, $where);

	$sql="select count(*) from $table n $where";
	$count=$db->count($sql, true);
	$i=ceil($count/$n)-1;
        $offset=(($offset>$i) ? $i : ($offset<0 ? 0 : $offset));
 	
	$sql="select
		n.id,
		n.name,
		n.groups,
		n.date_created,
		n.modify_time,
		n.last_sent
	from
		$table n
	$where
	group by n.id
        order by $sort ".($sort_d ? 'DESC' : '')." limit ".($offset*$n). ", $n";
        $rs = $db->execute($sql);
        while ($data=$db->get_row($rs)) {
		$sql="select subscribers_id from $table_sg where
			'{$data['groups']}' like concat(group_id) or 
			'{$data['groups']}' like concat(group_id, ',%') or
			'{$data['groups']}' like concat('%,', group_id) or
			'{$data['groups']}' like concat('%,', group_id, ',%')
		group by subscribers_id";

		$data['name']=shortStr($data['name']);
		$data['subscribers']=number_format($db->count($sql), 0, ',', ' ');
                $data["date_created"]=date($settings["date_format"], $data["date_created"]);
                $data["modify_time"]=date($settings["date_format"], $data["modify_time"]);
                $data["last_sent"]=($data["last_sent"] ? date($settings["date_format"], $data["last_sent"]) : '-');
                $page->addrow("LINE", $data);
        }
	$sort_link=$sort_link->get();
        $page->assign(array(
                "count"                 => $count,
                "offset"                => get_new_offsets($count, $n, $offset_link->get(), $offset),
                "search"                => html_string($_search),
                "BACK_LINK"             => $PATH,
                "BACK_TITLE"            => "{rs:TITLE_MAIN_MENU}",
                "sort_by_name"		=> $sort_link.($sort_link ? '&' : '?')."sort=name".($sort=="name" ? ($sort_d ? "" : "&sort_d=1") : ""),
                //"sort_by_subscribers"	=> $sort_link.($sort_link ? '&' : '?')."sort=subscribers".($sort=="subscribers" ? ($sort_d ? "" : "&sort_d=1") : ""),
                "sort_by_date"          => $sort_link.($sort_link ? '&' : '?')."sort=date_created".($sort=="date_created" ? ($sort_d ? "" : "&sort_d=1") : ""),
                "sort_by_modify"        => $sort_link.($sort_link ? '&' : '?')."sort=modify_time".($sort=="modify_time" ? ($sort_d ? "" : "&sort_d=1") : ""),
                "sort_by_last_sent"     => $sort_link.($sort_link ? '&' : '?')."sort=last_sent".($sort=="last_sent" ? ($sort_d ? "" : "&sort_d=1") : ""),

                "sort_name_img"      	=> ($sort=="name" ? ($sort_d ? "_asc" : "_desc") : ""),
                //"sort_subscribers_img"  => ($sort=="subscribers" ? ($sort_d ? "_asc" : "_desc") : ""),
                "sort_date_img"         => ($sort=="date_created" ? ($sort_d ? "_asc" : "_desc") : ""),
                "sort_modify_img"       => ($sort=="modify_time" ? ($sort_d ? "_asc" : "_desc") : ""),
                "sort_lust_sent_img"    => ($sort=="lust_sent" ? ($sort_d ? "_asc" : "_desc") : ""),
        ));
	$page->section("SHOW_SEARCH");
} else {
        $page->section("ACCESS_DENIED");
        $error->add("{rs:ERROR_ACCESS_RESTRICTED}");
}
add_to_tree("{rs:TITLE_STATISTICS}", "statistics/");
add_to_tree('{rs:'.strtoupper($module_name).'}', "statistics/campaigns/");
$db->close();
$page->assign(array(
        "TITLE"                 => "{rs:STATISTICS_CAMPAIGNS}",
        "ERROR"                 => ($error->count ? $error->get() : ""),
        "MESSAGE"               => ($message->count ? $message->get() : ""),
        "LINK"                  => $link->get(),
        "FILTER_LINK"           => $filter_link->get(),
	"is_statistics"		=> 1,
        "IMG"                   => "statistics.gif",
	"WINDOW_TITLE"		=> "{rs:TITLE_STATISTICS}",
));
print $page->evaluate();
?>