<?php
include_once "../../../tools/inc.php";
include "../../common.php";
include("../tools/function.string.php");
include("../tools/function.sql.php");

$n=$settings["rec_count"];
$table="subscribers".$config["table_prefix"];
$table_group		= "mailing_groups".$config["table_prefix"];
$table_black_list	= "black_list".$config["table_prefix"];
$table_sg		= "subscribers_group".$config["table_prefix"];
$table_um		= "users_mailing_groups".$config["table_prefix"];
$table_cf		= "custom_fields".$config["table_prefix"];
$table_scf		= "scf".$config["table_prefix"];

$access=check_level("Import");

function update_position($old, $new) {
	global $db, $table_group;
	$sql=array();
	$sql[]="update $table_group set position=position-1 where position>$old";
	$sql[]="update $table_group set position=position+1 where position>=$new";
	$db->execute_all($sql);
}

if (isset($access["view"]) or isset($access["admin"])) {
	if (isset($l)) {
		$ic=$import["ic"];
		$sc=$import["sc"];
		$uc=$import["uc"];
		$ec=$import["ec"];
		$gr=(isset($import["gr"]) ? ($import["gr"] ? explode("-", $import["gr"]) : array()) : array());
		$filename=$import["filename"].str_pad($l, 4, "0", STR_PAD_LEFT);
		include("../tools/class.csv.php");
		$csv=new CSV($import["delimiter"], $import["enclosure"]);
		if ($csv->open("../files/import/$filename")) {
			if ($import["put_header"]) $csv->get_row();
			$fields="date_created, name, mail, receive_format, encoding, status, owner_id, modify_user, modify_time";
			while ($array=$csv->get_row()) {
				$data=array(
					"date_created"	=> time(),
					"name"		=> trim($array[$import["name"]]),
					"mail"		=> trim($array[$import["mail"]]),
					"receive_format"=> ($array[$import["format"]]=="text" ? 1 : 0),
					"encoding"	=> trim(isset($array[$import["encoding"]]) ? $array[$import["encoding"]] : $settings["encoding"]),
					"status"	=> (isset($array[$import["status"]]) ? $array[$import["status"]] : 1),
					"owner_id"	=> $ui["id"],
					"modify_user"	=> $ui["id"],
					"modify_time"	=> time(),
				);
				$custom_fields=array();
				if (is_array($import["cf"])) {
					foreach ($import["cf"] as $key => $val) {
						$custom_fields["$key"] = trim($array[$val]);
						// Сделать проверку данных!!!
						// см. subscriber.save()
					}
				}
				if (check_email($data["mail"])) {
					if (is_val($table, "where mail='".$data["mail"]."'")) {// and owner_id='".$ui["id"]."'")) {
						//$sql=($import["duplicated"] ? get_sql_update($table, $fields, $data, "where mail='".$data["mail"]."' and owner_id='".$ui["id"]."'") : "");
						$sql=($import["duplicated"] ? get_sql_update($table, $fields, $data, "where mail='".$data["mail"]."'") : "");
						if ($import["duplicated"]) $uc++;
						else $sc++;
					} else {
						$sql=get_sql_insert($table, $fields, $data);
						$ic++;
					}
				} else $ec++;
				if ($sql) {
					$db->execute($sql);
					//$s_id=get_val($table, "id", "where mail='".$data["mail"]."' and owner_id='".$ui["id"]."'");
					$s_id=get_val($table, "id", "where mail='".$data["mail"]."'");
					$sql=array();

					// Custom Fields
					$sql[]="delete from $table_scf where  subscriber_id='$s_id'";
					foreach($custom_fields as $key => $val) {
						$sql[]=get_sql_insert($table_scf, "custom_field_id, subscriber_id, value", array("custom_field_id" => $key, "subscriber_id" => $s_id, "value" => $val));
					}

					// Groups
					$group_id=$import["add_groups"];
					$array[$import["group"]]=explode("|", $array[$import["group"]]);
					for ($i=0; $i<count($array[$import["group"]]); $i++) {
						$id=get_val($table_group, "id", "where title like '".$array[$import["group"]][$i]."'");
						if ($id and !in_array($id, $group_id)) $group_id[]=$id;
					}
					$sql[]="delete from $table_sg where subscribers_id='$s_id'";
					reset($group_id);
					$key=key($group_id);
					while (isset($key)) {
						if (!in_array($group_id[$key], $gr)) $gr[]=$group_id[$key];
						$sql[]=get_sql_insert($table_sg, "subscribers_id, group_id", array("subscribers_id" => $s_id, "group_id" => $group_id[$key]));
						next($group_id);
						$key=key($group_id);
					}
					$db->execute_all($sql);
				}
			}
			$csv->close();
			@unlink("../files/import/".$filename);
			$import["ic"]=$ic;
			$import["sc"]=$sc;
			$import["uc"]=$uc;
			$import["ec"]=$ec;
			$import["gr"]=implode("-", $gr);
			$_SESSION['import']=$import;
		}
	}
}
$link->path=$PATH."subscribers/";
$link->add("act", "import");
$link->add("ic");
$link->add("sc");
$link->add("uc");
$link->add("ec");
$link->add("gr", implode("-", $gr));
print $link->get();
$db->close();
exit;
?>