#!/usr/bin/perl
# ----------------------------------------------------------------------------
# This software is Copyright (C) 2007, All Rights Reserved
# ----------------------------------------------------------------------------
# This program is protected by local and international copyright laws. Any
# use of this program is subject to the the terms of the license agreement
# included as part of this distribution archive. Any other uses are strictly
# prohibited without the written permission of the Vendor and all
# other rights are reserved.
# -----------------------------------------------------------------------------
# Warning : Modifying this program violates your license agreement!
# -----------------------------------------------------------------------------

package IT::App::Menu::List;

# load modules
use strict;

use IT::Globals;

# ------------------------------------------------------------------------------
# Function    : createUserSearchDbQueryFromKeyword
# Description :
# Usage       : my $userSearchDbQuery = &createUserSearchDbQueryFromKeyword($attr);
# ------------------------------------------------------------------------------

sub createUserSearchDbQueryFromKeyword {
  my $attr              = shift || die "no menu attributes defined!";

  # error checking
  if (ref $attr ne 'HASH') { die "menu attribute must be hash ref!"; }

  # escape user search keyword
  my $escapedUserSearchKeyword = &IT::DB::getEscapedValue($attr->{'keyword'});
  $escapedUserSearchKeyword    = &IT::DB::escapeWildcardChars($escapedUserSearchKeyword);

  # create query from user search keyword
  my $userSearchDbQuery;
  if ($escapedUserSearchKeyword ne '') {
    my $searchFields = &IT::DB::Schema::getTableAttributes($attr->{'tableNameWithPrefix'}, 'appDefaultSearchFields') || [];
    unless (@$searchFields) { die "No search fields specified for this table.  Specify search fields in Admin > Database Editor.\n"; }

    foreach my $searchField (@$searchFields) {
      my($dbFieldType) = &IT::DB::Schema::getFieldAttributes($attr->{'tableNameWithPrefix'}, $searchField, 'type');

      $userSearchDbQuery .= " OR " if $userSearchDbQuery;
      if ($dbFieldType eq 'int') { $userSearchDbQuery .= "$searchField = '$escapedUserSearchKeyword'"; }
      else                       { $userSearchDbQuery .= "$searchField CONTAINS ALL '$escapedUserSearchKeyword'"; }
    }
  }

  # add braces
  if ($userSearchDbQuery) { $userSearchDbQuery = "($userSearchDbQuery)"; }

  return $userSearchDbQuery;
}

# ------------------------------------------------------------------------------
# Function    : loadLastQueryValueFromCookie
# Description : return perPage and other values for menuName from cookie.  If no menuName
#               is specified return hash of menuNames and perpage values
#               Cookie format is: menuName:perPage=15;menuName:keyword=test;
# Usage       : my $specifiedQueryValue = &loadLastQueryValueFromCookie($menuName, $valueName);
# Usage       : my $allLastQueryValues  = &loadLastQueryValueFromCookie();
# ------------------------------------------------------------------------------

sub loadLastQueryValueFromCookie {
  my $menuName  = shift;
  my $valueName = shift;

  my @pairs = split /;/, $COOKIE->{'lastMenuQuery'};

  my $lastQueryValueByMenuName = {};
  foreach my $pair (@pairs) {
    my($menuAndValueName, $encodedValue) = split /=/, $pair;
    my($menuName, $valueName)            = split /:/, $menuAndValueName;
    my $value                            = &IT::Common::urlDecode($encodedValue);
    $lastQueryValueByMenuName->{$menuName}{$valueName} = $value;
  }

  if ($menuName && $valueName) { return $lastQueryValueByMenuName->{$menuName}{$valueName}; }
  else                         { return $lastQueryValueByMenuName; }
}

# ------------------------------------------------------------------------------
# Function    : saveLastQueryToCookie
# Description : save listed list query values to cookie.  Maintains existing values
#               from cookie if no new values are specified.
#               Cookie format is: menuName:perPage=15;menuName:keyword=test;
#
# Usage       : &saveLastQueryToCookie({
#                 menuName => $attr->{'menuName'},
#                 keyword  => $attr->{'keyword'},
#                 perPage  => $attr->{'recordsPerPage'},
#               });
# ------------------------------------------------------------------------------

sub saveLastQueryToCookie {
  my $args = shift;

  # error checking
  unless (ref $args eq 'HASH')        { die "args must be a hash ref!"; }
  unless (exists $args->{'menuName'}) { die "menuName argument not specified!"; }

  # get last query values
  my $allLastQueryValues  = &loadLastQueryValueFromCookie();

  # add/overwrite new values
  my $menuName = $args->{'menuName'};
  foreach my $queryName (keys %$args) {
    if ($queryName eq 'menuName') { next; }

    my $queryValue = $args->{$queryName};
    $allLastQueryValues->{$menuName}{$queryName} = $queryValue;
  }

  # set new cookie
  my $cookieValue;
  foreach my $menuName (keys %$allLastQueryValues) {
    foreach my $valueName (keys %{ $allLastQueryValues->{$menuName} }) {
      my $value        = $allLastQueryValues->{$menuName}{$valueName};
      my $encodedValue = &IT::Common::urlEncode($value);

      $cookieValue .= ";" if $cookieValue;
      $cookieValue .= "$menuName:$valueName=$encodedValue";
    }
  }

  &IT::Cookie::setCookie("lastMenuQuery", $cookieValue);
}

# ----------------------------------------------------------------------------
# end of file
1;

