###############################################################################
# CP_DoCreateBoard.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 DoCreateBoard {

# =========================================================================== #
# Verify Variables

  # checking missing fields
  if ($UB::QUERY{'FORM_TITLE'}    eq "") { &Error("You forgot to fill the \"Title\" field.") };
  if ($UB::QUERY{'FORM_CATEGORY'} eq "") { &Error("You forgot to fill the \"Category\" field.") };

  # checking length of fields
  if (length($UB::QUERY{'FORM_TITLE'}) > 128) {
    &Error("The \"Title\" field cannot more than 128 characters.");
  }

  if (length($UB::QUERY{'FORM_DESCRIPTION'}) > 512) {
    &Error("The \"Description\" field cannot more than 512 characters.");
  }

# =========================================================================== #
# Assign Variables
  my %FORM;

  $FORM{'TITLE'}            = $UB::QUERY{'FORM_TITLE'};
  $FORM{'TITLE'}            = &Standard::EncodeHTML($FORM{'TITLE'});

  $FORM{'DESCRIPTION'}      = $UB::QUERY{'FORM_DESCRIPTION'};
  $FORM{'DESCRIPTION'}      = &Standard::EncodeHTML($FORM{'DESCRIPTION'});

  $FORM{'CATEGORY'}         = $UB::QUERY{'FORM_CATEGORY'};

  $FORM{'MAJOR_MODERATOR'}  = $UB::QUERY{'FORM_MAJOR_MODERATOR'};

  if ($FORM{'MAJOR_MODERATOR'} ne "") {
    if (
      my ($Profile) = &DB::BinarySelect (
        TABLE   => "Accounts",
        COLUMNS => ["USERNAME", "NICKNAME", "EMAIL"],
        KEY     => $FORM{'MAJOR_MODERATOR'}
      )
    ) {
      $FORM{'MAJOR_MODERATOR_NICKNAME'} = $Profile->{'NICKNAME'};
      $FORM{'MAJOR_MODERATOR_EMAIL'}    = $Profile->{'EMAIL'};
    } else {
      &HTML::Error("Internal Database Error", $DB::Error);
    }
  }

  $FORM{'MINOR_MODERATORS'} = join ( ",", $UB::QUERY{'FORM_MINOR_MODERATORS'} );

  if (
    ($UB::QUERY{'FORM_STATUS'} eq          "70")  ||  # id of activated status
    ($UB::QUERY{'FORM_STATUS'} eq    "Activate")
  ) {
    $FORM{'STATUS'}   = 70;                                 # id of activated status
  } elsif (
    ($UB::QUERY{'FORM_STATUS'} eq            "50")  ||  # id of activated status
    ($UB::QUERY{'FORM_STATUS'} eq "OnlyActivate")
  ) {
    $FORM{'STATUS'}   = 50;                                 # id of only activated status
  } else {
    $FORM{'STATUS'}   = 30;                                 # id of deactivated status
  }

  $FORM{'HEADER'} = $UB::QUERY{'FORM_HEADER'};

  $FORM{'FOOTER'} = $UB::QUERY{'FORM_FOOTER'};

  if (
    ($UB::QUERY{'FORM_ULTRABOARD_CODES'} eq "1" ) ||
    ($UB::QUERY{'FORM_ULTRABOARD_CODES'} eq "on")
  ) {
    $FORM{'UB_CODES'} = 1;
    if (
      ($UB::QUERY{'FORM_HTML_TAGS'} eq "1" ) ||
      ($UB::QUERY{'FORM_HTML_TAGS'} eq "on")
    ) {
      $FORM{'HTML_TAGS'} = 1;
    }
  }

  if (
    ($UB::QUERY{'FORM_FILE_ATTACH'} eq "1" ) ||
    ($UB::QUERY{'FORM_FILE_ATTACH'} eq "on")
  ) {
    $FORM{'FILE_ATTACH'} = 1;
  }

  if (
    ($UB::QUERY{'FORM_APPROVED'} eq "1" ) ||
    ($UB::QUERY{'FORM_APPROVED'} eq "on")
  ) {
    $FORM{'NEED_APROVED'} = 1;
  }

  ($FORM{'TITLE'}, $FORM{'DESCRIPTION'}) = &Standard::WordFilter($FORM{'TITLE'}, $FORM{'DESCRIPTION'});

