<?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($multi_update) && $multi_update){
	include($module."_multiupload_do.php");
} else if ($update) {

	if (isset($c_levels) && $c_levels){
		$parent_guidv4 = array();
		if (isset($_POST['parent_guidv4']) && $_POST['parent_guidv4']){
			foreach ($_POST['parent_guidv4'] AS $key => $value){
				$parent_guidv4[] = $value;
			}
			$parent_guidv4 = array_unique($parent_guidv4);
		}
	}

	if (isset($_POST['date'])) {
		$datetime = DateTime::createFromFormat('Y-m-d\TH:i', $_POST['date']);
		$datetime = $datetime->getTimestamp();
	}
	
	if (isset($_POST['file_old']) && $_POST['file_old']){
		$file_old = stripcslashes(str_replace("'", "&#39;", str_replace("<br />","",str_replace("<br>","\n", $_POST['file_old']))));
	} else {
		$error = LABEL_ERROR_MANDATORY;
	}
	
	
	if(isset($_FILES['upload']) && file_exists($_FILES['upload']['tmp_name']) && is_uploaded_file($_FILES['upload']['tmp_name'])) {
		
		if (isset($_POST['filename']) && $_POST['filename']) $filename = trim(strip_tags(stripslashes(str_replace("'", "&#39;", $_POST['filename']))));
		
		$file_data = $_FILES['upload']['tmp_name'];
		$file_name = $_FILES['upload']['name'];
		$file_type = $_FILES['upload']['type'];
		$filesize = $_FILES['upload']['size'];
				
		$extension_tmp = explode(".", $file_name);
		$extension = end($extension_tmp);
		
		$extension_new = mime2ext($file_type, $extension);
		$category = mime2category($file_type);
		
		if ($category == 'image'){
			$error = LABEL_ERROR_FILE_TYPE;
		}
		if (!isset($file)){
			if (isset($filename) && $filename) $file_new = url_slug($filename).".".$extension_new;
			else if (isset($file_name) && $file_name) $file_new = url_slug(str_replace('.'.$extension, '', $file_name)).".".$extension_new;
		} else $file_new = url_slug($file).".".$extension_new;
	} else {
		$extension_tmp = explode(".", $file_old);
		$extension = end($extension_tmp);
		$file_new = url_slug($file).".".$extension;
	}
	
	
	$sql_tmp = "SELECT id FROM ".$module." WHERE file = :file AND id != :id LIMIT 1";
	$result_tmp = $conn->prepare($sql_tmp);
	$result_tmp->bindValue(':file', $file_new, PDO::PARAM_STR);
	$result_tmp->bindValue(':id', $id, PDO::PARAM_INT);
	$result_tmp->execute();
	if ($result_tmp->rowCount()){
		$error = LABEL_ERROR_ID_EXISTS;
		$file = $file_old;
	}
	
	if (!isset($error) && $update == 1){
		
		$sql = "UPDATE ".$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.= " WHERE id = :id";
		
		$result = $conn->prepare($sql);
    for($i = 1; $i <= $count_fields; $i++){
  		if (isset(${$field[$i]})){
			  if ($field[$i] == 'file') $result->bindValue(':'.$field[$i], $file_new);
				else $result->bindValue(':'.$field[$i], ${$field[$i]});
			}
		}
		$result->bindValue(':id', $id);
		
		if ($result->execute()){
			
			if (isset($c_levels) && $c_levels){
				$sql_sub = "DELETE FROM ".$module."_levels_links WHERE id_module = :id ";
				$result_sub = $conn->prepare($sql_sub);
				$result_sub->bindValue(':id', $id, PDO::PARAM_INT);
				$result_sub->execute();
				if (isset($parent_guidv4) && is_array($parent_guidv4)){
					foreach ($parent_guidv4 AS $key => $value){
						$sql_tmp = "INSERT INTO ".$module."_levels_links SET id_module = :id, guidv4 = :guidv4 ";
						$result_tmp = $conn->prepare($sql_tmp);
						$result_tmp->bindValue(':id', $id, PDO::PARAM_INT);
						$result_tmp->bindValue(':guidv4', $value, PDO::PARAM_STR);
						$result_tmp->execute();
					}
				}
			}
			
			$file_dir = "../".$module."/";
		  if (!is_dir($file_dir)) mkdir($file_dir);
			
			
			
			if ($file_old != $file_new){
				rename($file_dir.$file_old, $file_dir.$file_new);
			}
			
			
			if(isset($_FILES['upload']) && file_exists($_FILES['upload']['tmp_name']) && is_uploaded_file($_FILES['upload']['tmp_name'])) {
				if (is_file($file_dir.$file_new)) unlink($file_dir.$file_new);
				copy($file_data, $file_dir.$file_new);
				chmod($file_dir.$file_new, 0644);
			}
			
			$update_parent = true;
		}
	}
}


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

if ((!isset($_POST['submit']) && isset($id)) OR (isset($_POST['submit']) && $_POST['submit'] != 'reload' && isset($id) && !isset($error))){	
	if (isset($c_levels) && $c_levels){
		$sql_tmp = "SELECT guidv4 FROM ".$module."_levels_links WHERE id_module = :id ORDER BY id";
		$result_tmp = $conn->prepare($sql_tmp);
		$result_tmp->bindValue(':id', $id, PDO::PARAM_INT);
		$result_tmp->execute();
		if ($result_tmp->rowCount()){
			$parent_guidv4 = array();
			while($arr_tmp = $result_tmp->fetch()){
				$parent_guidv4[] = $arr_tmp['guidv4'];
			}
		}
	}
}

if (isset($datetime) && $datetime) $datetime = date('Y-m-d\TH:i', $datetime);
else $datetime = date('Y-m-d\TH:i');