<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 1/13/2016	ELL
// Revised: 5/16/2017	ELL
// Modified to use thumb for thumb display if exists to speed console loading and reduce band width.
// 
// 
// 
	include ("check_session.php");
	
	$files = array();	
	$doc_root = getenv('DOCUMENT_ROOT');
	if (BWB_BASE_DIR == '')
		$userfiles = '';
	else
		$userfiles = "/" . BWB_BASE_DIR;
	$orig_image_path = $doc_root . $userfiles . "/" . BWB_IMAGE_DIR . "/gallery/orig";
	$orig_image_path_rel = $userfiles . "/" . BWB_IMAGE_DIR . "/gallery/orig/";
	$tn_image_path = $doc_root . $userfiles . "/" . BWB_IMAGE_DIR .  "/gallery/tn/";
	$srch = array(".jpg", ".JPG", ".Jpg", ".gif", ".GIF", ".Gif", ".png", ".PNG", ".Png", ".jpeg", ".JPEG", ".Jpeg");
	if ($handle = opendir("$orig_image_path")) 
	{
		while (false !== ($file = readdir($handle)))
		{
			if (($file != ".") && ($file != "..") && !is_dir($orig_image_path_rel . $file))	{
				$file_parts = explode(".", $file);
				$ext = "." . $file_parts[count($file_parts) -1];
				if (in_array($ext, $srch))	{
					if (file_exists($tn_image_path . $file))
						$tn = 1;
					else
						$tn = 0;
					$files[$file] = $tn;
				}
			}
		}
		closedir($handle);
	}
	
	ksort($files);
	
	$files_arr = array();
	$i = 1000000;
	foreach ($files as $file => $tn)
	{
		$query = "SELECT * FROM gallery_titles WHERE image = '" . $file . "'";
		$result = $bwb_dbh->query($query);
		if ($result->rowCount() > 0)	{
			$row = $result->fetch(PDO::FETCH_ASSOC);
			$title = $row['title'];
			$sort_order = $row['sort_order'];
		}
		else	{
			$title = "";
			$sort_order = $i;
		}
		
		$files_arr[$sort_order] = array("file" => $file, "title" => $title, "sort_order" => $sort_order, "tn" => $tn);
		$i++;
	}
	
	ksort($files_arr);
		
	$delim = "";
	foreach ($files_arr as $file_arr)	{
		print $delim . json_encode($file_arr);
		$delim = "@@@@@";
	}
?>