###############################################################################
# CP_ListMessages.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 ListMessages {

  my ($Action, $Address, $Description, $Script, $Form, $Submit);
  if ($UB::QUERY{'FORM_ACTION'} eq "ReadOnlyMessages") {
    $Action      = "DoReadOnlyMessages";
    $Address     = "Close / Open messages";
    $Description = "Please select which message that you want to close / open them.";
    $Script      = "<SCRIPT LANGUAGE=\"JavaScript\"> function Confirm () { return confirm(\"Are you sure to close / open these message?\"); } </SCRIPT>";
    $Form        = " onSubmit = \"return Confirm();\"";
    $Submit      = "Close / Open";
  } elsif ($UB::QUERY{'FORM_ACTION'} eq "ModifyMessage") {
    $Action      = "ModifyMessage";
    $Address     = "Modify message";
    $Description = "Please choose which message that you want to modify.";
    $Script      = "";
    $Form        = "";
    $Submit      = "Next &gt;";
  } elsif ($UB::QUERY{'FORM_ACTION'} eq "MoveMessages") {
    $Action      = "MoveMessages";
    $Address     = "Move messages";
    $Description = "Please select which messages that you want to move.";
    $Script      = "";
    $Form        = "";
    $Submit      = "Next &gt;";
  } elsif ($UB::QUERY{'FORM_ACTION'} eq "RemoveMessages") {
    $Action      = "DoRemoveMessages";
    $Address     = "Remove messages";
    $Description = "Please select which messages that you want to remove.";
    $Script      = "<SCRIPT LANGUAGE=\"JavaScript\"> function Confirm () { return confirm(\"Are you sure to remove these messages?\"); } </SCRIPT>";
    $Form        = " onSubmit = \"return Confirm();\"";
    $Submit      = "Remove";
  }

  my $time = time;

  my $POST_SECOND;
  if (($UB::QUERY{'FORM_DATE_DAYS'} ne "*") && ($UB::QUERY{'FORM_DATE_DAYS'} ne "NA")) {
    if ($UB::QUERY{'FORM_DATE_BOOLEAN'} eq "GT") {
      $POST_SECOND = "<";
    } else {
      $POST_SECOND = ">";
    }
    $POST_SECOND .= $time - ($UB::QUERY{'FORM_DATE_DAYS'} * 60 * 60 * 24);
  } else {
    if ($UB::QUERY{'FORM_DATE_DAYS'} eq "*") {
      $POST_SECOND = "*";
    } else {
      $POST_SECOND = "";
    }
  }

  my @Results;
  foreach my $ID ($UB::CGI->param('FORM_BOARDS')) {
    my $Root = "*";
    if (($UB::QUERY{'FORM_ACTION'} eq "ReadOnlyMessages") || ($UB::QUERY{'FORM_ACTION'} eq "MoveMessages")) {
      $Root = 0;
    }
    my $Messages = [];
    ($Messages) = &DB::Query (
      TABLE   => "B".$ID,
      COLUMNS => ["ID", "ROOT", "SUBJECT", "USERNAME", "NICKNAME", "READ_ONLY", "POST_SECOND", "POST_DATE", "POST_TIME"],
      WHERE   => {
        POST_SECOND => [$POST_SECOND],
        ROOT        => [$Root]
      },
      MATCH   => "ALL",
      SORT    => $UB::QUERY{'FORM_SORT_FIELD'} || "",
      BY      => $UB::QUERY{'FORM_SORT_ORDER'} || "",
    );

    next if (ref $Messages ne "ARRAY");

    my $Keyword = $UB::QUERY{'FORM_KEYWORD'};
    $Keyword =~ s/^\s+//g;
    $Keyword =~ s/\s+$//g;

    if ($Keyword ne "") {
      foreach my $Message (@{ $Messages }) {
        $Message->{'BID'} = $ID;
        push (@Results, $Message) if (($UB::QUERY{'FORM_METHOD'} =~ /S/) && ($Message->{'SUBJECT'} =~ /$Keyword/i));
        push (@Results, $Message) if (($UB::QUERY{'FORM_METHOD'} =~ /U/) && ($Message->{'NICKNAME'} =~ /$Keyword/i));

        if ($UB::QUERY{'FORM_METHOD'} =~ /B/) {
          &Standard::FileOpen(*BODY, "r", $SYSTEM::DB_PATH."/B".$ID."/".$Message->{'ID'}.".src");
            my $Body = join ("", <BODY>);
          close (BODY);

          if ($Body =~ /$Keyword/i) {
            push (@Results, $Message);
          }
        }
      }
    } else {
      foreach my $Message (@{ $Messages }) {
        $Message->{'BID'} = $ID;
        push (@Results, $Message);
      }
    }
  }

  my $Found = scalar @Results;

  &Standard::PrintHTMLHeader();
  print &HTML::Header;
  print<<HTML;
