Location: PHPKode > projects > Kwalbum > kwalbum/include/funcAppendPeople.php
<?php
// 2.0

function AppendPeople($itemId, $newPeople)
{
	global $DB, $P, $errorMsg;

	$query = 'SELECT ItemId, 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>';
		return;
	}
	$row = $result->fetch_array();
	if (!USER_CAN_ADD or (USER_ID != $row[1] and !USER_IS_ADMIN))
	{
		if (TEST_MODE)
			echo '<p class="error">Invalid Permissions</p>';
		return;
	}

	// save new people
	$people = explode(',', $newPeople);
	foreach ($people as $person)
	{
		$person = $DB->FilterString(trim($person));
		if (!empty($person))
		{
			$alreadyAdded = false;	// to prevent duplicates being added
			$query = 'SELECT PeopleId FROM ' . PEOPLE_TABLE .
			" WHERE PeopleName='$person'" .
			" LIMIT 1";
			$result = $DB->Query($query);
			if (!$row = $result->fetch_array())
			{
				$insertData = array (
					'PeopleName' => $person,
					'PeopleCount' => 1
				);
				$peopleId = $DB->Insert(PEOPLE_TABLE, $insertData);
				if (false === $peopleId)
				{
					if (TEST_MODE)
						echo '<p class="error">Error inserting into people table.</p>';
					return false;
				}
			} else
			{
				$peopleId = $row[0];
				$query = 'SELECT PeopleIdFk FROM ' . PEOPLE_MAP_TABLE .
				" WHERE PeopleIdFk=$peopleId AND ItemIdFk=$itemId" .
				" LIMIT 1";
				$result = $DB->Query($query);
				if (1 == $result->num_rows)
					$alreadyAdded = true;
				else
					$DB->UpdateSingle(PEOPLE_TABLE, 'PeopleCount', 'PeopleCount+1', "PeopleId=$peopleId");
			}
			if (false == $alreadyAdded)
			{
				$insertData = array (
					'ItemIdFk' => $itemId,
					'PeopleIdFk' => $peopleId
				);
				$DB->Insert(PEOPLE_MAP_TABLE, $insertData);
			}
		}
	}
}
?>
Return current item: Kwalbum