# =========================================================================== #
# Add Board to Database

  (
    my $ID = &DB::Insert (
      TABLE  => "Boards",
      VALUES => \%FORM
    )
  ) || &HTML::Error ("Internal Database Error", $DB::Error);

  (
    &DB::Update(
      TABLE  => "Categories",
      VALUES => {
        TOTAL_BOARDS => "++\${TOTAL_BOARDS}"
      },
      KEY    => $FORM{'CATEGORY'}
    )
  ) || &HTML::Error ("Internal Database Error", $DB::Error);

  $FORM{'ID'} = $ID;

  &DB::CreateTable (
    TABLE       => "B".$ID,
    VALUE       => {
      ID                  => [ 0, "autonumber",    32, 1,           ""],
      SUBJECT             => [ 1,     "string",   128, 1,           ""],
      USERNAME            => [ 2,     "string",    16, 0,           ""],
      EMAIL               => [ 3,     "string",    64, 0,           ""],
      NICKNAME            => [ 4,     "string",    32, 0,           ""],
      MEMBER_LEVEL        => [ 5,     "string",    64, 0,           ""],
      MEMBER_GROUP        => [ 6,     "string",    32, 0,           ""],
      ROOT                => [ 7,     "number",     8, 0,          "0"],
      REPLIES             => [ 8,     "string",    -1, 0,           ""],
      NUMBER_REPLIES      => [ 9,     "number",     8, 0,          "0"],
      POST_SECOND         => [10,     "number",    16, 1,           ""],
      POST_DATE           => [11,     "string",    64, 1,           ""],
      POST_TIME           => [12,     "string",    64, 1,           ""],
      LAST_POST_SECOND    => [13,     "number",    16, 0,           ""],
      LAST_POST_DATE      => [14,     "string",    64, 0,           ""],
      LAST_POST_TIME      => [15,     "string",    64, 0,           ""],
      LAST_UPDATE_SECOND  => [16,     "number",    16, 0,           ""],
      LAST_UPDATE_DATE    => [17,     "string",    64, 0,           ""],
      LAST_UPDATE_TIME    => [18,     "string",    64, 0,           ""],
      TOTAL_MODIFIED      => [19,     "number",     3, 0,          "0"],
      HOST                => [20,     "string",   128, 0,           ""],
      IP                  => [21,     "string",    16, 0,           ""],
      REPLY_NOTIFY        => [22,     "number",     1, 0,          "0"],
      SIGNATURE           => [23,     "number",     1, 0,          "0"],
      SYMBOL              => [24,     "string",   256, 0,           ""],
      APROVED             => [25,     "number",     1, 0,          "1"],
      READ_ONLY           => [26,     "number",     1, 0,          "0"],
      ATTACHMENT          => [27,     "string",   512, 0,          "0"],
      HITS                => [28,     "number",     8, 0,          "0"]
    },
    PRIMARY_KEY => "ID"
  );

  &Standard::lock("file.lock") if ($SYSTEM::FLOCK == 1);

  chmod (0777, $SYSTEM::DB_PATH."/B$ID");

  &Standard::FileOpen(*TEXT, "w", "$SYSTEM::DB_PATH/B$ID/header.inc");
    print TEXT $FORM{'HEADER'};
  close (TEXT);

  &Standard::FileOpen(*TEXT, "w", "$SYSTEM::DB_PATH/B$ID/footer.inc");
    print TEXT $FORM{'FOOTER'};
  close (TEXT);

  if (
    ($UB::QUERY{'FORM_POST_LEVEL_00'} eq "1"  ) ||
    ($UB::QUERY{'FORM_POST_LEVEL_00'} eq "on" ) ||
    ($UB::QUERY{'FORM_POST_LEVEL_00'} eq "y"  ) ||
    ($UB::QUERY{'FORM_POST_LEVEL_00'} eq "yes")
  ) {
    &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/READ-LEVEL-00.dat");
      print DATA "*";
    close (DATA);
    &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/READ-LEVEL-30.dat");
      print DATA "*";
    close (DATA);
    &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/READ-LEVEL-50.dat");
      print DATA "*";
    close (DATA);

    &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/REPLY-LEVEL-00.dat");
      print DATA "*";
    close (DATA);
    &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/REPLY-LEVEL-30.dat");
      print DATA "*";
    close (DATA);
    &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/REPLY-LEVEL-50.dat");
      print DATA "*";
    close (DATA);

    &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/POST-LEVEL-00.dat");
      print DATA "*";
    close (DATA);
    &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/POST-LEVEL-30.dat");
      print DATA "*";
    close (DATA);
    &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/POST-LEVEL-50.dat");
      print DATA "*";
    close (DATA);
  } elsif (
    ($UB::QUERY{'FORM_READ_LEVEL_50'} eq ""  )
  ) {
    &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/READ-LEVEL-00.dat");
    close (DATA);
    &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/READ-LEVEL-30.dat");
    close (DATA);
    &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/READ-LEVEL-50.dat");
    close (DATA);

    &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/REPLY-LEVEL-00.dat");
    close (DATA);
    &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/REPLY-LEVEL-30.dat");
    close (DATA);
    &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/REPLY-LEVEL-50.dat");
    close (DATA);

    &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/POST-LEVEL-00.dat");
    close (DATA);
    &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/POST-LEVEL-30.dat");
    close (DATA);
    &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/POST-LEVEL-50.dat");
    close (DATA);
  } else {
    my @Permission;
    my %Level;

    if ($UB::QUERY{'FORM_READ_LEVEL_00'} eq "1") {
      &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/READ-LEVEL-00.dat");
        print DATA "*";
      close (DATA);
      &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/READ-LEVEL-30.dat");
        print DATA "*";
      close (DATA);
      &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/READ-LEVEL-50.dat");
        print DATA "*";
      close (DATA);
    } else {
      &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/READ-LEVEL-00.dat");
      close (DATA);

      if ( # checking the reading permission of normal member groups
        ($UB::QUERY{'FORM_READ_LEVEL_30'} eq "1"  ) ||
        ($UB::QUERY{'FORM_READ_LEVEL_30'} eq "on" ) ||
        ($UB::QUERY{'FORM_READ_LEVEL_30'} eq "y"  ) ||
        ($UB::QUERY{'FORM_READ_LEVEL_30'} eq "yes")
      ) {
        my @Input = $UB::CGI->param('FORM_READ_GROUP_30');
        &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/READ-LEVEL-30.dat");
          if ($#Input != -1) {
            if ($Input[0] eq "*") {
              print DATA "*";
            } else {
              print DATA join ( "\n", @Input );
            }
          }
        close (DATA);
      } else {
        &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/READ-LEVEL-30.dat");
        close (DATA);
      }

      if ( # checking the reading permission of premium member groups
        ($UB::QUERY{'FORM_READ_LEVEL_50'} eq "1"  ) ||
        ($UB::QUERY{'FORM_READ_LEVEL_50'} eq "on" ) ||
        ($UB::QUERY{'FORM_READ_LEVEL_50'} eq "y"  ) ||
        ($UB::QUERY{'FORM_READ_LEVEL_50'} eq "yes")
      ) {
        my @Input = $UB::CGI->param('FORM_READ_GROUP_50');
        &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/READ-LEVEL-50.dat");
          if ($#Input != -1) {
            if ($Input[0] eq "*") {
              print DATA "*";
            } else {
              print DATA join ( "\n", @Input );
            }
          }
        close (DATA);
      } else {
        &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/READ-LEVEL-50.dat");
        close (DATA);
      }
    }

    if ($UB::QUERY{'FORM_REPLY_LEVEL_00'} eq "1") {
      &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/REPLY-LEVEL-00.dat");
        print DATA "*";
      close (DATA);
      &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/REPLY-LEVEL-30.dat");
        print DATA "*";
      close (DATA);
      &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/REPLY-LEVEL-50.dat");
        print DATA "*";
      close (DATA);
    } else {
      &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/REPLY-LEVEL-00.dat");
      close (DATA);

      if ( # posting replies permission of normal member groups
        ($UB::QUERY{'FORM_REPLY_LEVEL_30'} eq "1"  ) ||
        ($UB::QUERY{'FORM_REPLY_LEVEL_30'} eq "on" ) ||
        ($UB::QUERY{'FORM_REPLY_LEVEL_30'} eq "y"  ) ||
        ($UB::QUERY{'FORM_REPLY_LEVEL_30'} eq "yes")
      ) {
        my @Input = $UB::CGI->param('FORM_REPLY_GROUP_30');
        &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/REPLY-LEVEL-30.dat");
          if ($#Input != -1) {
            if ($Input[0] eq "*") {
              print DATA "*";
            } else {
              print DATA join ( "\n", @Input );
            }
          }
        close (DATA);
      } else {
        &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/REPLY-LEVEL-30.dat");
        close (DATA);
      }

      if ( # posting replies permission of premium member groups
        ($UB::QUERY{'FORM_REPLY_LEVEL_50'} eq "1"  ) ||
        ($UB::QUERY{'FORM_REPLY_LEVEL_50'} eq "on" ) ||
        ($UB::QUERY{'FORM_REPLY_LEVEL_50'} eq "y"  ) ||
        ($UB::QUERY{'FORM_REPLY_LEVEL_50'} eq "yes")
      ) {
        my @Input = $UB::CGI->param('FORM_REPLY_GROUP_50');
        &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/REPLY-LEVEL-50.dat");
          if ($#Input != -1) {
            if ($Input[0] eq "*") {
              print DATA "*";
            } else {
              print DATA join ( "\n", @Input );
            }
          }
        close (DATA);
      } else {
        &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/REPLY-LEVEL-50.dat");
        close (DATA);
      }
    }

    &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/POST-LEVEL-00.dat");
    close (DATA);

    # posting topics permission of normal member groups
    if ($UB::QUERY{'FORM_POST_LEVEL_30'} eq "1") {
      my @Input = $UB::CGI->param('FORM_POST_GROUP_30');
      &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/POST-LEVEL-30.dat");
        if ($#Input != -1) {
          if ($Input[0] eq "*") {
            print DATA "*";
          } else {
            print DATA join ( "\n", @Input );
          }
        }
      close (DATA);
    } else {
      &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/POST-LEVEL-30.dat");
      close (DATA);
    }

    # posting topics permission of normal premium groups
    if ($UB::QUERY{'FORM_POST_LEVEL_50'} eq "1") {
      my @Input = $UB::CGI->param('FORM_POST_GROUP_50');
      &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/POST-LEVEL-50.dat");
        if ($#Input != -1) {
          if ($Input[0] eq "*") {
            print DATA "*";
          } else {
            print DATA join ( "\n", @Input );
          }
        }
      close (DATA);
    } else {
      &Standard::FileOpen(*DATA, "w", "$SYSTEM::DB_PATH/B$ID/POST-LEVEL-50.dat");
      close (DATA);
    }
  }

  &Standard::unLock("file.lock") if ($SYSTEM::FLOCK == 1);

  &Success(\%FORM);
}

