<?php
$changesection="main";
$insert=$_POST["insert"];
$update=$_POST["update"];
$delete=$_GET["delete"];
$edit=$_GET["edit"];
if ($insert)
{
$galleryname=$_POST["galleryname"];
if ($galleryname)
{
$galleries=mysql_query("select * from galleries where gallery='$galleryname'",$db);
$gallery=mysql_fetch_assoc($galleries);
if (!$gallery)
{
mkdir("./phptmp/thumbs/".$galleryname,0777);
mkdir("./phptmp/images/".$galleryname,0777);
mysql_query("insert into galleries (gallery) values ('$galleryname')",$db);
$changes[$changesection].='Gallery <b>'.$galleryname.'</b> has been inserted into database<br>';
}
else
{
$changes[$changesection].='Gallery could not be inserted into database<br>';
$changes[$changesection].='Gallery <b>'.$galleryname.'</b> already exists<br>';
$changes[$changesection].='<hr>';
}
}
else
{
$changes[$changesection].='Gallery could not be inserted into database<br>';
$changes[$changesection].='Gallery missing<br>';
$changes[$changesection].='<hr>';
}
}
if ($update)
{
$galleryid=$_POST["galleryid"];
$galleryname=$_POST["galleryname"];
$galleries=mysql_query("select * from galleries where id='$galleryid'",$db);
$gallery=mysql_fetch_assoc($galleries);
if (($galleryname) && ($galleryname!=$gallery[gallery]))
{
$galleries=mysql_query("select * from galleries where gallery='$galleryname'",$db);
$gallery=mysql_fetch_assoc($galleries);
if (!$gallery)
{
$galleries=mysql_query("select * from galleries where id='$galleryid'",$db);
$gallery=mysql_fetch_assoc($galleries);
rename("./phptmp/thumbs/".$gallery[gallery],"./phptmp/thumbs/".$galleryname);
rename("./phptmp/images/".$gallery[gallery],"./phptmp/images/".$galleryname);
mysql_query("update galleries set gallery='$galleryname' where id='$galleryid'",$db);
$changes[$changesection].='Gallery has been changed to <b>'.$galleryname.'</b><br>';
}
else
{
$changes[$changesection].='Gallery could not be updated, gallery <b>'.$galleryname.'</b> already exists<br>';
}
}
}
if ($delete)
{
$galleries=mysql_query("select * from galleries where id='$delete'",$db);
$gallery=mysql_fetch_assoc($galleries);
$images=mysql_query("select * from images where gallery='$delete'",$db);
while ($image=mysql_fetch_assoc($images))
{
unlink("./phptmp/thumbs/".$gallery[gallery]."/".$image[filename]);
unlink("./phptmp/images/".$gallery[gallery]."/".$image[filename]);
}
rmdir("../../phptmp/thumbs/".$gallery[gallery]);
rmdir("../../phptmp/images/".$gallery[gallery]);
mysql_query("delete from images where gallery='$delete'",$db);
mysql_query("delete from galleries where id='$delete'",$db);
$changes[$changesection].='Gallery <b>'.$gallery[gallery].'</b> has been deleted from database<br>';
}
$changes[$changesection].='<form method="post" action="index.php?subsite=galleries-admin.php'.$sessionparm_link.'">';
$changes[$changesection].='<table class="vistable">';
if ($edit)
{
$galleries=mysql_query("select * from galleries where id='$edit'",$db);
$gallery=mysql_fetch_assoc($galleries);
$changes[$changesection].='<tr><td>Gallery:</td><td><input type="text" name="galleryname" value="'.$gallery[gallery].'"></td></tr>';
$changes[$changesection].='</table>';
$changes[$changesection].='<input type="hidden" name="galleryid" value="'.$gallery[id].'">';
$changes[$changesection].='<input type="submit" name="update" value="Update Gallery">';
$changes[$changesection].='<input type="submit" name="cancel" value="Cancel">';
}
else
{
$changes[$changesection].='<tr><td>Gallery:</td><td><input type="text" name="galleryname"></td></tr>';
$changes[$changesection].='</table>';
$changes[$changesection].='<input type="submit" name="insert" value="Add Gallery">';
$changes[$changesection].='</form>';
$changes[$changesection].='<table class="vistable" border="1">';
$changes[$changesection].='<tr><th>Gallery</th><th>Images</th><th colspan="3">Action</th></tr>';
$galleries=mysql_query("select * from galleries order by gallery",$db);
while ($gallery=mysql_fetch_assoc($galleries))
{
$images=mysql_query("select * from images where gallery='$gallery[id]'",$db);
$num_images=mysql_num_rows($images);
$changes[$changesection].='<tr><td>'.$gallery[gallery].'</td><td>'.$num_images.'</td>';
$changes[$changesection].='<td><a href="index.php?subsite=galleries-admin.php&edit='.$gallery[id].'">Edit</a></td>';
$changes[$changesection].='<td><a href="index.php?subsite=galleries-admin.php&delete='.$gallery[id].'">Delete</a></td>';
$changes[$changesection].='<td><a href="index.php?subsite=images-admin.php&gallery='.$gallery[id].'">Edit Images</a></td>';
$changes[$changesection].='</tr>';
}
$changes[$changesection].='</table>';
}
?>