###############################################################################
# UB_Category.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 Category {
  my (%Index);

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

  my ($Categories, $CategoriesIndex) = &DB::Query (
    TABLE   => "Categories" ,
    SORT    => "POSITION"   ,
    BY      => "A-Z"
  );

  my ($Boards, $BoardsIndex) = &DB::Query (
    TABLE   => "Boards"   ,
    SORT    => "POSITION" ,
    BY      => "A-Z"
  );

  %Index = %{ &Standard::GetBoardsList($Categories, $Boards, $BoardsIndex, "", "", "") };

# =========================================================================== #
# Printing Boards

  my $CID = $UB::QUERY{'CID'};

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

  my $time = time;

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

  my $Category = $Categories->[ $CategoriesIndex->{ $CID } ];

  $UB::COOKIES{"C".$Category->{'ID'}."_LAST_VISITED"} = $time;

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

    "ADDRESS"         => "",

    %EXTRA
  );

  if ( exists $Index { $Category->{'ID'} } ) {
    BOARD: for (my $b = 0; $b <= $#{ $Index { $Category->{'ID'} } }; $b++ ) {
      my $Board = $Boards->[ $BoardsIndex->{ $Index { $Category->{'ID'} }->[$b] } ];

      my ($Reply, $Post);

      if ( ( ($UB::MEMBER{'LOGIN'} == 1) && ($UB::MEMBER{'LEVEL'} ne "70") && ($UB::MEMBER{'LEVEL'} ne "100") ) || ($UB::MEMBER{'LOGIN'} == 0)) {
        &Standard::FileOpen(*DATA, "r", "$SYSTEM::DB_PATH/B".$Board->{'ID'}."/REPLY-LEVEL-00.dat");
          my $REPLY_LEVEL_00 = join ("", <DATA>);
        close (DATA);

        if ($REPLY_LEVEL_00 ne "*") {
          $Reply = 0;
          if ($UB::MEMBER{'LOGIN'} == 1) {
            $Reply = 1 if &Standard::GetBoardPermission ($Board->{'ID'}, "REPLY", $UB::MEMBER{'LEVEL'}, $UB::MEMBER{'GROUP'});
          }
        } else {
          $Reply = 1;
        }

        &Standard::FileOpen(*DATA, "r", "$SYSTEM::DB_PATH/B".$Board->{'ID'}."/POST-LEVEL-00.dat");
          my $POST_LEVEL_00 = join ("", <DATA>);
        close (DATA);

        if ($POST_LEVEL_00 ne "*") {
          $Post = 0;
          if ($UB::MEMBER{'LOGIN'} == 1) {
            $Post = 1 if &Standard::GetBoardPermission ($Board->{'ID'}, "POST", $UB::MEMBER{'LEVEL'}, $UB::MEMBER{'GROUP'});
          }
        } else {
          $Post  = 1;
        }
      } else {
        $Reply = 1;
        $Post  = 1;
      }

      &Standard::FileOpen(*HEADER, "r", "$SYSTEM::DB_PATH/B".$Board->{'ID'}."/header.inc");
        my $HEADER = join ("", <HEADER>);
      close (HEADER);

      &Standard::FileOpen(*FOOTER, "r", "$SYSTEM::DB_PATH/B".$Board->{'ID'}."/footer.inc");
        my $FOOTER = join ("", <FOOTER>);
      close (FOOTER);

      #my $LAST_VISITED = $UB::CGI->cookie("B".$Board->{'ID'}."_LAST_UPDATE");
      my $LAST_VISITED = $UB::COOKIES{"B".$Board->{'ID'}."_LAST_UPDATE"};

      my %BOARD = (
        "BOARD.ID"                        => $Board->{'ID'                      },
        "BOARD.TITLE"                     => $Board->{'TITLE'                   },
        "BOARD.DESCRIPTION"               => $Board->{'DESCRIPTION'             },
        "BOARD.MAJOR_MODERATOR.USERNAME"  => $Board->{'MAJOR_MODERATOR'         },
        "BOARD.MAJOR_MODERATOR.NICKNAME"  => $Board->{'MAJOR_MODERATOR_NICKNAME'},
        "BOARD.MAJOR_MODERATOR.EMAIL"     => $Board->{'MAJOR_MODERATOR_EMAIL'   },
        "BOARD.TOTAL_POSTS"               => $Board->{'TOTAL_POSTS'             },
        "BOARD.TOTAL_TOPICS"              => $Board->{'TOTAL_TOPICS'            },
        "BOARD.LAST_POST_DATE"            => $Board->{'LAST_POST_DATE'          },
        "BOARD.LAST_POST_TIME"            => $Board->{'LAST_POST_TIME'          },
        "BOARD.LAST_UPDATE_DATE"          => $Board->{'LAST_UPDATE_DATE'        },
        "BOARD.LAST_UPDATE_TIME"          => $Board->{'LAST_UPDATE_TIME'        },

        "BOARD.HEADER"                    => $HEADER                             ,
        "BOARD.FOOTER"                    => $FOOTER                             ,

        "BOARD.READ"                      => 1                                   ,
        "BOARD.REPLY"                     => $Reply                              ,
        "BOARD.POST "                     => $Post                               ,

        "BOARD.IS_NEW"                   => (($LAST_VISITED - (2 * 60 * 60)) < $Board->{'LAST_UPDATE_SECOND'}) ? "1" : "0",

        %EXTRA
      );

      $CATEGORY{'CATEGORY.BOARDS'} .= &Category_Boards::Parse(\%BOARD);
    }
  }

  &Standard::PrintHTMLHeader();
  print &Category::Parse(\%CATEGORY);
}

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