<?php
include_once "../../tools/inc.php";
$page = include("../common.php");
include("../tools/function.string.php");
include("../tools/function.sql.php");
$n=$settings["rec_count"];
$table="black_list".$config["table_prefix"];
$access=check_level("Black List");
$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());
$offset_link=new Link($link->get());
$link->add("offset");

$message_list=array(
        1 => "{rs:MSG_BLACK_LIST} {rs:MSG_CREATED}",
        2 => "{rs:MSG_BLACK_LIST} {rs:MSG_UPDATED}",
        3 => "{rs:MSG_BLACK_LIST} {rs:MSG_DELETED}",
);

$message=new Message();
$error=new Message("error");
if (isset($_r)) $message->add($message_list[$_r]);
if (isset($del) and (isset($access["delete"]) or isset($access["admin"]))) {
        $del=(is_array($del) ? implode($del, ", ") : $del);
        $sql="delete from $table where id in ($del)";
        if (!isset($access["admin"])) $sql.=" and owner_id='".$ui["id"]."'";
        $db->execute($sql);
        $link->add("_r", 3);
        $db->close();
        header("Location: ".$link->get());
        exit;
} elseif (isset($edit) and (isset($access["edit"]) or isset($access["view"]) or isset($access["admin"]))) {
        $sql="select * from $table where id='$edit'";
        if (!isset($access["admin"])) $sql.=" and owner_id='".$ui["id"]."'";
        $rs=$db->select($sql);
        $data=$rs[0];
        if (!isset($data["id"])) {
                $edit=0;
                $data["enabled"]=1;
                $r["date_created"]=time();
                $r["owner_id"]=$ui["id"];
        } else {
                $edit=$data["id"];
                $r["date_created"]=$data["date_created"];
                $r["owner_id"]=$data["owner_id"];
        }
        if (isset($save)) {
                $r["title"]=check_string($r["title"], 1);
                $r["rule"]=check_string($r["rule"], 1);
                $r["enabled"]=(isset($r["enabled"]) ? 1 : 0);
                $r["modify_user"]=$ui["id"];
                $r["modify_time"]=time();

                if (!$r["title"]) $error->add("{rs:ERROR_TITLE}");
                if (!$r["rule"]) $error->add("{rs:ERROR_RULE}");
		if (!$error->count and !isset($access['edit']) and !isset($access['admin'])) $error->add('{rs:ERROR_ACCESS_RESTRICTED}');
                if (!$error->count) {
                        $fields="date_created, title, rule, enabled, owner_id, modify_user, modify_time";
                        $sql=($edit ? get_sql_update($table, $fields, $r, "where id='$edit'") : get_sql_insert($table, $fields, $r));
                        $db->execute($sql);
                        $db->close();
                        $link->add("_r", ($edit ? 2 : 1));
                        header("Location: ".$link->get());
                        exit;
                } else {
                        $r["id"]=$edit;
                        $data=html_string($r, true);
                }
        } else $data=html_string($data);
        $data["BACK_LINK"]=($link->get() ? $link->get() : './');
        $data["BACK_TITLE"]="{rs:TITLE_BLACK_LIST}";
	$data["WINDOW_TITLE"]="{rs:EDIT_BLACK_LIST}";
        $page->section("BLACK_LIST_EDIT");
        $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("BLACK_LIST");
        $where=(!isset($access["admin"]) ? "where owner_id='".$ui["id"]."'" : "");
        $sql="select count(*) from $table $where";
        $rs=$db->select($sql);
        $count=$rs[0][0];
        $i=ceil($count/$n)-1;
        $offset=(($offset>$i) ? $i : ($offset<0 ? 0 : $offset));
        $sql="select * from $table $where order by title".($sort_d ? " DESC" : "")." limit ".($offset*$n). ", $n";
        $rs = $db->select($sql);
        for ($i=0; $i<count($rs); $i++) {
                $data=$rs[$i];
                $data["disabled"]=(!$data["enabled"]);
                $page->addrow("LINE", $data);
        }
        $page->assign(array(
                "count"         	=> $count,
                "offset"        	=> get_new_offsets($count, $n, $offset_link->get(), $offset),
                "BACK_LINK"     	=> $PATH."subscribers/",
                "BACK_TITLE"    	=> "{rs:TITLE_SUBSCRIBERS}",
                "sort_by_title" 	=> $sort_link->get().($sort_d ? "./" : "?sort_d=1"),
                "sort_title_img"	=> ($sort_d ? "_asc" : "_desc"),
		"WINDOW_TITLE"		=> "{rs:TITLE_BLACK_LIST}",
        ));
} else {
        $page->section("ACCESS_DENIED");
        $error->add("{rs:ERROR_ACCESS_RESTRICTED}");
}
add_to_tree("{rs:TITLE_SUBSCRIBERS}", "subscribers/");
add_to_tree("{rs:TITLE_BLACK_LIST}", "black_list/");
$db->close();
$page->assign(array(
        "TITLE"                 => "{rs:TITLE_BLACK_LIST}",
        "ERROR"                 => ($error->count ? $error->get() : ""),
        "MESSAGE"               => ($message->count ? $message->get() : ""),
        "LINK"                  => $link->get(),
        "FILTER_LINK"           => $filter_link->get(),
        "is_subscribers"        => 1,
        "IMG"                   => "black_list.gif",
));
print $page->evaluate();
?>