<?php


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

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

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

    exit;

}
*/

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

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

if (isset($_GET['tid'])) $topic_id=$_GET['tid'];

if (isset($_POST['tid'])) $topic_id=$_POST['tid'];

if (isset($_GET['pid'])) $post_id=$_GET['pid'];

if (isset($_POST['pid'])) $post_id=$_POST['pid'];

$cur_post_status = (isset($_POST['cps']))?$_POST['cps']:"";



# retrieve the template

$area = 'member';



switch ($mode) {

    case 'save':

        $txtTitle = mysql_escape_string($_POST['txtTitle']);

        $txtDescription = mysql_escape_string($_POST['txtDescription']);



        $lstStatus=$_POST['lstStatus'];

        $query = "UPDATE bb_topics SET topic_title='$txtTitle', topic_desc='$txtDescription', topic_time=NOW(), topic_status='$lstStatus' WHERE topic_id = '$topic_id'";

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

        //header("Location: $CONST_LINK_ROOT/admin/adm_bbtopics.php?PHPSESSID=".session_id());
echo "<script>location.href=\"admin_topics.php?PHPSESSID=.session_id()\"</script>";

        break;

    case 'approve':

        $query = "UPDATE bb_posts SET post_approved='1' WHERE topic_id = '$topic_id' AND post_id = '$post_id'";

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

        break;

    case 'delete':

        $query = "DELETE FROM bb_posts WHERE topic_id = '$topic_id' AND post_id = '$post_id'";

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

        break;

}

$query = "SELECT * FROM bb_topics WHERE topic_id = '$topic_id'";

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

$cur_topic = mysql_fetch_object($result);

$_where = ($cur_post_status !== "")? " AND post_approved = '$cur_post_status'":"";



$query = "	SELECT *

			FROM bb_posts

				LEFT JOIN members

					ON (poster_id=mem_userid)

			WHERE topic_id = '$topic_id' $_where

			ORDER BY post_time";

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



mysql_close( $link );



?>

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

  </tr>

  <tr>

    <td class="pageheader"><?=BB_TOPIC_EDIT_SECTION_NAME?></td>

  </tr>

  <tr>

    <td>

      <table width="100%"  border="0" cellspacing="<?php print("$CONST_SUBTABLE_CELLSPACING"); ?>" cellpadding="<?php print("$CONST_SUBTABLE_CELLPADDING"); ?>">

      <form enctype='multipart/form-data' method='post' action='<?php echo $CONST_LINK_ROOT?>/admin/admin_topics_edit.php<?php print("?PHPSESSID=".session_id()); ?>' name="FrmTEdit">

        <input type='hidden' name='tid' value='<?=$topic_id?>'>

        <input type='hidden' name='cps' value='<?=$cur_post_status?>'>

        <input type='hidden' name='mode' value='save'>

        <tr>

          <td class="tdhead" align="right">

            <select class="input"  size="1" name="cps" onChange="FrmTEdit.mode.value='view'; FrmTEdit.submit(); return true;">

                <option <?php if ($cur_post_status==0) print("selected"); ?> value="0"><?=BB_TOPIC_POST_WAIT?></option>

                <option <?php if ($cur_post_status==1) print("selected"); ?> value="1"><?=BB_TOPIC_POST_APPROVE?></option>

                <option <?php if ($cur_post_status==="") print("selected"); ?> value=""><?=BB_TOPIC_POST_ALL?></option>

            </select>

          </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 class="tdeven"><b><?=BB_TOPICS_TITLE?>:</b></td>

                  <td class="tdeven"><input type="text" name="txtTitle" size="40" value='<?=$cur_topic->topic_title?>'></td>

                </tr>

                <tr>

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

                  <td class="tdeven"><textarea name="txtDescription" rows="4" cols="80" class="input1"><?=$cur_topic->topic_desc?></textarea></td>

                </tr>

                <tr>

                  <td width="25%" class="tdodd"><b><?=BB_TOPICS_STATUS?>:</b></td>

                  <td width="75%" class="tdodd">

                    <select name="lstStatus" size="1" style="width:auto;" class="input">

                      <option value="1" <?php if ($cur_topic->topic_status) print("selected"); ?>>active</option>

                      <option value="0" <?php if (!$cur_topic->topic_status) print("selected"); ?>>not active</option>

                    </select>

                  </td>

                </tr>

                <tr>

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

                    <input type='submit' class='button' value='<?=GENERAL_SAVE?>'>

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

                  </td>

                </tr>

              </table>

            </td>

        </tr>

        <tr>

          <td class="tdhead"><?=BB_TOPIC_POSTS_SECTION_NAME?></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_TOPIC_POST_SENDER?></b></td>

                <td height='30' class='tdtoprow' align='center'><b><?=BB_TOPIC_POST_TEXT?></b></td>

                <td height='30' class='tdtoprow' align='center'><b><?=BB_TOPIC_POST_TIME?></b></td>

                <td height='30' class='tdtoprow' align='center'><b><?=BB_TOPIC_POST_STATUS?></b></td>

                <td height='30' class='tdtoprow'>&nbsp;</td>

              </tr>

<?php

    if (mysql_num_rows($posts_arr)>0) {

        while ($sql_array = mysql_fetch_object($posts_arr)) {

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

                        <td height='30' align='center' valign='middle'><a href='$CONST_LINK_ROOT/user_profile.php?PHPSESSID=".session_id()."&userid=$sql_array->poster_id'>$sql_array->mem_username</a></td>

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

                        <td height='30' align='center' valign='middle'>$sql_array->post_time</td>

                ");

                if (!$sql_array->post_approved) {

                    print(" <td height='30' align='center' valign='middle'><a href='$CONST_LINK_ROOT/admin/admin_topics_edit.php.php?$sess_id&mode=approve&tid=$sql_array->topic_id&pid=$sql_array->post_id'>".BB_TOPIC_POST_APPROVE."</a></td>");

                } else {

                    print(" <td height='30' align='center' valign='middle'>Approved</td>");

                }

                print(" <td height='30' align='center' valign='middle'><a href='$CONST_LINK_ROOT/admin/admin_topics_edit.php.php?$sess_id&mode=delete&tid=$sql_array->topic_id&pid=$sql_array->post_id'>".GENERAL_DELETE."</a></td>

                      </tr>");

        }

    }

?>

            </table>

          </td>

        </tr>

        <tr>

          <td class="tdfoot" align="center" colspan='5'>

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

          </td>

        </tr>

      </table>

      </form>

    </td>

  </tr>

</table>

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