Location: PHPKode > projects > Kwalbum > kwalbum/pages/AdminDeleteItemsPage.php
<?php
/*
 * Created on Sep 23, 2008
 * 2.0
 */
require_once 'include/IPage.php';

class AdminDeleteItemsPage implements IPage
{
	private $items;

	function GetHead(& $title)
	{
		if (false == USER_IS_ADMIN)
			return;
		global $DB, $URLP;

		$query = 'SELECT ItemId, ItemPath, ItemFilename, ItemTypeId, ItemDate, ItemTime, ItemDescription, Location FROM '.ITEM_TABLE.', '.LOCATION_TABLE.
			" WHERE ItemIsHidden='5' AND LocationId=ItemLocationIdFk" .
			" LIMIT 50";
		$result = $DB->Query($query);
		if (0 == $result->num_rows)
			return;

		while ($row = $result->fetch_array(MYSQLI_ASSOC))
		{
			$this->items[$row['ItemId']] = $row;
		}

		$title = 'Permanently Delete Items';
		return '';
	}

	function ShowBody()
	{
		if (false == USER_IS_ADMIN)
			return;
		global $URLP;
		require 'AdminDeleteItemsPage.html.php';
	}

	private function DeleteFile($itemId, $path, $filename, $type)
	{
		global $DB;
		$query = 'SELECT ItemId FROM ' . ITEM_TABLE .
			" WHERE ItemId='$itemId'" .
			" LIMIT 1";
		$result = $DB->Query($query);
		if (0 == $result->num_rows)
			return;

		// delete original
		switch ($type)
		{
			case DESCRIPTION_ONLY:
				break;
			case IMAGETYPE_JPEG:
			default:
				if (file_exists(PICS_PATH.$path.$filename) and !@unlink(PICS_PATH.$path.$filename))
					return PICS_PATH.$path.$filename.' could not be deleted<br/>';
		}
	}

	private function DeleteData($itemId, $path, $filename, $type)
	{
		global $DB;
		$query = 'SELECT ItemId FROM ' . ITEM_TABLE .
			" WHERE ItemId='$itemId'" .
			" LIMIT 1";
		$result = $DB->Query($query);
		if (0 == $result->num_rows)
			return "Item ID ($itemId) was not found in the DB.</p>";

		// delete thumbnail, resize, and original
		$msg = '';
		switch ($type)
		{
			case DESCRIPTION_ONLY:
				break;
			case IMAGETYPE_JPEG:
			default:
				if (file_exists(PICS_PATH.$path.'r/'.$filename) and !@unlink(PICS_PATH.$path.'r/'.$filename))
					$msg .= PICS_PATH.$path.'r/'.$filename.' could not be deleted<br/>';
				if (file_exists(PICS_PATH.$path.'t/'.$filename) and !@unlink(PICS_PATH.$path.'t/'.$filename))
					$msg .= PICS_PATH.$path.'t/'.$filename.' could not be deleted<br/>';
		}

		// delete directories if empty
		if (($files = @scandir(PICS_PATH.$path.'r')) && count($files) <= 2)
			if (!rmdir(PICS_PATH.$path.'r'))
				$msg .= PICS_PATH.$path.$filename.'r/ is empty but could not be deleted<br/>';
		if (($files = @scandir(PICS_PATH.$path.'t')) && count($files) <= 2)
			if (!rmdir(PICS_PATH.$path.'t'))
				$msg .= PICS_PATH.$path.'t/ is empty but could not be deleted<br/>';
		if (($files = @scandir(PICS_PATH.$path)) && count($files) <= 2)
			if (!rmdir(PICS_PATH.$path))
				$msg .= PICS_PATH.$path.' is empty but could not be deleted<br/>';

		// delete database entries
		$DB->Delete(FAVORITE_TABLE, "FavoriteItemIdFk='$itemId'");
		$DB->Delete(TAG_MAP_TABLE, "ItemIdFk='$itemId'");
		$DB->Delete(PEOPLE_MAP_TABLE, "ItemIdFk='$itemId'");
		$DB->Delete(COMMENT_TABLE, "CommentItemIdFk='$itemId'");
		$DB->Delete(ITEM_TABLE, "ItemId='$itemId'");

		return $msg.'Item Permanently Deleted';
	}

	private function RestoreItem($itemId)
	{
		global $DB;
		$query = 'SELECT ItemDate, ItemLocationIdFk FROM ' . ITEM_TABLE .
			" WHERE ItemId='$itemId'" .
			" LIMIT 1";
		$result = $DB->Query($query);
		if (0 == $result->num_rows)
			return "Item ID ($itemId) was not found in the DB.</p>";
		$row = $result->fetch_array();

		$date = explode('-', $row[0]);
		$year = $date[0];
		$month = $date[1];
		$locationId = $row[1];

		// date
		$DB->UpdateSingle(DATE_TABLE, 'DateCount', 'DateCount+1', "DateYear='$year' AND DateMonth='$month'");
		// location
		$DB->UpdateSingle(LOCATION_TABLE, 'LocationCount', 'LocationCount+1', "LocationId=$locationId");
		// tags
		$DB->UpdateSingle(TAG_TABLE, 'TagCount', 'TagCount+1', "TagId IN " .
				"(SELECT TagIdFk FROM " . TAG_MAP_TABLE .
				" WHERE ItemIdFk=$itemId)");
		// people
		$DB->UpdateSingle(PEOPLE_TABLE, 'PeopleCount', 'PeopleCount+1', "PeopleId IN " .
				"(SELECT PeopleIdFk FROM " . PEOPLE_MAP_TABLE .
				" WHERE ItemIdFk=$itemId)");
		// visibility
		$DB->UpdateSingle(ITEM_TABLE, 'ItemIsHidden', "'0'", "ItemId='$itemId'");

		return 'Item Restored';
	}
}
?>
Return current item: Kwalbum