#------------------------------------------------------------ 
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------ 
package RPS::Contract::ProductFormatList;

use strict;
use Data::Dumper;

use lib '/app/tools/rps/lib';
use RPS::Contract::ProductFormat;

use RPS::XMLObject;
use base 'RPS::XMLObject';


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

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


    # I want to arrange these 'hierarchically'.
    # Each top-level format, followed by all sub levels.
    #
    my %map;

	my $collection = RPS::DB::Item::ProductFormat::GetAll();
	while ($collection->hasNext())
	{
		my $obj = $collection->next();

        # jpk - this is a hack.
        # We don't want to display _all_ the digital formats... Just the
        # 'All Digital' format.
        # To do this, though, I need to hard-code the fact that
        # the 'All Physical' format has an id of 1.
        #
		# actually, now we only want physical products to show
		# (still hard-coding the fact that 'All Physical has id==1 
		# -jff-
		#
        my $objParent = $obj->parent_id();

        # next if ($objParent > 0 && $objParent != 1);
        next if ($obj->product_format_id != 1 && $objParent != 1);

        push @{$map{$objParent}}, $obj;
	}

    my @list;
    _buildList(\@list, \%map, 0);

    $self->{ProductFormat} = \@list;

	return $self;
}


sub getList
{
	my ($self) = @_;

	return $self->{Channel};
}


# Recursively build a 'hierarchical' array of product formats, where
# items are clustered underneath their parent.
#
sub _buildList
{
    my ($list, $map, $parentID) = @_;

    my $childList = $map->{$parentID};
    return unless $childList;

    foreach my $dbItem (@$childList)
    {
        push @$list, RPS::Contract::ProductFormat->new(_dbItem => $dbItem);
        _buildList($list, $map, $dbItem->product_format_id);
    }
}

###
1;#
###


