###############################################################################
# UB_Headlines.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 Headlines {
  my %Headlines;

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

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

  (
    my ($Category) = &DB::BinarySelect (
      TABLE   => "Categories",
      COLUMNS => ["ID", "TITLE", "LAST_UPDATE_SECOND"],
      KEY     => $Board->{'CATEGORY'}
    )
  ) || &HTML::Error(MESSAGE => $DB::Error);

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

      if ($READ_LEVEL_00 ne "*") {
        if ($UB::MEMBER{'LOGIN'} == 1) {
          &Login unless &Standard::GetBoardPermission ($Board->{'ID'}, "READ", $UB::MEMBER{'LEVEL'}, $UB::MEMBER{'GROUP'});
        } else {
          &Login;
        }
      }
    } elsif ($Board->{'STATUS'} == 30) {
      &HTML::Error(CODE => "BOARD_INACTIVATE");
    } elsif ($Board->{'STATUS'} != 70) {
      &HTML::Error(CODE => "BOARD_INACTIVATE");
    }
  }

  my ($Reply, $Post);

  if ( ( ($UB::MEMBER{'LOGIN'} == 1) && ($UB::MEMBER{'LEVEL'} ne "70") && ($UB::MEMBER{'LEVEL'} ne "100") ) || ($UB::MEMBER{'LOGIN'} == 0)) {
    if (($Board->{'STATUS'} == 50) || ($Board->{'STATUS'} == 70)) {
      &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;
      }
    }
  } elsif (($UB::MEMBER{'LEVEL'} ne "70") || ($UB::MEMBER{'LEVEL'} ne "100")) {
    $Reply = 1;
    $Post  = 1;
  }

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

  #push (
  #  @UB::COOKIES,
  #  $UB::CGI->cookie(
  #    -name     => "C".$Category->{'ID'}."_LAST_UPDATE",
  #    -value    => $Category->{'LAST_UPDATE_SECOND'}   ,
  #    -expires  => '+1Y'
  #  )
  #);

  #push (
  #  @UB::COOKIES,
  #  $UB::CGI->cookie(
  #    -name     => "B".$Board->{'ID'}."_LAST_UPDATE",
  #    -value    => $Board->{'LAST_UPDATE_SECOND'}   ,
  #    -expires  => '+1Y'
  #  )
  #);

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