###############################################################################
# Error function
# - function of printing error message of creating board
sub Error {
  my ($message) = @_;

  require "$UB::SOURCES_PATH/CP_CreateBoard.src";
  &Source::CreateBoard($message);

  exit;
}

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

  ( # querying data from categories table
    my ($DB) = &DB::BinarySelect (
      TABLE   => "Categories",
      COLUMNS => ["ID", "TITLE"],
      KEY     => $FORM->{'CATEGORY'}
    )
  ) || &HTML::Error("Boards Management / Create a new board", "No category available for you to choose yet.<br>Sorry, you have to <a href=\"$SYSTEM::CP_URL/Panel.$SYSTEM::EXT?action=CreateCategory\" target=\"UBBODY\">create a new category</a> first!");

  my $Category = $DB->{'TITLE'};

  my %Status = (
    30 => "Deactivate to everyone",
    50 => "Only activated to permited members",
    70 => "Activated to everyone"
  );

  &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> / Boards Management / Create a new Board
    </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>The board has been created, following is the information of this board.</td>
                    </tr>
                  </table>
                </td>
              </tr>
            </table>
            &nbsp;<br>
            &nbsp;&nbsp;General Information
            <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 width="100" valign="top" nowrap>Title</td>
                      <td width="100%">$FORM->{'TITLE'}</td>
                    </tr>
                    <tr>
                      <td width="100" valign="top" nowrap>Description</td>
                      <td width="100%">$FORM->{'DESCRIPTION'}</td>
                    </tr>
                    <tr>
                      <td width="100" valign="top" nowrap>Category</td>
                      <td width="100%">$Category</td>
                    </tr>
                    <tr>
                      <td width="100" valign="top" nowrap>Major Moderator</td>
                      <td width="100%">$FORM->{'MAJOR_MODERATOR'}</td>
                    </tr>
                    <tr>
                      <td width="100" valign="top" nowrap>Minor Moderators</td>
                      <td width="100%">$FORM->{'MINOR_MODERATORS'}</td>
                    </tr>
                    <tr>
                      <td width="100" valign="top" nowrap>Status</td>
                      <td width="100%">$Status{ $FORM->{'STATUS'} }</td>
                    </tr>
                  </table>
                </td>
              </tr>
            </table>
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>
</body>
</html>
HTML

  exit;
}

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