<?php
include '../config.php';
include '../func.php';
head();
aut();
ban();

####################Запись онлайна##################
mysql_query("UPDATE `profile` SET `online` = 'service' WHERE `id` = '".$id_session."'");
####################################################
echo head ."Сервисы / <b>Афоризмы</b>".div.katalog;

switch ($_GET['mod']) {
    default:
    $asd2 = mysql_result(mysql_query("SELECT COUNT(*) FROM `aforism` WHERE `type`='yes';"), 0);
    $strt = mt_rand(1, $asd2);
    $nst = mysql_fetch_assoc(mysql_query("SELECT * FROM `aforism` WHERE `type`='yes' LIMIT $strt, 1"));
    if ($asd2 > 0 && !empty($nst['text'])) {
        $novm = mysql_result(mysql_query("SELECT COUNT(*) FROM `aforism` WHERE `type`='no';"), 0);
            if ($level_session >= 4 && $novm > 0)
                echo '<a href="aforism.php?mod=new">Новые</a> [' .$novm. ']</div>'; 
            
                echo $nst['text'] . '<br/>';
                echo 'Добавил(а): <b>' . user($nst['id']) . '</b>, '.date2($nst['time']).'<br/>';
            
    echo '<a href="aforism.php">Обновить</a> | <a href="aforism.php?mod=add">Добавить свой</a><br/>';
    }
    break;
    
    case 'add':
    if (isset($_POST['submit'])) {
        $text = html($_POST['text']);
        if (empty($text)) $err = 'Вы не ввели афоризм'; 
        if ($text == @mysql_result(@mysql_query("SELECT `text` FROM `aforism` WHERE `text` = '" . $text . "';"), 0)) $err = 'Такой афоризм уже есть в базе';

        if (mysql_query("INSERT INTO `aforism` SET `time`='" .time(). "', `id`='".$id_session."', `text`='" . $text . "', `type`='no';"))
            echo 'Спасибо! ваша фраза сохранена и ожидает проверку администратором.<br />';
            else
            echo 'Возникла ошибка. Обратитесь администратору<br />';
    }
    echo '<form action="aforism.php?mod=add" method="post">';
    echo '<textarea id="tarea" name="text" cols="30" rows="5" style="width: 99%;"></textarea><br/>';
    echo '<input type="submit" name="submit" value="Добавить" /><br /></form>';
    echo '<a href="aforism.php">Назад</a><br/>';
    break;
    
    case 'new':
    $all = mysql_result(mysql_query("SELECT COUNT(*) FROM `aforism` ;"), 0);
    $new = mysql_result(mysql_query("SELECT COUNT(*) FROM `aforism` WHERE `type`='no' ;"), 0);

    echo '<a href="aforism.php?mod=all">Смотреть/Редактировать все</a> [' . $all . ']</div>';
    echo ($new > 0 ? 'Новых записей [<span class="red">' . $new . '</span>]' : '<b>Новых нет!</b>') . '<br/>';
     
    if ($new > 0) {
        $req = mysql_query("SELECT * FROM `aforism` WHERE `type`='no' ORDER BY `time`"); 
        while ($res = mysql_fetch_assoc($req)) {
  
  
    echo 'Добавил <b>' .user($res['id']). '</b> '.date2($res['time']).'<br/>';
    echo $res['text'] . '<br />';
    echo '[<a href="aforism.php?mod=yes&id=' . $res['id'] . '">принять</a>] [<a href="aforism.php?mod=edit&id=' . $res['id'] . '">изм</a>] [<a href="aforism.php?mod=del&id=' . $res['id'] . '">удал</a>]';
    }
    }
    break;
    
    case 'yes':
    $nom = num($_GET['id']);

    if (!empty($nom)) {
        mysql_query("UPDATE `aforism` SET `type`='yes' WHERE `id`='" .$nom. "';");
    }
    header("location: aforism.php?mod=new");
    break;
   
    case 'del':
    $nom = num($_GET['id']);
    if (!empty($nom)) { 
        mysql_query("DELETE FROM `aforism` WHERE `id`='" .$nom. "';");
    }
    header("location: aforism.php?mod=new");
    break; 
    
    case 'edit':
    if (isset($_POST['submit'])) {
        $numb = num($_POST['uid']);
        $text = html($_POST['text']);
        mysql_query("UPDATE `aforism` SET `text`='" . $text . "', `type`='yes' WHERE `id`='" . $numb . "';");
        header("location: aforism.php?mod=new"); 
    } else {
        if (isset($_GET['id'])) {
  $qu = mysql_result(mysql_query("SELECT `text` FROM `aforism` WHERE `id`='" . num($_GET['id']) . "' LIMIT 1;"), 0);
  
  echo '<form action="aforism.php?mod=edit" method="POST"><p>Текст:<br /><textarea name="afor">' . $qu . '</textarea><input name="uid" type="hidden" value="' . num($_GET['id']) . '" /><br /><input type="submit" name="submit" style="margin-top:2px" value="Сохранить" /></p></form>';
} else { header("location: aforism.php?mod=new"); }
}
   break;
   
   case 'all':
   $all = mysql_result(mysql_query("SELECT COUNT(*) FROM `aforism` WHERE `type`='yes';"), 0);
   if ($all > 0) {
  echo 'Всего фраз ' . $all  . '<br/>';
  $req = mysql_query("SELECT * FROM `aforism` WHERE `type`='yes' ORDER BY `id` DESC");
 while ($res = mysql_fetch_assoc($req)) {

  echo 'Добавил <b>' . $res['name'] . '</b> (' . date2($res['time']) . ')<br />';
  echo $res['text'];
  echo '<br/>[<a href="aforism.php?mod=yes&id=' . $res['id'] . '">принять</a>] [<a href="aforism.php?mod=edit&id=' . $res['id'] . '">изм</a>] [<a href="aforism.php?mod=del&id=' . $res['id'] . '">удал</a>]';
    }
  



} else {
  echo '<div class="menu"><b>Фраз не найдено!</b></div>';
}
  echo '<div class="menu">&raquo; <a href="afor_admin.php">Назад</a></div>';
   break;

   


}

echo div;
navig();
foot();
?>