<?php
// Cfg / function for inet zasuvky CSV
// (UTF 8) Php/mysql
@ini_set('error_reporting', E_ALL);
@ini_set("display_errors", "on"); 
error_reporting(E_ALL);

session_start();
include 'class_pdo_mini.php';
$CFG_SQL = array(
//	'debug' => true,
	'debug' => false,
	'host'  => 'localhost',
	'user'  => 'root',
	'psw'   => '',
	'db'    => 'test',
	'prefix'=> 'zas2_'
	);
$SQL = new classSql($CFG_SQL);


class classRequest
{
public function isSetX($arr,$key,$def=null)
	{
	return $arr && isset($arr[$key]) ? $arr[$key] : $def;
	}

public function sessionPostGet($key,$def='')
	{
	return $this->isSetX($_SESSION,$key,$this->isSetX($_POST,$key,$this->isSetX($_GET,$key,$def)));
	}

public function postGet($key,$def='')
	{
	return $this->isSetX($_POST,$key,$this->isSetX($_GET,$key,$def));
	}

public function get($key,$def='')
	{
	return $this->isSetX($_GET,$key,$def);
	}
}


// volim 22 cols, id, status, data
//'rack;za��zen�;z�suvka;vede do za��zen�;...a z�suvky;kabel;pozn.;m�stnost;infrastruktura;co;VLAN;Port security;MAC;IP;OS;skupina;name;�stav;patro;pozn. 2';
//"'1';'sw120 - Cisco Catalyst 4506';'Te1/1';'sw100 - c6513';'Te13/1';'sm';'10GE, arma, uplink';'';'o';'switch-uplink';'trunk';'';'';'';'';'';'';'';'';'"

$data = array(
'',
'',
'1',
'sw120 - Cisco Catalyst 4506',
'Te1/1',
'sw100 - c6513',
'Te13/1',
'sm',
'10GE, arma, uplink',
'',
'o',
'switch-uplink',
'trunk',
'',
'',
'',
'',
'',
'',
'',
'',
''
);

class classTableApi
{
var $sql;
var $keys;
function __construct($sql)
	{
	$this->sql = $sql;
	$keys = array();
	$keys[0] = explode(';','id_zas;status;rack;za��zen�;z�suvka;vede do za��zen�;...a z�suvky;kabel;pozn.;m�stnost;infrastruktura;co;VLAN;Port security;MAC;IP;OS;skupina;name;�stav;patro;pozn. 2');
	$keys[1] = array();
	$keys[2] = explode(';','id_rev;rev_user_id;rev_user_name;rev_date');
	$this->keys = $keys;
	}
function encode($arr)
	{
	return json_encode($arr);
	}
function decode($str)
	{
	return json_decode($str);
	}
function keysAdd($type,$arr)
	{
	$arr2 = array();
	$keys = $this->keys[$type];
	$i = 0;
	foreach ($keys as $key)
		{
		if (!($i<count($arr))) {break;}
		$arr2[$key] = $arr[$i];
		$i++;
		}
	$keys = $this->keys[0];
	foreach ($keys as $key)
		{
		if (!($i<count($arr))) {break;}
		$arr2[$key] = $arr[$i];
		$i++;
		}
	return $arr2;
	}
function keysRemove($type,$arr)
	{
	$arr2 = array();
	$keys = $this->keys[$type];
	$i = 0;
	foreach ($keys as $key)
		{
		if (!($i<count($arr))) {break;}
		$arr2[$i] = $arr[$key];
		$i++;
		}
	$keys = $this->keys[0];
	foreach ($keys as $key)
		{
		if (!($i<count($arr))) {break;}
		$arr2[$i] = $arr[$key];
		$i++;
		}
	return $arr2;
	}
function replace($str)
	{
	//$this->sql->insert
	}
function select($str)
	{
	//return json_decode($str);
	}
}


// -----------------------------------------------


function dbValueUpdateCount(&$db_values,$i)
{
$db_values[$i][3]++;
}

