###############################################################################
# UB_Result.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 Result {
  my $tmpResult = $UB::QUERY{'tmp'};

  my (@FORM_BOARDS, @FORM_KEYWORDS, $FORM_BOOLEAN, $FORM_METHOD, $FORM_DATE_BOOLEAN, $FORM_DATE_DAYS, $FORM_SORT_ORDER, $FORM_SORT_FIELD, $FORM_VIEW);
  my @lines = ();
  my $FOUND = 0;

  if (($tmpResult) && (-e $SYSTEM::TEMP_PATH."/S".$tmpResult.".tmp")) {
    my $Commands;
    &Standard::FileOpen(*TEMP, "r", $SYSTEM::TEMP_PATH."/S".$tmpResult.".tmp");
      ($Commands, $FOUND, @lines) = <TEMP>;
    close (TEMP);
    chomp ($FOUND, @lines);

    my @Commands = split (/;/, $Commands);
    pop (@Commands);

    my %Command;
    foreach (@Commands) {
      my ($KEY, $VALUE) = (/^(\w+)\s=\s(.*)$/);
      $Command{$KEY} = $VALUE;
    }

    @FORM_BOARDS        = split (/ /, $Command{'FORM_BOARDS'});
    @FORM_KEYWORDS      = split (/ /, $Command{'FORM_KEYWORDS'});
    $FORM_BOOLEAN       = $Command{'FORM_BOOLEAN'};
    $FORM_METHOD        = $Command{'FORM_METHOD'};
    $FORM_DATE_BOOLEAN  = $Command{'FORM_DATE_BOOLEAN'};
    $FORM_DATE_DAYS     = $Command{'FORM_DATE_DAYS'};
    $FORM_SORT_ORDER    = $Command{'FORM_SORT_ORDER'};
    $FORM_SORT_FIELD    = $Command{'FORM_SORT_FIELD'};
    $FORM_VIEW          = $Command{'FORM_VIEW'};
  }

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

  my ($Categories, $CategoriesIndex) = &DB::Query (
    TABLE   => "Categories"   ,
    COLUMNS => ["ID", "TITLE"]
  );

  my ($Boards, $BoardsIndex) = &DB::Query (
    TABLE   => "Boards"       ,
    COLUMNS => ["ID", "TITLE", "CATEGORY"]
  );

  my $TotalPages  = &Standard::ceil( $FOUND, $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 = $FOUND if $End > $FOUND;

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

  my %EXTRA = &Standard::ExtraVariables();
  my %RESULT = (
    "SHOW.BOARDS.LIST"=> 1                     ,
    "BOARDS.LIST"     => &Standard::GetBoardsList("", "", "", 1, "", "") || "",

    "ADDRESS"         => $Result::DYNAMIC{'LANGUAGE.ADDRESS'},

    %EXTRA
  );

  my $LAST_BOARD = 0;
  LINE: for (my $l = $Start; $l < $End; $l++) {
    my $line = $lines[$l];
       $line =~ s/^\>//g;
    my @Result = split (/\0/, $line);

    if ($LAST_BOARD ne $Result[0]) {
      my %BOARD = (
        "CATEGORY.ID"     => $Categories->[ $CategoriesIndex->{ $Boards->[ $BoardsIndex->{ $Result[0] } ]{'CATEGORY'} } ]{'ID'},
        "BOARD.ID"        => $Result[0],
        "CATEGORY.TITLE"  => $Categories->[ $CategoriesIndex->{ $Boards->[ $BoardsIndex->{ $Result[0] } ]{'CATEGORY'} } ]{'TITLE'},
        "BOARD.TITLE"     => $Boards->[ $BoardsIndex->{ $Result[0] } ]{'TITLE'},

        %EXTRA
      );

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

      $LAST_BOARD = $Result[0];
    }

    my %HEADLINE = (
      "BOARD.ID"                    => $Result[0],
      "TOPIC.ID"                    => ($Result[2] == 0) ? $Result[1] : $Result[2],
      "HEADLINE.ID"                 => $Result[1],
      "HEADLINE.SUBJECT"            => $Result[3],
      "HEADLINE.USERNAME"           => $Result[4],
      "HEADLINE.NICKNAME"           => $Result[5],
      "HEADLINE.REPLIES"            => ($Result[2] == 0) ? $Result[6] : "~",
      "HEADLINE.LAST_POST_DATE"     => ($FORM_VIEW eq "TopicOnly") ? $Result[13] : $Result[10],
      "HEADLINE.LAST_POST_TIME"     => ($FORM_VIEW eq "TopicOnly") ? $Result[14] : $Result[11],
      "HEADLINE.SYMBOL"             => $Result[7],
      "HEADLINE.READ_ONLY"          => $Result[8],
      "HEADLINE.ATTACHMENT"         => $Result[9],

      %EXTRA
    );

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

  $RESULT{'RESULT.FOUND'}  = $FOUND || 0;

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

  &Standard::PrintHTMLHeader();
  print &Result::Parse(\%RESULT);
}

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