<?php
if(isset($_POST['update']) && $_POST['update'] == 1){
	$sql_tmp = "SELECT title FROM ".$module." WHERE 
							title NOT IN ('logo', 'watermark') 
							AND title NOT LIKE 'image_width_%'
							AND title NOT LIKE 'image_height_%' 
							AND title NOT LIKE 'lightbox_%' 
							AND title NOT LIKE 'pdf_image_%'
							AND c_public = '1'";
	$result_tmp = $conn->prepare($sql_tmp);
	$result_tmp->execute();
	while ($arr_tmp = $result_tmp->fetch()){
	  $sql = "UPDATE ".$module." SET value = :value WHERE title = :title ";
		$result = $conn->prepare($sql);
		if (str_starts_with($arr_tmp['title'], 'c_')){
			if (isset($_POST[$arr_tmp['title']]) && $_POST[$arr_tmp['title']]){
				$result->bindValue(':value', $_POST[$arr_tmp['title']], PDO::PARAM_STR);
			} else {
				$result->bindValue(':value', 0, PDO::PARAM_STR);
			}
		} else if ($arr_tmp['title'] == 'default_guidv4') {
			if (isset($_POST[$arr_tmp['title']]) && $_POST[$arr_tmp['title']]) {
				$result->bindValue(':value', trim(stripslashes(nl2br($_POST[$arr_tmp['title']]))), PDO::PARAM_STR);
				${$arr_tmp['title']} = trim(stripslashes(nl2br($_POST[$arr_tmp['title']])));
			} else {
				$result->bindValue(':value', NULL, PDO::PARAM_STR);
				unset(${$arr_tmp['title']});
			}
		} else if (str_ends_with($arr_tmp['title'], '_parent_guidv4')) {
			if (isset($_POST[$arr_tmp['title']]) && $_POST[$arr_tmp['title']]) {
				$result->bindValue(':value', trim(stripslashes(nl2br($_POST[$arr_tmp['title']]))), PDO::PARAM_STR);
				${$arr_tmp['title']} = trim(stripslashes(nl2br($_POST[$arr_tmp['title']])));
			} else {
				$result->bindValue(':value', NULL, PDO::PARAM_STR);
				unset(${$arr_tmp['title']});
			}
		} else {
			if (isset($_POST[$arr_tmp['title']])) $result->bindValue(':value', trim(stripslashes(nl2br($_POST[$arr_tmp['title']]))), PDO::PARAM_STR);
			else $result->bindValue(':value', NULL, PDO::PARAM_STR);
		}
		$result->bindValue(':title', $arr_tmp['title'], PDO::PARAM_STR);
		$result->execute();
	  $save = 1;
	}
	
	if ($module == 'settings_options'){
		include('modules/settings/function/logo.php');
		include('modules/settings/function/favicon.php');
	}
	if ($module == 'settings_options' OR $module == 'gallery_options' OR $module == 'library_options'){
		include('modules/settings/function/watermark.php');
	}
	
	if (isset($c_images) && $c_images){
		if (isset($_POST['resolutions_data']) && is_array($_POST['resolutions_data'])){
			$i = 1;
			$resolutions_data = array();
			foreach ($_POST['resolutions_data'] AS $key => $value){
				if ((isset($value['width']) && $value['width']) OR (isset($value['height']) && $value['height'])) {
					$resolutions_data[$i] = $value;
					$i++;
				}
			}
			
			$resolutions_data_old = array();
			$sql = "SELECT id, title, value FROM ".$module." WHERE title LIKE 'image_width_%' OR title LIKE 'image_height_%' ORDER BY id ASC";
			$result = $conn->prepare($sql);
			$result->execute();
			if ($result->rowCount()){
				while ($arr = $result->fetch()){
					$key = str_replace('image_width_', '', str_replace('image_height_', '', $arr['title']));
					if (str_starts_with($arr['title'], 'image_width_') && $arr['value']) $resolutions_data_old[$key]['width'] = intval($arr['value']);
					else if (str_starts_with($arr['title'], 'image_width_')) $resolutions_data_old[$key]['width'] = NULL;
					else if (str_starts_with($arr['title'], 'image_height_') && $arr['value']) $resolutions_data_old[$key]['height'] = intval($arr['value']);
					else if (str_starts_with($arr['title'], 'image_height_')) $resolutions_data_old[$key]['height'] = NULL;
				}
			}
			
			if ($resolutions_data_old != $resolutions_data){
			
				$sql = "DELETE FROM ".$module." WHERE title LIKE 'image_width_%' OR title LIKE 'image_height_%' ";
				$result = $conn->prepare($sql);
				$result->execute();
				if (isset($resolutions_data) && is_array($resolutions_data)){
					$i = 101;
					foreach ($resolutions_data AS $key => $value){
						if (isset($value['width']) && $value['width']) $sql = "INSERT INTO ".$module." SET id = :id, c_public = '1', title = :title, value = :value ";
						else $sql = "INSERT INTO ".$module." SET id = :id, c_public = '1', title = :title, value = NULL ";
						$result = $conn->prepare($sql);
						$result->bindValue(':id', $i, PDO::PARAM_INT);
						$result->bindValue(':title', 'image_width_'.$key, PDO::PARAM_STR);
						if (isset($value['width']) && $value['width']) $result->bindValue(':value', $value['width'], PDO::PARAM_STR);
						$result->execute();
						$i++;
						
						if (isset($value['height']) && $value['height']) $sql = "INSERT INTO ".$module." SET id = :id, c_public = '1', title = :title, value = :value ";
						else $sql = "INSERT INTO ".$module." SET id = :id, c_public = '1', title = :title, value = NULL ";
						$result = $conn->prepare($sql);
						$result->bindValue(':id', $i, PDO::PARAM_INT);
						$result->bindValue(':title', 'image_height_'.$key, PDO::PARAM_STR);
						if (isset($value['height']) && $value['height']) $result->bindValue(':value', $value['height'], PDO::PARAM_STR);
						$result->execute();
						$i++;
						
					}
					
					$file_dir = '../images/';
					if (!is_dir($file_dir)) mkdir($file_dir);
					$module_dir = $file_dir.$backend_module."/";
					if (!is_dir($module_dir)) mkdir($module_dir);
					
					if (count($resolutions_data_old) > count($resolutions_data)){
						for ($thumb = count($resolutions_data_old); $thumb > count($resolutions_data); $thumb--){
							if (is_dir($module_dir.'thumb'.$thumb.'/')){
								foreach(glob($module_dir.'thumb'.$thumb.'/*.*') AS $thumbnail) {
									unlink($thumbnail);
								}
								rmdir($module_dir.'thumb'.$thumb.'/');
							}
						}
					}
					
					$resolutions_changed = array_multi_diff($resolutions_data, $resolutions_data_old);
					if (isset($resolutions_changed) && is_array($resolutions_changed)){
						$sql_modules = "SELECT title FROM ".$backend_module."_modules ";
						$result_modules = $conn->prepare($sql_modules);
						$result_modules->execute();
						if ($result_modules->rowCount()){
							while ($arr_modules = $result_modules->fetch()){
								if (check_table($arr_modules['title']."_images")){
									
									$sql = "SELECT images.file FROM ".$arr_modules['title']."_images AS module 
													LEFT JOIN images AS images ON module.id_media = images.id 
													GROUP BY images.file ";
									$result = $conn->prepare($sql);
									$result->execute();
									if ($result->rowCount()){
										while ($arr = $result->fetch()){
											foreach ($resolutions_changed AS $thumb => $resolution){
												$width = NULL;
												$height = NULL;
												if (isset($resolution['width']) && $resolution['width']) $width = $resolution['width'];
												if (isset($resolution['height']) && $resolution['height']) $height = $resolution['height'];
												if (!is_dir($module_dir.'thumb'.$thumb.'/')) mkdir($module_dir.'thumb'.$thumb.'/');
												resize($file_dir.'original/'.$arr['file'], $module_dir.'thumb'.$thumb.'/'.$arr['file'], $width, $height);
												chmod($module_dir.'thumb'.$thumb.'/'.$arr['file'], 0644);
											}
										}
									}
									
								}
							}
						}
					}
					
				}
				
			}
		} else {
			$resolutions_data_old = array();
			$sql = "SELECT id, title, value FROM ".$module." WHERE title LIKE 'image_width_%' OR title LIKE 'image_height_%' ORDER BY id ASC";
			$result = $conn->prepare($sql);
			$result->execute();
			if ($result->rowCount()){
				while ($arr = $result->fetch()){
					$key = str_replace('image_width_', '', str_replace('image_height_', '', $arr['title']));
					if (str_starts_with($arr['title'], 'image_width_') && $arr['value']) $resolutions_data_old[$key]['width'] = intval($arr['value']);
					else if (str_starts_with($arr['title'], 'image_width_')) $resolutions_data_old[$key]['width'] = NULL;
					else if (str_starts_with($arr['title'], 'image_height_') && $arr['value']) $resolutions_data_old[$key]['height'] = intval($arr['value']);
					else if (str_starts_with($arr['title'], 'image_height_')) $resolutions_data_old[$key]['height'] = NULL;
				}
			}
			
			if (count($resolutions_data_old) > 0){
					
				$file_dir = '../images/';
				$module_dir = $file_dir.$backend_module."/";
				
				for ($thumb = count($resolutions_data_old); $thumb > 0; $thumb--){
					if (is_dir($module_dir.'thumb'.$thumb.'/')){
						foreach(glob($module_dir.'thumb'.$thumb.'/*.*') AS $thumbnail) {
							unlink($thumbnail);
						}
						rmdir($module_dir.'thumb'.$thumb.'/');
					}
				}
			}
			
			$sql = "DELETE FROM ".$module." WHERE title LIKE 'image_width_%' OR title LIKE 'image_height_%' ";
			$result = $conn->prepare($sql);
			$result->execute();
		}
		
		if ((isset($_POST['lightbox_width']) && $_POST['lightbox_width']) OR (isset($_POST['lightbox_height']) && $_POST['lightbox_height'])){
			$lightbox_width = NULL;
			$lightbox_height = NULL;
			if (isset($_POST['lightbox_width']) && $_POST['lightbox_width']) $lightbox_width = $_POST['lightbox_width'];
			if (isset($_POST['lightbox_height']) && $_POST['lightbox_height']) $lightbox_height = $_POST['lightbox_height'];
			
			$lightbox_width_old = NULL;
			$lightbox_height_old = NULL;
			$sql = "SELECT id, title, value FROM ".$module." WHERE title LIKE 'lightbox_%' ORDER BY id ASC";
			$result = $conn->prepare($sql);
			$result->execute();
			if ($result->rowCount()){
				while ($arr = $result->fetch()){
					if ($arr['title'] == 'lightbox_width' && $arr['value']) $lightbox_width_old = intval($arr['value']);
					else if ($arr['title'] == 'lightbox_width') $lightbox_width_old = NULL;
					else if ($arr['title'] == 'lightbox_height' && $arr['value']) $lightbox_height_old = intval($arr['value']);
					else if ($arr['title'] == 'lightbox_height') $lightbox_height_old = NULL;
				}
			}
			
			if (($lightbox_width != $lightbox_width_old) OR ($lightbox_height != $lightbox_height_old)){
				
				$sql = "DELETE FROM ".$module." WHERE title LIKE 'lightbox_%' ";
				$result = $conn->prepare($sql);
				$result->execute();
				
				if (isset($lightbox_width) && $lightbox_width) $sql = "INSERT INTO ".$module." SET id = '201', c_public = '1', title = 'lightbox_width', value = :value ";
				else $sql = "INSERT INTO ".$module." SET id = '201', c_public = '1', title = 'lightbox_width', value = NULL ";
				$result = $conn->prepare($sql);
				if (isset($lightbox_width) && $lightbox_width) $result->bindValue(':value', $lightbox_width, PDO::PARAM_STR);
				$result->execute();
				
				if (isset($lightbox_height) && $lightbox_height) $sql = "INSERT INTO ".$module." SET id = '202', c_public = '1', title = 'lightbox_height', value = :value ";
				else $sql = "INSERT INTO ".$module." SET id = '202', c_public = '1', title = 'lightbox_height', value = NULL ";
				$result = $conn->prepare($sql);
				if (isset($lightbox_height) && $lightbox_height) $result->bindValue(':value', $lightbox_height, PDO::PARAM_STR);
				$result->execute();
				
				$file_dir = '../images/';
				if (!is_dir($file_dir)) mkdir($file_dir);
				$module_dir = $file_dir.$backend_module."/";
				if (!is_dir($module_dir)) mkdir($module_dir);
					
				if (is_dir($module_dir.'lightbox/')){
					foreach(glob($module_dir.'lightbox/*.*') AS $thumbnail) {
						unlink($thumbnail);
					}
					rmdir($module_dir.'lightbox/');
				}
					
				$sql_modules = "SELECT title FROM ".$backend_module."_modules ";
				$result_modules = $conn->prepare($sql_modules);
				$result_modules->execute();
				if ($result_modules->rowCount()){
					while ($arr_modules = $result_modules->fetch()){
						if (check_table($arr_modules['title']."_images")){
							
							$sql = "SELECT images.file FROM ".$arr_modules['title']."_images AS module 
											LEFT JOIN images AS images ON module.id_media = images.id 
											GROUP BY images.file ";
							$result = $conn->prepare($sql);
							$result->execute();
							if ($result->rowCount()){
								while ($arr = $result->fetch()){
									$width = NULL;
									$height = NULL;
									if (isset($lightbox_width) && $lightbox_width) $width = $lightbox_width;
									if (isset($lightbox_height) && $lightbox_height) $height = $lightbox_height;
									if (!is_dir($module_dir.'lightbox/')) mkdir($module_dir.'lightbox/');
									resize($file_dir.'original/'.$arr['file'], $module_dir.'lightbox/'.$arr['file'], $width, $height);
									chmod($module_dir.'lightbox/'.$arr['file'], 0644);
								}
							}
							
						}
					}
				}
				
			}
			
		} else {
			$lightbox_width_old = NULL;
			$lightbox_height_old = NULL;
			$sql = "SELECT id, title, value FROM ".$module." WHERE title LIKE 'lightbox_%' ORDER BY id ASC";
			$result = $conn->prepare($sql);
			$result->execute();
			if ($result->rowCount()){
				while ($arr = $result->fetch()){
					if ($arr['title'] == 'lightbox_width' && $arr['value']) $lightbox_width_old = intval($arr['value']);
					else if ($arr['title'] == 'lightbox_width') $lightbox_width_old = NULL;
					else if ($arr['title'] == 'lightbox_height' && $arr['value']) $lightbox_height_old = intval($arr['value']);
					else if ($arr['title'] == 'lightbox_height') $lightbox_height_old = NULL;
				}
			}
			
			if ($lightbox_width_old OR $lightbox_height_old){
				$file_dir = '../images/';
				$module_dir = $file_dir.$backend_module."/";
				
				if (is_dir($module_dir.'lightbox/')){
					foreach(glob($module_dir.'lightbox/*.*') AS $thumbnail) {
						unlink($thumbnail);
					}
					rmdir($module_dir.'lightbox/');
				}
			}
			
			$sql = "DELETE FROM ".$module." WHERE title LIKE 'lightbox_%' ";
			$result = $conn->prepare($sql);
			$result->execute();
			
		}
	}
}

