<?php
if (isset($_GET['action'])) $action = addslashes(trim(strip_tags(stripslashes(rawurldecode($_GET['action'])))));
else $action = false;

if (isset($_POST['submit'])) {
	if ($_POST['submit'] == 'reload') $update = 0;
}

if (isset($_POST['category_data'])) {
	foreach ($_POST['category_data'] AS $key => $value){
		$category_data[$key] = $value;
	}
}

if ($update) {
	
	if ($action != 'quick'){
		if (!$category){
			$error = LABEL_ERROR_MANDATORY;
		}
	}
	
	if (!isset($error) && $update == 1){
		if ($action == 'quick'){
			$key = str_replace('quick[', '', str_replace(']', '', addslashes(trim(strip_tags(stripslashes(rawurldecode($_POST['key'])))))));
			$value = trim(stripslashes(nl2br($_POST['quick'][$key])));
			if ($key == 'category'){
				$sql_tmp = "SELECT category FROM ".$module." WHERE id = :id ";
				$result_tmp = $conn->prepare($sql_tmp);
				$result_tmp->bindValue(':id', $id, PDO::PARAM_INT);
				$result_tmp->execute();
				if ($result_tmp->rowCount()){
					$arr_tmp = $result_tmp->fetch();
					$sql = "UPDATE ".$module." SET category = :category, title = IF (BINARY title = :category_old, :category, title) WHERE BINARY category = :category_old";
					$result = $conn->prepare($sql);
					$result->bindValue(':category', $value);
					$result->bindValue(':category_old', $arr_tmp['category']);
					if ($result->execute()){
						print $value;
					}
				}				
			} else {
				$sql = "UPDATE ".$module." SET ".$key." = :".$key." WHERE id = :id";
				$result = $conn->prepare($sql);
				$result->bindValue(':'.$key, $value);
				$result->bindValue(':id', $id);
				if ($result->execute()){
					print $value;
				}
			}
		} else if ($action == 'edit'){
			$sql_tmp = "SELECT category FROM ".$module." WHERE id = :id ";
			$result_tmp = $conn->prepare($sql_tmp);
			$result_tmp->bindValue(':id', $id, PDO::PARAM_INT);
			$result_tmp->execute();
			if ($result_tmp->rowCount()){
				$arr_tmp = $result_tmp->fetch();
				foreach ($category_data AS $key => $value){
					$sql_sub = "SELECT * FROM ".$module." WHERE language = :language AND BINARY category = :category LIMIT 1";
					$result_sub = $conn->prepare($sql_sub);
					$result_sub->bindValue(':language', $value['language'], PDO::PARAM_STR);
					$result_sub->bindValue(':category', $arr_tmp['category'], PDO::PARAM_STR);
					$result_sub->execute();
					if ($result_sub->rowCount()){
						$sql = "UPDATE ".$module." SET 
										sortorder = :sortorder, 
										category = :category,
										language = :language, 
										c_active = :c_active ";
				    for($i = 1; $i <= $count_fields; $i++){
				    	if (isset($value['value_'.$field[$i]])){
					    	$sql.= ", ".$field[$i]." = :".$field[$i]." ";
					    }
				    }
				    $sql.= "WHERE language = :language AND BINARY category = :category_old ";
						$result = $conn->prepare($sql);
												
						$result->bindValue(':sortorder', $sortorder, PDO::PARAM_INT);
						$result->bindValue(':category', $category, PDO::PARAM_STR);
						$result->bindValue(':c_active', $c_active, PDO::PARAM_STR);
						
				    for($i = 1; $i <= $count_fields; $i++){
				    	if (isset($value['value_'.$field[$i]])){
								$result->bindValue(':'.$field[$i], $value['value_'.$field[$i]]);
					    }
				    }
						
						$result->bindValue(':language', $value['language'], PDO::PARAM_STR);
						$result->bindValue(':category_old', $arr_tmp['category'], PDO::PARAM_STR);
						$result->execute();
					} else {
						$sql = "INSERT INTO ".$module." SET 
										sortorder = :sortorder, 
										category = :category,
										language = :language, 
										c_active = :c_active ";
				    for($i = 1; $i <= $count_fields; $i++){
				    	if (isset($value['value_'.$field[$i]])){
					    	$sql.= ", ".$field[$i]." = :".$field[$i]." ";
					    }
				    }
						$result = $conn->prepare($sql);
												
						$result->bindValue(':sortorder', $sortorder, PDO::PARAM_INT);
						$result->bindValue(':category', $category, PDO::PARAM_STR);
						$result->bindValue(':c_active', $c_active, PDO::PARAM_STR);
						
				    for($i = 1; $i <= $count_fields; $i++){
				    	if (isset($value['value_'.$field[$i]])){
								$result->bindValue(':'.$field[$i], $value['value_'.$field[$i]]);
					    }
				    }
						
						$result->bindValue(':language', $value['language'], PDO::PARAM_STR);
						$result->execute();
					}
				}
			} else {
				$error = LABEL_ERROR_MANDATORY;
			}
		} else {
			$sql_tmp = "SELECT id FROM ".$module." WHERE BINARY category = :category LIMIT 1";
			$result_tmp = $conn->prepare($sql_tmp);
			$result_tmp->bindValue(':category', $category, PDO::PARAM_STR);
			$result_tmp->execute();
			if (!$result_tmp->rowCount()){
				$ii = 0;
				foreach ($category_data AS $key => $value){
					if (!$value['value_title']) $value['value_title'] = $category;
					$sql = "INSERT INTO ".$module." SET 
									sortorder = :sortorder, 
									category = :category,
									language = :language, 
									c_active = :c_active ";
			    for($i = 1; $i <= $count_fields; $i++){
			    	if (isset($value['value_'.$field[$i]])){
				    	$sql.= ", ".$field[$i]." = :".$field[$i]." ";
				    }
			    }
					$result = $conn->prepare($sql);
											
					$result->bindValue(':sortorder', $sortorder, PDO::PARAM_INT);
					$result->bindValue(':category', $category, PDO::PARAM_STR);
					$result->bindValue(':c_active', $c_active, PDO::PARAM_STR);
					
			    for($i = 1; $i <= $count_fields; $i++){
			    	if (isset($value['value_'.$field[$i]])){
							$result->bindValue(':'.$field[$i], $value['value_'.$field[$i]]);
				    }
			    }
					
					$result->bindValue(':language', $value['language'], PDO::PARAM_STR);
					$result->execute();
					if ($ii == 0) $id = $conn->lastInsertId();
					$ii++;
				}
			} else {
				$error = LABEL_ERROR_ID_EXISTS;
			}
		}
		
		
		if (!isset($error)){
			if ($action == 'add'){
		    for($i = 1; $i <= $count_fields; $i++){
		    	unset(${$field[$i]});
		    }
		    unset($update);
		    $url_form = "popup_form.php?module=".$module."&backend_module=".$backend_module."&id=".$id."&action=edit&success";
		  } elseif ($action == 'copy'){
		    $url_form = "popup_form.php?module=".$module."&backend_module=".$backend_module."&id=".$id."&action=edit&success";
			} else $update_parent = true;
							
			if (isset($url_form) || (!empty($update_parent))) {
				include('includes/function/reload.php');
			}
		}
	}
}

