<?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="templates".$config["table_prefix"];
$table_c="custom_fields".$config["table_prefix"];
$access=check_level("Templates");
$sort_d=(isset($sort_d) ? 1 : 0);

$sort_link=new Link($link->get());
$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_list=array(
	1 => "{rs:MSG_TEMPLATE} {rs:MSG_CREATED}",
	2 => "{rs:MSG_TEMPLATE} {rs:MSG_UPDATED}",
	3 => "{rs:MSG_TEMPLATES} {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;
		$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["text_content"]=check_string($r["text_content"], 1);
		$r["html_header"]=check_string($r["html_header"]);
		$r["html_css"]=check_string($r["html_css"]);
		$r["html_content"]=check_string(preg_replace("/<P([^>]*?>.*?)<\/P>/", "<DIV\\1</DIV>", $r["html_content"]));
		$r["modify_user"]=$ui["id"];
		$r["modify_time"]=time();

		if (!$r["title"]) $error->add("{rs:ERROR_TITLE}");
		if (!$error->count and !isset($access['edit']) and !isset($access['admin'])) $error->add('{rs:ERROR_ACCESS_RESTRICTED}');
		if (!$error->count) {
			$src=array('/&amp;/i', '/(<a[^>]+href=\\\")([^\\\]*)(%unsubscribe%\\\"[^>]*>)/i');
			$rep=array('&', '$1$3');
			$r["html_content"]=preg_replace($src, $rep, $r["html_content"]);
			$fields="date_created, title, text_content, html_header, html_css, html_content, 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;
			$r["html_content"]=stripslashes($r["html_content"]);
			$data=html_string($r, true);
		}
        } else $data=html_string($data, false);
	$page->section("TEMPLATES_EDIT");
	include("../tools/var.tmp.php");
	get_row_from_array("VAR_LIST", $var_tmp_list);
	$sql="select id, title from $table_c where enabled=1 and owner_id='".$r["owner_id"]."'";
	get_row_from_sql("CUSTOM_FIELDS_VAR", $sql);
	$data["BACK_LINK"]=($link->get() ? $link->get() : './');
	$data["BACK_TITLE"]="{rs:TITLE_TEMPLATES}";
	$data["WINDOW_TITLE"]="{rs:EDIT_TEMPLATES}";
        $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("TEMPLATES");
	$where=get_search_where("title", $_search);
	if (!isset($access["admin"])) $where.=($where ? " and " : "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];
                $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_title"	=> $sort_link.($sort_d ? ($sort_link ? '' : './') : ($sort_link ? '&' : '?')."sort_d=1"),
		"sort_title_img"=> ($sort_d ? "_asc" : "_desc"),
		"WINDOW_TITLE"	=> "{rs:TITLE_TEMPLATES}",
        ));
	$page->section("SHOW_SEARCH");

} else {
	$page->section("ACCESS_DENIED");
	$error->add("{rs:ERROR_ACCESS_RESTRICTED}");
}
add_to_tree("{rs:TITLE_TEMPLATES}", "template/");
$db->close();
$page->assign(array(
	"TITLE"			=> "{rs:TITLE_TEMPLATES}",
	"ERROR"			=> ($error->count ? $error->get() : ""),
	"MESSAGE"		=> ($message->count ? $message->get() : ""),
	"LINK"			=> $link->get(),
	"FILTER_LINK"		=> $filter_link->get(),
	"is_templates"		=> 1,
	"IMG"			=> "templates.gif",
));
print $page->evaluate();
?>