<?
include '../config.php';
include '../func.php';
head();
aut();
ban();
////////////////////////
$id = num($_GET['id']);
$clan = mysql_fetch_assoc(mysql_query("SELECT * FROM `clans` WHERE `id` = '".$id."'"));

if ( empty( $clan['id'] ) ) {
    
    echo "<div class=\"head\"><b>Ошибка</b></div><div class=\"main\">";  
    echo 'Данной группы не существует.<br/>';
    pod_navig();
    foot();
    exit();
    
}
echo head . "<b>Группа | ".$clan['name']."</b>" . div . katalog;   

switch ($_GET['mod']) {
    
    default:
    echo '<b>Обсуждения:</b><br/ >';
    
    $result = mysql_query("SELECT * FROM clans_forumt WHERE `clan` = '".$clan['id']."'");
    while($a = mysql_fetch_assoc($result)){
        echo '<b>Тема:</b> <a href="forum.php?mod=theme&id='.$a['id'].'">'.$a['name'].'</a><hr>';
    }

    break;
    
    case 'theme':
    $id = num($_GET['t']);
    $theme = mysql_fetch_assoc(mysql_query("SELECT * FROM `clans_forumt` WHERE `id` = '".$id."'"));
    if ( empty( $theme['id'] ) ) {
    echo 'Данной темы не существует.<br/>';
    navig();
    foot();
    exit();
    }   
    
    echo '<b>Обсуждение темы:</b> '.$theme['name'].' | Автор: '.user($theme['user']).'<br/ >';
    echo ''.$theme['msg'].'<br/><br/>';
    
        $result = mysql_query("SELECT * FROM clans_forump WHERE `theme` = '".num($_GET['t'])."'");
    while($a = mysql_fetch_assoc($result)){
        echo user($a['user']) . ' ' . date2($a['time']) . '<br/ >' . $a['msg'] . '<hr>';
    }
    mysql_query("SELECT * FROM `clans_user` WHERE `clan` = '".num($_GET['id'])."' and  `user` = '".$id_session."'");
    if( mysql_affected_rows()!= 0)
    echo "<b><a href=\"forum.php?mod=add_post&t=".$theme['id']."&id=".$theme['clan']."\">Написать</a></b><br/ >";
    break;
    
    case 'add_theme';
    
    if ( isset($_POST['submit']) ) {
        $name = html($_POST['name']);
        $msg = html($_POST['msg']);
        $cl = num($_GET['id']);
        
        if (empty($name)) header('Location: forum.php?id='.$_GET['id'].'&err=noname');
        if (empty($msg)) header('Location: forum.php?id='.$_GET['id'].'&err=nomsg');
        if (empty($cl)) header('Location: forum.php?id='.$_GET['id'].'&err=noclan');
        if (strlen($cl) > 50) header('Location: forum.php?id='.$_GET['id'].'&err=simvol');
        
        if(mysql_query("INSERT INTO `clans_forumt` SET `name` = '".$name."', `msg` = '".$msg."', `clan` = '".$cl."', `user` = '".$id_session."',`time` = '".time()."'")) {
            $last = mysql_insert_id();
            header('Location: forum.php?mod=theme&id='.$cl.'&t='.$last.'&good=add');
        } else {
            header('Location: forum.php?id='.$cl.'&err=noadd');
        }
        
        
    }
    echo '<form action="forum.php?mod=add_theme&id='.$clan['id'].'" method="post" name="form">';

    echo '<b>Название темы:</b> [max 50]<br/ >';
    echo '<input name="name" type="text" maxlength="50" /><br/ >';

    echo '<b>Текст:</b><br/>';
    echo '<textarea id="tarea" name="msg" cols="30" rows="5" style="width: 99%;"></textarea><br/ >';

    echo '<input name="submit" type="submit" class="button" value="Создать" /></form><br/ >';

    echo "<b><a href=\"../info.php?act=bb\">BB-Коды</a></b> <a href=\"../smiles.php?\">Смайлы</a><br/ >";
    break;
    
    case 'add_post':
    $t = num($_GET['t']);
    
    if (isset($_POST['submit'])) {
        
        $msg = html($_POST['msg']);
        $cl = num($_GET['id']);
        $t = num($_GET['t']);
        
        if(mysql_query("INSERT INTO `clans_forump` SET  `msg` = '".$msg."', `theme` = '".$t."', `user` = '".$id_session."',`time` = '".time()."'")) {
        
        header('Location: forum.php?mod=theme&t='.$t.'&id='.$cl.'&err=yesadd');
        } else {
        header('Location: forum.php?mod=theme&t='.$t.'&id='.$cl.'&err=noadd');
        }
    }
    echo '<form action="forum.php?mod=add_post&id='.$clan['id'].'&t='.num($_GET['t']).'" method="post" name="form">';

    echo '<b>Текст:</b><br/>';
    echo '<textarea id="tarea" name="msg" cols="30" rows="5" style="width: 99%;"></textarea><br/ >';

    echo '<input name="submit" type="submit" class="button" value="Написать" /></form><br/ >';

    echo "<b><a href=\"../info.php?act=bb\">BB-Коды</a></b> <a href=\"../smiles.php?\">Смайлы</a><br/ >";
    break;
    
}

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

pod_navig();
foot();
?>