#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::DB::Item::User;
use strict;
use warnings;
use lib '/app/tools/common/lib';
use Common::DB::Item;
use base 'Common::DB::Item';

use constant kTable => 'user';
use constant kDB    => Common::DB::Item::kCommonDB();

sub Search {
    my $class      = shift;
    my $searchTerm = shift;

    return unless $searchTerm;

    my $where;

    if ($searchTerm) {
        $where =
            "WHERE "
          . $class->SearchSyntax( field => "first_name", value => $searchTerm ) . " OR "
          . $class->SearchSyntax( field => "last_name",  value => $searchTerm ) . " OR "
          . $class->SearchSyntax( field => "email",      value => $searchTerm );
    } else {
        die "no valid param passed to album search";
    }

    my $sql = "SELECT * FROM " . kTable . " $where ORDER BY last_name, first_name, email";

    Common::Log::Debug("SQL: $sql");
    return $class->SUPER::GetAll($sql);
}

sub GetAllRoyaltyShare {
    my $class = shift;

    my $sql =
        "SELECT user.* FROM "
      . kTable
      . " INNER JOIN user_access USING (user_id) "
      . " INNER JOIN access_level ON ( access_level_id = access_level ) "
      . "WHERE access_level.name like 'RoyaltyShare%' "
      . "GROUP BY user.user_id "
      . "ORDER BY last_name ";

    return $class->SUPER::GetAll($sql);
}

1;
