<?
include '../config.php';
include '../func.php';
head();
aut();
ban();
////////////////////////////////////////
####################Запись онлайна##################
mysql_query("UPDATE `profile` SET `online` = 'admin' WHERE `id` = '".$id_session."'");
####################################################

echo "<div class=\"head\"><b>Подарки</b></div>"; 


switch($_GET['act']){
default:

$all = mysql_result(mysql_query("SELECT count(*) FROM presents"), 0);
if($all){
$onpage = 5;
$allpage = ceil($all/$onpage);
$this_page = (isset($_GET['p'])) ? intval($_GET['p']) : 1 ;
if($this_page < 1 || $this_page > $allpage)$this_page = 1;
$start = $this_page*$onpage - $onpage;
$result = mysql_query("SELECT * FROM presents  limit ".$start.",".$onpage."");
while($pr = mysql_fetch_assoc($result)){
    $cat = mysql_fetch_assoc(mysql_query("SELECT * FROM `presents_cat` WHERE `id` = '".$pr['cat']."'"));
echo '<div class="link2"><img src="../'.$pr['url'].'"alt="."/><br/>';
echo 'Цена: <b>'.$pr['cena'].'</b><br/>';
echo 'Каталог: <b>'.$cat['name'].'</b><br/>';
echo "[<a href=\"presents.php?act=edit&id=".$pr['id']."\">Edit</a>]</div>";
}
echo '<div class="link1"><b>Страницы:</b> ';
navigation($allpage,"presents.php?p={p}",1,1);
}
break;

case 'upload':
    if (isset($_POST['submit'])) {
            $file_2 = $_FILES['f_brand2']['tmp_name'];
            $filename_2 = $_FILES['f_brand2']['name'];
            $type_2 = strtolower(substr($filename_2, 1+strrpos($filename_2,".")));
            $new_name_2 = 'file2-'.time().'.'.$type_2;
            if (!copy($file_2, "../presents/".$new_name_2)) echo 'no';
            
    if (mysql_query("INSERT INTO `presents` SET `url` = 'presents/".$new_name_2."', `cena` = '".num($_POST['cena'])."', `cat` = '".num($_POST['cat'])."'")) {
        echo 'Подарок успещно добавлен';
        } else {
        echo 'Возникла ошибка';
    }
    }
    echo '<form action="presents.php?act=upload" method="POST" name="form" enctype="multipart/form-data">';
    echo '<b>Подарок:</b><br /><input type="file" name="f_brand2"><br /><br />';
    echo '<b>Цена:</b><br/><input name="cena" type="text" maxlength="50"/><br/>';
    echo '<b>Каталог:</b><br/>';
    echo '<select name="cat">';
    $result = mysql_query("SELECT * FROM presents_cat");
    while($cat = mysql_fetch_assoc($result))
    echo '<option value="'.$cat['id'].'">'.$cat['name'].'</option>';
    echo '</select><br/>';
    echo '<input name="submit" type="submit" class="button" value="Загрузить" /></form>';
break;
    
case 'edit':
$id = num($_GET['id']);
$present = mysql_fetch_assoc(mysql_query("SELECT * FROM `presents` WHERE `id` = '".$id."'"));

if (isset($_POST['submit'])) {
    $cena = num($_POST['cena']);
    $cat = num($_POST['cat']);
    if (mysql_query("UPDATE `presents` SET `cena` = '".$cena."', `cat` = '".$cat."' WHERE `id` = '".$id."'"))
    echo 'Подарок изменен<br/>';
    else
    echo 'Возникла ошибка<br/>';
}

echo '<form action="presents.php?act=edit&id='.$id.'" method="post" name="form">';
echo '<b>Цена:</b><br/ > <input name="cena" type="text" value="'.$present['cena'].'" maxlength="25" /><br/ ><br/>';

echo '<select name="cat">';
$result = mysql_query("SELECT * FROM presents_cat");
    while($cat = mysql_fetch_assoc($result))
    echo '<option value="'.$cat['id'].'">'.$cat['name'].'</option>';
    echo '</select><br/>';
echo '<input name="submit" class="submit" type="submit" value="Изменить" /></form>';
break;

case 'cats':
$result = mysql_query("SELECT * FROM presents_cat");
while($cat = mysql_fetch_assoc($result))
    echo $cat['name'].' [<a href="presents.php?act=edit_cat&id='.$cat['id'].'">Edit</a>] [<a href="presents.php?act=del_cat&id='.$cat['id'].'">Del</a>]';
break;

case 'add_cat':
if ( isset($_POST['submit']) ) {
    
    $name = html( $_POST['name'] );
    if (mysql_query("INSERT INTO `presents_cat` SET `name` = '".$name."'"))
    echo 'Каталог успешно создан<br/>';
    else
    echo 'Возникла ошибка<br/>';
    
}
echo '<form action="presents.php?act=add_cat" method="post" name="form">';
echo '<b>Название раздела:</b><br/ ><input name="name" type="text" maxlength="50"/><br/ >';
echo '<input name="submit" class="submit" type="submit" value="Создать" /></form>';
break;

}

//////////////////////////////////////

navig();
foot();
?>