# =========================================================================== #
# Printing Headlines

  my $TotalHeadlines = 0;

  my $DayPrune = $GENERAL::DAY_PRUNE;
     $DayPrune = $UB::QUERY{'DayPrune'} if ( ($UB::QUERY{'DayPrune'}) && (($UB::QUERY{'DayPrune'} > 0) || ($UB::QUERY{'DayPrune'} eq "*")));

  my ($Headlines, $HeadlinesIndex) = &DB::Query (
    TABLE   => "B".$UB::QUERY{'BID'},
    COLUMNS => [
      "ID"                ,
      "SUBJECT"           ,
      "USERNAME"          ,
      "NICKNAME"          ,
      "ROOT"              ,
      "NUMBER_REPLIES"    ,
      "LAST_POST_SECOND"  ,
      "LAST_POST_DATE"    ,
      "LAST_POST_TIME"    ,
      "SYMBOL"            ,
      "APROVED"           ,
      "READ_ONLY"         ,
      "ATTACHMENT"        ,
      "HITS"
    ]                                 ,
    WHERE   => {
      ROOT             => [0],
      APROVED          => [1],
      LAST_POST_SECOND => [($DayPrune eq "*") ? ($DayPrune) : (">".(time - ($DayPrune * 60 * 60 * 24)))]
    }                                 ,
    MATCH   => "ALL"                  ,
    SORT    => "LAST_POST_SECOND"     ,
    BY      => "Z-A"
  );

  $TotalHeadlines = scalar ( @{ $Headlines } ) if defined $Headlines;

  my $TotalPages  = &Standard::ceil( $TotalHeadlines, $GENERAL::HEADLINES_PER_PAGE);

  my $Page = 1;
     $Page = $UB::QUERY{'P'} if ( ($UB::QUERY{'P'}) && ($UB::QUERY{'P'} > 0) && ($UB::QUERY{'P'} <= $TotalPages));

  my $Start =  ($Page - 1) * $GENERAL::HEADLINES_PER_PAGE;
  my $End   = (($Page - 1) * $GENERAL::HEADLINES_PER_PAGE) + $GENERAL::HEADLINES_PER_PAGE;
     $End   = $TotalHeadlines if $End > $TotalHeadlines;
     $End  -= 1;

  #@{$Headlines} = @{$Headlines}[$Start..$End];
  $Headlines = [@{$Headlines}[$Start..$End]];

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

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

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

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

    "SHOW.DAY_PRUNE"  => 1                     ,
    "DAY_PRUNE"       => $DayPrune             ,

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

    "SHOW.BOARD.DESCRIPTION" => 1              ,

    "ADDRESS"         => "<a href=\"$SYSTEM::UB_URL/UltraBoard.$SYSTEM::EXT?action=Category&CID=".$Category->{'ID'}."&SID=".$UB::MEMBER{'SESSION'}."\" style=\"color:$UB::STYLE{'NORMAL_TEXT_COLOR'}\">".$Category->  {'TITLE'}."</a>",

    "BOARD.MAJOR_MODERATOR.USERNAME"  => $Board->{'MAJOR_MODERATOR'         },
    "BOARD.MAJOR_MODERATOR.NICKNAME"  => $Board->{'MAJOR_MODERATOR_NICKNAME'},
    "BOARD.MAJOR_MODERATOR.EMAIL"     => $Board->{'MAJOR_MODERATOR_EMAIL'   },

    "BOARD.HAVE.MAJOR_MODERATOR"      => ($Board->{'MAJOR_MODERATOR'})  ? 1 : 0,
    "BOARD.HAVE.MINOR_MODERATORS"     => ($Board->{'MINOR_MODERATORS'}) ? 1 : 0,

    &Standard::ExtraVariables()
  );

  HEADLINE: for (my $h = 0; $h <= $#$Headlines; $h++) {
    my $Headline = $Headlines->[$h];
    my %HEADLINE = (
      "BOARD.ID"                    => $Board   ->{'ID'               },
      "HEADLINE.ID"                 => $Headline->{'ID'               },
      "HEADLINE.SUBJECT"            => $Headline->{'SUBJECT'          },
      "HEADLINE.USERNAME"           => $Headline->{'USERNAME'         },
      "HEADLINE.NICKNAME"           => $Headline->{'NICKNAME'         },
      "HEADLINE.REPLIES"            => $Headline->{'NUMBER_REPLIES'   },
      "HEADLINE.LAST_POST_DATE"     => $Headline->{'LAST_POST_DATE'   },
      "HEADLINE.LAST_POST_TIME"     => $Headline->{'LAST_POST_TIME'   },
      "HEADLINE.HITS"               => $Headline->{'HITS'             },
      "HEADLINE.NUMBER_REPLIES"     => $Headline->{'NUMBER_REPLIES'   },
      "HEADLINE.SYMBOL"             => ($UB::STYLE{'ICONS'}->{ $Headline->{'SYMBOL'} }) ? $Headline->{'SYMBOL'} : "",
      "HEADLINE.SYMBOL.URL"         => $SYSTEM::IMAGE_URL."/".$SYSTEM::DEFAULT_THEME."/".$UB::STYLE{'ICONS'}->{ $Headline->{'SYMBOL'} },
      "HEADLINE.READ_ONLY"          => $Headline->{'READ_ONLY'        },
      "HEADLINE.ATTACHMENT"         => $Headline->{'ATTACHMENT'       },

      "HEADLINE.IS_NEW"             => (($LAST_VISITED - (24 * 60 * 60)) < $Headline->{'LAST_POST_SECOND'}) ? ( ($UB::CGI->cookie("T".$Board->{'ID'}."-".$Headline->{'ID'}) < $Headline->{'LAST_POST_SECOND'}) ? 1 : 0 ) : 0,
      "HEADLINE.IS_HOT"             => ($Headline->{'NUMBER_REPLIES'} >= $GENERAL::HOT) ? 1 : 0,
      "HEADLINE.IS_POP"             => ($Headline->{'HITS'} >= $GENERAL::POP) ? 1 : 0,

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

      &Standard::ExtraVariables()
    );

    $BOARD{'BOARD.HEADLINES'} .= &Threads_Headlines::Parse(\%HEADLINE);
  }

  $BOARD{'PAGE.CURRENT'}  = $Page;
  $BOARD{'PAGE.PREVIOUS'} = $Page - 1 if $Page > 1;
  $BOARD{'PAGE.NEXT'}     = $Page + 1 if $Page < $TotalPages;

  &Standard::PrintHTMLHeader();
  print &Threads::Parse(\%BOARD);
}

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

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