<?

include 'connect.php';
include 'config.php';
include 'head.php';


if ($_GET['act']==add_url){
if (isset($_POST['url'])){
$q_vijet2=mysql_query("SELECT * FROM `vijet1` WHERE `id_user` = '$u[id]' LIMIT 4");
while ($vijet2 = mysql_fetch_assoc($q_vijet2))
{
$id_is = $id_is + 1;
mysql_query("UPDATE `vijet1` SET `vijet_text` = '".check(substr($_POST['t'.$id_is.''], 0, 50))."', `url` = '".check(substr($_POST['u'.$id_is.''], 0, 50))."' WHERE `id`='$vijet2[id]' AND `id_user` = '$u[id]'");
}
header("Location: start_set.php");
}

echo '<form action="?act=add_url&save" method="post">';
$q_vijet1=mysql_query("SELECT * FROM `vijet1` WHERE `id_user` = '$u[id]' LIMIT 4");
while ($vijet1 = mysql_fetch_assoc($q_vijet1))
{
$id_is = $id_is + 1;
echo '<div class="p_t" >';
echo 'Название: <input name="t'.$id_is.'" value="'.$vijet1['vijet_text'].'" maxlength="128"/><br />
Ссылка: <input name="u'.$id_is.'" value="'.$vijet1['url'].'" maxlength="255"/><br />';
echo '</div>';
}
echo '<input type="submit" name="url" value="Сохранить"/></form>';



}elseif ($_GET['act']==add){
if (isset($_GET['id']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `vijet_set` WHERE `id` = '".intval($_GET['id'])."'"),0))
{
mysql_query("INSERT INTO `vijet` (id_user, id_vijet, pos) values('$u[id]', '".intval($_GET['id'])."', '0')");
echo 'Пункт меню добавлен';
include 'foot.php';
}

$q_vijet2=mysql_query("SELECT * FROM `vijet_set`");
while ($vijet_set2 = mysql_fetch_assoc($q_vijet2))
{
echo '<div class="p_t">';
echo '<img src="'.$vijet_set2['img'].'" alt="">'.$vijet_set2['text'].'';

if (mysql_result(mysql_query("SELECT COUNT(*) FROM `vijet` WHERE `id_user` = '$u[id]' && `id_vijet` = '".$vijet_set2['id']."'"),0) == '0')
echo 'Настройки <div style="float:right;"><a href="?act=add&id='.$vijet_set2['id'].'"><button type="submit" name="del3" value="1">Добавить</button></a>
</div>
<br style="clear:both;"/>';
echo '</div>';
}
echo '<a href="?">Назад</a>';
}else{

if (isset($_GET['id']) && isset($_GET['act']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `vijet` WHERE `id` = '".intval($_GET['id'])."' and `id_user` = '$u[id]'"),0))
{
$menu=mysql_fetch_assoc(mysql_query("SELECT * FROM `vijet` WHERE `id` = '".intval($_GET['id'])."' LIMIT 1"));
if ($_GET['act']=='up')
{
mysql_query("UPDATE `vijet` SET `pos` = '".($menu['pos'])."' WHERE `pos` = '".($menu['pos']-1)."' and `id_user` = '$u[id]' LIMIT 1");
mysql_query("UPDATE `vijet` SET `pos` = '".($menu['pos']-1)."' WHERE `id` = '".intval($_GET['id'])."' and `id_user` = '$u[id]' LIMIT 1");

//msg('Пункт меню сдвинут на позицию вверх');
}
if ($_GET['act']=='down')
{
mysql_query("UPDATE `vijet` SET `pos` = '".($menu['pos'])."' WHERE `pos` = '".($menu['pos']+1)."' and `id_user` = '$u[id]' LIMIT 1");
mysql_query("UPDATE `vijet` SET `pos` = '".($menu['pos']+1)."' WHERE `id` = '".intval($_GET['id'])."' and `id_user` = '$u[id]' LIMIT 1");

//msg('Пункт меню сдвинут на позицию вниз');
}
if ($_GET['act']=='del')
{
$del=mysql_fetch_array(mysql_query("SELECT * FROM `vijet` WHERE `id` = '".intval($_GET['id'])."' and `id_user` = '$u[id]' LIMIT 1"));
$links = mysql_query("SELECT * FROM `vijet` WHERE `pos` > '$del[pos]' ORDER BY `pos` ASC");
while ($p = mysql_fetch_array($links))
{
mysql_query("UPDATE `vijet` SET `pos` = '".($p['pos']-1)."' WHERE `id` = '$p[id]' LIMIT 1");
}
mysql_query("DELETE FROM `vijet` WHERE `id` = '".intval($_GET['id'])."' and `id_user` = '$u[id]' LIMIT 1");
echo "Пункт успешно удален";
include 'foot.php';
}
}



echo '<a href="?act=add"><button name="add1" value="1" style="margin-bottom:3px;">
<img src="/style/icons/add.png" class="icon" alt=""/> Добавить виджет</button></a>';
//Виджет 1
echo '<div class="block_2">';
$q_vijet1=mysql_query("SELECT * FROM `vijet1` WHERE `id_user` = '$u[id]' LIMIT 4");
echo "<img src='http://new-i09.spaces.ru/link_icon.gif' alt=''><b>Ссылки</b><br />";
while ($vijet1 = mysql_fetch_assoc($q_vijet1))
{
echo '&nbsp;<a href="'.$vijet1['url'].'">'.$vijet1['vijet_text'].'</a><br />';
}

echo '<a href="?act=add_url"><button name="le1" value="1"><img src="/style/icons/edit.png" alt="" class="icon"/> Ред</button></a></div>';
//
//виджеты
$q_vijet=mysql_query("SELECT * FROM `vijet` WHERE `id_user` = '$u[id]' ORDER BY pos");
while ($vijet = mysql_fetch_assoc($q_vijet))
{
$vijet_set=mysql_fetch_assoc(mysql_query("SELECT * FROM `vijet_set` WHERE `id` = $vijet[id_vijet] LIMIT 1"));
echo '<div class="block_2" >';
echo '<img src="'.$vijet_set['img'].'" alt=""><b><a href="'.$vijet_set['url'].'">'.$vijet_set['text'].'</a></b><br />';

echo '<div style="float:right;"><a href="?act=del&id='.$vijet['id'].'"><button type="submit" name="del3" value="1"><img src="/style/icons/cross.png"/> Удалить</button></a>
<a href="?act=up&id='.$vijet['id'].'"><button type="submit" name="mv3up" value="1" ><img src="/style/icons/up.png" class="icon" alt=""/> Выше</button></a>
<a href="?act=down&id='.$vijet['id'].'"><button type="submit" name="mv3down" value="1" ><img src="/style/icons/down.png" class="icon" alt=""/> Ниже</button></a>
</div>
<br style="clear:both;"/>
</div>';
}

echo '<a href="?act=add"><button name="add1" value="1" style="margin-bottom:3px;">
<img src="/style/icons/add.png" class="icon" alt=""/> Добавить виджет</button></a>';
}
include 'foot.php';
?>