<?
if($ku['dostup']<1)
{
echo '<div class="block">';
echo '<img src="/images/smiles/zayac.gif"> Упс! Вход только админу.';
echo '</div>';
include_once 'inc/foot.php';
exit;
}
if(isset($_GET['cat']) && intval($_GET['cat'])!=NULL && mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_gift_cat` WHERE `id` = '".intval($_GET['cat'])."'"),0)!=0)
{
$cat=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_gift_cat` WHERE `id` = '".intval($_GET['cat'])."'"));
if(isset($_GET['add']))
{
echo "<div class='event'><h1><a href='?admin=$admin&cat=$cat[id]'>".hsc($cat['name'])."</a> / Добавить</h1></div>";
echo '<div class="content"><div class="block">';
if(isset($_POST['ok']))
{
if(isset($_FILES['file']))
{
$type = $_FILES['file']['type'];
if ($type!=='image/jpeg' && $type!=='image/jpg' && $type!=='image/gif' && $type!=='image/png'){$err=1;err_game('Это не картинка');}
}
else {$err=1;err_game('Выберите картинку');}
$name=$_POST['name'];
if(strlen2($name)<1){$err=1;err_game('Слишком короткое название');}
$cena=intval($_POST['cena']);
if(!is_numeric($cena) || strlen2($cena)<1){$err=1;err_game('Неверная цена');}
if(!isset($err))
{
$tmp = $_FILES['file']['tmp_name'];
mysql_query("INSERT INTO `kolhoz_gift` SET `name` = '$name', `cena` = '$cena', `id_cat` = '$cat[id]'");
$id=mysql_insert_id();
move_uploaded_file($tmp, 
"images/gifts/$id.png");
chmod(H."images/gifts/$id.png",0777);
header("location: ?admin=$admin&cat=$cat[id]");
}
}
echo "<form method=\"post\" enctype='multipart/form-data'>\n";
echo "Название подарка:<br /><input name=\"name\" type=\"text\" maxlength='32' value='' /><br />\n";
echo "Цена:<br /><input name=\"cena\" type=\"text\" maxlength='32' value='' /><br />\n";
echo "Изображение подарка (потом можно изменить):<br/><input type='file' name='file' /><br/>\n";
echo "<input value='Добавить' type='submit' name='ok' /><br />\n";
echo "</form>\n";
echo "</div>";
include_once 'inc/foot.php';
exit;
}





if(isset($_GET['edit']) && intval($_GET['edit'])!=NULL && mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_gift` WHERE `id` = '".intval($_GET['edit'])."'"),0)!=0)
{
$edit=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_gift` WHERE `id` = '".intval($_GET['edit'])."'"));


if(isset($_GET['change_img']))
{
echo "<div class='event'><h1><a href='?admin=$admin&cat=$cat[id]'>".hsc($cat['name'])."</a> / Заменить изображение</h1></div>";
echo '<div class="content"><div class="block">';
if(isset($_POST['ok']))
{
if(isset($_FILES['file']))
{
$type = $_FILES['file']['type'];
if ($type!=='image/jpeg' && $type!=='image/jpg' && $type!=='image/gif' && $type!=='image/png'){$err=1;err_game('Это не картинка');}
}
else {$err=1;err_game('Выберите картинку');}
if (!isset($err))
{
$tmp = $_FILES['file']['tmp_name'];
unlink("images/gifts/$edit[id].png");
move_uploaded_file($tmp, 
"images/gifts/$edit[id].png");
chmod("images/gifts/$edit[id].png",0777);
header("Location:?admin=$admin&cat=$cat[id]&edit=$edit[id]");
}
}
echo "<form method=\"post\" enctype='multipart/form-data'>\n";
echo "Изображение подарка:<br/><input type='file' name='file' /><br/>\n";
echo "<input value='Сохранить' type='submit' name='ok' /></form><br />\n";
echo "</div>";
include_once 'inc/foot.php';
exit;
}

echo "<div class='event'><h1><a href='?admin=$admin&cat=$cat[id]'>".hsc($cat['name'])."</a> / Редактировать</h1></div>";
echo '<div class="content"><div class="block">';

if(isset($_POST['ok']))
{
$name=$_POST['name'];
if(strlen2($name)<1){$err=1;err_game('Слишком короткое название');}
$cena=intval($_POST['cena']);
if(!is_numeric($cena) || strlen2($cena)<1){$err=1;err_game('Неверная цена');}
if (!isset($err))
{
mysql_query("UPDATE `kolhoz_gift` SET `name` = '$name', `cena` = '$cena' WHERE `id` = '$edit[id]'");
header("Location:?admin=$admin&cat=$cat[id]");
}
}
echo "<ul>";
echo "<li style='padding-bottom:8px'>";
echo "<img width='48' height='48' src='images/gifts/$edit[id].png' alt='o' class='portrait'>";
echo "<div class='small'>";
echo "<a href='?admin=$admin&cat=$cat[id]&edit=$edit[id]&change_img'>Заменить изображение</a>";
echo "</div></li></ul><div style='clear:both'></div>";
echo "<form method=\"post\">\n";
echo "Название подарка:<br /><input name=\"name\" type=\"text\" maxlength='32' value='$edit[name]' /><br />\n";
echo "Цена:<br /><input name=\"cena\" type=\"text\" maxlength='32' value='$edit[cena]' /><br />\n";
echo "<input value='Добавить' type='submit' name='ok' /></form>\n";
echo "<div><br/>&raquo; <a href='?admin=$admin&cat=$cat[id]&del=$edit[id]'>Удалить</a></div>";
echo "</div>";
include_once 'inc/foot.php';
exit;
}



if(isset($_GET['del']) && intval($_GET['del'])!=NULL && mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_gift` WHERE `id` = '".intval($_GET['del'])."'"),0)!=0)
{
$del=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_gift` WHERE `id` = '".intval($_GET['del'])."'"));
if(isset($_GET['ok']))
{
mysql_query("DELETE FROM `kolhoz_gift` WHERE `id` = '$del[id]'");
mysql_query("DELETE FROM `kolhoz_gifts` WHERE `id_gift` = '$del[id]'");
unlink("images/gifts/$del[id].png");
header("Location:?admin=$admin&cat=$cat[id]");
}
podtv("?admin=$admin&cat=$cat[id]&del=$del[id]&ok","?admin=$admin&cat=$cat[id]");
include_once 'inc/foot.php';
exit;
}

echo "<div class='event'><h1><a href='?admin=$admin'>$admin_name</a> / ".hsc($cat['name'])."</h1></div>";
echo '<div class="content"><div class="block">';


$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_gift` WHERE `id_cat` = '$cat[id]'"),0);
if ($k_post==0)
{
echo "Список пуст...";
}
$k_page=k_page($k_post,$set['p_str']);
$page=page($k_page);
$start=$set['p_str']*$page-$set['p_str'];
$q=mysql_query("SELECT * FROM `kolhoz_gift` WHERE `id_cat` = '$cat[id]' ORDER BY `id` DESC LIMIT $start, $set[p_str]");
echo "<ul>";
while($post=mysql_fetch_array($q))
{
echo "<li style='padding-bottom:8px'>";
echo "<a href='?admin=$admin&cat=$cat[id]&edit=$post[id]&change_img'><img width='48' height='48' src='images/gifts/$post[id].png' alt='o' class='portrait'></a>";
echo "<div class='small'>";
echo "<a href='?admin=$admin&cat=$cat[id]&edit=$post[id]'><span>".hsc($post['name'])."</span></a><span class='minor'>, цена: </span><span><img width='16' height='16' src='images/icons/money.png' alt='o'><span class='title'>$post[cena]</span></span></div><div style='clear:both'></div></li>";
}
if ($k_page>1)str("?admin=$admin&cat=$cat[id]&",$k_page,$page); // Вывод страниц
echo "<div>&raquo; <a href='?admin=$admin&cat=$cat[id]&add'>Добавить подарок</a></div>";
echo "</ul></div>";
include_once 'inc/foot.php';
exit;
}

elseif(isset($_GET['edit']) && intval($_GET['edit'])!=NULL && mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_gift_cat` WHERE `id` = '".intval($_GET['edit'])."'"),0)!=0)
{
echo "<div class='event'><h1><a href='?admin=$admin'>$admin_name</a> / Редактировать</h1></div>";
echo '<div class="content"><div class="block">';
$edit=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_gift_cat` WHERE `id` = '".intval($_GET['edit'])."'"));
if(isset($_POST['ok']))
{
$name=$_POST['name'];
if(strlen2($name)<1){$err=1;err_game('Слишком короткое название');}
if (!isset($err))
{
mysql_query("UPDATE `kolhoz_gift_cat` SET `name` = '$name' WHERE `id` = '$edit[id]'");
header("Location:?admin=$admin");
}
}

echo "<form method=\"post\">\n";
echo "Название категории:<br /><input name=\"name\" type=\"text\" maxlength='32' value='$edit[name]' /><br />\n";
echo "<input value='Сохранить' type='submit' name='ok' /></form><br />\n";
echo "</div>";
include_once 'inc/foot.php';
exit;
}
elseif(isset($_GET['add']))
{
echo "<div class='event'><h1><a href='?admin=$admin'>$admin_name</a> / Добавить</h1></div>";
echo '<div class="content"><div class="block">';
if(isset($_POST['ok']))
{
$name=$_POST['name'];
if(strlen2($name)<1){$err=1;err_game('Слишком короткое название');}
if (!isset($err))
{
mysql_query("INSERT INTO `kolhoz_gift_cat` SET `name` = '$name'");
header("Location:?admin=$admin");
}
}

echo "<form method=\"post\">\n";
echo "Название категории:<br /><input name=\"name\" type=\"text\" maxlength='32' value='' /><br />\n";
echo "<input value='Добавить' type='submit' name='ok' /></form>\n";
echo "<div><br/>&raquo; <a href='?admin=$admin&del=$edit[id]'>Удалить</a></div>";
echo "</div>";
include_once 'inc/foot.php';
exit;
}
if(isset($_GET['del']) && intval($_GET['del'])!=NULL && mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_gift_cat` WHERE `id` = '".intval($_GET['del'])."'"),0)!=0)
{
$del=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_gift_cat` WHERE `id` = '".intval($_GET['del'])."'"));
if(isset($_GET['ok']))
{
$q=mysql_query("SELECT * FROM `kolhoz_gift` WHERE `id_cat` = '$del[id]'");
while($post=mysql_fetch_array($q))
{
mysql_query("DELETE FROM `kolhoz_gift` WHERE `id` = '$post[id]'");
mysql_query("DELETE FROM `kolhoz_gifts` WHERE `id_gift` = '$post[id]'");
unlink("images/gifts/$post[id].png");
}
mysql_query("DELETE FROM `kolhoz_gift_cat` WHERE `id` = '$del[id]'");
header("Location:?admin=$admin");
}
podtv("?admin=admin&del=$del[id]&ok","?admin=$admin");
include_once 'inc/foot.php';
exit;
}
echo "<div class='event'><h1><a href='?admin=list'>Админка</a> / $admin_name</h1></div>";
echo '<div class="content"><div class="block">';
$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_gift_cat`"),0);
if ($k_post==0)
{
echo "Список пуст...";
}
$k_page=k_page($k_post,$set['p_str']);
$page=page($k_page);
$start=$set['p_str']*$page-$set['p_str'];
$q=mysql_query("SELECT * FROM `kolhoz_gift_cat` ORDER BY `id` DESC LIMIT $start, $set[p_str]");
while($post=mysql_fetch_array($q))
{
echo "<li><img src='images/icons/gift.png' alt='o' width='16' height='16'> <a href='?admin=$admin&cat=$post[id]'><span>".hsc($post['name'])."</span></a> <a hrer=''></li>";
}
if ($k_page>1)str("?admin=admin&",$k_page,$page); // Вывод страниц
echo "<div>&raquo; <a href='?admin=$admin&add'>Добавить кaтегорию</a></div>";
echo "</div>";
include_once 'inc/foot.php';
exit;
?>