<?php
include_once('function/breadcrumb.php');
$session_module = NULL;
if (isset($_GET['session_module']) && $_GET['session_module']){
	$session_module = addslashes(trim(strip_tags(stripslashes($_GET['session_module']))));
	if (isset($_POST) && $_POST){
		$_SESSION['backend']['filter'][$session_module]['order'] = $order;
		$_SESSION['backend']['filter'][$session_module]['lastorder'] = $lastorder;
		$_SESSION['backend']['filter'][$session_module]['orderdir'] = $orderdir;
	} else {
		if (isset($_SESSION['backend']['filter'][$session_module]['order'])) $order = $_SESSION['backend']['filter'][$session_module]['order'];
		if (isset($_SESSION['backend']['filter'][$session_module]['lastorder'])) $lastorder = $_SESSION['backend']['filter'][$session_module]['lastorder'];
		if (isset($_SESSION['backend']['filter'][$session_module]['orderdir'])) $orderdir = $_SESSION['backend']['filter'][$session_module]['orderdir'];
	}
}
print "
<div class='container'>
	<form action='".$_SERVER['REQUEST_URI']."' method='post' enctype='multipart/form-data'>
		<div class='form'>
			<div class='form-head toggle toggled'>
				<i class='material-icons md-24'>filter_alt</i>".LABEL_FILTER."<i class='ico-toggle material-icons md-24'>expand_less</i>
			</div>
			<div class='form-content hidden' data-module='".$module."' data-backend-module='".$backend_module."'>
				<input type='hidden' name='csrf_token' value='".htmlspecialchars(csrf_token())."'>
				<input type='hidden' name='multi_update' value='0' />
				<input type='hidden' name='filter' value='1' />
				<input type='hidden' name='export' value='0' />
				<input type='hidden' name='order' value='".$order."' />
				<input type='hidden' name='lastorder' value='".$lastorder."' />
				<input type='hidden' name='orderdir' value='".$orderdir."' />";
				
if (isset($c_levels) && $c_levels){
	if (isset($_POST['parent_guidv4'])) {
		$parent_guidv4 = addslashes(trim(strip_tags(stripslashes($_POST['parent_guidv4']))));
		if ($session_module) $_SESSION['backend']['filter'][$session_module]['parent_guidv4'] = $parent_guidv4;
	} else {
		if (isset($_GET['parent_guidv4'])) $parent_guidv4 = addslashes(trim(strip_tags(stripslashes($_GET['parent_guidv4']))));
		else if (isset($_GET['guidv4'])) {
			$parent_guidv4 = addslashes(trim(strip_tags(stripslashes($_GET['guidv4']))));
			if ($session_module) {
				if (isset($_SESSION['backend']['filter'][$session_module]) && is_array($_SESSION['backend']['filter'][$session_module])) {
					if (array_key_exists('parent_guidv4', $_SESSION['backend']['filter'][$session_module])) $parent_guidv4 = $_SESSION['backend']['filter'][$session_module]['parent_guidv4'];
				}
			}
		} else {
			$parent_guidv4 = NULL;
			if ($session_module) {
				if (isset($_POST) && $_POST){
					$_SESSION['backend']['filter'][$session_module]['parent_guidv4'] = $parent_guidv4;
				}
				if (isset($_SESSION['backend']['filter'][$session_module]) && is_array($_SESSION['backend']['filter'][$session_module])) {
					if (array_key_exists('parent_guidv4', $_SESSION['backend']['filter'][$session_module])) $parent_guidv4 = $_SESSION['backend']['filter'][$session_module]['parent_guidv4'];
				}
			}
		}
	}
	if (isset($parent_guidv4) && $parent_guidv4){
		print "<input type='hidden' name='parent_guidv4' class='parent_guidv4' id='parent_guidv4' value='".$parent_guidv4."' />";
	}
}

if (isset($c_period) && $c_period) include('includes/filter/period.php');
include('includes/filter/language.php');

include('includes/filter/search.php');
				
print "<hr/>
				<div class='form-action'>
					<button type='submit' class='btn search' title='".LABEL_DO_FILTER."'><i class='material-icons md-24'>filter_alt</i>".LABEL_DO_FILTER."</button>
				</div><!-- /.form-action -->
				
			</div><!-- /.form-content -->
		</div><!-- /.form -->";
			
if (isset($c_period) && $c_period) {
	$datetime_from = DateTime::createFromFormat('Y-m-d', $datetime_from)->setTime(0, 0, 0);
	$datetime_to = DateTime::createFromFormat('Y-m-d', $datetime_to)->setTime(23, 59, 59);
	$datetime_from = $datetime_from->getTimestamp();
	$datetime_to = $datetime_to->getTimestamp();
}

