<?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");

$image = $_REQUEST['image'];
$title = $_REQUEST['title'];
$sort_order = $_REQUEST['sort_order'];

$title = str_replace("'", "&#39;", $title);
$title = str_replace('"', "&#34;", $title);

$query = "SELECT * FROM `gallery_titles` WHERE (`image` <> '" . $image . "') AND (`sort_order` = '" . $sort_order . "')";
$result = $bwb_dbh->query($query);
if ($result->rowCount() < 1)
{
	$query = "SELECT * FROM `gallery_titles` WHERE `image` = '" . $image . "'";
	$result = $bwb_dbh->query($query);
	if ($result->rowCount() > 0)
		$query = "UPDATE `gallery_titles` SET `title` = '" . $title . "', `sort_order` = '" . $sort_order . "' WHERE `image` = '" . $image . "'";
	else
		$query = "INSERT INTO `gallery_titles` SET `image` = '" . $image . "', `title` = '" . $title . "', `sort_order` = '" . $sort_order . "'";
}
else	// Some how sort order got mucked up and matches sort of some other image.
{
	$query = "SELECT * FROM `gallery_titles` WHERE `image` = '" . $image . "'";
	$result = $bwb_dbh->query($query);
	if ($result->rowCount() > 0)
		$query = "UPDATE `gallery_titles` SET `title` = '" . $title . "' WHERE `image` = '" . $image . "'";
	else
		$query = "INSERT INTO `gallery_titles` SET `image` = '" . $image . "', `title` = '" . $title . "'";
}

$cnt = $bwb_dbh->exec($query);

print $cnt;
?>
