<?php
if (isset($c_templates) && $c_templates){
	$templates_options_data = array();
	$sql_tmp = "SELECT id, label FROM content_templates ";
	$result_tmp = $conn->prepare($sql_tmp);
	$result_tmp->execute();
	if ($result_tmp->rowCount()){
		while ($arr_tmp = $result_tmp->fetch()){
			$templates_options_data[$arr_tmp['id']]['title'] = id($arr_tmp['label']);
		}
	}
}

if (isset($c_languages) && $c_languages){
	$languages_options_data = array();
	$sql_tmp = "SELECT iso, label FROM languages ";
	$result_tmp = $conn->prepare($sql_tmp);
	$result_tmp->execute();
	if ($result_tmp->rowCount()){
		while ($arr_tmp = $result_tmp->fetch()){
			$languages_options_data[$arr_tmp['iso']]['title'] = id($arr_tmp['label']);
		}
	}
}


if (isset($c_levels) && $c_levels){
	$levels_options_data = array();
	$sql_tmp = "SELECT language, guidv4, title, parent_guidv4 FROM ".$level_module."_levels ORDER BY sortorder ASC";
	$result_tmp = $conn->prepare($sql_tmp);
	$result_tmp->execute();
	
	if ($result_tmp->rowCount()){
		while($arr_tmp = $result_tmp->fetch()){
			$levels_options_data[$arr_tmp['language']][] = $arr_tmp;
		}
	}
}

