Location: PHPKode > projects > Kwalbum > kwalbum/include/FavoriteURLParser.php
<?php
/**
 * Parse the URL then get necessary values and create common strings for
 * viewing Favorites.
 *
 * reserved $_GET parameters that are checked for every page
 *  cols = number of thumbnail columns per page
 * 	i = item id for viewing
 * 	n = page number
 *  rows = number of thumbnail rows per page
 *  user = user with the favorites
 *
 * @version 2.1.3
 * @since 2.0 2008-09-09
 * @package kwalbum
 */

require_once 'URLParser.php';

class FavoriteURLParser extends URLParser
{
	private $id, $idQuery;
	private $user, $userQuery;
	private $thumbnailQuery;
	private $title;
	private $itemNumber, $totalItems;
	private $pageNumber, $totalPages;
	private $extendedURL;

	public function GetId()
	{
		if (!isset ($this->id))
			if (!empty ($_GET['i']))
				$this->id = (int) $_GET['i'];
			else
				$this->id = 0;
		$this->idQuery = "itemID = '$this->id'";
		return $this->id;
	}

	public function GetUser()
	{
		global $DB;
		if (!isset ($this->user))
		{
			if (!empty ($_GET['user']))
			{
				$this->user = $DB->FilterString($_GET['user']);
				$query = "SELECT UserId" .
				" FROM " . USER_TABLE .
				" WHERE UserTitle='$this->user'";
				$result = $DB->Query($query);
				if (0 == $result->num_rows)
					$id = 0;
				else
				{
					$row = $result->fetch_array();
					$id = $row[0];
				}
			} else
			{
				$id = USER_ID;
				$query = "SELECT UserTitle" .
				" FROM " . USER_TABLE .
				" WHERE UserId='$id'";
				$result = $DB->Query($query);
				if (0 == $result->num_rows)
					$this->user = null;
				else
				{
					$row = $result->fetch_array();
					$this->user = $row[0];
				}
			}
			$this->userQuery = "FavoriteUserIdFk='$id'";
		}
		return $this->user;
	}

	public function GetItemNumber()
	{
		if (!isset($this->itemNumber))
		{
			if ($id = $this->GetId())
			{
				global $DB;
				$whereQuery = $this->GetWhereQuery(false);
				$query = $whereQuery.($whereQuery ? ' AND ' : null).
				"(ItemOrderBy < (SELECT ItemOrderBy FROM ".ITEM_TABLE.
				" WHERE ItemId= '$id') OR " .
				"(ItemOrderBy = (SELECT ItemOrderBy FROM ".ITEM_TABLE.
				" WHERE ItemId= '$id') AND ItemId <= '$id'))";
				$number = $DB->GetCount(ITEM_TABLE . ', '. FAVORITE_TABLE, $query);
			}
			else
				$number = 0;
			$this->itemNumber = $number;
		}
		return $this->itemNumber;
	}

	public function GetPageNumber()
	{
		if (!isset($this->pageNumber))
		{
			global $DB;
			$pageNumber = (int)@$_GET['n'];
			if (!$pageNumber and $id = $this->GetId())
			{
				$count = $this->GetItemNumber();
				$pageNumber = ceil($count/($this->numColumns*$this->numRows));
			}
			else
				if (1 > $pageNumber)
					$pageNumber = 1;
			$this->pageNumber = $pageNumber;
		}
		return $this->pageNumber;
	}

	public function GetTotalItems()
	{
		if (!isset($this->totalItems))
		{
			global $DB;
			$this->totalItems = $DB->GetCount(ITEM_TABLE . ', ' . FAVORITE_TABLE, $this->GetWhereQuery(false));
		}
		return $this->totalItems;
	}

	public function GetTotalPages()
	{
		if (!isset($this->totalPages))
		{
			global $DB;
			$count = $this->GetTotalItems();
			$this->totalPages = ceil($count/($this->numColumns*$this->numRows));
		}
		return $this->totalPages;
	}

	public function GetPageNumberLinks($max = 5)
	{
		$firstPage = 1;
		$lastPage = $this->GetTotalPages();
		if (0 == $lastPage)
			return '0 Pages';
		$currentPage = $this->GetPageNumber();
		if (1 > $currentPage)
			$currentPage = 0;
		if ($currentPage > $lastPage)
			$currentPage = $lastPage+1;
		if ($currentPage > $max)
			$startPage = $currentPage-$max;
		else
			$startPage = 1;
		if ($currentPage < $lastPage-$max)
			$endPage = $currentPage+$max;
		else
			$endPage = $lastPage;

		$url = $this->GetExtendedURL().'n';

		if (1 == $currentPage)
			$html = 'First Page';
		else
			$html = "<a href='$url=1'>First Page</a>";

		for ($i = $startPage; $i <= $endPage; $i++)
			if ($i == $currentPage)
				$html .= " - [<b>$i</b>]";
			else
				$html .= " - <a href='$url=$i'>$i</a>";

		if ($lastPage == $currentPage)
			$html .= ' - Last Page';
		else
			$html .= " - <a href='$url=$lastPage'>Last Page</a>";
		return $html;
	}

