<?php

/**
 * @package eggblog
 * @author eggblog.net, tenfourzero@users.sourceforge.net
 * @version $Id: v4.0RC3 2008/01/21 11:25:04 tenfourzero Exp $
 * @copyright Copyright (c) 2005, http://www.eggblog.net
 * @license http://www.gnu.org/licenses/gpl.html GNU Lesser General Public License
 */


/*
 * return array of photo album id and text
 * @return array
 */
function eb_albums_array() {
	$sql="SELECT album_id,album_title FROM eb_albums ORDER BY album_id DESC";
	$query=mysql_query($sql);
	if(mysql_num_rows($query)>0) {
		while($row=mysql_fetch_array($query)) $output[]=array($row[0],$row[1]);
		return $output;
	}
	else return array();
}


/*
 * return list of albums with photo and text
 * @return string
 */
function eb_albums() {
	global $lang;
	$albums=eb_albums_array();
	if(count($albums)>0) {
		$output="\t\t<ul id=\"albums\">\r\n";
		foreach($albums as $album) $output.="\t\t\t<li><a href=\"photos.php?album=".$album[0]."\"><img src=\"photos/category/".$album[0].".jpg\" alt=\"".$album[1]."\" /></a></li>\r\n";
		$output.="\t\t</ul>\r\n";
	}
	else $output="";
	return $output;
}


/*
 * return photo album text
 * @param $id int album id
 * @return string
 */
function eb_album_title($id) {
	$sql="SELECT album_title FROM eb_albums WHERE album_id=".$id;
	$query=mysql_query($sql);
	return mysql_result($query,0);
}


/*
 * return photo text
 * @param $id int photo id
 * @return string
 */
function eb_photo_description($id) {
	$sql="SELECT photo_description FROM eb_photos WHERE photo_id=".$id;
	$query=mysql_query($sql);
	return mysql_result($query,0);
}


/*
 * return list of albums with photo and text
 * @param $id int album id
 * @return string
 */
function eb_album($id) {
	global $lang;
	settype($id,"integer");
	$output="\t\t<h3>".ucwords(eb_album_title($id))."</h3>\r\n";
	if(!empty($_SESSION['user_id'])) {
		if(eb_checkadmin($_SESSION['user_id'])) $output.="\t\t<p><a href=\"admin.php?editalbum=".$id."\">".$lang['edit']."</a> | <a href=\"admin.php?deletealbum=".$id."\">".$lang['delete']."</a></p>\r\n";
	}
	$output.="\t\t<ul id=\"album\">\r\n";
	$sql="SELECT photo_id,photo_description FROM eb_photos WHERE album_id=".$id;
	$query=mysql_query($sql);
	while($row=mysql_fetch_array($query)) $output.="\t\t\t<li><a href=\"photos.php?photo=".$row[0]."&album=".$id."\"><img src=\"photos/thumbnail/".$row[0].".jpg\" alt=\"".eb_textpreview($row[1],50)."\" /></a></li>\r\n";
	$output.="\t\t</ul>\r\n";
	return $output;
}


/*
 * return string of photo title and image
 * @param $id int photo id
 * @return string
 */
function eb_photo($photo,$album) {
	global $lang;
	settype($photo,"integer");
	settype($album,"integer");
	$output ="\t\t<h3><a href=\"photos.php?album=".$album."\">".eb_album_title($album)."</a></h3>\r\n";
	$output.="\t\t<p>".str_replace("\r\n","<br />",str_replace("\r\n\r\n","</p><p>",eb_photo_description($photo)))."</p>\r\n";
	$output.="\t\t<img id=\"photo\" src=\"photos/full/".$photo.".jpg\" />\r\n";
	if(!empty($_SESSION['user_id'])) {
		if(eb_checkadmin($_SESSION['user_id'])) $output.="\t\t<p><a href=\"admin.php?editphoto=".$photo."&album=".$album."\">".$lang['edit']."</a> | <a href=\"admin.php?deletephoto=".$photo."&album=".$album."\">".$lang['delete']."</a></p>\r\n";
	}

	$sql="SELECT photo_id FROM eb_photos WHERE album_id=".$album;
	$query=mysql_query($sql);
	while($row=mysql_fetch_array($query)) $array[]=$row[0];
	$count=count($array);
	while($photo_id=current($array)) {
		if($photo_id==$photo) $key=key($array)+1;
		next($array);
	}
	$prev=$key-2;
	$next=$key;
	$output.="\t\t<table width=\"100%\">\r\n";
	$output.="\t\t\t<tr><td>";
	if($key>1) $output.="\t\t<a href=\"photos.php?photo=".$array[$prev]."&album=".$album."\">&lt;- ".$lang['previous']."</a>";
	$output.="</td><td align=\"right\">";
	if($key<$count) $output.="<a href=\"photos.php?photo=".$array[$next]."&album=".$album."\">".$lang['next']." -&gt;</a>";
	$output.="</td></tr>\r\n";
	$output.="\t\t</table>\r\n";

	return $output;
}


?>