Location: PHPKode > projects > Kwalbum > kwalbum/include/ItemPageInfo.php
<?
/**
 * @version 2.1.3
 * @since 2.0 2008-09-11
 * @package kwalbum
 */

/**
 * Contains logic shared between all types of Item pages.
 *
 * @since 2.0 2008-09-011
 * @package kwalbum
 */
class ItemPageInfo
{
	public $id, $owner;
	public $datetime;
	public $date, $time;
	public $description;
	public $tags, $tagLinks;
	public $people, $peopleLinks;
	public $location;
	public $type;
	public $resizedItem;
	public $fullURL, $downloadURL;
	public $prev, $next;
	public $numComments, $comments;
	public $canEdit;
	public $pageNumber;
	public $itemNumber, $totalItems;
	public $count;

	function ItemPageInfo(& $title, & $URLP)
	{
		$this->id = $id = $URLP->GetId();
		if (!$title)
			$title = $URLP->GetTitle();
		if (!$id)
		{
			$this->title = $title.': Invalid Item ID';
			return null;
		}

		global $DB;
		$monthNames = array('', 'January', 'February', 'March', 'April', 'May', 'June',
				'July', 'August', 'September', 'October', 'November', 'December');
		$this->pageNumber = $URLP->GetPageNumber();
		$this->itemNumber = $URLP->GetItemNumber();
		$this->totalItems = $URLP->GetTotalItems();

		// add comment if submitted
		if (!empty($_POST['nsme']) and !empty($_POST['csmment']))
			$this->AddComment($id);

		// get Item data and finish setting $title
		$query = 'SELECT ItemPath, ItemFilename, ItemDate, ItemTime, ItemOrderBy, ItemTypeId, ItemDescription, Location, ItemUserIdFk, ItemCount, ItemHasComments, UserTitle, ' .
			$URLP->GetOrderByField() .
			' FROM ' . ITEM_TABLE .
			' LEFT JOIN ' . LOCATION_TABLE .
			' ON LocationId=ItemLocationIdFk' .
			' LEFT JOIN ' . USER_TABLE .
			' ON UserId=ItemUserIdFk' .
			' WHERE ItemId=' . $id . (USER_CAN_VIEW_QUERY ? ' AND '.USER_CAN_VIEW_QUERY : null);
		$result = $DB->Query($query);

		if (0 == $result->num_rows)
			return false;
		$row = $result->fetch_array(MYSQLI_ASSOC);
		$title .= ': ';
		$this->title = $title;
		$this->owner = $row['UserTitle'];
		$hasComments = $row['ItemHasComments'];

		$this->canEdit = (EDIT_MODE and USER_CAN_ADD and (USER_ID == $row['ItemUserIdFk'] or USER_IS_ADMIN));
		if ($this->canEdit)
		{
			// edit date and time values
			$this->date = $row['ItemDate'];
			$this->time = $row['ItemTime'];
			// delete comment if marked
			if (!empty($_POST['delcsmment']))
				$this->DeleteComment($id, (int)$_POST['delcsmment']);
		}
		else
		{
			$year = substr($row['ItemDate'], 0, 4);
			$month = $monthNames[(int)substr($row['ItemDate'], 5, 2)];
			if ($month)
			{
				$this->datetime = $month;
				$day = substr($row['ItemDate'], 8, 2);
				if ('00' != $day)
					$this->datetime .= " $day";
			}
			if ('0000' != $year)
				$this->datetime .= ($month ? ', ' : null).$year;
			if ('00:00:00' != $row['ItemTime'])
				$this->datetime .= ($this->datetime ? ' @ ' : null).date('g:i a', strtotime($row['ItemTime']));
		}
		$this->type = $row['ItemTypeId'];
		$this->description = $row['ItemDescription'];
		$this->count = $row['ItemCount'];
		$this->location = $row['Location'];
		if (HIDE_PATH)
			$this->fullURL = PAGE_URL.'f='.$this->id;
		else
			$this->fullURL = PICS_URL . $row['ItemPath'] . $row['ItemFilename'];
		$this->downloadURL = PAGE_URL.'s=3&amp;f='.$this->id;
		if (DESCRIPTION_ONLY != $this->type)
			$this->resizedItem = "<a href='$this->fullURL'>" .
				GetImageCode($row['ItemPath'], $row['ItemFilename'], $this->type, $this->id, true).'</a>';
		else
			$this->resizedItem = GetImageCode('', '', $this->type, $this->id, true, $this->description);

		$orderByValue = $row[$URLP->GetOrderByField()];

		// previous item
		$result = $DB->Query($URLP->GetPrevItemQuery($orderByValue));
		if (1 == $result->num_rows)
		{
			$row = $result->fetch_array(MYSQLI_ASSOC);
			$this->prev = Array (
				'resizedURL' => $URLP->GetExtendedURL() . 'i=' . $row['ItemId'],
				'thumbnail' => GetImageCode($row['ItemPath'], $row['ItemFilename'], $row['ItemTypeId'], $row['ItemId'])
			);
		}

		// next item
		$result = $DB->Query($URLP->GetNextItemQuery($orderByValue));
		if (1 == $result->num_rows)
		{
			$row = $result->fetch_array(MYSQLI_ASSOC);
			$this->next = Array (
				'resizedURL' => $URLP->GetExtendedURL() . 'i=' . $row['ItemId'],
				'thumbnail' => GetImageCode($row['ItemPath'], $row['ItemFilename'], $row['ItemTypeId'], $row['ItemId'])
			);
		}

		// tags
		$this->tags = '';
		$this->tagLinks = '';
		$query = 'SELECT Tag FROM '.TAG_TABLE.', '.TAG_MAP_TABLE.
			" WHERE ItemIdFk=$id AND TagId=TagIdFk ".
			" ORDER BY Tag ASC";
		$result = $DB->query($query);
		if (0 != $result->num_rows)
			while ($row = $result->fetch_array())
			{
				if ($this->tags)
				{
					$this->tags .= ',';
					$this->tagLinks .= ', ';
				}
				$this->tags .= $row[0];
				$this->tagLinks .= "<a href='".PAGE_URL."tags=$row[0]'>$row[0]</a>";
			}

		// people
		$this->people = '';
		$this->peopleLinks = '';
		if (USER_CAN_VIEW)
		{
			$query = 'SELECT PeopleName FROM '.PEOPLE_TABLE.', '.PEOPLE_MAP_TABLE.
				" WHERE ItemIdFk=$id AND PeopleId=PeopleIdFk";
			$result = $DB->query($query);
			if (0 < $result->num_rows)
				while ($row = $result->fetch_array())
				{
					if ($this->people)
					{
						$this->people .= ', ';
						$this->peopleLinks .= ', ';
					}
					$this->people .= $row[0];
					$this->peopleLinks .= "<a href='".PAGE_URL."people=$row[0]'>$row[0]</a>";
				}
		}

		// comments
		$this->comments = array();
		if ($hasComments)
		{
			$query = 'SELECT CommentId, CommentName, CommentText, CommentDt, CommentIp FROM '.COMMENT_TABLE.
				" WHERE CommentItemIdFk=$id ".
				" ORDER BY CommentId ASC";
			$result = $DB->query($query);
			if (0 != $this->numComments = $result->num_rows)
				while ($row = $result->fetch_array())
				{
					$this->comments[] = $row;
				}
		}
	}

