<?php
//
// Copyright (C) 2003 Jan de Graaff
// All rights reserved.
//
// This program uses parts of the original Simpleboard Application
// 0.7.0b written by Josh Levine; http://www.joshlevine.net
//
// This source file is part of the SimpleBoard Component, a Mambo 4.5
// custom Component By Jan de Graaff - http://tsmf.jigsnet.com
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// Please note that the GPL states that any headers in files and
// Copyright notices as well as credits in headers, source files
// and output (screens, prints, etc.) can not be removed.
// You can extend them with your own credits, though...
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
// The "GNU General Public License" (GPL) is available at
// http://www.gnu.org/copyleft/gpl.html.


//In this file you'll find code for Category/Forum Administration.
//Which code is executed depends on the $action variable passed throught the link calling
// Dont allow direct linking
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
global $my;
//securing form elements
$catid=(int)$catid;
$id=(int)$id;
$Itemid=(int)$Itemid;
//first check if this a moderator
$is_Mod=0;
if (!$is_admin)
{
   $database->setQuery("SELECT userid FROM #__sb_moderation WHERE catid='$catid' and userid='$my->id'");
   if ($database->loadResult() ) {$is_Mod=1;}
}else{ $is_Mod=1;} //superadmins always are

if (!$is_Mod) { die ("You are not a moderator!!<br />This error is logged and your IP address has been sent to the SuperAdmin(s) of this site; sorry..");}
//but we don't send the email; we might do that in the future, but for now we just want to scare 'em off..

//TODO: need to check if the mod has privs on this particular forum... no rush; we trust mods, don't we?

