###############################################################################
# UB_TopicCommands.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 TopicCommands {
  my ($ERROR) = @_;

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

  (
    my ($Boards, $BoardsIndex) = &DB::Query (
      TABLE   => "Boards"       ,
      COLUMNS => ["ID", "TITLE", "MAJOR_MODERATOR", "MINOR_MODERATORS", "STATUS", "CATEGORY"],
      SORT    => "POSITION"     ,
      BY      => "A-Z"
    )
  ) || &HTML::Error(MESSAGE => $DB::Error);

  my ($Board) = $Boards->[ $BoardsIndex->{ $UB::QUERY{'BID'} } ];

  (
    my ($Categories, $CategoriesIndex) = &DB::Query (
      TABLE   => "Categories"   ,
      COLUMNS => ["ID", "TITLE"],
      SORT    => "POSITION"     ,
      BY      => "A-Z"
    )
  ) || &HTML::Error(MESSAGE => $DB::Error);

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

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

  my $isAdmin = 0;
  if ($UB::MEMBER{'LOGIN'} == 1) {
    if (($UB::MEMBER{'LEVEL'} eq "70") || ($UB::MEMBER{'LEVEL'} eq "100")) {
      $isAdmin = 1
    } else {
      foreach my $Moderator (@Moderators) {
        next if $Moderator eq "";
        if ($Moderator eq $UB::MEMBER{'USERNAME'}) {
          $isAdmin = 1;
          last;
        }
      }
    }

    if (!$isAdmin) {
      &HTML::Error(CODE => "WRONG_MODERATOR");
    }
  } else {
    &Login();
  }

# =========================================================================== #
# Printing Topic Commands Form

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

  my ($BoardOption, $Index) = &Standard::GetBoardsList($Categories, $Boards, $BoardsIndex, 1, "", $Board->{'ID'});

  my $SelectOptions;
  BOARD: for (my $b = 0; $b <= $#{ $Boards }; $b++) {
    my $Board = $Boards->[$b];

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

    if (($isAdmin) || (@Moderators eq $UB::MEMBER{'USERNAME'})) {
      my ($Category) = $Categories->[ $CategoriesIndex->{ $Board->{'CATEGORY'} } ];
      $SelectOptions .= "<option value=\"".$Board->{'ID'}."\">".$Category->{'TITLE'}." / ".$Board->{'TITLE'}."</option>";
    }
  }

  (
    my ($Topic) = &DB::BinarySelect (
      TABLE   => "B".$Board->{'ID'} ,
      COLUMNS => [
        "ID"                ,
        "SUBJECT"           ,
        "USERNAME"          ,
        "NICKNAME"          ,
        "ROOT"              ,
        "POST_SECOND"       ,
        "POST_DATE"         ,
        "POST_TIME"         ,
        "SYMBOL"            ,
        "APROVED"           ,
        "ATTACHMENT"
      ]                             ,
      KEY     => $UB::QUERY{'TID'}
    )
  ) || &HTML::Error (CODE => "POST_NOT_FOUND");

  if ($Topic->{'APROVED'} != 1) {
    &HTML::Error(CODE => "POST_NOT_APPROVED");
  }

  my ($Headlines) = &DB::Query (
    TABLE   => "B".$Board->{'ID'},
    COLUMNS => [
      "ID"                ,
      "SUBJECT"           ,
      "USERNAME"          ,
      "NICKNAME"          ,
      "ROOT"              ,
      "POST_SECOND"       ,
      "POST_DATE"         ,
      "POST_TIME"         ,
      "SYMBOL"            ,
      "APROVED"           ,
      "ATTACHMENT"
    ]                                 ,
    WHERE   => {
      ROOT             => [$Topic->{'ID'}],
      APROVED          => [1],
    }                                 ,
    MATCH   => "ALL"                  ,
    SORT    => "POST_SECOND"          ,
    BY      => "A-Z"
  );

  unshift (@$Headlines, $Topic);

  my %EXTRA = &Standard::ExtraVariables();

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

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

    "TOPIC.ID"          => $Topic->{'ID'}         ,

    "BOARDS.MOVE_LIST"  => $SelectOptions         ,

    "SHOW.BOARDS.LIST"  => 1                      ,
    "BOARDS.LIST"       => $BoardOption           ,

    %EXTRA
  );

  HEADLINE: for (my $h = 0; $h <= $#{ $Headlines }; $h++) {
    my $Headline = $Headlines->[$h];
    my %HEADLINE = (
      "BOARD.ID"                    => $Board   ->{'ID'               },
      "HEADLINE.ID"                 => $Headline->{'ID'               },
      "HEADLINE.NUMBER"             => $h + 1                          ,
      "HEADLINE.SUBJECT"            => $Headline->{'SUBJECT'          },
      "HEADLINE.USERNAME"           => $Headline->{'USERNAME'         },
      "HEADLINE.NICKNAME"           => $Headline->{'NICKNAME'         },
      "HEADLINE.POST_DATE"          => $Headline->{'POST_DATE'        },
      "HEADLINE.POST_TIME"          => $Headline->{'POST_TIME'        },
      "HEADLINE.SYMBOL"             => $Headline->{'SYMBOL'           },
      "HEADLINE.READ_ONLY"          => $Headline->{'READ_ONLY'        },
      "HEADLINE.ATTACHMENT"         => $Headline->{'ATTACHMENT'       },

      %EXTRA
    );

    $FORM{'TOPIC.HEADLINES'} .= &TopicCommands_Headlines::Parse(\%HEADLINE);
  }

  $FORM{'ERROR.MESSAGE'}    = ($Post::DYNAMIC{"LANGUAGE.".$ERROR}) ? $Post::DYNAMIC{"LANGUAGE.".$ERROR} : $ERROR;

  &Standard::PrintHTMLHeader();
  print &TopicCommands::Parse(\%FORM);
}

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_TopicCommands.src file
###############################################################################