<?php
$sql = "SELECT addition FROM content_levels WHERE guidv4 = :guidv4 AND language = :language LIMIT 1";
$result = $conn->prepare($sql);
if (isset($content_data[0]) && $content_data[0]) $result->bindValue(':guidv4', $content_data[0], PDO::PARAM_STR);
else $result->bindValue(':guidv4', $content_guidv4, PDO::PARAM_STR);
$result->bindValue(':language', $language, PDO::PARAM_STR);
$result->execute();
if ($result->rowCount()){
	$arr = $result->fetch();
}

if (isset($id) && $id && isset($content) && $content && is_file('content_id_'.$content.'.php')){
	include('content_id_'.$content.'.php');
} else {

	$z = 0;
	$zz = 0;

	$last_group_key = NULL;

	$last_group_end = NULL;
	$last_row_end = NULL;

	$group_open = false;
	$row_open = false;
	$row_counter = 0;
	
	$sql = "SELECT content.*,
					content_templates.path AS template_path,
					content_templates.group_key AS template_group_key,
					content_templates.group_start AS template_group_start,
					content_templates.group_end AS template_group_end,
					content_templates.row_start AS template_row_start,
					content_templates.row_end AS template_row_end,
					content_templates.row_limit AS template_row_limit,
					content_templates.row_count AS template_row_count 
					FROM content AS content 
					LEFT JOIN content_templates AS content_templates ON content_templates.id = content.id_template
					LEFT JOIN content_levels_links AS levels_links ON levels_links.id_module = content.id 
					WHERE levels_links.guidv4 = :guidv4 
					AND content.language = :language 
					AND content.c_active = '1' 
					GROUP BY content.id 
					ORDER BY content.sortorder ASC";
	
	$result = $conn->prepare($sql);
	$result->bindValue(':guidv4', $content_guidv4, PDO::PARAM_STR);
	$result->bindValue(':language', $language, PDO::PARAM_STR);
	$result->execute();
	$zz = $result->rowCount();
	
	if ($zz){
	
		while($arr = $result->fetch()){
			$z++;
			
			$current_id_template = (int) $arr['id_template'];
			
			$template_group_key = trim((string) ($arr['template_group_key'] ?? ''));
			
			if ($template_group_key !== '') {
				$current_group_key = $template_group_key;
			} else {
				$current_group_key = 'template_'.$current_id_template;
			}
			
			$group_start = (string) ($arr['template_group_start'] ?? '');
			$group_end = (string) ($arr['template_group_end'] ?? '');

			$row_start = (string) ($arr['template_row_start'] ?? '');
			$row_end = (string) ($arr['template_row_end'] ?? '');
			$row_limit = (int) ($arr['template_row_limit'] ?? 0);
			$row_count = (int) ($arr['template_row_count'] ?? 1);
			if ($row_count <= 0) $row_count = 1;

			$has_group = ($group_start !== '' && $group_end !== '');
			$has_row = ($row_start !== '' && $row_end !== '');

			if ($last_group_key !== NULL && $last_group_key !== $current_group_key) {
				if ($row_open) {
					print $last_row_end;
					$row_open = false;
				}

				if ($group_open) {
					print $last_group_end;
					$group_open = false;
				}

				$row_counter = 0;
				$last_row_end = NULL;
				$last_group_end = NULL;
			}
			
			if ($has_group && !$group_open) {
				print $group_start;

				$group_open = true;
				$last_group_end = $group_end;
			}
			
			if ($has_row &&	$row_open && $row_limit > 0 && $row_counter > 0 &&	($row_counter + $row_count) > $row_limit) {
				print $last_row_end;

				$row_open = false;
				$row_counter = 0;
			}
			
			if ($has_row && !$row_open) {
				print $row_start;

				$row_open = true;
				$last_row_end = $row_end;
			}
						
			$template_path = template_resolve_path($arr['template_path'] ?? NULL);

			if ($template_path) {
				include($template_path);
			}
						
			if ($has_row) {
				$row_counter += $row_count;

				if ($row_limit > 0 && $row_counter >= $row_limit) {
					print $row_end;

					$row_open = false;
					$row_counter = 0;
				}
			}
			
			$last_group_key = $current_group_key;
			
			if ($zz == $z) {
				if ($row_open) {
					print $last_row_end;
					$row_open = false;
				}

				if ($group_open) {
					print $last_group_end;
					$group_open = false;
				}
			}
			
		}
	}
}