<?php
include("../../../tools/inc.php");
$page = include("../../common.php");
include("../tools/function.string.php");
include("../tools/function.sql.php");
$n=$settings["rec_count"];
$table="msg_newsletters".$config["table_prefix"];
$table_n="newsletters".$config["table_prefix"];
$table_s="subscribers".$config["table_prefix"];
$table_un="users_newsletter".$config["table_prefix"];
$status=-1;
$access=check_level("Newsletters");
$sort=(in_array($sort, array("id", "last_update", "mail_to", "name")) ? $sort : "last_update");
$sort_d=(isset($sort_d) ? $sort_d : ($sort=="last_update" ? 1 : 0));

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

$message_list=array(
        1 => "{rs:MSG_NEWSLETTER} {rs:MSG_SENT}",
        2 => "{rs:MSG_NEWSLETTERS} {rs:MSG_DELETED}",
);

function get_sql($fields="*", $where="", $limit=0, $order="") {
        global $status, $table, $table_s, $table_t, $table_n, $_search, $offset, $n_id;
        $sql="select {fields} from $table a
        LEFT JOIN $table_s s ON s.id=a.subscriber_id
        LEFT JOIN $table_n n ON n.id=a.newsletter_id
        ".get_search_where("n.name, s.mail", $_search, "where a.status='$status'".($n_id ? " and a.newsletter_id='$n_id'" : "").($where ? " and $where" : ""))."
        ".$order.($limit ? " limit ".($offset*$limit).", $limit" : "");
        return str_replace("{fields}", $fields, $sql);
}

$message=new Message();
$error=new Message("error");
if (isset($_r)) $message->add($message_list[$_r]);
if (isset($act) and $act=="del" and (isset($access["delete"]) or isset($access["admin"]))) {
        $id=(is_array($id) ? implode($id, ", ") : $id);
        $sql="update $table set status=status-3 where id in ($id) and status='$status'";
        $db->execute($sql);
        $link->add("_r", "2");
        $db->close();
        header("Location: ".$link->get());
        exit;
} elseif (isset($act) and $act=="send" and (isset($access["edit"]) or isset($access["admin"]))) {
        $id=(is_array($id) ? implode($id, ", ") : $id);
        $sql="update $table set status=0 where id in ($id) and status='$status'";
        $db->execute($sql);
        $link->add("_r", "1");
        $db->close();
        header("Location: ".$link->get());
        exit;
} elseif (isset($edit) and (isset($access["edit"]) or isset($access['view']) or isset($access["admin"]))) {
        $_search="";
        $sql=get_sql("a.*, n.name, s.mail as mail_to", "a.id='$edit'");
        $rs=$db->select($sql);
        $data=$rs[0];
        $data["date_created"]=date($settings["date_format"]." [H:i:s]", $data["date_created"]);
        $data["date_start"]=date($settings["date_format"]." [H:i:s]", $data["date_start"]);
        $data["last_update"]=date($settings["date_format"]." [H:i:s]", $data["last_update"]);
        $data["log"]=nl2br($data["log"]);
        $page->section("NEWSLETTERS_MESSAGES_EDIT");
        $data["BACK_LINK"]=($link->get() ? $link->get() : './');
        $data["BACK_TITLE"]="{rs:TITLE_UNDELIVERED}";
	$data["WINDOW_TITLE"]="{rs:EDIT_NEWSLETTERS_UNDELIVERED}";
        $page->assign($data);
} elseif (isset($access["view"]) or isset($access["admin"])) {
        if (isset($edit) or isset($del) or isset($permission)) $error->add("{rs:ERROR_ACCESS_RESTRICTED}");
        $page->section("NEWSLETTERS_MESSAGES");
	$where=(isset($user_permissions['newsletters_list']) ? "a.newsletter_id in (".$user_permissions['newsletters_list'].")" : '');
        $sql=get_sql("count(*)", $where);
        $rs=$db->select($sql);
        $count=$rs[0][0];
        $i=ceil($count/$n)-1;
        $offset=(($offset>$i) ? $i : ($offset<0 ? 0 : $offset));
        $sql=get_sql("a.id, a.last_update, n.name, s.mail as mail_to", $where, $n, "order by $sort ".($sort_d ? "DESC" : ""));
        $rs = $db->select($sql);
        for ($i=0; $i<count($rs); $i++) {
                $data=$rs[$i];
                $data["last_update"]=date($settings["date_format"]." [H:i]", $data["last_update"]);
		$data['name']=shortStr($data['name']);
                $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."newsletters/",
                "BACK_TITLE"            => "{rs:TITLE_NEWSLETTERS}",
                "sort_by_id"            => $sort_link.($sort_link ? '&' : '?')."sort=id".($sort=="id" ? ($sort_d ? "" : "&sort_d=1") : ""),
                "sort_by_date"          => $sort_link.($sort_link ? '&' : '?')."sort=last_update".($sort=="last_update" ? ($sort_d ? "&sort_d=0" : "&sort_d=1") : ""),
                "sort_by_mail"          => $sort_link.($sort_link ? '&' : '?')."sort=mail_to".($sort=="mail_to" ? ($sort_d ? "" : "&sort_d=1") : ""),
                "sort_by_name"       	=> $sort_link.($sort_link ? '&' : '?')."sort=name".($sort=="name" ? ($sort_d ? "" : "&sort_d=1") : ""),
                "sort_id_img"           => ($sort=="id" ? ($sort_d ? "_asc" : "_desc") : ""),
                "sort_date_img"         => ($sort=="last_update" ? ($sort_d ? "_asc" : "_desc") : ""),
                "sort_mail_img"         => ($sort=="mail_to" ? ($sort_d ? "_asc" : "_desc") : ""),
                "sort_name_img"      	=> ($sort=="name" ? ($sort_d ? "_asc" : "_desc") : ""),
		"WINDOW_TITLE"		=> "{rs:TITLE_NEWSLETTERS} {rs:TITLE_UNDELIVERED}",
        ));
        $sql="select id, name as title from $table_n".(isset($user_permissions['newsletters_list']) ? " where id in (".$user_permissions['newsletters_list'].")" : "")." order by title";
        get_row_from_sql("NEWSLETTERS_SELECT", $sql, $n_id);
	$page->section("SHOW_SEARCH");
} else {
        $page->section("ACCESS_DENIED");
        $error->add("{rs:ERROR_ACCESS_RESTRICTED}");
}
add_to_tree("{rs:TITLE_NEWSLETTERS}", "newsletters/");
add_to_tree("{rs:TITLE_UNDELIVERED}", "newsletters/undelivered/");
$db->close();
$page->assign(array(
        "TITLE"                 => "{rs:TITLE_UNDELIVERED}",
        "ERROR"                 => ($error->count ? $error->get() : ""),
        "MESSAGE"               => ($message->count ? $message->get() : ""),
        "LINK"                  => $link->get(),
        "FILTER_LINK"           => $filter_link->get(),
        "is_newsletters"        => 1,
        "IMG"                   => "newsletters_undelivered.gif",
));
print $page->evaluate();
?>