<?php
include("../includes/site_config.php");
include('../session_handler.inc');
include ('../functions.php');
include('../error.php');

$db = & db::getInstance();
/*
if ($Sess_UserType != "A") {
    exit;
}*/

if (isset($_GET['mode'])) $mode=form_get('mode');
if (isset($_POST['mode'])) $mode=form_get('mode');
if (isset($_GET['recid'])) $recid=form_get('recid');
//if (isset($_REQUEST['approved'])) echo $approved=$_REQUEST['approved'];

# retrieve the template
$area = 'member';

switch ($mode) {
    case 'add':
        $txtTitle = mysql_escape_string($_POST['txtTitle']);
        $query = "	INSERT INTO bb_forum
        				(forum_title, forum_time, forum_status)
        			VALUES
        				('$txtTitle', NOW(), 1)";

        $db->query($query);
        break;
    case 'switchon':
        $query="UPDATE bb_forum SET forum_status = '1' WHERE forum_id = '$recid'";
        $db->query($query);
        break;
    case 'switchoff':
        $query="UPDATE bb_forum SET forum_status = '0' WHERE forum_id = '$recid'";
        $db->query($query);
        break;
    case 'delete':
        $query="DELETE FROM bb_forum WHERE forum_id = '$recid'";
        $db->query($query);

        $query="SELECT topic_id FROM bb_topics WHERE forum_id = '$recid'";
        $aId = $db->get_col($query);
        if (count($aId)){
            $aId[] = 0;
            $query_topics = join(",",$aId);
	        $query="DELETE FROM bb_topics WHERE topic_id = '$recid'";
	        $db->query($query);

	        $query="DELETE FROM bb_posts WHERE topic_id IN ($query_topics)";
	        $db->query($query);
        }
        break;
}
$query="SELECT f.forum_id, forum_title, forum_time, forum_status,
				COUNT(p1.post_id) as all_posts
		FROM bb_forum f
			LEFT JOIN bb_topics t
				ON (f.forum_id=t.forum_id)
			LEFT JOIN bb_posts p1
				ON (t.topic_id=p1.topic_id)
		GROUP BY t.forum_id, forum_title, forum_time, forum_status
		ORDER BY forum_time DESC";
$forums_arr = $db->get_results($query);
?>
<?php include("include/header.php"); ?>
<div style="width:680px; padding-left:10px">
<div class="borderpage"><div class="borderpage2">
<table width="<?php print("$CONST_TABLE_WIDTH"); ?>" align="<?php print("$CONST_TABLE_ALIGN"); ?>" border="0" cellspacing="<?php print("$CONST_TABLE_CELLSPACING"); ?>" cellpadding="<?php print("$CONST_TABLE_CELLPADDING"); ?>">
  <tr>
    <td align="right">
      <?php 	  //require_once("$CONST_INCLUDE_ROOT/user_status.inc.php");	  require_once("../user_status.inc.php");	  ?>
    </td>
  </tr>
  <tr>
    <td class="titlehead"><?=BB_FORUMS_SECTION_NAME?></td>
  </tr>
  <tr>
    <td>
      <form method="post" action="<?php echo $CONST_LINK_ROOT ?>/admin/admin_forums.php<?php print("?PHPSESSID=".session_id()); ?>" name="FrmTList">
        <input type="hidden" name="mode">
      <table width="100%"  border="0" cellspacing="0" cellpadding="0">
        <tr>
            <td class="tdhead">&nbsp;</td>
        </tr>
        <tr>
            <td align="center">
              <table border='0' cellpadding='2' cellspacing='1' width='100%'>
                <tr>
                  <td height='30' class="tdtoprow" align='center'><b><?=BB_TOPICS_TITLE?></b></td>
                  <td height='30' class="tdtoprow" align='center'><b><?=BB_TOPICS_TIME?></b></td>
                  <td height='30' class="tdtoprow" align='center'><b><?=BB_TOPICS_POSTS?></b></td>
                  <td height='30' class="tdtoprow" align='center'><b><?=BB_TOPICS_POSTS_WAIT?></b></td>
                  <td height='30' class="tdtoprow" align='center'><b><?=BB_TOPICS_STATUS?></b></td>
                  <td height='30' width='60' class="tdtoprow">&nbsp;</td>
                  <td height='30' width='60' class="tdtoprow">&nbsp;</td>
                </tr>
