<?php

/************************************************************************/
/* 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 editmedia($gid, $pid="") {
         global $prefix, $gallerypath, $basepath, $baseurl, $adminurl, $offset, $galleryvar, $font;

        $limit = 5;

        galleryHeader();
          navigationAdmin("<a class=\"".$font['title']."\" href=\"$adminurl\">"._GALCONFIG."</a><font class=\"".$font['title']."\"> &gt; </font><a class=\"".$font['title']."\" href=\"$adminurl&amp;do=editmedia\">"._GALVIEWMEDIA."</a><font class=\"".$font['title']."\"> &gt; Edit Existing Media</font>");

          echo "<br>";
          OpenTable();


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

        if (isset($pid) && $pid!="")
                $numrows = 1;
        else {
                $sql = "SELECT count(*) FROM $prefix"._4nalbum_pictures." WHERE gid=$gid";
                $result = mysql_query($sql);
                list($numrows) = mysql_fetch_row($result);
        }

        if ($numrows>0) {

                if (!isset($offset) || $offset=="") {
                        $offset=0;
                }
                $pages = intval($numrows/$limit);


                if (isset($pid) && $pid!="")
                        $sql = "SELECT p.pid, p.gid, c.galloc, p.img, p.counter, p.submitter, p.date, p.name, p.description, p.votes, p.rate, p.width, p.height, c.thumbwidth  FROM $prefix"._4nalbum_pictures." p, $prefix"._4nalbum_categories." c WHERE p.pid=$pid AND p.gid=$gid and p.gid=c.gallid limit $offset, $limit";
                else
                        $sql = "SELECT p.pid, p.gid, c.galloc, p.img, p.counter, p.submitter, p.date, p.name, p.description, p.votes, p.rate, p.width, p.height, c.thumbwidth  FROM $prefix"._4nalbum_pictures." p, $prefix"._4nalbum_categories." c WHERE p.gid=$gid and p.gid=c.gallid limit $offset, $limit";
                //echo $sql;
                $res = mysql_query($sql);

                if ($numrows%$limit) {
                        $pages++;
                }

                for ($i=1;$i<=$pages; $i++) {
                        $newoffset=$limit*($i-1);
                        if ($newoffset==$offset) {
                                $nav .= "[$i]";
                                $end = $limit*$i;
                                if ($end>$numrows)
                                        $end = $numrows;
                        }
                        else {
                                $nav .= "[<a class=\"".$font['normal']."\" href=\"$adminurl&do=editmedia&type=edit&category=$gid&offset=$newoffset\">$i</a>]";
                        }
                }
                $deb = $offset+1;

                 echo "
                <form method=\"POST\" action=\"$adminurl\">
                <input type=\"hidden\" name=\"do\" value=\"editmedia\">
                <input type=\"hidden\" name=\"type\" value=\"existing\">
                <input type=\"hidden\" name=\"limit\" value=\"$limit\">

                    <p align=\"center\"><font class=\"".$font['title']."\">"._GALDISPLAYMEDIAIN." $gallname<br>
                        "._GALNOWDISPLAYFILES." $deb - $end of $numrows<br>
                        "._GALJUMP2PAGE." $nav</font>
                    </p>";
                $i=0;
                print '<table width=\"100%\">';
                while (list($pid, $gid, $galloc, $img, $counter, $submitter, $date, $name, $description, $votes, $rate, $width, $height, $thumbnailwidth ) = mysql_fetch_array($res)) {
                        $dir="../album/$galloc/"; // the directory where your image's at with "/" at the end, leave it                                      // blank if you are calling the image from the same directory
                        $pic="$img"; // your image file name
                        $target++; // leave this alone
                        $nmdir="$basepath/public/"; // the directory where your nmimage.php's at with "/" at the end

                        $print = "<A HREF=\"#\" onMouseOver=\"window.status='pop up';return true\" onMouseOut=\"window.status='';return true\" onClick=\"window.open('$nmdir";
                        $print .= "nmimage.php?z=$dir$pic&width=$width&height=$height','$target','width=$width,height=$height,directories=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=no,left=0,top=0,screenx=50,screeny=50');return false\">";

                echo "
                  <tr>
                        <td width=\"100%\">
                        <table border=\"0\" cellpadding=\"5\" cellspacing=\"0\">
                        <tr>
                                <td>$print";
                                        //<a href=\"$gallerypath/$galloc/$img\" target=\"_new\">";
                                        $image = "$gallerypath/$galloc/$img";
                                        $thumb_dir = "$gallerypath/$galloc/thumb";
                                        $thumb = "$thumb_dir/$img";
                                        if(!file_exists($thumb))
                                        {
                                                if (!file_exists($thumb_dir) && $galleryvar['imageSoftware']!="browser")
                                                {
                                                        makeDir("$gallerypath/$galloc", "thumb");
                                                }
                                                $ext = substr($img, (strrpos($img,'.') +  1));
                                                $sql = "select filetype from $prefix"._4nalbum_media_types." where extension='$ext'";
                                                list($filetype) = mysql_fetch_row(mysql_query($sql));
                                                if ($filetype==1)
                                                        $thumb = RatioResizeImg("$gallerypath/$galloc/$img", $thumb, $gid);

                                                if ($thumb=="browser")
                                                        $thumb = $image;
                                                else
                                                        $thumb = getThumbnail($img, $galloc);
                                        }

                                        if ($filetype == 1)
                                        {
                                                print '<img src="'.$thumb.'" alt="'.$name.'" width="'.$thumbnailwidth.'" border="1">';
                                        }
                                        else
                                        {
                                                print '<img src="'.$thumb.'" alt="'.$name.'" border="1">';
                                        }
                                        echo"
                                        </a><br>
                                        <font class=\"".$font['normal']."\">"._GALFILENAME."</font><br>
                                        <font class=\"".$font['normal']."\">$img</font><br>
                                        <input type=\"hidden\" name=\"FileName$i\" size=\"14\" value=\"$img\">
                                        <input type=\"hidden\" name=\"FileId$i\" size=\"14\" value=\"$pid\">
                                        <font class=\"".$font['normal']."\">
                                        <INPUT TYPE=\"radio\" NAME=\"Task$i\" VALUE=\"Save\" checked><small>"._GALSAVECHANGES."</small><br>
                                        <INPUT TYPE=\"radio\" NAME=\"Task$i\" VALUE=\"Delete\"><small>"._GALDELETE."</small>
                                  </td>
                                  <td align=\"left\" valign=\"top\">
                                        <font class=\"".$font['normal']."\">"._GALSELECTCATEGORY."</font><br>
                                        <input type=\"hidden\" name=\"OldCategory$i\" size=\"14\" value=\"$gid\">
                                            <select size=\"1\" name=\"Category$i\">";

                                        $categories = listcategories("$gallerypath");
                                        $categories = explode(" ", trim($categories));
                                        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 ($j=0; $j<$nbtabs; $j++)
                                                        $tab .= "&nbsp;&nbsp;";
                                                if ($gallid==$gid)
                                                        echo "<option selected value=\"$gallid\">$tab$gallname</option> ";
                                                else
                                                        echo "<option value=\"$gallid\">$tab$gallname</option> ";
                                         }
                            echo "        </select><br>
                                        <font class=\"".$font['normal']."\">"._GALWIDTHXHEIGHT."</font><br>
                                        <input type=\"text\" name=\"Width$i\" size=\"4\" value=\"$width\"> x <input type=\"text\" name=\"Height$i\" size=\"4\" value=\"$height\"><br>
                                        <font class=\"".$font['normal']."\">"._GALMEDIANAME."<br></font>
                                        <input type=\"text\" name=\"MediaName$i\" size=\"14\" value=\"$name\"><br>
                                        <font class=\"".$font['normal']."\">"._GALDATEADDED."<br></font>
                                        <input type=\"text\" name=\"Date$i\" size=\"14\" value=\"$date\"><br>
                                        <font class=\"".$font['normal']."\">"._GALSUBMITTER."</font><br>
                                        <input type=\"text\" name=\"Submitter$i\" size=\"14\" value=\"$submitter\"><br>
                                        <font class=\"".$font['normal']."\">
                                            "._GALHITS." $counter<br>
                                            "._GALVOTES." $votes<br>
                                            "._GALRATING." $rate</font>
                                </td>
                                <td align=\"left\" valign=\"top\">
                                        <font class=\"".$font['normal']."\">"._GALDESCRIPTION."</font><br>
                                        <textarea  name=\"Description$i\" rows=\"9\" cols=\"25\">$description</textarea>
                                </td>
                        </tr>
                        </table>
                        </td>
                </tr>";
                $i++;
                } //while
                echo "</table>
                <p align=\"center\"><input type=\"submit\" value=\""._SAVECHANGES."\" name=\"add\"></p>
                </form>";
        }
        else
                echo "<br><font class=\"".$font['normal']."\">미디어 파일이 발견되지 않습니다.</font><br>";
        CloseTable();
        print '<br>';
        adminFooter();
}
?>