function dbValueFind(&$db_values,$str='',&$values_cnt,$i)
{
$id  = false;
foreach ($db_values as $key=>$value)
	{
	if ($value[0] == $str) {
		dbValueUpdateCount($db_values,$key);
		$id = $key;
		break;
		}
//	if ($value === $str) {return $key;}
	}
if ($id===false)
	{
	$id = dbValueAdd($db_values,$values_cnt,array($str,$i));
	}
return $id;
}

function dbValueAdd(&$db_values,&$n,$data)
{
$data[] = $n;
$data[] = 0;
$db_values[$n] = $data;
//$db_values[$n] = $str;
++$n;
return $n - 1;
}


/*
$file = 'zasuvky_bn13.csv';


$db_keys   = array();
$db_values = array();
$db_map    = array();
$db_filters = array();
$values_cnt = 0;

//for ($i=0; $i<20; $i++)
//{
//$db_data[]  = array($i,'');
//}
//dbValueAdd($db_values,$values_cnt,0,'');
dbValueAdd($db_values,$values_cnt,array('',0,0));

$csv = array(
'sep_row'    => "\n",
'sep_col'    => ';',
'enclosure'  => '"',
'escape'     => '"',
'row_length' => 1000
);
$row_length = 1000;	//chars with csv escape
if (($handle = fopen($file, "r")) !== FALSE)
	{
	$row  = array();
	$bool = false;
	$j = 0;
	while (($row = fgetcsv($handle, $csv['row_length'], $csv['sep_col'], $csv['enclosure'], $csv['escape'])) !== FALSE)	
		{
//var_dump($row);
//exit;
//if (count($db_map)>5) break;

		if ($bool)
			{
			$row_table = array();
			$value = $j.'';
			$id    = dbValueFind($db_values,$value,$values_cnt,0);
//			if ($id===false)
//				{
//				$id = dbValueAdd($db_values,$values_cnt,array($value,$i));
//				}
			$row_table[] = $id;	//row id
		        for ($i=0; $i<count($row); ++$i)
				{
				$value = trim($row[$i]);
				if ($i>0) {$value = substr(sha1($value),0,strlen($value));}	//hash str
				$id    = dbValueFind($db_values,$value,$values_cnt,$i+1);
//				if ($id===false)
//					{
//					$id = dbValueAdd($db_values,$values_cnt,array($value,$i));
//					}
				$row_table[] = $id;
				}
			if (count($row_table)>1)
				{
				$db_map[] = $row_table;
				}
			++$j;//row id
			}
		else	{
			array_unshift($row,'id');	//row id
			$db_keys = $row;
			$bool = true;
			}
		}
	fclose($handle);
	}

//usort($db_values,'sortCmpValue');
// sort by words count + remap
//$db_values[0][3]--; // remove one '';
//var_dump($db_values);

usort($db_values,'sortCmpCount');
$cnv = array();
foreach ($db_values as $key => $value)
	{
	$cnv[$value[2]] = $key;
//	$cnv[] = array($value[2],$key);
	}
//var_dump($cnv);

foreach ($db_map as $key => $value)	//remap
	{
	foreach ($value as $key2 => $value2)
		{
		//if (isset($cnv[$value2]) )
		//	{
//			$v = $value2;
		$v = $cnv[$value2];
		$db_map[$key][$key2] = $v;
		if (!isset($db_filters[$key2]))
			{
			$db_filters[$key2] = array();
			}
		//var_dump($db_filters[$key2]);
		if (!in_array($v,$db_filters[$key2]))
			{
			$db_filters[$key2][] = $v;
			}
		//	}
		//else {echo $value2.'<br>';}
		}
	}

//var_dump($db_keys);
//var_dump($db_values);
//echo count($db_values);
//var_dump($db_map);
*/

// convert to table
/*
$x = array();
for ($i=0; $i<3; $i++)
	{
	$row = $db_map[$i];
	foreach ($row as $key=>$value)
		{
//		$row[$key] = $db_values[$value];
		$row[$key] = $db_values[$value][0];
		}
	$x[] = $row;
	}
var_dump($x);
*/
/*
$x = array();
for ($i=0; $i<3; $i++)
	{
	$row = $db_map[$i];
	$x[] = $row;
	}
var_dump($x);
*/