<?php
        foreach($forums_arr as $sql_array) {
           $query="	SELECT COUNT(post_id) as wait_posts
           			FROM bb_topics t
           				LEFT JOIN bb_posts p
           					ON (t.topic_id = p.topic_id)
           			WHERE post_approved=0 AND t.forum_id='$sql_array->forum_id'";

           $wait_posts = $db->get_row($query);
           $sql_array->topic_time =  date(" $CONST_FORMAT_DATE_SHORT $CONST_FORMAT_TIME_SHORT",strtotime($sql_array->forum_time));
           print("<tr onMouseOver='selected(this)' onMouseOut='deselected(this)' bgcolor='#DCE8FC'>
                    <td height='30' valign='middle'>$sql_array->forum_title</td>
                    <td height='30' align='center' valign='middle'>$sql_array->forum_time</td>
                    <td height='30' align='center' valign='middle'>$sql_array->all_posts</td>
                    <td height='30' align='center' valign='middle'>$wait_posts->wait_posts</td>
           ");
if ($sql_array->forum_status) {
             print("<td height='30' align='center' valign='middle'><a href='$CONST_LINK_ROOT/admin/admin_forums.php?$sess_id&mode=switchoff&recid=$sql_array->forum_id' title='Change status'>".ADM_PAYMENTS_DEACTIVATE."</a></td>");
} else {
             print("<td height='30' align='center' valign='middle'><a href='$CONST_LINK_ROOT/admin/admin_forums.php?$sess_id&mode=switchon&recid=$sql_array->forum_id' title='Change status'>".ADM_PAYMENTS_ACTIVATE."</a></td>");
}
           print("
                    <td height='30' align='center' valign='middle'><a href='$CONST_LINK_ROOT/admin/admin_topics.php?$sess_id&fid=$sql_array->forum_id'>".GENERAL_DETAILS."</a></td>
                    <td height='30' align='center' valign='middle'><a href='$CONST_LINK_ROOT/admin/admin_forums.php?$sess_id&mode=delete&recid=$sql_array->forum_id'>".GENERAL_DELETE."</a></td>
                  </tr>");
        }
?>
              </table>
            </td>
        </tr>
        <tr>
            <td class="tdfoot">&nbsp;</td>
        </tr>
        <tr>
            <td align="center">
              <table width="100%"  border="0" cellspacing="<?php print("$CONST_SUBTABLE_CELLSPACING"); ?>" cellpadding="<?php print("$CONST_SUBTABLE_CELLPADDING"); ?>">
                <tr>
                  <td colspan="2" class="admin-head2"><?=BB_FORUMS_ADD_NEW?></td>
                </tr>
                <tr>
                  <td class="tdeven"><b><?=BB_TOPICS_TITLE?>:</b></td>
                  <td class="tdeven"><input type="text" name="txtTitle" size="40" ></td>
                </tr>
				
                <tr>
                  <td colspan="2" class="tdfoot" align="center">
                    <input type='submit' class='button' value='<?=BUTTON_ADD?>' onClick="FrmTList.mode.value='add';">
                    <input type='button' class='button' value='<?=BUTTON_BACK?>' onClick="document.location='<?= $CONST_LINK_ROOT ?>/admin/index.php?<?php print("?PHPSESSID=".session_id()); ?>';">
                  </td>
                </tr>
              </table>
            </td>
        </tr>
      </table>
      </form>
    </td>
  </tr>
</table></div></div></div>
<?php include("include/footer.php"); ?>