<?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 ($action != 'quick'){
		if (!$label){
			$error = LABEL_ERROR_MANDATORY;
		}
	}
	
	if ($action == 'add' OR $action == 'copy'){
		$sql_check = "SELECT id FROM ".$module." WHERE id = :id ";
		$result_check = $conn->prepare($sql_check);
		$result_check->bindValue(':id', $_POST['id'], PDO::PARAM_INT);
		$result_check->execute();
		if ($result_check->rowCount()){
			$error = LABEL_ERROR_ID_EXISTS;
		}
	}
	
	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])));
			$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 = "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]." ";
		    }
	    }
    	$sql.= ", id = :post_id ";
			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]});
				}
			}
    	$result->bindValue(':post_id', $_POST['id'], PDO::PARAM_INT);
			if ($action == 'edit') $result->bindValue(':id', $id);
			
			if ($result->execute()){
				if ($action != 'edit') $id = $conn->lastInsertId();
				
				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";
				} elseif ($_POST['id'] != $id) {
			    $url_form = "popup_form.php?module=".$module."&backend_module=".$backend_module."&id=".$_POST['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;
}