function jsEscapeString($str)
{
$str = preg_replace('~[\']~' ,'\\$1',$str);
$str = preg_replace('~[\\n]~','\\$1',$str);
return $str;
}

function escapeJs($str)
{
$str = preg_replace('~[\']~' ,'\\$1',$str);
$str = preg_replace('~[\\n]~','\\$1',$str);
return $str;
}

function toJS(&$db,$js)
{
//var_dump($db_keys);
$nl  = "\n";
//$nl = "";
if (!is_array($js))
	{return;}
$str = 'var '.implode(',',$js).';';
if (isset($js['keys']) && is_array($db['keys']))
	{
	$str .=  "\n\n".$js['keys']." = [\n";
	$bool = false;
	foreach ($db['keys'] as $key => $value)
		{
		$str .= "'".escapeJs($value)."',".$nl;
		$bool = true;
		}
	if ($bool==true) {$str  = substr($str,0,-(1+strlen($nl)));}
	$str .= "\n];";
	}
if (isset($js['values']) && is_array($db['values']))
	{
	$str .= "\n\n".$js['values']." = [\n";
	$bool = false;
	foreach ($db['values'] as $key => $value)
		{
//	$str .= "'".jsEscapeString($value[0])."',".$nl;
//if ($key==25) var_dump($value);
		if (is_array($value))
			{
			foreach($value as $key2 => $value2)
				{
				$value[$key2] = "'".escapeJs($value2[2])."'";
				}
			$str .= "[".implode(",",$value)."],".$nl;
			$bool = true;
			}
		}
	if ($bool==true) {$str  = substr($str,0,-(1+strlen($nl)));}
	$str .= "\n];";
	}
if (isset($js['map']) && is_array($db['map']))
	{
	$str .= "\n\n".$js['map']." = [\n";
	$bool = false;
	foreach ($db['map'] as $key => $value)
		{
		if (is_array($value))
			{
			$str .= "[".implode(",",$value)."],".$nl;
			$bool = true;
			}
		}
	if ($bool==true) {$str  = substr($str,0,-(1+strlen($nl)));}
	$str .= "\n];";
	}
if (isset($js['filters']) && is_array($db['filters']))
	{
$str .= "\n\n".$js['filters']." = [\n";
	foreach ($db['filters'] as $key => $value)
		{
		if (is_array($value))
			{
			$str .= "[".implode(",",$value)."],".$nl;
			$bool = true;
			}
		}
	if ($bool==true) {$str  = substr($str,0,-(1+strlen($nl)));}
	$str .= "\n];";
	}
$str .= "\n\n";
return $str;
}

//echo '<pre>'.toJS($db_keys,$db_values,$db_map,$db_filters);

// ------------

function csvParseOpen($opt)
{
$file = $opt['csv']['file'];
return fopen($file, "r");
}

function csvParseRow(&$fp,$opt)
{
$csv = $opt['csv'];
return fgetcsv($fp, $csv['row_length'], $csv['sep_col'], $csv['enclosure'], $csv['escape']);
}

function csvParseClose(&$fp)
{
fclose($fp);
}

function csvParseValueAdd(&$db,$data)
{
$id = count($db);
array_unshift($data,$id);	//id
array_unshift($data,0);			//frequency of word
$db[] = $data;
return $id;
}

function csvParseValueFindAdd(&$db,$word='',$opt)
{
foreach ($db as $key=>$value)
	{
	if ($value[2] == $word)
		{
		$db[$key][0]++;	//for sort by frequency word ||| //$db[$key][3]++;	//for sort by frequency word
		return $key;
		}
	}
return csvParseValueAdd($db,array($word));
}

// ---------

function sqlParseOpen($opt=array())
{
global $SQL;
$table = $opt['sql']['table'];
$query = 'SELECT * FROM `'.$table.'`';
return $SQL->query($query);
}

function sqlParseRow(&$res,$opt=array())
{
global $SQL;
return $SQL->fetch(null,PDO::FETCH_NUM);
}

