<?php
include_once "../../../tools/inc.php";
$page = include("../../common.php");
include("../tools/function.string.php");
include("../tools/function.sql.php");
$table_group="mailing_groups".$config["table_prefix"];
$table_cf="custom_fields".$config["table_prefix"];
$access=check_level("Subscribe Form");

$mail_format_list=array(
        0 => "{rs:MAIL_FORMAT_0}",
        1 => "{rs:MAIL_FORMAT_1}",
);

function get_custom_fields_row($name, $cf, $p="page") {
        global $db, $$p, $table_cf, $ui;
        $cf=implode(", ", (is_array($cf) ? $cf : array(0)));
        $sql="select id, title, type, validation, def_value, list_val, not_null from $table_cf where enabled='1' and (id in ($cf) or not_null>0) and owner_id='".$ui["id"]."' order by position";
        $rs=$db->select($sql);
        $res=array();
        for ($i=0; $i<count($rs); $i++) {
                $data=$rs[$i];
                $pp=new Template("templates/subscribe_form/".$data["type"].".tpl");
                if (in_array($data["type"], array("radio", "select"))) {
                        $array=explode("\r\n", $data["list_val"]);
                        for ($j=0; $j<count($array); $j++) {
                                if (trim($array[$j])) {
                                        $pp->addrow("LINE", array(
                                                "id"    => $data["id"],
                                                "value" => html_string($array[$j]),
                                                "active"=> $array[$j]==$data["def_value"],
                                        ));
                                }
                        }
                }
                $pp->assign(array(
                        "id"    => $data["id"],
                        "value" => $data["def_value"],
                ));
                $data["first"]=!$i;
                $data["line"]=$pp->evaluate();
                $data["class"]=($i%2);
                $$p->addrow($name, $data);
                next($custom_fields);
        }
}

function get_custom_fields($name, $cf) {
        global $db, $page, $table_cf, $ui;
        $cf=(is_array($cf) ? $cf : array());
        $sql="select * from $table_cf where enabled='1' and owner_id='".$ui["id"]."' order by position";
        $rs=$db->select($sql);
        for ($i=0; $i<count($rs); $i++) {
                $data=$rs[$i];
                $data["checked"]=((isset($cf[$data["id"]]) or $data["not_null"]) ? " checked" : "");
                $data["class"]=$i%2;
                $page->addrow($name, $data);
        }
}

function get_form($r) {
        global $mail_format_list, $table_group, $_page, $_SERVER, $PATH, $config, $ui, $settings, $groups_list;
        $_page = new Template("templates/subscribe_form/form.tpl");
        $_page->dictionary("common/dict.ru");
        $sql="select id, title from $table_group where id in (".implode(", ", $r["group_id"]).")".(isset($user_permissions['groups_list']) ? " and id in (".$user_permissions['groups_list'].")" : "")." order by position";
        get_row_from_array("RECEIVE_FORMAT_SELECT", $mail_format_list, 0, "_page");
        get_row_from_table("ENCODING_SELECT", "encodes".$config["table_prefix"], $settings["encoding"], "code", "title", "_page");
        get_row_from_sql("GROUPS_SELECT", $sql, 0, "_page");
        get_custom_fields_row("CUSTOM_FIELDS", $r["cf"], "_page");
        $r["subscribe_url"]="http://".$_SERVER['HTTP_HOST'].preg_replace("/\/admin\//", "", $PATH)."/subscribe/";
        $r["owner_id"]=$ui["id"];
	$r["PATH"]=$PATH;
        $_page->assign($r);
        return $_page->evaluate();
}

$message=new Message();
$error=new Message("error");
if (isset($_r)) $message->add($message_list[$_r]);
if (isset($access["view"]) or isset($access["admin"])) {
        $tmp="SUBSCRIBE_FORM_STEP_0";
        if (isset($save)) {
                $groups=$db->get_array($table_group, "id", "title", "where id in (".implode(", ", $r["group_id"]).")".(isset($user_permissions['groups_list']) ? " and id in (".$user_permissions['groups_list'].")" : ""));
                if (!count($groups)) $error->add("{rs:ERROR_MAILING_GROUP}");
		if (!$error->count and !isset($access['edit']) and !isset($access['admin'])) $error->add('{rs:ERROR_ACCESS_RESTRICTED}');
                if (!$error->count) {
                        $tmp="SUBSCRIBE_FORM_RESULT";
                        $r["form"]=get_form($r);
                        $r["code"]=html_string($r["form"]);
                        $r["BACK_TITLE"]="{rs:TITLE_SUBSCRIPTION_FORM}";
                        $r["BACK_LINK"]=$link->get();
			$r["WINDOW_TITLE"]="{rs:TITLE_SUBSCRIPTION_FORM}";
                }
        } else $r=array();
        $page->section($tmp);
        if (!$r["form"]) {
                $sql="select id, title from $table_group".(isset($user_permissions['groups_list']) ? " where id in (".$user_permissions['groups_list'].")" : "")." order by position";
                get_row_from_sql("GROUPS_SELECT", $sql, $r["group_id"]);
                get_custom_fields("CUSTOM_FIELDS_SELECT", $r["cf"]);
                $r["BACK_TITLE"]="{rs:TITLE_MAIN_MENU}";
                $r["BACK_LINK"]=$PATH;
		$r["WINDOW_TITLE"]="{rs:EDIT_SUBSCRIPTION_FORM}";
        }
        $page->assign($r);
} else {
        $page->section("ACCESS_DENIED");
        $error->add("{rs:ERROR_ACCESS_RESTRICTED}");
}
$db->close();
add_to_tree("{rs:TITLE_DESIGN}", "design/");
add_to_tree("{rs:TITLE_SUBSCRIPTION_FORM}", "design/subscribe_form/");
$page->assign(array(
        "TITLE"                 => "{rs:TITLE_SUBSCRIPTION_FORM}",
        "ERROR"                 => ($error->count ? $error->get() : ""),
        "MESSAGE"               => ($message->count ? $message->get() : ""),
        "LINK"                  => $link->get(),
        "is_design"             => 1,
        "IMG"                   => "subscription_form.gif",
	//"WINDOW_TITLE"		=> "{rs:EDIT_SUBSCRIPTION_FORM}",
));
print $page->evaluate();
?>