$Script
<form action="$SYSTEM::CP_URL/Panel.$SYSTEM::EXT" method="post" name="form"$Form>
<input type="hidden" name="action" value="$Action">
<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> / Members Accounts Management /  $Address
    </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>
                      $Description (Found $Found messages)
                      </td>
                    </tr>
                  </table>
                </td>
              </tr>
            </table>
            &nbsp;<br>
            <table width="100%" border="1" cellspacing="0" cellpadding="2" bordercolordark="White" bordercolorlight="Gray">
              <tr height=24>
                <td nowrap>&nbsp;</td>
                <td width="70%">&nbsp;Subject&nbsp;</td>
                <td width="30%">&nbsp;Author&nbsp;</td>
                <td nowrap>&nbsp;Post date&nbsp;</td>
                <td nowrap>&nbsp;Read only&nbsp;</td>
              </tr>
HTML
  for (my $i = 0; $i <= $#Results; $i++) {
    my $Result = $Results[$i];
    my $Form;
    if ($UB::QUERY{'FORM_ACTION'} eq "ModifyMessage") {
      $Form = &HTML::Radio (
        NAME    => "PID"          ,
        VALUE   => "BID_".$Result->{'BID'}."-PID_".$Result->{'ID'}
      );
    } elsif (($UB::QUERY{'FORM_ACTION'} eq "ReadOnlyMessages") || ($UB::QUERY{'FORM_ACTION'} eq "MoveMessages") || ($UB::QUERY{'FORM_ACTION'} eq "RemoveMessages")) {
      $Form = &HTML::Checkbox (
        NAME    => "PID",
        VALUE   => "BID_".$Result->{'BID'}."-PID_".$Result->{'ID'},
      );
    }
    print<<HTML;
              <tr bgcolor="White">
                <td bgcolor="Silver" nowrap>$Form</td>
                <td width="70%">&nbsp;<a href="$SYSTEM::UB_URL/UltraBoard.$SYSTEM::EXT?action=Read&BID=$Result->{'BID'}&TID=${ \(($Result->{'ROOT'} != 0) ? $Result->{'ROOT'} : $Result->{'ID'}) }" target="UltraBoard">$Result->{'SUBJECT'}</a>&nbsp;</td>
                <td width="30%">&nbsp;$Result->{'NICKNAME'}&nbsp;</td>
                <td nowrap>&nbsp;${ \(($Result->{'POST_DATE'}) ? $Result->{'POST_DATE'}." ".$Result->{'POST_TIME'} : "n/a") }&nbsp;</td>
                <td align="center" nowrap>&nbsp;${ \(($Result->{'READ_ONLY'}) ? "YES" : "NO") }&nbsp;</td>
HTML
  }
print<<HTML;
            </table>
          </td>
        </tr>
      </table>
      <table width="100%" border="0" cellspacing="0" cellpadding="5">
        <tr>
          <td align="right"><input type="submit" value="$Submit">&nbsp;&nbsp;<input type="Reset" value="Cancel"></td>
        </tr>
      </table>
    </td>
  </tr>
</table>
</form>
</body>
</html>
HTML
}

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