#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package Support::Client::ClientList;

use strict;
use warnings;
use Carp;

use lib '/app/tools/support/lib';
use Support::DB::Item::Client;
use Support::Client::Client;

use lib '/app/tools/common/lib';
use Common::Assert;

use Common::FormObject;
use base 'Common::FormObject';

sub _init {
    my ( $self, %args ) = @_;

    my $clientID   = $args{clientID};

    # clientFilter is a list of clients that the user has access to.  It is empty
    # if the user has access to all client sites.
    my $clientFilter = $args{clientFilter} || [];

    $self->SUPER::_init(%args);

    my $client;
    $self->{Client} = [];

    if ($clientID) {
        $client = Support::DB::Item::Client->Lookup( client_id => $clientID );
    } else {
        $client = Support::DB::Item::Client->GetAll();
    }

    while ( $client->hasNext() ) {
        my $clientData = $client->next();
        my $c = new Support::Client::Client( _dbItem => $clientData );

        # Check if the user has any client restrictions.  If so then we'll only show clients
        # that the user has access to.
        #
        if ( scalar @$clientFilter > 0 ) {
            my $_clientID = $c->ClientID();
            push( @{ $self->{Client} }, $c ) if ( grep( /^$_clientID$/, @$clientFilter ) );
        } else {
            push( @{ $self->{Client} }, $c )
        }
    }

    return $self;
}

sub Clients {
    my $self = shift;
    return @{ $self->{Client} };
}

1;
