# ==================================================================
# Gossamer Links - enhanced directory management system
#
#   Website  : http://gossamer-threads.com/
#   Support  : http://gossamer-threads.com/scripts/support/
#   CVS Info : 087,071,086,086,085      
#   Revision : $Id: Reviews.pm,v 1.1 2007/11/16 07:15:00 brewt Exp $
#
# Copyright (c) 2007 Gossamer Threads Inc.  All Rights Reserved.
# Redistribution in part or in whole strictly prohibited. Please
# see LICENSE file for full details.
# ==================================================================

package Links::Table::Reviews;
# ==================================================================
use strict;
use Links qw/:objects/;
use GT::SQL;
use GT::SQL::Table;
use vars qw/@ISA $ERROR_MESSAGE/;

@ISA = qw/GT::SQL::Table/;
$ERROR_MESSAGE = 'GT::SQL';

sub add {
# -----------------------------------------------------------------------------
# Add a review.
#
    my $self = shift;
    my $rec = (ref $_[0] eq 'HASH') ? shift : { @_ };

    my $id = $self->SUPER::add($rec) or return;

# Update the link/category timestamp if the review is validated.
    _update_timestamp($rec->{Review_LinkID}) if $rec->{Review_Validated} eq 'Yes';

    $id;
}

sub modify {
# -----------------------------------------------------------------------------
# Modify a review.
#
    my $self = shift;
    my $set  = shift            or return $self->fatal(BADARGS => 'Usage: $reviews->modify({ col => value ... }).');
    my $id   = $set->{ReviewID} or return $self->fatal(BADARGS => 'No primary key passed to modify!');

    my ($old, $link_id) = $self->select('Review_Validated', 'Review_LinkID', { ReviewID => $id })->fetchrow;

    my $ret = $self->SUPER::modify($set) or return;

# Only update the timestamp if it was unvalidated and still is - this is the
# only case where the pages shouldn't be rebuilt.
    my $new = $set->{Review_Validated} || $old;
    _update_timestamp($link_id) unless $old eq 'No' and $new eq 'No';

    $ret;
}

sub delete {
# -----------------------------------------------------------------------------
# Delete one or more reviews.
#
    my ($self, $cond) = @_;
    ref $cond or return $self->fatal(BADARGS => '$reviews->delete(condition)');

# Get the link ids of the reviews that are about to be deleted and are
# validated (as only those pages need to be rebuilt).
    my @link_ids = $self->select('Review_LinkID', $cond, { Review_Validated => 'Yes' })->fetchall_list;

    my $ret = $self->SUPER::delete($cond) or return;

    _update_timestamp(\@link_ids) if @link_ids;

    $ret;
}

sub _update_timestamp {
# -----------------------------------------------------------------------------
# Given a link ID (or an array ref if you want to update more than one link),
# update the Timestmp columns of the link as well as all the categories that
# the link is in.  This ensures that these pages will be rebuilt on "Build
# Changed".
#
    my $link_id = shift;
    return unless $link_id;

    my @cats = $DB->table('Links', 'CatLinks')->select('CategoryID', { LinkID => $link_id })->fetchall_list;
    $DB->table('Category')->update({ Timestmp => \'NOW()' }, { ID => \@cats }) if @cats;
    $DB->table('Links')->update({ Timestmp => \'NOW()' }, { ID => $link_id });
}

1;
