<?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['label_data'])) {
	foreach ($_POST['label_data'] AS $key => $value){
		$label_data[$key] = $value;
		if (isset($value['value']) && $value['value']) $label_data[$key]['value'] = trim(stripslashes(nl2br($value['value'])));
	}
}

if ($update) {
	
	if ($action != 'quick'){
		if (!$label){
			$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 == 'label'){
				$sql_tmp = "SELECT label 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 label = :label, text = IF (BINARY text = :label_old, :label, text) WHERE BINARY label = :label_old";
					$result = $conn->prepare($sql);
					$result->bindValue(':label', $value);
					$result->bindValue(':label_old', $arr_tmp['label']);
					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 strip_tags($value);
				}
			}
		} else if ($action == 'edit'){
			$sql_tmp = "SELECT label 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 ($label_data AS $key => $value){
					$sql_sub = "SELECT * FROM ".$module." WHERE language = :language AND BINARY label = :label LIMIT 1";
					$result_sub = $conn->prepare($sql_sub);
					$result_sub->bindValue(':language', $value['language'], PDO::PARAM_STR);
					$result_sub->bindValue(':label', $arr_tmp['label'], PDO::PARAM_STR);
					$result_sub->execute();
					if ($result_sub->rowCount()){
						$sql = "UPDATE ".$module." SET label = :label, text = :text, c_active = :c_active WHERE language = :language AND BINARY label = :label_old ";
						$result = $conn->prepare($sql);
						if ($value['value'] === $arr_tmp['label']) $value['value'] = $label;
						$result->bindValue(':label', $label, PDO::PARAM_STR);
						$result->bindValue(':text', $value['value'], PDO::PARAM_STR);
						$result->bindValue(':c_active', $c_active, PDO::PARAM_STR);
						$result->bindValue(':language', $value['language'], PDO::PARAM_STR);
						$result->bindValue(':label_old', $arr_tmp['label'], PDO::PARAM_STR);
						$result->execute();
					} else {
						$sql = "INSERT INTO ".$module." SET language = :language, label = :label, text = :text, c_active = :c_active";
						$result = $conn->prepare($sql);
						$result->bindValue(':language', $value['language'], PDO::PARAM_STR);
						$result->bindValue(':label', $label, PDO::PARAM_STR);
						$result->bindValue(':text', $value['value'], PDO::PARAM_STR);
						$result->bindValue(':c_active', $c_active, PDO::PARAM_STR);
						$result->execute();
					}
				}
			} else {
				$error = LABEL_ERROR_MANDATORY;
			}
		} else {
			$sql_tmp = "SELECT id FROM ".$module." WHERE BINARY label = :label LIMIT 1";
			$result_tmp = $conn->prepare($sql_tmp);
			$result_tmp->bindValue(':label', $label, PDO::PARAM_STR);
			$result_tmp->execute();
			if (!$result_tmp->rowCount()){
				$ii = 0;
				foreach ($label_data AS $key => $value){
					if (!$value['value']) $value['value'] = $label;
					$sql = "INSERT INTO ".$module." SET language = :language, label = :label, text = :text, c_active = :c_active";
					$result = $conn->prepare($sql);
					$result->bindValue(':language', $value['language'], PDO::PARAM_STR);
					$result->bindValue(':label', $label, PDO::PARAM_STR);
					$result->bindValue(':text', $value['value'], PDO::PARAM_STR);
					$result->bindValue(':c_active', $c_active, 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, label 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()){
			$label_data[$arr_tmp['id']]['language'] = $arr_tmp['iso'];
			$label_data[$arr_tmp['id']]['title'] = id($arr_tmp['label']);
			if ($arr_tmp['iso'] == $language) {
				$label_data[$arr_tmp['id']]['value'] = $text;
			} else {
				$sql_sub_tmp = "SELECT text FROM ".$module." WHERE language = :language AND BINARY label = :label 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(':label', $label, PDO::PARAM_STR);
				$result_sub_tmp->execute();
				if ($result_sub_tmp->rowCount()){
					$arr_sub_tmp = $result_sub_tmp->fetch();
					$label_data[$arr_tmp['id']]['value'] = stripcslashes(str_replace("'", "&#39;", str_replace("<br />","",str_replace("<br>","\n", $arr_sub_tmp['text']))));
				} else {
					$label_data[$arr_tmp['id']]['value'] = $label;
				}
			}
		}
	}
}


if ($action != 'edit' && (!isset($update) OR !$update) && !isset($label_data)){
	$sql_tmp = "SELECT id, iso, label 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()){
			$label_data[$arr_tmp['id']]['language'] = $arr_tmp['iso'];
			$label_data[$arr_tmp['id']]['title'] = id($arr_tmp['label']);
			$label_data[$arr_tmp['id']]['value'] = NULL;
		}
	}
}