print "
<div class='container settings'>
	<div class='form'>
		<div class='form-head'>
			<i class='material-icons md-24'>edit</i></i>".LABEL_ENTER_DATA;
if (isset($id) && $id) print "<span class='id'>#".$id."</span>";
print "
		</div>
		<div class='form-content'>
			<form action='".$_SERVER['SCRIPT_NAME']."?".$_SERVER['QUERY_STRING']."' method='post' enctype='multipart/form-data'>
				<input type='hidden' name='csrf_token' value='".htmlspecialchars(csrf_token())."'>
				<input type='hidden' name='update' value='1' />";

if (isset($update_parent) && $update_parent === true) print "<input type='hidden' id='update_parent' name='update_parent' value='1' />";


$sql = "SELECT id, title, value FROM ".$module." WHERE c_public = '1' ORDER BY id ASC";
$result = $conn->prepare($sql);
$result->execute();
if ($result->rowCount()){
	while ($arr = $result->fetch()){
		if (defined('LABEL_OPTIONS_'.strtoupper($arr['title']))) $title_str = constant('LABEL_OPTIONS_'.strtoupper($arr['title']));
	  else $title_str = ucwords(str_replace("_", " ", $arr['title']));
	  if ($arr['value']) $arr['value'] = stripcslashes(str_replace("<br />", "", $arr['value']));
	  
	  
	  if (str_starts_with($arr['title'], 'c_')){
			print "
				<div class='form-row'>
					<label for='".$arr['title']."' class='form-label'>".$title_str."</label>
					<div class='form-cols'>
						<div class='form-col'>
							<div class='form-controls'>";
			if ($arr['value']){
				print "
								<input type='checkbox' class='field' id='".$arr['title']."' name='".$arr['title']."' value='1' checked='checked'>";
			} else {
				print "
								<input type='checkbox' class='field' id='".$arr['title']."' name='".$arr['title']."' value='1'>";
			}
			print "
							</div><!-- /.form-controls -->
						</div><!-- /.form-col-->
					</div><!-- /.form-cols -->
				</div><!-- /.form-row -->";
		} else if ($arr['title'] == 'default_guidv4' OR $arr['title'] == 'default_language') {
		} else if (str_ends_with($arr['title'], '_parent_guidv4')) {
		} else if (!str_starts_with($arr['title'], 'image_width_') && !str_starts_with($arr['title'], 'image_height_') && !str_starts_with($arr['title'], 'lightbox') && !str_starts_with($arr['title'], 'pdf_image')){
			print "
				<div class='form-row'>
					<label for='".$arr['title']."' class='form-label'>".$title_str."</label>
					<div class='form-cols'>
						<div class='form-col form-full'>";
			if (!$arr['value'] OR (strlen($arr['value']) < 35 && !strpos($arr['value'],"\n"))){
				print "
							<div class='form-controls'>
								<input type='text' class='field' id='".$arr['title']."' name='".$arr['title']."' value='".$arr['value']."'>
							</div><!-- /.form-controls -->";
			} else {
				print "
							<div class='form-controls big'>
								<ul class='textarea-panel'>
									<li class='show_default-editor' data-id='".$arr['title']."'><i class='material-icons md-24'>edit</i>".LABEL_DEFAULT_EDITOR."</li>
									<li class='show_richtext-editor' data-id='".$arr['title']."'><i class='material-icons md-24'>wysiwyg</i>".LABEL_RICHTEXT_EDITOR."</li>
									<li class='show_code-editor' data-id='".$arr['title']."'><i class='material-icons md-24'>code</i>".LABEL_CODE_EDITOR."</li>
								</ul>
								<textarea name='".$arr['title']."' class='field big' id='".$arr['title']."_textarea' data-mode='html' data-autocomplete='true'>".$arr['value']."</textarea>
								<div id='".$arr['title']."' class='code-editor'></div>
							</div><!-- /.form-controls -->";
			}
			print "
						</div><!-- /.form-col -->
					</div><!-- /.form-cols -->
				</div><!-- /.form-row -->";
		}
	}

print "<hr />";
}