//******************************
//LIST MESSAGES
//******************************
if ($action == "list" ){
      ?>
      <script>
      function ConfirmDelete(url){
         if (confirm("Are you sure you want to delete this message? \n\n NOTE: There is NO way to retrieve deleted messages"))
            location.href=url;
      }
      function ConfirmApprove(url){
         if (confirm("Are you sure you want to approve this message?"))
            location.href=url;
      }
      </script>
      <link rel="stylesheet" href="components/com_simpleboard/template/<?php echo $sbConfig['template']; ?>/forum.css" type="text/css" />
      <table width="100%" border=0 cellspacing=1 cellpadding=3>
      <tr align="center">
         <td colspan="5" align="center">
            <p align="center">
              <table border="0" width="100%" align="center">
                <?php include ($sbp.'/searchbox.php');?>
                <tr>
                  <td align="center">
                    <b><font size="2"><?php echo $board_title;?></font></b>
                    <?php echo $sbConfig['board_offline'] ? '<br /><font color="red">' ._FORUM_IS_OFFLINE. '</font>' : '&nbsp;' ;?>
                  </td>
                </tr>
             </table>
             </form>
               <a href="<?php echo sefRelToAbs('index.php?option=com_simpleboard&amp;Itemid='.$Itemid);?>">
               <?php echo $sbIcons['home'] ? '<img src="'.$mls.'/modules/mod_sbicons/'.$sbIcons['home'].'" border=0 alt="'._HOME.'" />' : _HOME; ?>
               </a>
               <?php echo $sbIcons['back'] ?'':'| ';?><a href="<?php echo sefRelToAbs('index.php?option=com_simpleboard&amp;Itemid='.$Itemid.'&amp;func=showcat&amp;catid='.$catid);?>"">
                 <?php echo $sbIcons['back'] ? '<img src="'.$mls.'/modules/mod_sbicons/'.$sbIcons['back'].'" border=0 alt="'._BACK_TO_FORUM.'" />' : ' | '._BACK_TO_FORUM; ?>
               </a>
               <?php echo $sbIcons['help'] ?'':'| ';?><a href="<?php echo sefRelToAbs('index.php?option=com_simpleboard&amp;Itemid='.$Itemid.'&amp;func=faq');?>">
                  <?php echo $sbIcons['help'] ? '<img src="'.$mls.'/modules/mod_sbicons/'.$sbIcons['help'].'" border="0" alt="'._GEN_HELP.'" title="'._GEN_HELP.'"/>' : _GEN_HELP; ?>
               </a>
			 <?php
			 if ( $sbConfig['enableRulesPage'] ) {
	         	echo $sbIcons['rules'] ?'':'| ';
				echo '<a href="'.sefRelToAbs('index.php?option=com_simpleboard&Itemid='.$Itemid.'&func=rules').'">';
		        echo $sbIcons['rules'] ? '<img src="'.$mls.'/modules/mod_sbicons/'.$sbIcons['RULES'].'" border=0 alt="'._GEN_RULES.'" title="'._GEN_RULES.'"/>' : _GEN_RULES; 
				echo '</a>';
			 }?>			   
            </p>
            <p class="sectionname"><?php echo _MESSAGE_ADMINISTRATION; ?></p>
         </td>
      </tr>
      <tr height="10" class="sb_table_header">
         <th align="center"><b><?php echo _GEN_DATE; ?></b></th>
         <th width="8%" align="center"><b><?php echo _GEN_AUTHOR; ?></b></th>
         <th width="13%" align="center"><b><?php echo _GEN_SUBJECT; ?></b></th>
         <th width="55%" align="center"><b><?php echo _GEN_MESSAGE; ?></b></th>
         <th width="13%" align="center"><b><?php echo _GEN_ACTION; ?></b></th>
      </tr>
      <?php
      $nummess=0;
      $database->setQuery("SELECT * FROM #__sb_messages WHERE hold='1' AND catid=$catid ORDER BY id ASC");
      $allMes=$database->loadObjectList();
      $i=1;
      foreach ($allMes as $message){
         $threadids[]=$message->id;
         $messages[$message->pending][]=$message;
         $database->setQuery("SELECT message FROM #__sb_messages_text WHERE mesid='$message->id'");
         $num = $database->loadObjectList();
         $numtopics=count($num);
         $i= 1-$i;
         foreach($num as $messrow){}
         echo '<tr class="sb_message'.$i.'">';
         echo "<td valign=top>".date(_DATETIME , $message->time)."</td>";
         echo "<td valign=top>$message->name</td>";
         echo "<td valign=top><b>$message->subject<b></td>";
         echo "<td valign=top>".smile::smileReplace($messrow->message,0,$sbs,$sbConfig['disemoticons'])."</td>";
         echo "<td colspan=\"1\" valign=top>";
         echo "<form action=".sefRelToAbs('index.php?option='.$option.'&Itemid='.$Itemid.'&func=review&action=approve')." method=post>";
         $name= _MOD_APPROVE;
         echo "<input type=hidden name=id value=$message->id>";
         echo "<input type=hidden name=catid value=$catid>";
         echo "<input type=\"submit\" align=center class=\"button\" value=\"$name\" border=0>";
         echo "</form>";
         echo "<form action=".sefRelToAbs('index.php?option='.$option.'&Itemid='.$Itemid.'&func=review&action=delete')." method=post>";
         $name= _MOD_DELETE;
         echo "<input type=hidden name=id value=$message->id>";
         echo "<input type=hidden name=catid value=$catid>";
         echo "<input type=\"submit\" align=center class=\"button\" value=\"$name\" border=0>";
         echo "</form>";
         echo "</td></tr>";
         $nummess++;
      }
      if($nummess==0){
      ?>
         <tr><td colspan="2">&nbsp;</td>
         <td colspan="2">
         <p align="center">       
         <?php echo _MODERATION_MESSAGES; ?>
         </p>
         </td><td>&nbsp;</td>
         </tr>
      <?php } ?>
      <tr height="10" bgcolor="#e2e2e2">
         <td colspan=5>&nbsp;</td>
      </tr>
      </p>
      </table>
<?php
}
//******************************
//DELETE A MESSAGE
//******************************
else if ($action == "delete"){
   require_once("$sbp/sb_helpers.php");
   $id    = intval( mosgetParam( $_REQUEST, 'id'   , 1 ) );
   $catid = intval( mosgetParam( $_REQUEST, 'catid', 1 ) );
   $database->setQuery("delete from `#__sb_messages` WHERE `id` = $id");
   if($database->query()){
      $database->setQuery("delete from `#__sb_messages_text` WHERE `mesid` = '$id'");
      if ($database->query()){
//   	   sbRedirect ('index.php?option=com_simpleboard&Itemid='.$Itemid.'&func=review&action=list&catid='.$catid,3500,_POST_SUCCESS_DELETE);
     mosRedirect( "index.php?option=$option&Itemid=$Itemid&func=review&action=list&catid=$catid", "The post has been deleted" );
      }
   	  else{
//      	sbRedirect('index.php?option=com_simpleboard&Itemid='.$Itemid.'&func=review&action=list&catid='.$catid,3500,"ERROR: The post has been deleted but the text could not be deleted\n Check the #__sb_messages_text tabe for mesid=$id");
     	mosRedirect( "index.php?option=$option&Itemid=$Itemidfunc=review&action=list&catid=$catid", "ERROR: The post has been deleted but the text could not be deleted\n Check the #__sb_messages_text tabe for mesid=$id." );
      }
   }else{
//	sbRedirect('index.php?option=com_simpleboard&Itemid='.$Itemid.'&func=review&action=list&catid='.$catid,3500,"ERROR: The post could not be deleted");
  mosRedirect( "index.php?option=$option&Itemid=$Itemid&func=review&action=list&catid=$catid", "ERROR: The post could not be deleted" );
   }
}


//******************************
//APPROVE A MESSAGE
//******************************
else if ($action=="approve"){
   require_once("$sbp/sb_helpers.php");
   $id    = intval( mosgetParam( $_REQUEST, 'id'   , 1 ) );
   $catid = intval( mosgetParam( $_REQUEST, 'catid', 1 ) );
   $database->setQuery("UPDATE `#__sb_messages` SET `hold` ='0' WHERE `id` = '$id'");
   if($database->query()){
//  		sbRedirect('index.php?option=com_simpleboard&Itemid='.$Itemid.'&func=review&action=list&catid='.$catid,3500,"The post has been approved.");	
      mosRedirect( "index.php?option=$option&Itemid=$Itemid&func=review&action=list&catid=$catid", "The post has been approved" );
   }else{
//  		sbRedirect('index.php?option=com_simpleboard&Itemid='.$Itemid.'&func=review&action=list&catid='.$catid,3500,"ERROR: The post could not be approved.");
      mosRedirect( "index.php?option=$option&Itemid=$Itemid&func=review&action=list&catid=$catid", "ERROR: The post could not be approved" );
   }
}
?>