function sqlParseClose(&$res,$opt)
{
}

function sqlParseValueAdd(&$db,$data)
{
$id = count($db);
array_unshift($data,$id);	//id
array_unshift($data,0);			//frequency of word
$db[] = $data;
return $id;
//$id = $data[0];
//array_unshift($data,0);			//frequency of word
//$db[$id] = $data;
//return $id;
}

function sqlParseValueFindAdd(&$db,$word='',$opt=array())
{
//$word = substr(sha1($word),0,strlen($word));	//hash str
foreach ($db as $key=>$value)
	{
	if ($value[2] == $word)
		{
		$db[$key][0]++;	//for sort by frequency word ||| //$db[$key][3]++;	//for sort by frequency word
		return $key;
		}
	}
return sqlParseValueAdd($db,array($word));
}

// ---------

function sortCmpValue($a,$b)
{
return $b[0]>$a[0];
}

function sortCmpCol($a,$b)
{
return $b[1]>$a[1];
}

function sortCmpCount($a,$b)
{
return $b[0]>$a[0];
//return $b[3]>$a[3];
}

function dbParse($func_open,$func_readRow,$func_close,$func_valueFindAdd,$opt)
{
$db = array(
	'keys'   => array(),
	'values' => array(),
	'map'    => array(),
	'filters'=> array()
	);
$bool_remove = isset($opt['remove']) && count($opt['remove'])>0;
$bool_add    = isset($opt['add'])    && $opt['add']>=0;
if ($bool_remove)
	{
	rsort($opt['remove']);
	}
$value_none = '';
if (($res = $func_open($opt))!==false)
	{
//var_dump($res);
//	$n = 0;
//$row_length = 1000;	//chars with csv escape
//if (($handle = fopen($file, "r")) !== FALSE)
//	{
	$row  = array();
	$j = 0;
	while (($row = $func_readRow($res, $opt))!==false)
		{
		// build keys
		$bool_num = false;
		if (isset($opt['keys']))
			{
			$db['keys'] = $opt['keys'];
			}
		else	{
			if (isset($opt['csv']))
				{
				$db['keys'] = $row;
				}
			else	{
				$bool_num = true;
				$db['keys'] = (count($row)>0) ? range(0,count($row)-1) : array();
				}
			}
		if ($bool_remove)				// col numbers
				{
				foreach ($opt['remove'] as $value)
					{
					if (isset($db['keys'][$value]))
						{unset($db['keys'][$value]);}
					}
				}
		if ($bool_add)	// col number
				{
				array_splice($db['keys'],$opt['add'],0,array('id')); //add 'id' column
				}
		// build values = ''
	        for ($i=0; $i<count($row); ++$i)
			{
			if ($bool_remove && in_array($i,$opt['remove']))
				{
//var_dump($i);
				continue;}
			$db['values'][$i] = array();
			$func_valueFindAdd($db['values'][$i],$value_none,$opt);
			}
		if (!isset($opt['keys'])) {break;}
		// build map, values
		if (is_array($row))
			{
//var_dump($row);
//exit;
			$row_table = array();
			$k = 0;
	        	for ($i=0; $i<count($row); ++$i)
				{
				if ($bool_remove && in_array($i,$opt['remove']))
					{continue;}
				if ($bool_add    && $k==$opt['add'])	//add row_id
					{
					$value = $j;
					$id    = $func_valueFindAdd($db['values'][$k],$value,$opt);
					$row_table[] = $id;	//row id
					++$k;
					}
				$value = trim($row[$i]);
				$id    = $func_valueFindAdd($db['values'][$k],$value,$opt);
				$row_table[] = $id;	//row id
				++$k;
				}
			if (count($row_table)>0)
				{
				$db['map'][] = $row_table;
				}
			++$j;
			}
		break;
		}
	while (($row = $func_readRow($res, $opt))!==false)
		{
		// build map, values
		if (is_array($row))
			{
			$row_table = array();
			$k = 0;
	        	for ($i=0; $i<count($row); ++$i)
				{
				if ($bool_remove && in_array($i,$opt['remove'])) {continue;}
				if ($bool_add    && $k==$opt['add'])	//add row_id
					{
					$value = $j;
					$id    = $func_valueFindAdd($db['values'][$k],$value,$opt);
					$row_table[] = $id;	//row id
					++$k;
					}
				$value = trim($row[$i]);
				$id    = $func_valueFindAdd($db['values'][$k],$value,$opt);
				$row_table[] = $id;	//row id
				++$k;
				}
			if (count($row_table)>0)
				{
				$db['map'][] = $row_table;
				}
			++$j;
			}
		}
	$func_close($res,$opt);
	}
//var_dump($db);
// sort by word count
$cnv = array();
//if (!is_array($db['values']))
//	{echo 'Chyba not values';}
foreach ($db['values'] as $col_key => $col_value)
	{
	$cnv[$col_key] = array();
	usort($db['values'][$col_key],'sortCmpCount');
	foreach ($db['values'][$col_key] as $row_key => $row_value)
		{
		$cnv[$col_key][$row_value[1]] = $row_key;
		}
	}
//var_dump($cnv);
//var_dump($db['map']);
// remap sorted
//if (!is_array($db['values']))
//	{echo 'Chyba not map';}
foreach ($db['map'] as $row_key => $row_value)
	{
	foreach ($row_value as $col_key => $col_value)
		{
		//if (isset($cnv[$value2]) )
		//	{
//			$v = $value2;
		$v = $cnv[$col_key][$col_value];
		$db['map'][$row_key][$col_key] = $v;
		// build filters
		if (!isset($db['filters'][$col_key]))
			{
			$db['filters'][$col_key] = array();
			}
		//var_dump($db_filters[$key2]);
		if (!in_array($v,$db['filters'][$col_key]))
			{
			$db['filters'][$col_key][] = $v;
			}
		//	}
		//else {echo $value2.'<br>';}
		}
	}
return $db;

/*
//usort($db_values,'sortCmpValue');
// sort by words count + remap
//$db_values[0][3]--; // remove one '';
//var_dump($db_values);

usort($db_values,'sortCmpCount');
$cnv = array();
foreach ($db_values as $key => $value)
	{
	$cnv[$value[2]] = $key;
//	$cnv[] = array($value[2],$key);
	}
//var_dump($cnv);

foreach ($db_map as $key => $value)	//remap
	{
	foreach ($value as $key2 => $value2)
		{
		//if (isset($cnv[$value2]) )
		//	{
//			$v = $value2;
		$v = $cnv[$value2];
		$db_map[$key][$key2] = $v;
		if (!isset($db_filters[$key2]))
			{
			$db_filters[$key2] = array();
			}
		//var_dump($db_filters[$key2]);
		if (!in_array($v,$db_filters[$key2]))
			{
			$db_filters[$key2][] = $v;
			}
		//	}
		//else {echo $value2.'<br>';}
		}
	}
*/
/*
//			$value = $j.'';
//			if ($id===false)
//				{
//				$id = dbValueAdd($out['values'],$values_cnt,array($value,$i));
//				}
//		        for ($i=0; $i<count($row); ++$i)
//				{
//				$value = trim($row[$i]);
//				if ($i>0) {$value = substr(sha1($value),0,strlen($value));}	//hash str
//				$id    = $valueFindAdd($db['values'][$i],$value);
//				if ($id===false)
//					{
//					$id = dbValueAdd($db_values,$values_cnt,array($value,$i));
//					}
//				$row_table[] = $id;
//				}
//		else	{
//			}
//		++$j;//row id
//------------
*/
}



