<?php 
/************************************
 pragmaMx  Content Management System
 Copyright (c) 2005 pragmaMx Dev Team - http://pragmaMx.org
 ***********************************
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.
 ***********************************
 $Source: /home/cvs/pragmamx/Avatar/album.php,v $
 $Revision: 1.7 $
 $Author: bowlingx $
 $Date: 2005/11/01 19:12:25 $
 
 This Modul is Copyright (c) 2003 - 2005 by David Heidrich; Myself Design
 Visit http://www.myself-design.com for Updates
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
************************************/
// Der Modulname:
if(!defined("AV_MODNAME")) define("AV_MODNAME",basename(dirname(__FILE__)));
// Die benötigten Dateien includen:
include "modules/".AV_MODNAME."/moreconfig.php";
include "modules/".AV_MODNAME."/settings.php";
include "modules/".AV_MODNAME."/plugins.php";

// Sprache für das Modul ermitteln:
mxGetLangfile(AV_MODNAME);

function displayAlbum($id){
	// Globale Variablen:
	global $modul_version, $bgcolor2, $bgcolor1, $back_conf, $layout, $Site_URL, $table, $table_comments, $cookie,$bewertung, $yaccountName, $maxheight_foto, $maxwidth_foto, $maxwidth, $maxheight;
    $isseradmin = mxIsAdmin($_COOKIE["admin"]);
	$gets = new plugin();
	include("header.php");
	if ($gets->isUpdateNeccesary()) {
	OpenTable();
	mxErrorScreen(_NOTINSTALLED);
	CloseTable();
	include("footer.php");
	die();
    }
    
    // User, ja oder nein?
	if (mxIsUser()) {
		$username = $cookie[1];
	} else {
		OpenTable();
		mxErrorScreen(_AV_ERROR_LOGIN);
		CloseTable();
		include("footer.php");
		return;
	}
    
    if($username == $id){
	if (defined('MX_VERSION') && file_exists("modules/$yaccountName/navbar.php")) {
		include_once("modules/$yaccountName/navbar.php");
		OpenTable();
		nav();
		CloseTable();
	}
	// Falls der User selbst entscheiden will ob vor dem Avatar Upload noch etwas includiert wird:
	elseif (file_exists("modules/".AV_MODNAME."/nav_inclusion.php")) {
		include_once("modules/".AV_MODNAME."/nav_inclusion.php");
	}
    }
	// Die Bilder vom angegebenen User lesen:
	$row = $gets->getPicsFromUser($id);
	$avatarFileNew = "";
	OpenTable();
	// Wenn keine Bilder im entsprechenden Album gefunden wurden:
	if (!$row[0]) {
		echo "<table width=\"100%\" border=\"0\">";
		echo "<tr bgcolor=\"$back_conf\">";
		echo "<td>"._AV_LEER_1." <b>$id</b> "._AV_LEER_2."</td></tr></table>";
		echo "<br><a href=\"modules.php?name=Userinfo&uname=$id\">"._BACK_U."</a>";
		CloseTable();
		include("footer.php");
		return;
	}
	if($username != $id){
		echo "<br><br>"._AV_WELCOME_TO_ALBUM." <b>$id</b>"._AV_TO_VOTE_FOR."<br><br>"; 
	}
	else {
		echo ""._AV_WELCOME_ALBUM." <b>$id</b>!<br>";
	}
	// Zuerst generieren wir das Form
	// Alle verfügbaren Dateien (Bilder) des Users anzeigen:
	$avatarFileNew = explode("|", $row[0]);
	// Bisher noch keine Bilder angeklickt!
	/************* Benötigte Variabeln: ***********/
	$ii = 0;
	$foto_array = array();
	/************* Benötigte Variabeln: ***********/
	# ALLE AVATARE UND FOTOS AUFLISTEN: #
	echo "<hr size='1' noshade><b>"._AV_USER_UPLOADED_FOLLOWING_AVATARS."</b><br><br>
         <table width=\"100%\" border=\"0\">";
	for($i = 0;$i < $row[1];$i++) {
		echo "<tr bgcolor=\"$back_conf\">";
		for($n = 1;$n <= $layout;$n++) {
			// NUR Wenn $i kleiner als die Gesamtzahl der Avatare ist:
			if ($i < $row[1]) {
				// Die Hochgeladenen Dateinamen extrahieren:
				$endFile = $avatarFileNew[$i];
				$AvatarEndFile = explode(".", $endFile);
				if ($AvatarEndFile[1] != "foto") {
					echo "<td bgcolor=\"$back_conf\" width=\"33%\">
                          <a href=\"#\" onClick=\"window.open('modules.php?name=".AV_MODNAME."&amp;file=index&amp;wat=ShowBigImage&amp;pic=$endFile',null,'width=$maxwidth,height=$maxheight,status=yes,toolbar=no,menubar=no,location=no');return false;\" target=\"newWin\"><img src=\"modules.php?name=".AV_MODNAME."&amp;file=index&amp;wat=displayThumb&amp;pic=$endFile\" alt=\"$endFile\"></a><br>";
					if($isseradmin){
                     echo " <a href=\"modules.php?name=".AV_MODNAME."&amp;file=deletAvatar&amp;mode=deleteForAdmin&amp;username=$id&amp;fileName=$endFile&amp;id=".$row[6]."\"><img src=\"modules/".AV_MODNAME."/images/delComment.png\" alt=\"checked\" style=\"vertical-align: middle;\"> "._AV_DELL_PIC."</a></td>";
					}
					else {
					echo "</td>";
					}
					} else {
					// Das Layout muss hier einen zurückgesetzt werden damit die Proportionen noch stimmen!
					$n--;
					$foto_array[] = $endFile;
					$ii++;
				}
			}
			// Innere Schleife austricksen:
			$i++;
		}
		// Damit es richtig zuende läuft:
		$i--;
		if ($ii == $row[1]) {
			// Wenn keine Avatare hochgeladen wurden, sondern nur Fotos:
			// Dann muss trotzdem eine Meldung angezeigt werden!
			echo "<td>"._AV_NO_AVATARS_TIME."</td>";
		}
		echo "</tr>";
	}
	echo "</table>";
	echo "<hr size='1' noshade><b>"._AV_USER_UPLOADED_FOLLOWING_PICS."</b><br><br>";
	echo "<table width=\"100%\" border=\"0\">";
	if ($ii) {
		for($i = 0;$i < $ii;$i++) {
			echo "<tr bgcolor=\"$back_conf\">";
			for($n = 1;$n <= $layout;$n++) {
				if ($i < $ii) {
					echo "<td bgcolor=\"$back_conf\" width=\"33%\">
                     <a href=\"#\" onClick=\"window.open('modules.php?name=".AV_MODNAME."&amp;file=index&amp;wat=ShowBigImage&amp;pic=".$foto_array[$i]."',null,'width=$maxwidth_foto,height=$maxheight_foto,status=yes,toolbar=no,menubar=no,location=no');return false;\" target=\"newWin\"><img src=\"modules.php?name=".AV_MODNAME."&amp;file=index&amp;wat=displayThumb&amp;pic=".$foto_array[$i]."\" alt=\"".$foto_array[$i]."\"></a><br>";
				if($isseradmin){
                     echo " <a href=\"modules.php?name=".AV_MODNAME."&amp;file=deletAvatar&amp;mode=deleteForAdmin&amp;username=$id&amp;fileName=".$foto_array[$i]."&amp;id=".$row[6]."\"><img src=\"modules/".AV_MODNAME."/images/delComment.png\" alt=\"checked\" style=\"vertical-align: middle;\"> "._AV_DELL_PIC."</a></td>";
					}
					else {
					echo "</td>";
					}
				}
				$i++;
			}
			$i--;
			echo "</tr>";
		}
		echo "</table>";
	} else {
		echo "<tr bgcolor=\"$back_conf\"><td>"._AV_NO_PICS_TIME."</td>";
		echo "</tr></table><br>";
	}
	/*********************************************************************************************************************/
	if($bewertung){
		// Eingabeformular für die Bewertungen des Users:
		echo "<br><br>"._AV_COMMENTS_LIKE."<br><br>";
		echo "<form action=\"modules.php?op=modload&amp;name=".AV_MODNAME."&amp;file=album&amp;func=saveComment\"
              method=\"post\" name=\"form_send\">
        <table width=\"100%\" border=\"0\">
        <tr> 
        <td width=\"100%\"><input type=\"radio\" name=\"punkte\" value=\"1\">
        <img src=\"modules/".AV_MODNAME."/images/star-1.png\" alt=\"1 Punkt\">
        <input type=\"radio\" name=\"punkte\" value=\"2\">
        <img src=\"modules/".AV_MODNAME."/images/star-2.png\" alt=\"2 Punkte\"><input type=\"radio\" name=\"punkte\" value=\"3\">
        <img src=\"modules/".AV_MODNAME."/images/star-3.png\" alt=\"3 Punkte\"><br><input type=\"radio\" name=\"punkte\" value=\"4\">
        <img src=\"modules/".AV_MODNAME."/images/star-4.png\" alt=\"4 Punkte\"> <input type=\"radio\" name=\"punkte\" value=\"5\">
        <img src=\"modules/".AV_MODNAME."/images/star-5.png\" alt=\"5 Punkte\"></td>
        </tr>
        <tr> 
        <td><textarea name=\"text\" cols=\"72\" rows=\"6\"></textarea> </td>
        </tr>
        <tr>
        <td><input type=\"submit\" name=\"Submit\" value=\""._AV_DO_VOTING."\">
        <input type=\"reset\" name=\"Submit2\" value=\""._AV_RECOVER."\"></td>
        <td>&nbsp;</td>
        </tr>
        </table><hr size='1' noshade>";

		// Listen wir die Kommentare alle auf:
		$sql = "SELECT a.id, a.alid, a.author, a.text, a.date, a.punkte
            FROM $table_comments AS a 
            LEFT JOIN $table AS b ON a.alid = b.id WHERE b.aid='$id'";
		$userid = "";
		$ergebnis = sql_query($sql);
		$ix = 0;
		$td_admin = "";
		// Wenn Kommentare vorliegen zeigen wir die Tabellen an:
		// Listen wir die einzelnen Kommentare auf: Kommentartabellen:
		echo "<br><center>"._AV_FOLLOWING_COMMENTS."</center><br>";
		echo "<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\" bgcolor=\"".$bgcolor2."\"><tr><td>
		  <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\">";
		while($go = sql_fetch_row($ergebnis)){
			// Datum Korrekt anzeigen:  (Nur Wenn die entsprechenden Funktionen vorhanden sind!)
			$datum = mx_strftime(_SHORTDATESTRING." - %H:%M", $go[4]);
			$go[3] = mxPrepareToHTMLDisplay($go[3]);
			if(mxIsAdmin()){
				$td_admin = "<a href=\"modules.php?name=".AV_MODNAME."&amp;file=album&amp;func=deleteComment&amp;id=".$go[0]."&amp;uid=$id\"><img src=\"modules/".AV_MODNAME."/images/delComment.png\" alt=\"delComment\" title=\""._AV_DEL_COMMENT."\" style=\"vertical-align: middle;\"></a>";
			}
			if($go[2] == $id){
			$userid = "<img src=\"modules/".AV_MODNAME."/images/user.png\" alt=\"Besitzer\" title=\""._AV_OWNER."\" style=\"vertical-align: middle;\">";
			}
			else {
			$userid = "";
			}
			echo "<tr bgcolor=\"$bgcolor2\">
		  <td><span class=\"tiny\">"._AV_WRITTEN_BY." <b>".$go[2]."</b> "._AV_AM." $datum</span></td>
		  <td align=\"right\"> $td_admin $userid <a href=\"modules.php?name=".AV_MODNAME."&amp;file=album&amp;uid=".$go[2]."\"><img src=\"modules/".AV_MODNAME."/images/visit.png\" title=\""._AV_ALBUM_VISIT1." ".$go[2]." "._AV_ALBUM_VISIT2."\" alt=\"Visit Album\" style=\"vertical-align: middle;\"></a> <img src=\"modules/".AV_MODNAME."/images/star-".$go[5].".png\" alt=\"".$go[5]."\" style=\"vertical-align: middle;\"></td></tr>
		  <tr bgcolor=\"$bgcolor1\"><td colspan=\"2\">".$go[3]."</td></tr>";
			$ix++;
		}
		if(!$ix){
			echo "<tr bgcolor=\"$bgcolor2\"><td><center><b>"._AV_NO_COMMENTS."</b></center></td></tr>";
		}
		echo "</table></td></tr></table>";
		echo "<br><br><center><span class=\"tiny\">"._AV_COMMENTS_VERANT."</span></center>";
		echo "<br><br><center><a href=\"modules.php?name=Userinfo&amp;uname=$id\">"._BACK_U."</a></center><br><br>";
	    echo "<center>Copyright &copy; by <a href=\"http://www.myself-design.com\">David Heidrich</a><br>Version: $modul_version</center>";

		$get_user = new plugin();
		echo "<input type=\"hidden\" name=\"alid_user\" value=\"".$row[6]."\">";
		echo "<input type=\"hidden\" name=\"username\" value=\"".$username."\">";
		echo "<input type=\"hidden\" name=\"uid\" value=\"".$id."\"></form>";
	    
	}
	else {
		echo "<br><br><center><a href=\"modules.php?name=Userinfo&amp;uname=$id\">"._BACK_U."</a></center><br><br>";
	    echo "<center>Copyright &copy; by <a href=\"http://www.myself-design.com\">David Heidrich</a><br>Version: $modul_version</center>";
	}
	// Kommentare anzeigen:
	CloseTable();
	include("footer.php");
}
function deleteComment($daten){
	global $table_comments;
	extract($daten);
	if(mxIsAdmin()){
		$qu = "DELETE FROM $table_comments WHERE `id`=$id";
		sql_query($qu);
		Header("Location: modules.php?name=".AV_MODNAME."&file=album&uid=$uid");
	}
	else {
		mxErrorScreen(_AV_NO_ACCESS);
	}
}
function saveComment($daten){
	// Kommentar eintragen:
	global $table_comments;
	extract($daten);
	if((!empty($text)) && (!empty($punkte))){
		$text = mxAddSlashesForSQL(nl2br(trim(htmlspecialchars($text))));
		if(mxIsUser() || mxIsAdmin()){
			$qu = "INSERT INTO $table_comments (`alid`,`author`,`text`,`date`,`punkte`)
	 VALUES('$alid_user','".mxAddSlashesForSQL($username)."','".$text."','".time()."','$punkte')";
			if(sql_query($qu)){
				Header("Location: modules.php?name=".AV_MODNAME."&file=album&uid=$uid");
			}
		}
		else {
			mxErrorScreen(_AV_ERROR_LOGIN);
		}
	}
	else {
		mxErrorScreen(_AV_FILL_ALL_F);
	}
}


// Programmanweisungen:
// Ist die Albumfunktion Aktiviert?
if ($album) {
	$func = (isset($_GET['func']))? $_GET['func']: "";
	switch($func){

		case "saveComment":
		saveComment($_POST); break;
		case "deleteComment":
		deleteComment($_GET); break;

		default:
		$id = $_GET['uid'];
		if((isset($id)) && (is_string($id)) && (!empty($id))) {
			displayAlbum($id);
		}
		else {
			Header("Location: index.php");
		}
	}
}
else {
	// Wenn das Fotoalbum nicht aktiviert ist:
	Header("Location: modules.php?name=".AV_MODNAME."");
}

?>