	public function GetPrevPageLink()
	{
		$prevNumber = $this->GetPageNumber()-1;
		if (0 < $prevNumber)
			$html = '<a href="'.$this->GetExtendedURL().'p=Favorites&amp;n='.$prevNumber.'">Previous Page</a>';
		else
			$html = 'Previous Page';
		return $html;
	}

	public function GetNextPageLink()
	{
		$nextNumber = $this->GetPageNumber()+1;
		if ($this->GetTotalPages() >= $nextNumber)
			$html = '<a href="'.$this->GetExtendedURL().'p=Favorites&amp;n='.$nextNumber.'">Next Page</a>';
		else
			$html = 'Next Page';
		return $html;
	}

	public function GetTitle()
	{
		if (!$this->title)
		{
			$title = MAIN_TITLE;
			if ($this->GetUser())
				$title .= ': '.$this->GetUser()."'s Favorites";
			$this->title = $title;
		}
		return $this->title;
	}

	public function GetExtendedURL($page = null)
	{
		if (!isset($this->extendedURL) or $page)
		{
			$url = '';
			$url .= ($this->GetUser() ? 'user='.$this->GetUser() : null);
			$url .= ((THUMB_COLS != $this->numColumns) ? ($url ? '&amp;' : null).'cols='.$this->numColumns : null);
			$url .= ((THUMB_ROWS != $this->numRows) ? ($url ? '&amp;' : null).'rows='.$this->numRows : null).'&amp;p=Favorites';
			$url = PAGE_URL.($url ? $url.'&amp;' : '');
			if (!$page)
				$this->extendedURL = $url;
		}
		else
			$url = $this->extendedURL;
		return $url;
	}

	// --- Queries: full and partial ---

	public function GetIdQuery()
	{
		if (!isset ($this->idQuery))
			$this->GetId();
		return $this->idQuery;
	}

	public function GetUserQuery()
	{
		if (!isset ($this->userQuery))
			$this->GetUser();
		return $this->userQuery;
	}

	public function GetWhereQuery($includeWhere = true)
	{
		if (!isset ($this->whereQuery))
		{
			$whereQuery = $this->GetUserQuery();

			if (USER_CAN_VIEW_QUERY)
				$whereQuery .= ' AND ';
			$whereQuery .= USER_CAN_VIEW_QUERY;
			$whereQuery .= ' AND ItemId=FavoriteItemIdFk';
			$this->whereQuery = $whereQuery;
		}
		if ($this->whereQuery and $includeWhere)
			return ' WHERE '.$this->whereQuery;
		return $this->whereQuery;
	}

	/**
	 * Create a query string to get all info required to generate thumbnails for
	 * the current page
	 *
	 * @version 2.1.2
	 * @since 2.0
	 */
	public function GetThumbnailQuery()
	{
		if (!isset ($this->thumbnailQuery))
		{
			$this->thumbnailQuery = 'SELECT ItemId, ItemPath, ItemFilename,' .
			' ItemDate, ItemTime, ItemTypeId, ItemDescription, ItemHasComments' .
			' FROM ' . ITEM_TABLE . ', '. FAVORITE_TABLE . $this->GetWhereQuery().
			' ORDER BY ItemOrderBy, ItemId' .
			' LIMIT '.($this->numColumns*$this->numRows).
			' OFFSET '.($this->numColumns*$this->numRows*
			($this->GetPageNumber()-1));
		}
		return $this->thumbnailQuery;
	}

	public function GetPrevItemQuery($datetime)
	{
		$whereQuery = $this->GetWhereQuery();
		$query = 'SELECT ItemId, ItemPath, ItemFilename, ItemTypeId' .
		' FROM ' . ITEM_TABLE . ', '. FAVORITE_TABLE . $whereQuery.
		($whereQuery ? ' AND ' : ' WHERE ') .
		" (ItemOrderBy<'$datetime' OR" .
		" (ItemOrderBy='$datetime' AND ItemId<'$this->id'))" .
		' ORDER BY ItemOrderBy DESC, ItemId DESC' .
		' LIMIT 1';
		return $query;
	}
	public function GetNextItemQuery($datetime)
	{
		$whereQuery = $this->GetWhereQuery();
		$query = 'SELECT ItemId, ItemPath, ItemFilename, ItemTypeId' .
		' FROM ' . ITEM_TABLE . ', '. FAVORITE_TABLE . $whereQuery.
		($whereQuery ? ' AND ' : ' WHERE ') .
		" (ItemOrderBy>'$datetime' OR" .
		" (ItemOrderBy='$datetime' AND ItemId>'$this->id'))" .
		' ORDER BY ItemOrderBy ASC, ItemId ASC' .
		' LIMIT 1';
		return $query;
	}
}
?>
Return current item: Kwalbum