###############################################################################
# CP_DoDisApprovePosts.src                                                    #
###############################################################################
# UltraBoard 2000 by UltraScripts.com, Inc.                                   #
# Scripts written by Jacky Wing Wa Yung, WebMaster@UltraScripts.com           #
# Available from http://www.UltraBoard.org/                                   #
# =========================================================================== #
# COPYRIGHT NOTICE :                                                          #
#                                                                             #
# Copyright (c) 1999-2000 UltraScripts.com, Inc. All Rights Reserved.         #
#                                                                             #
# This program is being distributed as shareware. It may be used and modified #
# free for personal, government or non-profit. Any commercial use must be     #
# registered. However, modified versions cannot be sold or distributed. You   #
# cannot alter the copyright and "powered by" notices throughout the scripts. #
# These notices must be clearly visible to the end users.                     #
#                                                                             #
# WARRANTY DISCLAIMER:                                                        #
#                                                                             #
# 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.                                           #
###############################################################################

package Source;

use strict;

sub DoDisApprovePosts {

  my $SendMail = 0;
  if ($SYSTEM::SENDMAIL == 1) {
    if ( ($SYSTEM::MAIL_SYSTEM eq "SMTP") && ($SYSTEM::SMTP_PATH ne "") ) {
      $SendMail = &Standard::Require("SMTP.lib", "$UB::LIBRARIES_PATH/SMTP.lib");
    } elsif ( ($SYSTEM::MAIL_SYSTEM eq "Sendmail") && ($SYSTEM::SENDMAIL_PATH ne "") ) {
      $SendMail = &Standard::Require("Sendmail.lib", "$UB::LIBRARIES_PATH/Sendmail.lib");
    }
  }

  &Standard::Require("Remove.lib", "$UB::LIBRARIES_PATH/Remove.lib");
  &Standard::Require("Thread.lib", "$UB::LIBRARIES_PATH/Thread.lib");

# =========================================================================== #
# Approve/Disapprove posts from Database

  &Standard::PrintHTMLHeader();

  if ($UB::QUERY{'Submit'} eq "Disapprove") {
    my @PIDS = $UB::CGI->param('PID');
    for (my $i = 0; $i <= $#PIDS; $i++) {
      my ($BID, $PID) = ($PIDS[$i] =~ /BID_(\d+)-PID_(\d+)/);

      my $Return = &Remove::Post (
        BOARD  => $BID,
        POST   => $PID,
        DETAIL => 0
      );
    }
  } else {
    (
      my ($Categories, $CategoriesIndex) = &DB::Query (
        TABLE   => "Categories",
        COLUMNS => ["ID", "TITLE"]
      )
    );

    my @PIDS = $UB::CGI->param('PID');
    for (my $i = 0; $i <= $#PIDS; $i++) {
      my ($BID, $PID) = ($PIDS[$i] =~ /BID_(\d+)-PID_(\d+)/);

      (
        my ($Board) = &DB::BinarySelect (
          TABLE   => "Boards",
          COLUMNS => ["ID", "TITLE", "CATEGORY"],
          KEY     => $BID
        )
      ) || &HTML::Error("Internal Database Error", $DB::Error);

      my $Category = $Categories->[ $CategoriesIndex->{ $Board->{'CATEGORY'} } ];

      (
        my ($Post) = &DB::BinarySelect (
          TABLE   => "B".$BID,
          KEY     => $PID
        )
      ) || &HTML::Error("Internal Database Error", $DB::Error);

      (
        &DB::Update (
          TABLE  => "B".$BID,
          VALUES => {
            APROVED => 1
          },
          KEY    => $PID
        )
      ) || &HTML::Error("Internal Database Error", $DB::Error);

      &Thread::Build (
        BOARD     => $BID,
        THREAD    => $PID,
      );

      if (($Post->{'ROOT'} > 0) && ($Post->{'REPLY_NOTIFY'} == 1) && ($GENERAL::NOTIFY_MEMBER_POST == 1)) {
        (
        my ($Topic) = &DB::BinarySelect (
          TABLE   => "B".$BID,
          COLUMNS => ["ID", "USERNAME", "NICKNAME", "EMAIL", "SUBJECT"],
          KEY     => $Post->{'ROOT'}
        )
      ) || &HTML::Error("Internal Database Error", $DB::Error);

        my %REPLY = (
          "CATEGORY.ID"       => $Category->{'ID'}              || "",
          "CATEGORY.TITLE"    => $Category->{'TITLE'}           || "",

          "BOARD.ID"          => $Board->{'ID'}                 || "",
          "BOARD.TITLE"       => $Board->{'TITLE'}              || "",

          "TOPIC.ID"          => $Topic->{'ID'}                 || "",
          "TOPIC.USERNAME"    => $Topic->{'USERNAME'}           || "",
          "TOPIC.NICKNAME"    => $Topic->{'NICKNAME'}           || "",
          "TOPIC.EMAIL"       => $Topic->{'EMAIL'}              || "",
          "TOPIC.SUBJECT"     => $Topic->{'SUBJECT'}            || "",

          "POST.ID"           => $Post->{'ID'}                  || "",
          "POST.USERNAME"     => $Post->{'USERNAME'}            || "",
          "POST.NICKNAME"     => $Post->{'NICKNAME'}            || "",
          "POST.EMAIL"        => $Post->{'EMAIL'}               || "",
          "POST.DATE"         => $Post->{'POST_DATE'}           || "",
          "POST.TIME"         => $Post->{'POST_TIME'}           || "",
          "POST.IP"           => $Post->{'IP'}                  || "",
          "POST.HOST"         => $Post->{'HOST'}                || "",
          "POST.SUBJECT"      => $Post->{'SUBJECT'}             || "",
          "POST.MESSAGE"      => $Post->{'MESSAGE'}             || "",
          "POST.ATTACHMENT"   => $Post->{'ATTACHMENT'}          || "",

          &Standard::ExtraVariables()
        );

        &Standard::Require("UB_Notify_Reply.src", "$UB::INTERFACES_PATH/$SYSTEM::DEFAULT_THEME/UB_Notify_Reply.src");

        &Mail::Send (
          SUBJECT => $Notify_Reply::DYNAMIC{'LANGUAGE.SUBJECT'},
          MESSAGE => &Notify_Reply::Parse(\%REPLY),
#          HEADER => $GENERAL::EMAIL_FORMAT,
          TO      => $Post->{'EMAIL'}
        );
      }
    }
  }

  &Success();
}

###############################################################################
# Success function
# - function of printing success message of de/activating members accounts
sub Success {
  my $text = "approved";
     $text = "disapproved" if ($UB::QUERY{'Submit'} eq "Disapprove");

  &Standard::PrintHTMLHeader();
  print &HTML::Header;
  print<<HTML;
<table width="100%" border="0" cellspacing="0" cellpadding="5">
  <tr bgcolor="Gray">
    <td><font color="White"><b>
      <a href="$SYSTEM::CP_URL/Panel.$SYSTEM::EXT?action=body" target="UBBODY" class="address">Control Panel</a>
    </b></font></td>
  </tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="3">
  <tr>
    <td>
      <table width="100%" border="0" cellspacing="0" cellpadding="2">
        <tr>
          <td>
            <table width="100%" border="1" cellspacing="0" cellpadding="3" bordercolordark="White" bordercolorlight="Gray">
              <tr>
                <td>
                  <table width="100%" border="0" cellspacing="0" cellpadding="2">
                    <tr>
                      <td>Those members accounts have been $text.</td>
                    </tr>
                  </table>
                </td>
              </tr>
            </table>
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>
</body>
</html>
HTML

  exit;
}

###############################################################################
1; # end of CP_DoDisApprovePosts.src file
###############################################################################