$level_db = $module."_levels";
if (isset($level_module) && $level_module){
	$level_db = $level_module."_levels";
}

			
$sql = "SELECT module.*, GROUP_CONCAT(levels.guidv4) AS level FROM ".$module." AS module 
				LEFT JOIN ".$module."_levels_links AS levels ON module.id = levels.id_module ";
	
if (isset($c_period) && $c_period) {
	if ($datetime_to){
		$sql .= "WHERE datetime BETWEEN :datetime_from AND :datetime_to ";
	} else {
		$sql .= "WHERE datetime >= :datetime_from ";
	}
} else $sql .= "WHERE 1 ";

if (isset($c_levels) && $c_levels && $parent_guidv4){
	$sql .= "AND levels.guidv4 = :parent_guidv4 ";
} else {
	$sql .= "AND levels.guidv4 IS NULL ";
}
			
if ($language) $sql .= "AND language = :language ";

if ($search){
	$sql.="AND (
					file LIKE :search OR text LIKE :search OR datetime LIKE :search) ";
}

$sql .= "GROUP BY module.id ";

if ($order == 'datetime'){
	$sql .= "ORDER BY ".$order." ".$orderdir.", id DESC";
} else {
	$sql .= "ORDER BY ".$order." ".$orderdir;
}

$result = $conn->prepare($sql);
	
if (isset($c_period) && $c_period) {
	if ($datetime_to){
		$result->bindValue(':datetime_from', $datetime_from, PDO::PARAM_INT);
		$result->bindValue(':datetime_to', $datetime_to, PDO::PARAM_INT);
	} else {
		$result->bindValue(':datetime_from', $datetime_from, PDO::PARAM_INT);
	}
}
			
if (isset($c_levels) && $c_levels && $parent_guidv4){
	 $result->bindValue(':parent_guidv4', $parent_guidv4, PDO::PARAM_STR);
}

if ($language) $result->bindValue(':language', $language, PDO::PARAM_STR);

if ($search){
	$result->bindValue(':search', "%{$search}%", PDO::PARAM_STR);
}

$result->execute();

	
print "
		<div class='library-toolbar'>
			<div class='library-toolbar-path'>";

if (isset($c_levels) && $c_levels) library_print_breadcrumb($conn, $module, $parent_guidv4, (isset($language) && $language) ? $language : NULL);

print "
			</div><!-- /.library-toolbar-path -->
		</div><!-- /.library-toolbar -->
		
		<div class='table'>
			
			<div class='table-head'>
				<div class='table-row'>
					<div class='table-cell multiselect'>
						<input id='c_multicheck' class='c_multicheck' value='1' type='checkbox' name='c_multicheck'>
					</div><!-- /.table-cell -->
					<div class='table-cell size-small'><a href='#' class='order' data-order='datetime'>".LABEL_DATETIME;
if ($order == 'datetime') print "<i class='ico-toggle material-icons md-24 md-white ".$orderdir."'>expand_less</i>";
print "</a></div><!-- /.table-cell -->
					<div class='table-cell'><a href='#' class='order' data-order='file'>".LABEL_FILE;
if ($order == 'file') print "<i class='ico-toggle material-icons md-24 md-white ".$orderdir."'>expand_less</i>";
print "</a></div><!-- /.table-cell -->
					<div class='table-cell size-medium'><a href='#' class='order' data-order='filesize'>".LABEL_FILESIZE;
if ($order == 'filesize') print "<i class='ico-toggle material-icons md-24 md-white ".$orderdir."'>expand_less</i>";
print "</a></div><!-- /.table-cell -->
					<div class='table-cell size-medium'><a href='#' class='order' data-order='category'>".LABEL_CATEGORY;
if ($order == 'category') print "<i class='ico-toggle material-icons md-24 md-white ".$orderdir."'>expand_less</i>";
print "</a></div><!-- /.table-cell -->";

if (isset($c_languages) && $c_languages) {
	print "<div class='table-cell size-small'><a href='#' class='order' data-order='language'>".LABEL_LANGUAGE;
	if ($order == 'language') print "<i class='ico-toggle material-icons md-24 md-white ".$orderdir."'>expand_less</i>";
	print "</a></div><!-- /.table-cell -->";
}
print "
				</div><!-- /.table-row -->
			</div><!-- /.table-head -->
			<div class='table-body no-context'>";
			
