<?php
//tag listánál ez adja vissza a keresés eredményét
ini_set('include_path', '../_class/');
include('class_sql.php');
if ($_SERVER['HTTP_HOST'] == 'badmintoncoach.hu') $sql = new sql('bc_mysql','root','','badminton_coach');
else $sql = new sql('localhost','tollashodos','uprRscU8bGpJ','tollashodos');

    //megnézzük, hogy a KID benne van-e olyan category-val, ami az adott grouphoz tartozik	
        //ha igen, akkor, akkor updateljük a categoryját
        //ha nem, akkor beszúrjuk az új category ID-val
if ($_POST['category_id'] == 'null') {
	//var_dump('null');

}

$ugk_query = "SELECT ugk_id FROM user_group_kid WHERE ugk_user_kid_uk_id = " . $_POST['uk_id'] . " AND ugk_category_ugc_id in (
				SELECT ugc_id FROM user_group_category WHERE ugc_user_group_ug_id = ".$_POST['group_id'].");";

if ($sql->num_of_rows($ugk_query)) {
	//update
	//ha null, akkor megnézzük, hogy null-e a csoportja is
		//ha igen, akkor töröljük
		//ha nem, akkor updateljük null-ra
	//egyébként update
	$ugk_id = $sql->single_variable($ugk_query);
	if ($_POST['category_id'] == 'null') {
		$group_check_query = "SELECT ugk_user_group_ug_id FROM user_group_kid WHERE ugk_id = " . $ugk_id;
		$group_id = $sql->single_variable($group_check_query);

		if ($group_id) {
			$sql->update_table('user_group_kid', array('ugk_category_ugc_id' => $_POST['category_id']), array('ugk_id' => $ugk_id));
		}
		else {
			$sql->execute_query('DELETE FROM user_group_kid WHERE ugk_id = ' . $ugk_id);
		}
	}
	else {
		$sql->update_table('user_group_kid', array('ugk_category_ugc_id' => $_POST['category_id']), array('ugk_id' => $ugk_id));
		
	}

}
else {
	//insert
	//megnézzük, hogy null-e a csoport
		//ha igen, insert
		//ha nem, akkor update
	$ugk_query = "SELECT ugk_id FROM user_group_kid WHERE ugk_user_kid_uk_id = " . $_POST['uk_id'] . " AND ugk_user_group_ug_id = " . $_POST['group_id'];
	$ugk_id = $sql->single_variable($ugk_query);

	if ($ugk_id) {
		$sql->update_table('user_group_kid', array('ugk_category_ugc_id' => $_POST['category_id']), array('ugk_id' => $ugk_id));
	}
	else {
		$sql->insert_into('user_group_kid', array(
				'ugk_user_kid_uk_id' => $_POST['uk_id'],
				//'ugk_user_group_ug_id' => $_POST['group_id'],
				'ugk_category_ugc_id' => $_POST['category_id'],
			));
	}
}

?>