<?php
function library_print_breadcrumb($conn, $module, $parent_guidv4 = NULL, $language = NULL) {
	$items = array();
	$current_guidv4 = $parent_guidv4;
	$visited = array();
	$depth = 0;

	if (!$language && isset($_SESSION['backend']['language'])) {
		$language = $_SESSION['backend']['language'];
	}

	while ($current_guidv4 && !isset($visited[$current_guidv4]) && $depth < 50) {
		$visited[$current_guidv4] = true;

		$sql = "SELECT guidv4, parent_guidv4, title FROM ".$module."_levels WHERE guidv4 = :guidv4 ";
		if ($language) $sql .= "AND language = :language ";
		$sql .= "GROUP BY guidv4 LIMIT 1";

		$result = $conn->prepare($sql);
		$result->bindValue(':guidv4', $current_guidv4, PDO::PARAM_STR);
		if ($language) $result->bindValue(':language', $language, PDO::PARAM_STR);
		$result->execute();

		if (!$result->rowCount()) break;

		$arr = $result->fetch();

		array_unshift($items, array(
			'guidv4' 				=> $arr['guidv4'],
			'parent_guidv4' => $arr['parent_guidv4'],
			'title' 				=> $arr['title']
		));

		$current_guidv4 = $arr['parent_guidv4'];
		$depth++;
	}
	
	print "
	<div class='library-breadcrumb' aria-label='Breadcrumb'>
		<a href='#' class='change_folder library-breadcrumb-item' data-folder=''><i class='material-icons md-24'>folder_open</i>".LABEL_ROOT."</a>";

	foreach ($items AS $key => $item) {
		print "
		<i class='material-icons md-24 library-breadcrumb-separator'>chevron_right</i>
		<a href='#' class='change_folder library-breadcrumb-item".($key == (count($items) - 1) ? " current" : "")."' data-folder='".$item['guidv4']."'>".$item['title']."</a>";
	}

	print "
	</div><!-- /.library-breadcrumb -->";
}