if (isset($c_levels) && $c_levels){
	$library_visible_level_guids = array();

	if ($search && isset($c_levels) && $c_levels) {
	
		$tmp_language = (isset($language) && $language) ? $language : $_SESSION['backend']['language'];
	
		$search_fields = array('file', 'text', 'datetime');
		$field_conditions = array();
	
		foreach ($search_fields AS $field) {
			if (function_exists('check_column')) {
				if (!check_column($module, $field)) continue;
			}
	
			$field_conditions[] = "module.".$field." LIKE :search";
		}
	
		if (count($field_conditions)) {
	
			$sql_visible = "SELECT DISTINCT levels.guidv4 
											FROM ".$module." AS module
											INNER JOIN ".$module."_levels_links AS levels ON module.id = levels.id_module
											WHERE levels.guidv4 IS NOT NULL
											AND levels.guidv4 != ''
											AND (".implode(' OR ', $field_conditions).") ";
	
			if ($tmp_language) {
				$sql_visible .= "AND module.language = :language ";
			}
	
			if (isset($c_period) && $c_period) {
				if ($datetime_to) {
					$sql_visible .= "AND module.datetime BETWEEN :datetime_from AND :datetime_to ";
				} else {
					$sql_visible .= "AND module.datetime >= :datetime_from ";
				}
			}
	
			$result_visible = $conn->prepare($sql_visible);
			$result_visible->bindValue(':search', "%{$search}%", PDO::PARAM_STR);
	
			if ($tmp_language) {
				$result_visible->bindValue(':language', $tmp_language, PDO::PARAM_STR);
			}
	
			if (isset($c_period) && $c_period) {
				if ($datetime_to) {
					$result_visible->bindValue(':datetime_from', $datetime_from, PDO::PARAM_INT);
					$result_visible->bindValue(':datetime_to', $datetime_to, PDO::PARAM_INT);
				} else {
					$result_visible->bindValue(':datetime_from', $datetime_from, PDO::PARAM_INT);
				}
			}
	
			$result_visible->execute();
	
			if ($result_visible->rowCount()) {
				while ($arr_visible = $result_visible->fetch()) {
					$parents = get_parent_levels($module."_levels", $arr_visible['guidv4']);
	
					foreach ($parents AS $parent_guidv4_tmp) {
						$library_visible_level_guids[$parent_guidv4_tmp] = true;
					}
	
					$library_visible_level_guids[$arr_visible['guidv4']] = true;
				}
			}
		}
	
		$sql_visible = "SELECT guidv4 
										FROM ".$module."_levels 
										WHERE language = :language 
										AND title LIKE :search";
	
		$result_visible = $conn->prepare($sql_visible);
		$result_visible->bindValue(':language', $tmp_language, PDO::PARAM_STR);
		$result_visible->bindValue(':search', "%{$search}%", PDO::PARAM_STR);
		$result_visible->execute();
	
		if ($result_visible->rowCount()) {
			while ($arr_visible = $result_visible->fetch()) {
				$parents = get_parent_levels($module."_levels", $arr_visible['guidv4']);
	
				foreach ($parents AS $parent_guidv4_tmp) {
					$library_visible_level_guids[$parent_guidv4_tmp] = true;
				}
	
				$library_visible_level_guids[$arr_visible['guidv4']] = true;
			}
		}
	}
	
	if (isset($parent_guidv4) && $parent_guidv4){
		$sql_tmp = "SELECT parent_guidv4 FROM ".$module."_levels WHERE language = :language AND guidv4 = :guidv4 GROUP BY guidv4 ORDER BY title ASC LIMIT 1";
		$result_tmp = $conn->prepare($sql_tmp);
		if (isset($language) && $language) $result_tmp->bindValue(':language', $language, PDO::PARAM_STR);
		else $result_tmp->bindValue(':language', $_SESSION['backend']['language'], PDO::PARAM_STR);
		$result_tmp->bindValue(':guidv4', $parent_guidv4, PDO::PARAM_STR);
		$result_tmp->execute();
		if ($result_tmp->rowCount()){		
			$arr_tmp = $result_tmp->fetch();
			print "
				<div class='table-row change_folder' data-folder='".$arr_tmp['parent_guidv4']."'>
					<div class='table-cell'></div><!-- /.table-cell -->
					<div class='table-cell'></div><!-- /.table-cell -->
					<div class='table-cell icon strong'><i class='material-icons out md-40'>folder</i>".LABEL_BACK."</div><!-- /.table-cell -->
					<div class='table-cell'></div><!-- /.table-cell -->
					<div class='table-cell'></div><!-- /.table-cell -->";
			if (isset($c_languages) && $c_languages) print "<div class='table-cell'></div><!-- /.table-cell -->";
			print "
				</div><!-- /.table-row -->";
		}
	}
	
	$sql_tmp = "SELECT * FROM ".$module."_levels WHERE 
							language = :language ";
	if (isset($parent_guidv4) && $parent_guidv4) $sql_tmp .= "AND parent_guidv4 = :parent_guidv4 ";
	else $sql_tmp .= "AND (parent_guidv4 IS NULL OR parent_guidv4 = '') ";
	$sql_tmp .= "GROUP BY guidv4 ORDER BY title ASC";
	$result_tmp = $conn->prepare($sql_tmp);
	if (isset($language) && $language) $result_tmp->bindValue(':language', $language, PDO::PARAM_STR);
	else $result_tmp->bindValue(':language', $_SESSION['backend']['language'], PDO::PARAM_STR);
	if (isset($parent_guidv4) && $parent_guidv4) $result_tmp->bindValue(':parent_guidv4', $parent_guidv4, PDO::PARAM_STR);
	$result_tmp->execute();
	if ($result_tmp->rowCount()){		
		while($arr_tmp = $result_tmp->fetch()){
			if ($search && !isset($library_visible_level_guids[$arr_tmp['guidv4']])) continue;
			
			$datetime = date("d.m.Y - H:i", $arr_tmp['datetime']);

			if (isset($c_languages) && $c_languages){
				$tmp_language = NULL;
				$sql_sub = "SELECT label FROM languages WHERE iso = :iso LIMIT 1";
				$result_sub = $conn->prepare($sql_sub);
				$result_sub->bindValue(':iso', $arr_tmp['language'], PDO::PARAM_STR);
				$result_sub->execute();
				if ($result_sub->rowCount()){
					$arr_sub = $result_sub->fetch();
					$tmp_language = id($arr_sub['label']);
				}
			}
			
			print "
				<div class='table-row change_folder' data-folder='".$arr_tmp['guidv4']."' data-id='".$arr_tmp['id']."' data-module='".$module."_levels' data-backend-module='".$backend_module."'>
					<div class='table-cell'></div><!-- /.table-cell -->
					<div class='table-cell'>".$datetime."</div><!-- /.table-cell -->
					<div class='table-cell icon strong'><i class='material-icons out md-40'>folder</i>".$arr_tmp['title']."</div><!-- /.table-cell -->
					<div class='table-cell'></div><!-- /.table-cell -->
					<div class='table-cell'></div><!-- /.table-cell -->";
			if (isset($c_languages) && $c_languages) print "<div class='table-cell'>".$tmp_language."</div><!-- /.table-cell -->";
			print "
				</div><!-- /.table-row -->";
		}
	}
}