include('includes/form/images_level.php');
include('includes/form/files_level.php');

$sql = "SELECT id FROM ".$module." WHERE title = 'c_modules' AND value = '1' ORDER BY id ASC";
$result = $conn->prepare($sql);
$result->execute();
if ($result->rowCount()){
	print "
				<div class='form-row'>
					<span class='form-label'>".LABEL_MODULES."</span>
					<div class='form-cols'>
						<div class='form-col form-full'>
							<div class='form-controls'>";
	if ($_SESSION['backend']['permissions']['admin']) print "<span class='form-label-link'><a href='#' class='popup_filter' data-module='".$backend_module."_modules' data-backend-module='".$backend_module."'><i class='material-icons md-24'>settings</i></a></span>";
	print "
						</div><!-- /.form-controls -->
					</div><!-- /.form-col -->
				</div><!-- /.form-cols -->
			</div><!-- /.form-row -->
			
			<hr />";
}

$sql = "SELECT id FROM ".$module." WHERE title = 'c_levels' AND value = '1' LIMIT 1";
$result = $conn->prepare($sql);
$result->execute();
if ($result->rowCount()){
	print "
			<div class='form-row'>
				<span class='form-label'>".LABEL_LEVELS."</span>
				<div class='form-cols'>
					<div class='form-col form-full'>
						<div class='form-controls'>";
	if ($_SESSION['backend']['permissions']['admin']) print "<span class='form-label-link'><a href='#' class='popup_filter' data-module='".$level_module."_levels' data-backend-module='".$level_module."'><i class='material-icons md-24'>settings</i></a></span>";
	print "
						</div><!-- /.form-controls -->
					</div><!-- /.form-col -->
				</div><!-- /.form-cols -->
			</div><!-- /.form-row -->
						
			<hr />";
}