/*
$csv = array(
	'file'       => 'zasuvky_bn13.csv',
	'sep_row'    => "\n",
	'sep_col'    => ';',
	'enclosure'  => '"',
	'escape'     => '"',
	'row_length' => 1000
	);
$sql1 = array(
	'table' => 'zas2_data'
	);
$sql2 = array(
	'table' => 'zas2_changes'
	);
$js1 = array(
	'keys'    => 'db_keys',
	'values'  => 'db_values', 
	'map'     => 'db_map', 
	'filters' => 'db_filters'
	);
$js2 = array(
//	'keys'    => 'db_keys',
	'values'  => 'db_changes_values', 
	'map'     => 'db_changes_map'
	);
//$db = dbParse('csvParseOpen','csvParseRow','csvParseClose','csvParseValueFindAdd',array('csv'=>$csv));
$keys = 'id;status;change_id;change_date;change_user;rack;zařízení;zásuvka;vede do zařízení;...a zásuvky;kabel;pozn.;místnost;infrastruktura;co;VLAN;Port security;MAC;IP;OS;skupina;name;ústav;patro;pozn. 2;data22';
$keys = explode(';',$keys);
//$db = dbParse('sqlParseOpen','sqlParseRow','sqlParseClose','sqlParseValueFindAdd',array('csv'=>$csv,'keys'=>$keys,'remove'=>array(1),'add'=>-1,'row_id'=>0));
//$db1 = dbParse('sqlParseOpen','sqlParseRow','sqlParseClose','sqlParseValueFindAdd',array('csv'=>$csv,'keys'=>$keys,'remove'=>array(25),'add'=>-1,'row_id'=>0)); // ted mam navic v db i sloupec 22
$db1 = array();
$db1 = dbParse('sqlParseOpen','sqlParseRow','sqlParseClose','sqlParseValueFindAdd',array('sql'=>$sql1,'csv'=>$csv,'keys'=>$keys,'remove'=>array(25),'add'=>-1,'row_id'=>0)); // ted mam navic v db i sloupec 22
$db2 = array();
$db2 = dbParse('sqlParseOpen','sqlParseRow','sqlParseClose','sqlParseValueFindAdd',array('sql'=>$sql2,'csv'=>$csv,'keys'=>$keys,'remove'=>array(25),'add'=>-1,'row_id'=>0)); // ted mam navic v db i sloupec 22
// remove - odstranit sloupce -- vhodne pro sql
// add - pridej id jako sloupec 0-n; -1 id uz tam je nebo ho nepouziji; prida 'id' do keys a prida cislo radku do values -- vhodne pro csv
// row_id - id pro ulozeni    -- vhodne pro sql
//var_dump($db);
//var_dump($db['values'][1]);
$js  = toJS($db1,$js1);
$js .= toJS($db1,$js2);
header('content-type: text/html; charset: utf-8');
//header('content-type: text/html; charset: windows-1250');
echo '<pre>'.$js;
//echo '<hr>';
*/

