<?

/************************************************************************/
/* 4nAlbum Version 0.9                                                  */
/* ==================================================================== */
/* 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.       */
/************************************************************************/

function movecategory($gid) {
        global $prefix, $adminurl, $gallerypath, $font;

  galleryHeader();
  navigationAdmin("<a class=\"".$font['title']."\" href=\"$adminurl\">"._GALCONFIG."</a><font class=\"".$font['title']."\"> &gt; </font><a class=\"".$font['title']."\" href=\"$adminurl&amp;do=editallcategories\">"._GALEDITALLCATEGORIES."</a><font class=\"".$font['title']."\"> &gt; "._GALMOVECATEGORY.'</font>');
  print '<br>';
  OpenTable();
  echo "
                <p align=\"center\">
                        <form action=\"$adminurl\" method=\"post\">
                        <input type=\"hidden\" name=\"do\" value=\"editcategory\">
                        <input type=\"hidden\" name=\"type\" value=\"move\">";

                        list($cat) = mysql_fetch_row(mysql_query("SELECT gallname FROM $prefix"._4nalbum_categories." WHERE gallid=$gid"));

                        echo "
                        <font class=\"pn-pagetitle\">"._GALMOVEOPTION." '$cat'</font><br>
                          <br><font class=\"".$font['title']."\">"._GALMOVEWARNING."</font><br><br><font class=\"".$font['normal']."\">"._GALMOVESELECT."</font>
                                <input type=\"hidden\" name=\"oldlocation\" size=\"14\" value=\"$gid\">
                                <select size=\"1\" name=\"newparent\">";

                                $categories = listcategories("$gallerypath");
                                $categories = explode(" ", trim($categories));
                                echo "<option selected value=\"-1\">/</option> ";
                                while(list($key, $val) = each($categories)) {
                                        $result = mysql_query("select gallid, gallname, parent from $prefix"._4nalbum_categories." where gallid=".$val);

                                        list($gallid, $gallname, $parent) = mysql_fetch_row($result);
                                        $nbtabs = indent($gallid);
                                        $tab = "";
                                        for ($i=0; $i<$nbtabs; $i++)
                                                $tab .= "&nbsp;&nbsp;&nbsp;";
                                        if ($gallid!=$gid)
                                                echo "<option value=\"$gallid\">$tab$gallname</option> ";
                                }
                                echo "</select><br><br>

                        <input type=\"submit\" name=\"confirmed\" value=\""._GALMOVE."\">

                </form>
                </p>";
  CloseTable();
  adminFooter();
}


function copydir($source , $destination){
        global $prefix, $adminurl, $gallerypath;


        $dossier=opendir($source);
        if (!file_exists($destination))
                mkdir($destination, fileperms($source));
        $total = 0;
        while ($file = readdir($dossier)) {
                  if ($file != "." && $file != ".."){
                          if (is_dir($source."/".$file)){
                                  $total += copydir("$source/$file", "$destination/$file");
                                $newgalloc = substr($destination, strlen("$gallerypath/"));
                                $oldgalloc = substr($source, strlen("$gallerypath/"));
                                mysql_query("UPDATE $prefix"._4nalbum_categories." SET galloc='$newgalloc/$file' WHERE galloc='$oldgalloc/$file'");
                          } else {
                                  copy("$source/$file", "$destination/$file");
                                  $total++;
                          }
                  }
        }
        return $total;
}



function rmove($source, $destination)
{
        if (copydir($source, $destination))
                rdel($source);
}

function rdel($path) {
    global $prefix, $adminurl, $gallerypath;

    if ($path[strlen($path)-1] != "/")
        $path .= "/";

    if (is_dir($path))
    {
        $d = opendir($path);

        while ($f = readdir($d))
        {
            if ($f != "." && $f != "..")
            {
                $rf = $path . $f;
                if (is_dir($rf)) {
                    $galloc = substr($rf, strlen("$gallerypath/"));
                    list($gallid) = mysql_fetch_row(mysql_query("SELECT gallid FROM $prefix"._4nalbum_categories." where galloc='$galloc'"));
                    mysql_query("DELETE FROM $prefix"._4nalbum_categories." WHERE gallid=$gallid");
                    mysql_query("DELETE FROM $prefix"._4nalbum_pictures." WHERE gid=$gallid");
                     mysql_query("DELETE FROM 4nalbum_template WHERE gid=$gallid");
                    rdel($rf);
                }
                else
                    unlink($rf);
            }
        }

        closedir($d);


        return rmdir($path);
    }
}

?>