if ($result->rowCount()){
		
	while ($arr = $result->fetch()){
		$datetime = date("d.m.Y - H:i", $arr['datetime']);
		$filesize = formatSizeUnits($arr['filesize']);
		
		if (isset($c_languages) && $c_languages){
			$tmp_language = NULL;
			$sql_tmp = "SELECT label FROM languages WHERE iso = :iso LIMIT 1";
			$result_tmp = $conn->prepare($sql_tmp);
			$result_tmp->bindValue(':iso', $arr['language'], PDO::PARAM_STR);
			$result_tmp->execute();
			if ($result_tmp->rowCount()){
				$arr_tmp = $result_tmp->fetch();
				$tmp_language = id($arr_tmp['label']);
			}
		}
		print "
				<div class='table-row' data-id='".$arr['id']."'>";
		if ($arr['text']) print "<input type='hidden' name='ajax_data[".$arr['id']."][text]' value='".$arr['text']."' />";
		print "
					<div class='table-cell multiselect'>
						<input id='c_check_".$arr['id']."' class='multicheck c_check_".$arr['id']."' value='1' type='checkbox' name='c_check[".$arr['id']."]'>
					</div><!-- /.table-cell -->
					<div class='table-cell'>".$datetime."</div><!-- /.table-cell -->
					<div class='table-cell icon'><i class='material-icons out md-40'>insert_drive_file</i>".$arr['file']."</div><!-- /.table-cell -->
					<div class='table-cell'>".$arr['category']."</div><!-- /.table-cell -->
					<div class='table-cell'>".$filesize."</div><!-- /.table-cell -->";
		if (isset($c_languages) && $c_languages) print "<div class='table-cell'>".$tmp_language."</div><!-- /.table-cell -->";
		print "
				</div><!-- /.table-row -->";
		
	}
}

print "
		</div><!-- /.table -->
	</form>
	<div class='button absolute'>
		<a href='#' class='btn select'><i class='material-icons md-24'>add</i>".LABEL_USE_FILES."</a>
	</div><!-- /.button -->
</div><!-- /.container -->";