$folder = 'modules/'.$backend_module.'/options';
if (is_dir($folder)){
	$is_file = false;
	foreach(glob($folder.'/*.*') AS $add_options) {
		include($add_options);
		$is_file = true;
	}
	if ($is_file === true) print "<hr />";
}

if ($module == 'gallery_options' OR $module == 'library_options'){
	include('modules/settings/options/watermark.php');
}

if (isset($c_images) && $c_images){
	$resolutions_data = array();
	$sql = "SELECT id, title, value FROM ".$module." WHERE title LIKE 'image_width_%' OR title LIKE 'image_height_%' ORDER BY id ASC";
	$result = $conn->prepare($sql);
	$result->execute();
	if ($result->rowCount()){
		while ($arr = $result->fetch()){
			$key = str_replace('image_width_', '', str_replace('image_height_', '', $arr['title']));
			if (str_starts_with($arr['title'], 'image_width_') && $arr['value']) $resolutions_data[$key]['width'] = intval($arr['value']);
			else if (str_starts_with($arr['title'], 'image_width_')) $resolutions_data[$key]['width'] = NULL;
			else if (str_starts_with($arr['title'], 'image_height_') && $arr['value']) $resolutions_data[$key]['height'] = intval($arr['value']);
			else if (str_starts_with($arr['title'], 'image_height_')) $resolutions_data[$key]['height'] = NULL;
		}
	}
	
	$lightbox_width = NULL;
	$lightbox_height = NULL;
	$sql = "SELECT id, title, value FROM ".$module." WHERE title = 'lightbox_width' OR title = 'lightbox_height' ORDER BY id ASC";
	$result = $conn->prepare($sql);
	$result->execute();
	if ($result->rowCount()){
		while ($arr = $result->fetch()){
			if ($arr['title'] == 'lightbox_width' && $arr['value']) $lightbox_width = intval($arr['value']);
			else if ($arr['title'] == 'lightbox_width') $lightbox_width = NULL;
			else if ($arr['title'] == 'lightbox_height' && $arr['value']) $lightbox_height = intval($arr['value']);
			else if ($arr['title'] == 'lightbox_height') $lightbox_height = NULL;
		}
	}
	
	include('includes/form/resolutions.php');
}

print "
				<div class='form-action'>
					<button type='submit' class='btn save' name='submit' title='".LABEL_SAVE."'><i class='material-icons md-24'>save_alt</i>".LABEL_SAVE."</button>
				</div><!-- /.form-action -->
				
			</form>
		</div><!-- /.form-content -->
	</div><!-- /.form -->
	
</div><!-- /.container -->";