<?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 ($update) {
	
	if (!$label){
		$error = LABEL_ERROR_MANDATORY;
	}
	
	$update_iso = false;
	$create_iso = false;
	
	if ($action != 'edit'){
		$sql_tmp = "SELECT id FROM ".$module." WHERE iso = :iso LIMIT 1";
		$result_tmp = $conn->prepare($sql_tmp);
		$result_tmp->bindValue(':iso', $iso, PDO::PARAM_STR);
		$result_tmp->execute();
		if ($result_tmp->rowCount()){
			$fehler = LABEL_ERROR_ID_EXISTS;
		} else {
			$sql_tmp = "SELECT id FROM ".$module." WHERE iso = :iso LIMIT 1";
			$result_tmp = $conn->prepare($sql_tmp);
			$result_tmp->bindValue(':iso', $iso, PDO::PARAM_STR);
			$result_tmp->execute();
			if (!$result_tmp->rowCount()) $create_iso = true;
		}
	}
	
	if (!isset($error) && $update == 1){
		if ($action == 'edit'){
			$sql = "UPDATE ".$module." SET ";
		} else {
			$sql = "INSERT INTO ".$module." SET ";
		}
    for($i = 1; $i <= $count_fields; $i++){
    	if (isset(${$field[$i]})){
	    	if ($i == 1) $sql.= " ".$field[$i]." = :".$field[$i]." ";
	    	else $sql.= ", ".$field[$i]." = :".$field[$i]." ";
	    }
    }
		if ($action == 'edit') $sql.= " WHERE id = :id";
		
		$result = $conn->prepare($sql);
    for($i = 1; $i <= $count_fields; $i++){
  		if (isset(${$field[$i]})){
				$result->bindValue(':'.$field[$i], ${$field[$i]});
			}
		}
		if ($action == 'edit') $result->bindValue(':id', $id);
		
		if ($result->execute()){
			if ($action != 'edit') $id = $conn->lastInsertId();
			
			if (isset($create_iso) && $create_iso === true){
				$sql = "SELECT iso FROM ".$module." ORDER BY id ASC LIMIT 1";
				$result = $conn->prepare($sql);
				$result->execute();
				if ($result->rowCount()){
					$arr = $result->fetch();
					$sql_tmp = "SELECT label, c_active FROM frontend_labels WHERE language = :language ";
					$result_tmp = $conn->prepare($sql_tmp);
					$result_tmp->bindValue(':language', $arr['iso'], PDO::PARAM_STR);
					$result_tmp->execute();
					if ($result_tmp->rowCount()){
						while ($arr_tmp = $result_tmp->fetch()){
							$sql_ins = "INSERT INTO frontend_labels SET language = :language, label = :label, text = :label, c_active = :c_active";
							$result_ins = $conn->prepare($sql_ins);
							$result_ins->bindValue(':language', $iso, PDO::PARAM_STR);
							$result_ins->bindValue(':label', $arr_tmp['label'], PDO::PARAM_STR);
							$result_ins->bindValue(':c_active', $arr_tmp['c_active'], PDO::PARAM_STR);
							$result_ins->execute();
						}
					}
					$sql_tmp = "SELECT label, c_active FROM settings_labels WHERE language = :language ";
					$result_tmp = $conn->prepare($sql_tmp);
					$result_tmp->bindValue(':language', $arr['iso'], PDO::PARAM_STR);
					$result_tmp->execute();
					if ($result_tmp->rowCount()){
						while ($arr_tmp = $result_tmp->fetch()){
							$sql_ins = "INSERT INTO settings_labels SET language = :language, label = :label, text = :label, c_active = :c_active";
							$result_ins = $conn->prepare($sql_ins);
							$result_ins->bindValue(':language', $iso, PDO::PARAM_STR);
							$result_ins->bindValue(':label', $arr_tmp['label'], PDO::PARAM_STR);
							$result_ins->bindValue(':c_active', $arr_tmp['c_active'], PDO::PARAM_STR);
							$result_ins->execute();
						}
					}
				}
			}
			
			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)){
		    print "
				<script type='text/javascript'>
				opener.document.forms[0].submit();
				window.open('".$url_form."', '_self');
				</script>";
			}
			
		}
	}
}

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

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;
}