<?
class mysql_save
{//DigiHex Systems\\


	function prepare_row($table, $current_id=false, $force_insert_id=false) {
		if((int)$current_id>0) return $current_id;

		$sql=$this->query(' SHOW COLUMNS FROM '.$table.' ');
		while($dane=$this->fetch_assoc($sql)) {
			if($dane['Key']=='PRI' && $dane['Extra']=='auto_increment') {
				$pri=$dane['Field'];
				$this->query(' INSERT INTO '.$table.' ('.$pri.') VALUES ('.($force_insert_id ? '"'.$force_insert_id.'"':"null").') ');
				$new_id=mysql_insert_id();
				if((int)$new_id>0) return (int)$new_id;
			}
		}
	}


	function save($fields) {
		global $X;
		if(count($fields)>0) foreach($fields as $field) {
			$cols[]=$field.'="'.$_POST[$field].'"';
		}
		return @implode(', ', $cols);
	}


	function save_cols() {
		global $X;
		for($i=0; $i<func_num_args(); $i++) {
			$cols[]=func_get_arg($i).'="'.$_POST[func_get_arg($i)].'"';
		}
		return @implode(', ', $cols);
	}


	function save_multiselect($name, $conf, $use_poz=false, $sep=',') {
		global $X;

		$values=(@is_array($_POST[$name]) ? $_POST[$name] : @explode($sep,$_POST[$name]));
		if(count($values)>0) foreach($values as $option_id) {
			if(is_numeric($option_id) && $option_id!==0) {
				if($use_poz==true) ++$pozycja;

				$sql=$X->DB->q(' SELECT id FROM '.$conf['table'].' WHERE '.$conf['key'].'="'.$conf['val'].'" '.(!empty($conf['key2']) ? 'AND '.$conf['key2'].'="'.$conf['val2'].'"':'').' AND '.$conf['col'].'="'.$option_id.'" ');
				if($X->DB->num_rows($sql)==0) {
					$X->DB->q(' INSERT INTO '.$conf['table'].' ('.$conf['key'].','.(!empty($conf['key2']) ? $conf['key2'].',':'').$conf['col'].($use_poz==true ? ',pozycja':'').') VALUES ("'.$conf['val'].'",'.(!empty($conf['key2']) ? '"'.$conf['val2'].'",':'').'"'.$option_id.'"'.($use_poz==true ? ',"'.$pozycja.'"':'').') ');
				} else {
					if($use_poz==true) $X->DB->q(' UPDATE '.$conf['table'].' SET pozycja="'.$pozycja.'" WHERE '.$conf['key'].'="'.$conf['val'].'" '.(!empty($conf['key2']) ? 'AND '.$conf['key2'].'="'.$conf['val2'].'"':'').' AND '.$conf['col'].'="'.$option_id.'" ');
				}
				$option_id_in_db[]='"'.$option_id.'"';
			}
		}
		$X->DB->q(' DELETE FROM '.$conf['table'].' WHERE '.$conf['key'].'="'.$conf['val'].'" '.(!empty($conf['key2']) ? 'AND '.$conf['key2'].'="'.$conf['val2'].'"':'').' '.(count($option_id_in_db)>0 ? 'AND '.$conf['col'].' NOT IN ('.@implode(',',$option_id_in_db).')':'').' ');
	}


}//DigiHex Systems\\
?>