# ==================================================================
# 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: CatLinks.pm,v 1.4 2006/03/25 01:13:35 brewt Exp $
#
# Copyright (c) 2004 Gossamer Threads Inc.  All Rights Reserved.
# Redistribution in part or in whole strictly prohibited. Please
# see LICENSE file for full details.
# ==================================================================

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

@ISA = qw/GT::SQL::Table/;
$ERROR_MESSAGE = 'GT::SQL';
@DELETING = (); # Used by Links::Table::Links

sub delete {
# -----------------------------------------------------------------------------
# We override the default CatLinks delete to delete any links that will no
# longer be referenced as a result of the deletion.
#
    my ($self, $cond) = @_;

    ref $cond or return $self->fatal(BADARGS => '$catlinks->delete(condition)');

# Get the CatLinks rows that are about to be deleted
    my (%delete, %links);
    my $sth = $self->select($cond);
    while (my $row = $sth->fetchrow_hashref) {
        $delete{$row->{LinkID}}++;
        if (exists $links{$row->{LinkID}}) {
            push @{$links{$row->{LinkID}}}, $row->{CategoryID};
        }
        else {
            $links{$row->{LinkID}} = [$row->{CategoryID}];
        }
    }

# Delete the CatLinks rows
    my $ret = $self->SUPER::delete($cond) or return;

# Get the links that still exist in the CatLinks table after the delete (ie.
# links that were in multiple categories).  These are the links that shouldn't
# be deleted from the Links table.
    my @remaining = keys %delete ? $self->select('LinkID', { LinkID => [keys %delete] })->fetchall_list : ();
    for (@remaining, @DELETING) {
        delete $delete{$_};
    }

# Non-validated links don't increment Category counts.
    my @notval = keys %links ? $DB->table('Links')->select('ID', { ID => [keys %links], isValidated => 'No' })->fetchall_list : ();
    for (@notval, @DELETING) {
        delete $links{$_};
    }

# Any links in %delete have no references to it from CatLinks
    if (keys %delete) {
        $DB->table('Links')->delete({ ID => [keys %delete] });
    }

# Build a list of categories that need their counts updated
    my %cats;
    for (keys %links) {
        for (@{$links{$_}}) {
            $cats{$_}++;
        }
    }

# Update the Category link counts
    if (keys %cats) {
        my $category = $DB->table('Category');
        my %change;
        while (my ($catid, $count) = each %cats) {
            push @{$change{-$count}}, $catid;
        }
        $category->link_count(\%change);

        while (my ($change, $ids) = each %change) {
            $category->update({ Direct_Links => \("Direct_Links - " . abs $change) }, { ID => $ids });
        }
    }

    $ret;
}

1;
