<?php
if (isset($_POST['submit'])) {
	if ($_POST['submit'] == 'reload') $multi_update = 0;
}

if ($multi_update) {	
	if (isset($c_levels) && $c_levels){
		$parent_guidv4 = array();
		if (isset($_POST['parent_guidv4']) && is_array($_POST['parent_guidv4'])){
			foreach ($_POST['parent_guidv4'] AS $key => $value){
				$parent_guidv4[] = $value;
			}
			$parent_guidv4 = array_unique($parent_guidv4);
		} else if (isset($_POST['parent_guidv4'])){
			$parent_guidv4[] = addslashes(trim(strip_tags(stripslashes(rawurldecode($_POST['parent_guidv4'])))));
		}
	}
	
	if (isset($parent_guidv4) && is_array($parent_guidv4)) $base_parent_guidv4 = $parent_guidv4;
	else $base_parent_guidv4 = array();
	
	$datetime = time();
	
	$language_foldername = $language;
	if (!isset($_POST['language'])) $language = NULL;
	if (isset($_POST['filename']) && $_POST['filename']) $filename = trim(strip_tags(stripslashes(str_replace("'", "&#39;", $_POST['filename']))));
	
	if (isset($_FILES['upload'])){
		$file_count = count($_FILES['upload']['tmp_name']);
		for ($y = 0; $y < $file_count; $y++){
			if (file_exists($_FILES['upload']['tmp_name'][$y]) && is_uploaded_file($_FILES['upload']['tmp_name'][$y])){
				unset($error);
				
				$file_data = $_FILES['upload']['tmp_name'][$y];
				$file_name = $_FILES['upload']['name'][$y];
				$file_type = $_FILES['upload']['type'][$y];
				$filesize = $_FILES['upload']['size'][$y];
				
				$parent_guidv4 = $base_parent_guidv4;

				$upload_path = NULL;
				if (isset($_POST['upload_path'])){
					if (is_array($_POST['upload_path'])){
						if (isset($_POST['upload_path'][$y])) $upload_path = $_POST['upload_path'][$y];
					} else {
						$upload_path = $_POST['upload_path'];
					}
				}
				
				if ($upload_path && substr($upload_path, -1) == '/' && isset($file_name) && $file_name){
					$upload_path .= $file_name;
				}
				
				if (isset($c_levels) && $c_levels && $upload_path){
					$parent_guidv4 = library_multiupload_levels_from_path($conn, $module, $language_foldername, $parent_guidv4, $upload_path);
				}
						
				$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;
				}
	
				$file_new = NULL;
				if (isset($filename) && $filename) $file_new = url_slug($filename);
				else if (isset($file_name) && $file_name) $file_new = url_slug(str_replace('.'.$extension, '', $file_name));
				
				$file_new = check_file($module, $file_new, $extension_new);
				
				if (!$file_new){
					$error = LABEL_ERROR_MANDATORY;
				}
				
				
				if (!isset($error) && $multi_update == 1){
					
					$sql = "INSERT INTO ".$module." SET ";
					
			    for($i = 1; $i <= $count_fields; $i++){
			    	if (isset(${$field[$i]}) OR $field[$i] == 'file') {
				    	if ($i == 1) $sql.= " ".$field[$i]." = :".$field[$i]." ";
				    	else $sql.= ", ".$field[$i]." = :".$field[$i]." ";
				    }
			    }
					
					$result = $conn->prepare($sql);
			    for($i = 1; $i <= $count_fields; $i++){
			  		if (isset(${$field[$i]}) OR $field[$i] == 'file') {
			  			if ($field[$i] == 'file') $result->bindValue(':'.$field[$i], $file_new);
							else $result->bindValue(':'.$field[$i], ${$field[$i]});
						}
					}
					
					if ($result->execute()){
						$id = $conn->lastInsertId();
						
						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);						
						copy($file_data, $file_dir.$file_new);
						chmod($file_dir.$file_new, 0644);
						
						
						
						if ($id){
							$return['id'] = $id;
							$return['upload'] = array();
							$return['upload']['date'] = date("d.m.Y - H:i", $datetime);
							$return['upload']['label'] = LABEL_CLOCK;
							$return['upload']['filesize'] = formatSizeUnits($filesize);
							$return['upload']['dir'] = $file_dir;
							$return['upload']['file'] = $file_new;
							$return['upload']['category'] = $category;
							if (isset($c_language) && $c_language){
								$sql_tmp = "SELECT title FROM languages WHERE iso = :iso LIMIT 1";
								$result_tmp = $conn->prepare($sql_tmp);
								$result_tmp->bindValue(':iso', $language, PDO::PARAM_STR);
								$result_tmp->execute();
								if ($result_tmp->rowCount()){
									$arr_tmp = $result_tmp->fetch();
									$return['upload']['language'] = $arr_tmp['title'];
								}
							}
							
							$return['class'] = "success";
							$return['status'] = LABEL_SUCCESS;
							print json_encode($return);
						}
						unset($category);
					}
				} else {
					$return['class'] = "error";
					$return['status'] = LABEL_ERROR;
					print json_encode($return);
				}
			}
		}
	}
}