if ($sub_page == 1){	
	print "
<div class='container'>
	<form action='".$_SERVER['REQUEST_URI']."' method='post' enctype='multipart/form-data'>
		<div class='form'>
			<div class='form-head toggle ";
	if (isset($_SESSION['backend']['toggle_filter']) && $_SESSION['backend']['toggle_filter']) print'toggled';
	print "'>
				<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 ";
	if (isset($_SESSION['backend']['toggle_filter'])) print $_SESSION['backend']['toggle_filter'];
	print "' data-module='".$module."' data-backend-module='".$backend_module."'>
				<input type='hidden' name='csrf_token' value='".htmlspecialchars(csrf_token())."'>
				<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."' />";
				
	include('includes/function/c_active.php');
	include('includes/function/c_delete.php');
	
	
	if (isset($c_period) && $c_period) include('includes/filter/period.php');
	include('includes/filter/sortorder.php');
	include('includes/filter/language.php');
	include('filter/template.php');
	
	include('includes/filter/level.php');
	
	include('includes/filter/search.php');
					
	print "<hr/>";
	
	include('includes/filter/c_options.php');
	print "
				<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();
	}
	
	
	if (isset($c_levels) && $c_levels && $parent_guidv4){
		$children_data = array();
		$children_data[] = $parent_guidv4;
		$children_data = get_children_levels($children_data, $level_module."_levels", $parent_guidv4);
	}
				
	$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 ($sortorder_from AND $sortorder_to){
		$sql .= "AND sortorder BETWEEN :sortorder_from AND :sortorder_to ";
	}
	
	if (isset($children_data) && $children_data) {
		$children_data = "'".implode( "', '", $children_data)."'";
		$sql .= "AND levels.guidv4 IN ($children_data) ";
	}
				
	if ($id_template) $sql .= "AND id_template = :id_template ";
	if ($language) $sql .= "AND language = :language ";
	
	if (($c_active_1 AND $c_active_0) OR (!$c_active_1 AND !$c_active_0)){
	} else {
		$sql .= "AND ( ";
		if ($c_active_1 == 1) $sql .= "c_active = '1' ";
		if ($c_active_0 == 1) $sql .= "c_active = '0' ";
		$sql .= ") ";
	}
	
	if ($search){
		$sql.="AND (
						title LIKE :search OR teaser LIKE :search OR 
						content LIKE :search OR addition LIKE :search OR 
						link LIKE :search OR linktext 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 ($sortorder_from AND $sortorder_to){
		$result->bindValue(':sortorder_from', $sortorder_from, PDO::PARAM_INT);
		$result->bindValue(':sortorder_to', $sortorder_to, PDO::PARAM_INT);
	}
				
	if ($id_template)  $result->bindValue(':id_template', $id_template, PDO::PARAM_INT);
	if ($language) $result->bindValue(':language', $language, PDO::PARAM_STR);
	
	if ($search){
		$result->bindValue(':search', "%{$search}%", PDO::PARAM_STR);
	}
	
	$result->execute();
		
	$count = $result->rowCount();
	
	$_SESSION['cmt'][$backend_module][$module]['id_list'] = array();
	$_SESSION['cmt'][$backend_module][$module]['pages'] = 1;
	if (isset($admin_settings['module'][$module.'_filter'])) $_SESSION['cmt'][$backend_module][$module]['pages'] = ceil($count / $admin_settings['module'][$module.'_filter']);

	print "
		<div class='button'>
			<div class='multicheck_control'>
				<a href='#' class='multi_activate' title='".LABEL_MULTI_ACTIVATE."'><i class='material-icons md-24'>toggle_on</i></a>
				<a href='#' class='multi_deactivate' title='".LABEL_MULTI_DEACTIVATE."'><i class='material-icons md-24 md-grey'>toggle_off</i></a>
				<a href='#' class='multi_delete' title='".LABEL_MULTI_DELETE."'><i class='material-icons md-24'>delete_forever</i></a>
				<a href='#' class='multi_edit' title='".LABEL_MULTI_EDIT."' data-module='".$module."' data-backend-module='".$backend_module."'><i class='material-icons md-24'>edit</i></a>
			</div><!-- /.multicheck_control -->
			<a href='#' class='btn all_quick' data-module='".$module."' data-backend-module='".$backend_module."'><i class='material-icons md-24'>bolt</i>".LABEL_QUICK_EDIT."</a>
			<a href='#' class='btn add' data-module='".$module."' data-backend-module='".$backend_module."'><i class='material-icons md-24'>add</i>".LABEL_NEW_ENTRY."</a>
		</div><!-- /.button -->
	
		<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='title'>".LABEL_TITLE;
	if ($order == 'title') print "<i class='ico-toggle material-icons md-24 md-white ".$orderdir."'>expand_less</i>";
	print "</a></div><!-- /.table-cell -->";
	if (isset($c_levels) && $c_levels) {
		print "<div class='table-cell size-large'><a href='#' class='order' data-order='level'>".LABEL_LEVEL;
		if ($order == 'level') print "<i class='ico-toggle material-icons md-24 md-white ".$orderdir."'>expand_less</i>";
		print "</a></div><!-- /.table-cell -->";
	}
	if (isset($c_templates) && $c_templates) {
		print "<div class='table-cell size-medium'><a href='#' class='order' data-order='id_template'>".LABEL_TEMPLATE;
		if ($order == 'id_template') 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 class='table-cell size-xsmall'><a href='#' class='order' data-order='sortorder'>".LABEL_SORTORDER;
	if ($order == 'sortorder') 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-xxsmall'><a href='#' class='order' data-order='c_active'><i class='material-icons md-24 md-white'>toggle_on</i>";
	if ($order == 'c_active') print "<i class='ico-toggle material-icons md-24 md-white ".$orderdir."'>expand_less</i>";
	print "</a></div><!-- /.table-cell -->
					<div class='table-cell toolbar'></div><!-- /.table-cell -->
				</div><!-- /.table-row -->
			</div><!-- /.table-head -->";
			
	if ($count){
		print "
			<div class='table-body ";
		if (!$_SESSION['backend']['settings']['context']) print "no-context";
		print "'>";
		while($arr = $result->fetch()){
			$_SESSION['cmt'][$backend_module][$module]['id_list'][$arr['id']] = $arr['id'];
		}
	}
}

if (isset($_SESSION['cmt'][$backend_module][$module]['id_list']) && count($_SESSION['cmt'][$backend_module][$module]['id_list']) >= 1){
	
	$q_array = $_SESSION['cmt'][$backend_module][$module]['id_list'];
	if (isset($admin_settings['module'][$module.'_filter']) && count($_SESSION['cmt'][$backend_module][$module]['id_list']) > $admin_settings['module'][$module.'_filter']) {
		$q_array = array_slice($_SESSION['cmt'][$backend_module][$module]['id_list'], 0, $admin_settings['module'][$module.'_filter'], true);
	}
	
	$q_array = join(",", $q_array);
	
	
	$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 
					WHERE module.id IN ($q_array) GROUP BY module.id ORDER BY FIELD (module.id, $q_array) ";
	if (isset($admin_settings['module'][$module.'_filter'])) $sql.= "LIMIT :limit ";
	$result = $conn->prepare($sql);
	if (isset($admin_settings['module'][$module.'_filter'])) $result->bindValue(':limit', $admin_settings['module'][$module.'_filter'], PDO::PARAM_INT);
	$result->execute();
	
	if ($result->rowCount()){
		while ($arr = $result->fetch()){
			unset($_SESSION['cmt'][$backend_module][$module]['id_list'][$arr['id']]);
			$datetime = date("d.m.Y - H:i", $arr['datetime']);
	
			if (isset($c_templates) && $c_templates){
				$template = NULL;
				$sql_tmp = "SELECT label FROM content_templates WHERE id = :id LIMIT 1";
				$result_tmp = $conn->prepare($sql_tmp);
				$result_tmp->bindValue(':id', $arr['id_template'], PDO::PARAM_INT);
				$result_tmp->execute();
				if ($result_tmp->rowCount()){
					$arr_tmp = $result_tmp->fetch();
					$template = id($arr_tmp['label']);
				}
			}
	
			if (isset($c_levels) && $c_levels){
				$level = NULL;
				if ($arr['level']){
					$levels = explode(',', $arr['level']);
					foreach ($levels AS $key){
						$sql_tmp = "SELECT title FROM ".$level_module."_levels WHERE guidv4 = :guidv4 ";
						if (isset($c_languages) && $c_languages){
							$sql_tmp .= "AND language = :language ";
						}
						$sql_tmp .= "LIMIT 1";
						$result_tmp = $conn->prepare($sql_tmp);
						$result_tmp->bindValue(':guidv4', $key, PDO::PARAM_STR);
						if (isset($c_languages) && $c_languages){
							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->execute();
						if ($result_tmp->rowCount()){
							$arr_tmp = $result_tmp->fetch();
							if ($level !== NULL) $level .= ", ".$arr_tmp['title'];
							else $level = $arr_tmp['title'];
						}
					}
				}
			}
			
	
			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 ";
			if (!$arr['c_active']) print "inactive";
			print "' data-id='".$arr['id']."' data-module='".$module."' data-backend-module='".$backend_module."'>
					<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 -->";
		
			$quick_data = array();
			$quick_data['name'] = 'title';
			$quick_data['value'] = $arr['title'];
			include('includes/filter/quick_input.php');
						
			if (isset($c_levels) && $c_levels){
				if (isset($c_levels_multi) && $c_levels_multi){
					print "<div class='table-cell'>".$level."</div><!-- /.table-cell -->";
				} else {
					$quick_data = array();
					$quick_data['name'] = 'parent_guidv4';
					$quick_data['class'] = 'quickselect'.$arr['id'];
					$quick_data['value'] = $level;
					if (isset($levels_options_data[$arr['language']])) $quick_data['data'] = $levels_options_data[$arr['language']];
					if (isset($levels) && is_array($levels)) $quick_data['selected'] = $levels[0];
					else $quick_data['selected'] = NULL;
					include('includes/filter/quick_level.php');
				}
			}
			
			if (isset($c_templates) && $c_templates) {
				$quick_data = array();
				$quick_data['name'] = 'id_template';
				$quick_data['value'] = $template;
				$quick_data['data'] = $templates_options_data;
				$quick_data['selected'] = $arr['id_template'];
				include('includes/filter/quick_select.php');
			}
			
			if (isset($c_languages) && $c_languages) {
				$quick_data = array();
				$quick_data['name'] = 'iso';
				$quick_data['value'] = $tmp_language;
				$quick_data['data'] = $languages_options_data;
				$quick_data['selected'] = $arr['language'];
				include('includes/filter/quick_select.php');		
			}
			
			$quick_data = array();
			$quick_data['name'] = 'sortorder';
			$quick_data['css'] = 'alignright';
			$quick_data['value'] = $arr['sortorder'];
			include('includes/filter/quick_input.php');
			
			print "
					<div class='table-cell aligncenter c_active'>
						<a href='#' id='".$arr['id']."' class='activate' title='";
			if ($arr['c_active'] == 1) print LABEL_DEACTIVATE;
			else print LABEL_ACTIVATE;
			print "'>";
			if ($arr['c_active'] == 1) print "<i class='material-icons md-24'>toggle_on</i>";
			else print "<i class='material-icons md-24 md-grey'>toggle_off</i>";
			print "</a></div><!-- /.table-cell -->
					<div class='table-cell toolbar'>
						<a href='#' class='edit' data-id='".$arr['id']."' data-module='".$module."' data-backend-module='".$backend_module."' title='".LABEL_EDIT."'><i class='material-icons md-22'>edit</i></a>
						<a href='#' class='copy' data-id='".$arr['id']."' data-module='".$module."' data-backend-module='".$backend_module."' title='".LABEL_COPY."'><i class='material-icons md-22'>content_copy</i></a>
						<a href='#' class='delete' data-id='".$arr['id']."' title='".LABEL_DELETE."'><i class='material-icons md-22'>delete_forever</i></a>
					</div><!-- /.table-cell -->
				</div><!-- /.table-row -->";
		}
		
		if (ceil($_SESSION['cmt'][$backend_module][$module]['pages']) != $sub_page){
			$next_page = $sub_page+1;
			print "<input type='hidden' name='sub_page' id='sub_page' value='".$next_page."' />";
		}
		
		if ($sub_page == 1){
			print "
			</div><!-- /.table-body -->
		</div><!-- /.table -->";
		}
	}
}

if ($sub_page == 1){
	print "
	</form>
</div><!-- /.container -->
<div class='context-menu'>
 <ul>
    <li><a href='#' class='edit' title='".LABEL_EDIT."'><i class='material-icons md-24'>edit</i>".LABEL_EDIT."</a></li>
    <li><a href='#' class='copy' title='".LABEL_COPY."'><i class='material-icons md-24'>content_copy</i>".LABEL_COPY."</a></li>
    <li><a href='#' class='delete' title='".LABEL_DELETE."'><i class='material-icons md-24'>delete_forever</i>".LABEL_DELETE."</a></li>
 </ul>
</div>";
}