include('includes/function/load.php');

if ((!isset($_POST['submit']) && isset($id)) OR (isset($_POST['submit']) && $_POST['submit'] != 'reload' && isset($id) && !isset($error))){		
	$sql_tmp = "SELECT id, iso, title FROM languages ORDER BY sortorder ASC";
	$result_tmp = $conn->prepare($sql_tmp);
	$result_tmp->execute();
	if ($result_tmp->rowCount()){
		while ($arr_tmp = $result_tmp->fetch()){
			$category_data[$arr_tmp['id']]['language'] = $arr_tmp['iso'];
			$category_data[$arr_tmp['id']]['title'] = $arr_tmp['title'];
			if ($arr_tmp['iso'] == $language) {
				for($i = 1; $i <= $count_fields; $i++){
					if (isset(${$field[$i]})){
						$category_data[$arr_tmp['id']]['value_'.$field[$i]] = ${$field[$i]};
					} else {
						$category_data[$arr_tmp['id']]['value_'.$field[$i]] = NULL;
					}
		    }
			} else {
				$sql_sub_tmp = "SELECT * FROM ".$module." WHERE language = :language AND BINARY category = :category LIMIT 1";
				$result_sub_tmp = $conn->prepare($sql_sub_tmp);
				$result_sub_tmp->bindValue(':language', $arr_tmp['iso'], PDO::PARAM_STR);
				$result_sub_tmp->bindValue(':category', $category, PDO::PARAM_STR);
				$result_sub_tmp->execute();
				if ($result_sub_tmp->rowCount()){
					$arr_sub_tmp = $result_sub_tmp->fetch();
					for($i = 1; $i <= $count_fields; $i++){
						if (isset($arr_sub_tmp[$field[$i]])){
							$category_data[$arr_tmp['id']]['value_'.$field[$i]] = $arr_sub_tmp[$field[$i]];
						} else {
							$category_data[$arr_tmp['id']]['value_'.$field[$i]] = NULL;
						}
			    }
				} else {
					for($i = 1; $i <= $count_fields; $i++){
						if (isset(${$field[$i]})){
							$category_data[$arr_tmp['id']]['value_'.$field[$i]] = NULL;
						} else {
							$category_data[$arr_tmp['id']]['value_'.$field[$i]] = NULL;
						}
			    }
					$category_data[$arr_tmp['id']]['value_title'] = $category;
				}
			}
		}
	}
	
}


if ($action != 'edit' && (!isset($update) OR !$update) && !isset($sortorder)){
	$sql_tmp = "SELECT sortorder FROM ".$module." ORDER BY sortorder DESC LIMIT 1";
	$result_tmp = $conn->prepare($sql_tmp);
	$result_tmp->execute();
	if ($result_tmp->rowCount()){
		$arr_tmp = $result_tmp->fetch();
		$sortorder = $arr_tmp['sortorder'];
		$sortorder++;
	} else $sortorder = 1;
	
	$result = $conn->prepare("SELECT * FROM ".$module." LIMIT 0");
	if ($result->execute()){
		$count_fields = $result->columnCount();
		$ii = 0;
		for($zz = 0; $zz < $count_fields; $zz++){
			$type = $result->getColumnMeta($zz);
			if ($type['name'] != 'id') {
				$ii++;
				$field[$ii] = $type['name'];
			}
		}
		$count_fields = $ii--;
	}
	
	$sql_tmp = "SELECT id, iso, title FROM languages ORDER BY sortorder ASC";
	$result_tmp = $conn->prepare($sql_tmp);
	$result_tmp->execute();
	if ($result_tmp->rowCount()){
		while ($arr_tmp = $result_tmp->fetch()){
			$category_data[$arr_tmp['id']]['language'] = $arr_tmp['iso'];
			$category_data[$arr_tmp['id']]['title'] = $arr_tmp['title'];
			for($i = 1; $i <= $count_fields; $i++){
				$category_data[$arr_tmp['id']]['value_'.$field[$i]] = NULL;
	    }
		}
	}
}