###############################################################################
# UB_DoCloseTopic.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 DoCloseTopic {

# =========================================================================== #
# Checking Permission

  my ($Board) = &DB::BinarySelect (
    TABLE   => "Boards" ,
    COLUMNS => ["ID", "TITLE", "MAJOR_MODERATOR", "MINOR_MODERATORS", "STATUS", "CATEGORY"],                          ,
    KEY     => $UB::QUERY{'BID'}
  ) || &HTML::Error(MESSAGE => $DB::Error);

  my @Moderators = ();
  push (@Moderators, $Board->{'MAJOR_MODERATOR'});
  push (@Moderators, split (/,/, $Board->{'MINOR_MODERATORS'}) );

  my $isAdmin = 0;
  if ($UB::MEMBER{'LOGIN'} == 1) {
    $isAdmin = 1 if (($UB::MEMBER{'LEVEL'} eq "70") || ($UB::MEMBER{'LEVEL'} eq "100"));
    if ((!$isAdmin) && (($UB::MEMBER{'USERNAME'} eq "") || (@Moderators ne $UB::MEMBER{'USERNAME'}))) {
      &HTML::Error(CODE => "WRONG_MODERATOR");
    }
  } else {
    &Login();
  }

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

  my $BID = $UB::QUERY{'BID'};
  my $TID = $UB::QUERY{'TID'};

  (
    &DB::Update (
      TABLE  => "B".$BID,
      VALUES => {
        READ_ONLY => "(\${READ_ONLY} == 1) ? \"0\" : \"1\""
      },
      KEY    => $TID
    )
  ) || &HTML::Error(MESSAGE => $DB::Error);

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

  my %FORM = (
    "BOARD.ID"          => $Board->{'ID'}                 || "",
    "BOARD.TITLE"       => $Board->{'TITLE'}              || "",

    "TOPIC.ID"          => $TID                           || "",

    &Standard::ExtraVariables()
  );

  &Success(\%FORM);
}

###############################################################################
# Success function
# - function of printing success message of closing topic
sub Success {
  my ($FORM) = @_;

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

  &Standard::PrintHTMLHeader();
  print &Close_Topic_Success::Parse($FORM);
  exit;
}

sub Login {
  print "Location: $SYSTEM::UB_URL/UltraBoard.$SYSTEM::EXT?action=Login&refer=TopicCommands&BID=".$UB::QUERY{'BID'}."&TID=".$UB::QUERY{'TID'}."\n\n";
}

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