Location: PHPKode > projects > Kwalbum > kwalbum/include/funcChangeLocation.php
<?php
// 2.0
function ChangeLocation($itemId, $newLocation, $echo = true)
{
	global $DB, $P, $errorMsg;
	$newLocation = trim($newLocation);
	if (empty($newLocation))
		$newLocation = 'unknown location';

	$query = 'SELECT ItemLocationIdFk, ItemUserIdFk FROM ' . ITEM_TABLE .
	" WHERE ItemId='$itemId'" .
	" LIMIT 1";
	$result = $DB->Query($query);
	if (0 == $result->num_rows)
	{
		if (TEST_MODE)
			echo '<p class="error">Item ID ('.$itemId.') was not found in the DB.</p>';
		else if ($echo)
			echo $errorMsg;
		return;
	}
	$row = $result->fetch_array();
	if (USER_ID != $row[1] and !USER_IS_ADMIN)
	{
		if (TEST_MODE)
			echo '<p class="error">Invalid Permissions</p>';
		else if ($echo)
			echo $errorMsg;
		return;
	}
	$oldLocationId = $row[0];

	// reduce old location count
	$DB->UpdateSingle(LOCATION_TABLE, 'LocationCount', 'LocationCount-1', "LocationId=$oldLocationId");

	// save new location
	$query = 'SELECT LocationId FROM ' . LOCATION_TABLE .
	" WHERE Location='$newLocation'" .
	" LIMIT 1";
	$result = $DB->Query($query);
	if (0 == $result->num_rows)
	{
		$insertData = array (
			'Location' => $newLocation,
			'LocationCount' => 1
		);
		$locationId = $DB->Insert(LOCATION_TABLE, $insertData);
		if (false === $locationId)
		{
			if ($echo)
				echo '<p class="error">Error inserting into location table.</p>';
			return false;
		}
	} else
	{
		$row = $result->fetch_array();
		$locationId = $row[0];
		$DB->UpdateSingle(LOCATION_TABLE, 'LocationCount', 'LocationCount+1', "LocationId=$locationId");
	}

	$DB->UpdateSingle(ITEM_TABLE, 'ItemLocationIdFk', $locationId, "ItemId=$itemId");

	if ($echo)
		echo $newLocation;
}
?>
Return current item: Kwalbum