<?php

include("../includes/site_config.php");
include('../session_handler.inc');
include('../error.php');
include('../functions.php');
include('../imagesizer.php');
$mode=$_GET['mode'];
include('../message.php');include_once __INCLUDE_CLASS_PATH."/class.ItemManager.php";$option_manager = new OptionManager();/*
if ($Sess_UserType != "A") {
    exit;
}*/

if (isset($_POST['SUBMIT'])) {


    $max_size=$option_manager->GetValue('maxpicsize');

    if ($_FILES['picture']['size'] != 0) {
                if ($_FILES['picture']['size'] > $max_size) {
                $max_size=$max_size/1000;
                        error_page(sprintf(PRGADVERTISE_TEXT22,$max_size),GENERAL_USER_ERROR);
                }
                if ($_FILES['picture']['type'] != "image/pjpeg" && $_FILES['picture']['type'] != "image/jpeg" ) {
             
           error_page(PRGADVERTISE_TEXT23.$_FILES['picture']['type'],GENERAL_USER_ERROR);
                }

                $query_avatar = "INSERT INTO avatars
                                SET
                                    pic_id = '0'
                                ";
                $result_avatar = mysql_query($query_avatar);
                $last_insert_id = mysql_insert_id();
           // $oAvatar = mysql_fetch_array($result_lastid);
            $extension=".jpg";
            $filename=$last_insert_id.$extension;
            $targetfile="$CONST_INCLUDE_ROOT/members/avatar_".$filename;			$targetfile="../members/avatar_".$filename;
            if (copy($_FILES['picture']['tmp_name'],$targetfile)) {
                $query_picture = "INSERT INTO pictures
                    SET
                        pic_picture  = '/members/avatar_$last_insert_id.jpg', approved='1'
                ";
                $result_picture = mysql_query($query_picture);
                $last_pic_id = mysql_insert_id();
                $query_avatar = "UPDATE avatars
                                    SET
                                        pic_id = '$last_pic_id'
                                 WHERE
                                        avatar_id = '$last_insert_id'
                                ";
                $result_avatar = mysql_query($query_avatar);
            }

            if ($CONST_THUMBS == 'Y') {
                                $thumbfile=str_replace("members/", "thumbs/", $targetfile);
                                $new_w=60;
                                $new_h=66;
                                createthumb($targetfile, $thumbfile,$new_w,$new_h);
                                $thumbfile=str_replace("members/", "thumbs/large-", $targetfile);
                                $new_w=120;
                                $new_h=160;
                                createthumb($targetfile, $thumbfile,$new_w,$new_h);
            }
    }
} elseif(isset($_POST['DELETE'])) {
    if ( isset($_POST['avatar']) ) { // the message variable is a list of msg_ids to delete from the email

            foreach ($_POST['avatar'] as $key => $value) {
                    $arr_value = explode("/",$value);
                    $query_av     = "SELECT pic_id FROM avatars WHERE avatar_id = $arr_value[0]";
                    $result_av    = mysql_query($query_av,$link) or die(mysql_error());
                    $oPic         = mysql_fetch_object($result_av);
                    $query        = "DELETE FROM pictures WHERE pic_id = '".$oPic->pic_id."' AND pic_userid = 0";
                    $result       = mysql_query($query,$link) or die(mysql_error());
                    $query        = "DELETE FROM avatars WHERE avatar_id = $arr_value[0]";
                    $result       = mysql_query($query,$link) or die(mysql_error());
            }
        }
}

# retrieve the template
$area = 'member';

$query_avat = "SELECT * FROM avatars AS a
                    INNER JOIN pictures AS p
                        ON (a.pic_id = p.pic_id)
                    WHERE
                        a.avatar_id > 0
              ";
$sql_result = mysql_query($query_avat, $link);
?>
<?php include("include/header.php"); ?>
<div class="mainwidth">
<div class="admin-page-header"><div class="admin-page-heading">&nbsp;&nbsp;Avatars Admin</div></div>
<div class="clear"></div>
<div class="admin-page">
<table width="<?php print("$CONST_TABLE_WIDTH"); ?>" align="<?php print("$CONST_TABLE_ALIGN"); ?>" border="0" cellspacing="<?php print("$CONST_TABLE_CELLSPACING"); ?>" cellpadding="<?php print("$CONST_TABLE_CELLPADDING"); ?>">
        
    <form method="POST" action="<?php echo $CONST_LINK_ROOT?>/admin/admin_avatars.php<?php print("?PHPSESSID=".session_id()); ?>"  enctype="multipart/form-data">
    <tr>
      <td align="center">
        Upload new picture: <input type="file" name="picture"  class="normal">&nbsp;&nbsp;<input type="submit" name="SUBMIT" value="Submit" class="normal">
      </td>
    </tr>
    </form>
    <tr>
      <td>

	<table width="100%"  border="0" cellspacing="<?php print("$CONST_SUBTABLE_CELLSPACING"); ?>" cellpadding="<?php print("$CONST_SUBTABLE_CELLPADDING"); ?>">
    <tr class="pageheader" align="left">
      <td>
        <?=ADM_PICTURE?>
      </td>
      <td >
        <?=BUTTON_REMOVE?>
      </td>
     </tr>
     <form method="POST" action="<?php echo $CONST_LINK_ROOT?>/admin/admin_avatars.php<?php print("?PHPSESSID=".session_id()); ?>">
    <?php
      while($avatar = mysql_fetch_object($sql_result)) {
    ?>

        <tr align=left class="normal" >
          <td>
          <?
          //Scale Picture
          $standart_width = '300';
          $standart_height = '200';

          $image = @getimagesize($CONST_LINK_ROOT.$avatar->pic_picture);
          $curr_width = $image['0'];
          $curr_height = $image['1'];

          if ($curr_width > $standart_width) {
              $koef = $curr_width / $standart_width;
              $curr_width = $curr_width / $koef;

              $curr_height =  $curr_height / $koef;
          }

          if ($curr_height > $standart_height) {
              $koef = $curr_height / $standart_height;
              $curr_height = $curr_height / $koef;

              $curr_width =  $curr_width / $koef;
          }
		  
          ?>

            <img src="<?=$CONST_LINK_ROOT.$avatar->pic_picture?>" border="0" width="96" height="96">
          </td>
          <td>
            <input type="checkbox" name="avatar[]" id="avatar" value="<?=$avatar->avatar_id?>">
          </td>
        </tr>
    <?php
      }
    ?>
	<tr>
      <td colspan="2" align="right" class="tdfoot"><input type="submit" name="DELETE" value="Delete" class="normal"></td>
    </tr>
    </form>
	<tr>
      <td colspan="2" align="center" class="tdfoot">&nbsp; </td>
    </tr>

  </table>

	  </td>
    </tr>
</table></div><div class="admin-page-bottom"></div></div>
<?php include("include/footer.php"); ?>
