<?php
include_once "../../../tools/inc.php";
$page = include("../../common.php");
include("../tools/function.string.php");
include("../tools/function.sql.php");
$table="pages".$config["table_prefix"];
$access=check_level("System Emails");

$message_list=array(
        1 => "{rs:MSG_SYSTEM_EMAILS} {rs:MSG_UPDATED}",
);

$page_list=array(
        "forgot_password",
        "subscribe_confirm",
        "subscribe_confirm_page_text",
        "subscribe_successful",
        "unsubscribe_confirm",
        "unsubscribe_confirm_page_text",
        "unsubscribe_successful",
);

$message=new Message();
$error=new Message("error");
if (isset($_r)) $message->add($message_list[$_r]);

if (isset($save) and (isset($access["edit"]) or isset($access["admin"]))) {
        $r=check_array($r, implode(", ", $page_list));
        $fields="name, subject, description";
        $sql="delete from $table where name in ('".implode("', '", $page_list)."')";
        $db->execute($sql);
        for ($i=0; $i<count($page_list); $i++) {
                $array=array(
                        "name"          => $page_list[$i],
                        "subject"       => $r[$page_list[$i]]['subject'],
                        "description"   => $r[$page_list[$i]]['description'],
                );
                $sql=get_sql_insert($table, $fields, $array);
                $db->execute($sql);
        }
        $db->close();
        $link->add("_r", "1");
        header("Location: ".$link->get());
        exit;
} elseif (isset($access["view"]) or isset($access["admin"])) {
        $sql="select * from $table where name in ('".implode("', '", $page_list)."')";
        $rs=$db->select($sql);
        for ($i=0; $i<count($rs); $i++) {
                $data[$rs[$i]['name']]=array(
                        'subject'       => $rs[$i]['subject'],
                        'description'   => $rs[$i]['description'],
                );
        }
        $data=html_string($data);
        $page->section("SYSTEM_EMAILS");
        for ($i=0; $i<count($page_list); $i++) {
                $array=array(
                        "title"         => "{rs:EMAIL_".strtoupper($page_list[$i])."}",
                        "name"          => $page_list[$i],
                        "subject"       => $data[$page_list[$i]]['subject'],
                        "description"   => $data[$page_list[$i]]['description'],
                        "show_subject"  => !in_array($page_list[$i], array("subscribe_confirm_page_text", "unsubscribe_confirm_page_text")),
                );
                $page->addrow("LINE", $array);
        }
} else {
        $page->section("ACCESS_DENIED");
        $error->add("{rs:ERROR_ACCESS_RESTRICTED}");
}
add_to_tree("{rs:TITLE_DESIGN}", "design/");
add_to_tree("{rs:TITLE_SYSTEM_EMAILS}", "design/system_emails/");
$db->close();
$page->assign(array(
        "TITLE"                 => "{rs:TITLE_SYSTEM_EMAILS}",
        "ERROR"                 => ($error->count ? $error->get() : ""),
        "MESSAGE"               => ($message->count ? $message->get() : ""),
        "LINK"                  => $link->get(),
        "is_design"             => 1,
        "IMG"                   => "system_emails.gif",
        "BACK_LINK"             => $PATH,
        "BACK_TITLE"            => "{rs:TITLE_MAIN_MENU}",
	"WINDOW_TITLE"		=> "{rs:EDIT_SYSTEM_EMAILS}",
));
print $page->evaluate();
?>