function fileWrite($file_name,$data_to_save,$end_time=1000)
{
$err = array();
if (($fp = fopen($file_name,'w'))!==false)
	{
	$dir = strrpos($file_name,'/')===false ? './' : substr($file_name,0,strrpos($file_name,'/'));
	$old = umask(0);
	@chmod($dir,0777);	//nastav prava adresare na 777
	$start_time = microtime();
	do	{
		$can_write = flock($fp, LOCK_EX);
		if (!$can_write)
			{usleep(round(rand(0,100)*1000));}
		}
	while ((!$can_write) and ((microtime() - $start_time) < $end_time));
	if ($can_write)
		{
//		if ($csv)
//			{csvSave($fp,$data_to_save);}
//		else	{
		fputs($fp,$data_to_save);
//		}	//,4096 = omezeni na 4k textu
		}
	else	{$err[] = 'write - file write error';}
	@chmod($file_name,0755);		//!nastavit prava k zapisu souboru, 755 (rwx r.x r.x) nebo 644 (rw. r.. r..) (owner-vlastnik, group-skupina, other-ostatni, r-read, w-write, x-execute; umask, mkdir)
	umask($old);
	fclose($fp); 
	}
else $err[] = 'write - file open error';
//var_dump($err);
return $err;
}



class classDBFiles
{
//	
//	
public $cfg;
public function __construct()
	{
	$this->cfg = array(
		'files_all' => array(
			'zip'     => 'zas2.zip', 
			'db'      => 'zas2-db.js', 
			'changes' => 'zas2-db-changes.js'
			),
		'zip_files_all' => array(
			'zas2.htm', 
			'zas2-css.css', 
			'zas2-db.js', 
			'zas2-db-changes.js', 
			'zas2-inc.js', 
			'zas2-eng.js'
			),
		'keys' => 'id;status;change_id;change_date;change_user;rack;zařízení;zásuvka;vede do zařízení;...a zásuvky;kabel;pozn.;místnost;infrastruktura;co;VLAN;Port security;MAC;IP;OS;skupina;name;ústav;patro;pozn. 2;data22',
		'csv' => array(
			'file'       => 'zasuvky_bn13.csv',
			'sep_row'    => "\n",
			'sep_col'    => ';',
			'enclosure'  => '"',
			'escape'     => '"',
			'row_length' => 1000
			),
		'sql1' => array(
			'table' => 'zas2_data'
			),
		'sql2' => array(
			'table' => 'zas2_changes'
			),
		'js1' => array(
			'keys'    => 'db_keys',
			'values'  => 'db_values', 
			'map'     => 'db_map', 
			'filters' => 'db_filters'
			),
		'js2' => array(
		//	'keys'    => 'db_keys',
			'values'  => 'db_changes_values', 
			'map'     => 'db_changes_map'
			)
		);
	$this->cfg['keys'] = explode(';',$this->cfg['keys']);
	}

public function exportFromSql()
	{
	$notice = array();
	$c = $this->cfg;
// remove - odstranit sloupce -- vhodne pro sql
// add - pridej id jako sloupec 0-n; -1 id uz tam je nebo ho nepouziji; prida 'id' do keys a prida cislo radku do values -- vhodne pro csv
// row_id - id pro ulozeni    -- vhodne pro sql
	//db
	$db1  = array();
	$db1  = dbParse('sqlParseOpen','sqlParseRow','sqlParseClose','sqlParseValueFindAdd',array('sql'=>$c['sql1'],'csv'=>$c['csv'],'keys'=>$c['keys'],'remove'=>array(25),'add'=>-1,'row_id'=>0)); // ted mam navic v db i sloupec 22
	$js   = toJS($db1,$c['js1']);
	$file_name = $c['files_all']['db'];
	fileWrite($file_name,$js);
	if (file_exists($file_name) && filesize($file_name)>0)
		{
		$size = ' ('.filesize($file_name).')';
		$notice[] = 'Export db: ok'.$size;
		}
	else	{
		$notice[] = 'Export db: failed';
		}
	//changes
	$db2  = array();
	$db2  = dbParse('sqlParseOpen','sqlParseRow','sqlParseClose','sqlParseValueFindAdd',array('sql'=>$c['sql2'],'csv'=>$c['csv'],'keys'=>$c['keys'],'remove'=>array(25),'add'=>-1,'row_id'=>0)); // ted mam navic v db i sloupec 22
	$js   = toJS($db2,$c['js2']);
	$file_name = $c['files_all']['changes'];
	fileWrite($file_name,$js);
	if (file_exists($file_name) && filesize($file_name)>0)
		{
		$size = ' ('.filesize($file_name).')';
		$notice[] = 'Export changes: ok'.$size;
		}
	else	{
		$notice[] = 'Export changes: failed';
		}
	//zip
	$zip_path = '';
	$zip_name = $c['files_all']['zip'];
	$zip  = new ZipArchive;
	$bool = $zip->open($zip_path.$zip_name,ZipArchive::CREATE | ZipArchive::OVERWRITE);
	if ($bool === true)
		{
		foreach($c['zip_files_all'] as $file)
			{
			$zip->addFile($file);
			}
		//var_dump($zip);
		$zip->close();
		$size = ' ('.filesize($zip_name).')';
		$notice[] = 'Export zip: ok'.$size;
		}
	else	{
		$notice[] = 'Export zip: failed'.$size;
		}
	echo implode('<br>',$notice).'<br>';
	}

public function updateSql()
	{
	//...
	$this->exportFromSql();
	}
}

//$SQL = new classSql($CFG_SQL);
$REQ = new classRequest();
$API = new classTableApi($SQL);
//$FFF = new classDBFiles();
//$FFF->exportFile('changes',false);
?>