#!/usr/bin/perl -w
#
# PROGRAM:	cgi-bin/dpsweb/gallery.pl
# PURPOSE:	Displays inspiration gallery index and individual pages
#
#------------------------------------------------------------------------------
#	Copyright (c) 2014-2017 Ferguson Enterprises, Inc.
#	$Id: gallery.pl,v 1.7 2017-03-22 16:07:42 dferruggia Exp $
#
#	Change history:
#	v 1.1  - Written by Kahil Jallad (bug 15257).
#	v 1.2  - Dummy commit to pick up executable status.
#	v 1.3  - Use template name from room.
#	v 1.4  - Change name of template variable.
#	v 1.5  - Add an Endeca query with a product filter (bug 15655).
# 	v 1.6  - Check for null query results.
#	v 1,7  - Use "external" ESB method for price checks.
#
#------------------------------------------------------------------------------

package dpsweb_gallery;
use base 'DPS::Web::Begin';

use strict;

use DPS::Data::Gallery;
use DPS::Data::GalleryRoom;
use DPS::Endeca::XMLQuery;

use DPS::Web::Page;

our $TEMPLATE_PREFIX = 'inspiration';

DPS::Web::Begin->startRun;

###############################################################################
#
#   main routine
#
sub main
{
	my ($self) = @_;
	my $env = $self->env;
	
	my $galleryID = $self->cgi->param('galleryID');
	my $gallerySlug = $self->cgi->param('gallery');

	my $galleryRoomID = $self->cgi->param('galleryRoomID');
	my $galleryRoomSlug = $self->cgi->param('room');
	my $action = $self->cgi->param('action');
	
	my $galleryRoom = undef;
	my $gallery = undef;
	my $template = undef;

	if ($galleryRoomID) {
		$galleryRoom = DPS::Data::GalleryRoom->exists($self->env, $galleryRoomID);
		$galleryRoom = undef unless $galleryRoom && $galleryRoom->active && $galleryRoom->gallery->active;
		$gallery = $galleryRoom->gallery if $galleryRoom;
		$template = $galleryRoom->templateName if $galleryRoom;
	} else {
		if ($galleryID) {
			$gallery = DPS::Data::Gallery->exists($self->env, $galleryID);
		} elsif ($gallerySlug) {
			$gallery = DPS::Data::Gallery->exists($self->env, SLUG => $gallerySlug);
			$gallery = undef unless $gallery && $gallery->active;
		}
		$template = $gallery->templateName if $gallery;
	}

	if ($gallery && $galleryRoomSlug) {
		$galleryRoom = $gallery->galleryRoomWithSlug($galleryRoomSlug);
		$template = $galleryRoom->templateName if $galleryRoom && $galleryRoom->templateName;
	}

	# Use the "get this room" template if requested.
	if ($galleryRoom && $action && $action eq 'getRoom') {
		$template = 'getRoom';
	}

	# Redirect to the home page if no gallery or template.
	unless ($template) {
		DPS::Web::Page->redirect('/');
		return;
	}

	$self->{page} = new DPS::Web::Page($self->env, "$TEMPLATE_PREFIX/$template");

	# Use the "external" method for connecting to the ESB because we were having
	# errors with price calls
	$ENV{ESB_EXTERNAL} = '1';

	if ($template eq 'designTrends') {
		my @productIDs = map {$_->productID} @{$galleryRoom->galleryProducts};
		my $query = new DPS::Endeca::XMLQuery($env);
		$query->queryString("N=0");
		$query->filterProductIDs(\@productIDs);
		$query->resultsPerPage(scalar @productIDs);
		$query->query;
		my %keyed_results;
		if ($query->results){
			foreach my $item (@{$query->results}) {
				$keyed_results{$item->{'Product_ID'}} = $item;
			}
		}
		$self->{page}->add(endecaResults => \%keyed_results);
	}

	$self->{page}->add(gallery => $gallery);
	$self->{page}->add(galleryRoom => $galleryRoom);
	$self->{page}->render;
}


__END__

=head1 NAME

gallery.pl - Display a gallery index page or a room page

=head1 DESCRIPTION

Called from the web for inspiration gallery.

=cut
