<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright EB Dynamics, Inc., Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 1/5/2016	ELL

include ("check_session.php");

$images = $_REQUEST['images'];
$titles = $_REQUEST['titles'];

$used_sort = array();
foreach ($images as $sort_order => $image)
{
	while (in_array($sort_order, $used_sort))	// Must prevent same sort order used for more than one image.
	{
		$sort_order = time();
	}
	$used_sort[] = $sort_order;

	$query = "SELECT * FROM `gallery_titles` WHERE `image` = '" . $image . "'";
	$result = $bwb_dbh->query($query);
	if ($result->rowCount() > 0)
		$query = "UPDATE `gallery_titles` SET `sort_order` = '" . $sort_order . "' WHERE `image` = '" . $image . "'";
	else	{
		$titles[$sort_order] = str_replace("'", "&#39;", $titles[$sort_order]);
		$titles[$sort_order] = str_replace('"', "&#34;", $titles[$sort_order]);
		$query = "INSERT INTO `gallery_titles` SET `image` = '" . $image . "', `sort_order` = '" . $sort_order . "', `title`  = '" . $titles[$sort_order] . "'";
	}
	
	$cnt = $bwb_dbh->exec($query);
}

print $cnt;
?>
