<?php
// Link example: index.php?FUNCTION_INIT=COPY_RECORD&ARG1=NR_POZ&ARG1_VAL=813


function COPY_RECORD( $COMMAND ) {
global $thiss,$ARG1,$ARG1_VAL,$result,$ZAP_SQL, $INTERFACE_ADDR, $SQL_USER, $SQL_USER_PASS, $SQL_DATABASE;

if ($_SESSION['DEBUG']) echo "<hr> Executing f. COPY_RECORD($COMMAND) .... $ARG1,$ARG1_VAL,$result <br>";

echo'<br />'."Proba skopiowania rekordu ID = [".$ARG1_VAL."] ...";
$ARG1_VAL = intval($ARG1_VAL);
if ($ARG1_VAL <= 0) {
	echo'<br />'."Wrong ID";
	return;
}

if ($_SESSION[$thiss->DETECT_TABLE_COLUMN]['TYPE']['A_ADM_COMPANY']) {
	$sql = "SELECT * from ".$thiss->DETECT_TABLE_NAME."  where `A_ADM_COMPANY` like '".$_SESSION['ADM_COMPANY']."' and `ID`='" . $ARG1_VAL . "'  ;";
} else {
	$sql = "SELECT * from ".$thiss->DETECT_TABLE_NAME."  where `ID`='" . $ARG1_VAL . "'  ;";
}
//echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">';print_r($sql);echo'</pre>';

$conn=mysql_pconnect($INTERFACE_ADDR,$SQL_USER,$SQL_USER_PASS);
if (!$conn) die("ERROR podczas tworzenia polaczenia do bazy $SQL_DATABASE !\n");
mysql_select_db($SQL_DATABASE) or die("ERROR podczas wyboru bazy R50!");

$result = mysql_query($sql, $conn) or die("Blad zapytania2!");
$num_rows = mysql_num_rows($result);
if ($num_rows < 1) {
	echo'<br />'."Rekord ID = [".$ARG1_VAL."] nie istnieje!";
	return;
}


if ($h = mysql_fetch_row( $result )) {
} else {
	echo'<br />'."Blad zapytania!";
	return;
}

if ($_SESSION['DEBUG']){ echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">TODO rekord: ';print_r($h);echo'</pre>'; }

$_SESSION[$thiss->DETECT_TABLE_COLUMN]['EDITRECORD'] = array();
foreach ($_SESSION[$thiss->DETECT_TABLE_COLUMN]['ID'] as $value) {
	if (array_key_exists($value, $h)) {
		$_SESSION[$thiss->DETECT_TABLE_COLUMN]['EDITRECORD'][ $_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC'][$value] ] = $h[$value];
	}
}

if ($_SESSION['DEBUG']){ echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">TODO SES EDITRECORD: ';print_r($_SESSION[$thiss->DETECT_TABLE_COLUMN]['EDITRECORD']);echo'</pre>'; }

$fields = array();
$values = array();
$_SESSION[$thiss->DETECT_TABLE_COLUMN]['EDITRECORD'] ['A_RECORD_CREATE_DATE'] = date("Y-m-d-H:i");
$_SESSION[$thiss->DETECT_TABLE_COLUMN]['EDITRECORD'] ['A_RECORD_CREATE_AUTHOR'] = $_SESSION['ADM_ACCOUNT'];
foreach ($_SESSION[$thiss->DETECT_TABLE_COLUMN]['EDITRECORD'] as $k => $v) {
	//echo "<br> \n $v  ... i $k   ";
	//if (!strstr($_SESSION[$thiss->DETECT_TABLE_COLUMN]['PERMEDIT'][$k], "C")) continue;
	if ($k == 'ID') continue;// pomin ID

	if ($k == 'T_DEVICE_SERIAL') {
		$v = $v."?";
	} else if ($k == 'S_ADDRESS_STREET') {
		$v = $v."?";
	}

	$fields []= "`".$k."`";
	$values []= "'".$v."'";
}
$fields = implode(", ", $fields);
$values = implode(", ", $values);

$sql = "insert into $thiss->DETECT_TABLE_NAME  ( $fields ) values ( $values )  ";
if ($_SESSION['DEBUG']) echo "ZAP_SQL to $sql  <br>\n";
if ($_SESSION['DEBUG']){ echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">';print_r($sql);echo'</pre>'; }

$result = mysql_query($sql, $conn) or die('<p style="color:red;border:1px solid red;padding:3px;">'."Blad zapytania3!\n<br>".mysql_error($conn).'</p>');

$new_id = mysql_insert_id( $conn );

echo'<br />'.'<b>'." Utworzono nowy rekord ID = [".$new_id."]. ".'</b>';

CREATE_MENUFUNC_INIT_2($thiss->DETECT_EDIT_FUNCTION, 'EDIT', $new_id, '<img src=icon/edit.gif alt=EDYCJA border=0 width=20>'." Edytuj nowy rekord") ;

}