	private function AddComment($id)
	{
		global $DB;

		if (PUBLIC_COMMENTS or USER_ID)
		{
			$name = $DB->FilterString($_POST['nsme']);
			$comment = $DB->FilterString($_POST['csmment']);
			$comment = strtr($comment, array("\n" => "<br>", "\r" => ""));
			$comment = ereg_replace('[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]','<a href="\\0" rel="nofollow">\\0</a>', $comment);

			$insertData = array (
				'CommentItemIdFk' => $id,
				'CommentName' => $name,
				'CommentText' => $comment,
				'CommentDt' => date('Y-m-d H:i:s'),
				'CommentIp' => $_SERVER['REMOTE_ADDR']
			);
			if ($DB->Insert(COMMENT_TABLE, $insertData))
			{
				$query = "UPDATE ".ITEM_TABLE.
					" SET ItemHasComments=1, ItemUpdateDt=NOW()" .
					" WHERE ItemId='$id'";
				$DB->Query($query);
			}
			else if (TEST_MODE)
				echo '<div class="error">Error inserting new comment.</div>';
		}
	}

	private function DeleteComment($itemId, $commentId)
	{
		global $DB;
		$DB->Delete(COMMENT_TABLE, "CommentItemIdFk='$itemId' AND CommentId='$commentId'");
	}

	public function GetHeader()
	{
		$commonHeader = '<script type="text/javascript" language="javascript" SRC="ajax/scriptaculous/prototype.js"></script>'.
				'<script type="text/javascript" language="javascript" SRC="ajax/addFavorite.js"></script>'.
				'<script type="text/javascript" language="javascript" SRC="ajax/getJpegExifData.js"></script>';
		if ($this->canEdit)
			return $commonHeader.
				'<script type="text/javascript" language="javascript" src="ajax/scriptaculous/scriptaculous.js"></script>'.
				'<link href="styles/edit.css" rel="stylesheet" type="text/css" />';
		else
			return $commonHeader;
	}
}
?>
Return current item: Kwalbum