<?php

include("../includes/site_config.php");

include('../session_handler.inc');

include ('../functions.php');

include('../error.php');

//include('../lib/class.DB.php');
$db = & db::getInstance();

/*
if ($Sess_UserType != "A") {

    exit;

}
*/


if (isset($_GET['mode'])) $mode=$_GET['mode'];

if (isset($_POST['mode'])) $mode=$_POST['mode'];

if (isset($_GET['recid'])) $recid=$db->escape($_GET['recid']);

if (isset($_REQUEST['fid'])) $_SESSION['forum_id'] = $db->escape($_GET['fid']);

$forum_id = $_SESSION['forum_id'];

$txtTitle = $db->escape($_POST['txtTitle']);

$txtDescription = $db->escape($_POST['txtDescription']);



$db = & db::getInstance();



# retrieve the template

$area = 'member';



switch ($mode) {

    case 'add':

        $query = "	INSERT INTO bb_topics

        				(topic_title, topic_time, topic_status,forum_id, topic_desc)

        			VALUES

        				('$txtTitle', NOW(), 1, '$forum_id', '$txtDescription')";

        $db->query($query);

        break;

    case 'save':

        $query="UPDATE bb_forum SET forum_title = '$txtTitle' WHERE forum_id = '$forum_id'";

        $db->query($query);

        break;

    case 'switchon':

        $query="UPDATE bb_topics SET topic_status = '1' WHERE topic_id = '$recid'";

        $db->query($query);

        break;

    case 'switchoff':

        $query="UPDATE bb_topics SET topic_status = '0' WHERE topic_id = '$recid'";

        $db->query($query);

        break;

    case 'delete':

        $query="DELETE FROM bb_posts WHERE topic_id = '$recid'";

        $db->query($query);

        $query="DELETE FROM bb_topics WHERE topic_id = '$recid'";

        $db->query($query);

        break;

}

$oForum = $db->get_row("SELECT * FROM bb_forum WHERE forum_id = '$forum_id'");

$query="SELECT t.topic_id, topic_title, topic_time, topic_status,

				COUNT(p1.post_id) as all_posts

		FROM bb_topics t

			LEFT JOIN bb_posts p1 ON (t.topic_id=p1.topic_id)

		WHERE forum_id = '$forum_id'

		GROUP BY t.topic_id, topic_title, topic_time, topic_status

		ORDER BY topic_time DESC";



$topics_arr = $db->get_results($query);

?>

<?php include("include/header.php"); ?>

<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="pageheader"><?=BB_TOPICS_SECTION_NAME?></td>

  </tr>

  <tr>

    <td>

      <table width="100%"  border="0" cellspacing="0" cellpadding="0">

      <form method="post" action="<?php echo $CONST_LINK_ROOT ?>/admin/admin_topics.php<?php print("?PHPSESSID=".session_id()); ?>" name="FrmEdit">

        <input type="hidden" name="mode">

        <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="tdhead">&nbsp;</td>

                </tr>

                <tr>

                  <td class="tdeven"><b><?=BB_TOPICS_TITLE?>:</b></td>

                  <td class="tdeven"><input type="text" name="txtTitle" size="40" value="<?=$oForum->forum_title?>" ></td>

                </tr>

                <tr>

                  <td colspan="2" class="tdfoot" align="center">

                    <input type='submit' class='button' value='<?=GENERAL_SAVE?>' onClick="FrmEdit.mode.value='save';">

                    <input type='button' class='button' value='<?=BUTTON_BACK?>' onClick="document.location='<?= $CONST_LINK_ROOT ?>/admin/adm_bbforums.php?<?php print("?PHPSESSID=".session_id()); ?>';">

                  </td>

                </tr>

              </table>

            </td>

        </tr>

	  </form>

      <form method="post" action="<?php echo $CONST_LINK_ROOT ?>/admin/admin_topics.php<?php print("?PHPSESSID=".session_id()); ?>" name="FrmTList">

        <input type="hidden" name="mode">



        <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 ($topics_arr as $sql_array) {

           $query="SELECT COUNT(post_id) as wait_posts FROM bb_posts WHERE post_approved=0 AND topic_id='$sql_array->topic_id'";

           $wait_res = mysql_query($query,$link) or die(mysql_error());

           $wait_posts = mysql_fetch_object($wait_res);

           $sql_array->topic_time =  date(" $CONST_FORMAT_DATE_SHORT $CONST_FORMAT_TIME_SHORT",strtotime($sql_array->topic_time));

           print("<tr onMouseOver='selected(this)' onMouseOut='deselected(this)' bgcolor='#DCE8FC'>

                    <td height='30' valign='middle'>$sql_array->topic_title</td>

                    <td height='30' align='center' valign='middle'>$sql_array->topic_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->topic_status) {

             print("<td height='30' align='center' valign='middle'><a href='$CONST_LINK_ROOT/admin/admin_topics.php?$sess_id&mode=switchoff&recid=$sql_array->topic_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_topics.php?$sess_id&mode=switchon&recid=$sql_array->topic_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_edit.php?$sess_id&tid=$sql_array->topic_id'>".GENERAL_DETAILS."</a></td>

                    <td height='30' align='center' valign='middle'><a href='$CONST_LINK_ROOT/admin/admin_topics.php?$sess_id&mode=delete&recid=$sql_array->topic_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="tdhead"><?=BB_TOPICS_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 class="tdeven"><?=BB_TOPICS_DESCRIPTION?>:</td>

                  <td class="tdeven"><textarea name="txtDescription" rows="4" cols="80" class="input1"></textarea></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/adm_bbforums.php?<?php print("?PHPSESSID=".session_id()); ?>';">

                  </td>

                </tr>

              </table>

            </td>

        </tr>

      </table>

      </form>

    </td>

  </tr>

</table>

<?php include("include/footer.php"); ?>