discourse-legacysite-perl/site/slowtwitch.com/cgi-bin/articles/admin/Links/User/Treecats.pm
2024-06-17 21:49:12 +10:00

120 lines
4.1 KiB
Perl

# ==================================================================
# 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: Treecats.pm,v 1.3 2006/09/12 06:07:12 brewt Exp $
#
# Copyright (c) 2006 Gossamer Threads Inc. All Rights Reserved.
# Redistribution in part or in whole strictly prohibited. Please
# see LICENSE file for full details.
# ==================================================================
package Links::User::Treecats;
# ==================================================================
use strict;
use Links qw/:objects/;
sub handle {
# Fetch these categories (and select them)
my @cid = $IN->param('cid');
# Fetch these links (and select them)
my @lid = $IN->param('lid');
# Fetch these categories
my @id = $IN->param('id');
# Fetch links as well as Categories
my $fetchlinks = $IN->param('links');
my $category = $DB->table('Category');
my $catlinks = $DB->table('CatLinks', 'Links');
# Fetching selected categories
if (@cid) {
@lid = ();
@id = @cid;
$fetchlinks = 0;
}
# Fetching selected links
elsif (@lid) {
# Get all the categories that the links are in
@id = $catlinks->select('CategoryID', { LinkID => \@lid }, VIEWABLE)->fetchall_list;
$fetchlinks = 1;
}
# Fetching categories/links
else {
@cid = ();
@lid = ();
@id = (0) unless @id;
}
my %vars;
# Only allow the use of treecats.cgi if db_gen_category_list == 2 or if
# treecats_enabled (hidden config option) is true
if ($CFG->{db_gen_category_list} != 2 and not $CFG->{treecats_enabled}) {
$vars{error} = 'Permission denied - treecats is currently disabled.';
}
else {
my @fetchlinks;
my $cond;
if (@cid or @lid) {
my $parents = $category->parents(\@id);
my @ids;
my @fids = (0);
for (keys %$parents) {
# Fetch all the parents and their children
push @ids, @{$parents->{$_}};
push @fids, @{$parents->{$_}};
# Fetch the category itself
push @ids, $_;
# When pre-selecting links, @id contains the category the link(s) are in. To
# completely draw the tree, the children of those categories need to be
# retreived as well.
if (@lid) {
push @fids, $_;
push @fetchlinks, $_;
}
push @fetchlinks, @{$parents->{$_}};
}
$cond = GT::SQL::Condition->new(ID => IN => \@ids, FatherID => IN => \@fids);
$cond->bool('OR');
}
else {
push @fetchlinks, @id;
$cond = GT::SQL::Condition->new(FatherID => IN => \@id);
}
$category->select_options("ORDER BY Full_Name");
$vars{categories} = $category->select($cond)->fetchall_hashref;
# Find the children counts of all the categories and check if they should be selected or not
my @cats;
for (@{$vars{categories}}) {
push @cats, $_->{ID};
}
$category->select_options("GROUP BY FatherID");
my %children = $category->select('FatherID', 'COUNT(*)', { FatherID => \@cats })->fetchall_list;
my %selected = map { $_ => 1 } @cid;
for (@{$vars{categories}}) {
$_->{children} = $children{$_->{ID}} || 0;
$_->{selected} = $selected{$_->{ID}} || 0;
}
if ($fetchlinks and @fetchlinks) {
# Remove CategoryID = 0 (shouldn't normally happen)
@fetchlinks = grep $_, @fetchlinks;
$catlinks->select_options("ORDER BY CategoryID, Title");
$vars{links} = $catlinks->select({ CategoryID => \@fetchlinks }, VIEWABLE)->fetchall_hashref;
%selected = map { $_ => 1 } @lid;
for (@{$vars{links}}) {
$_->{selected} = $selected{$_->{ID}} || 0;
}
}
}
print $IN->header('text/xml');
print Links::user_page('treecats.xml', \%vars);
}
1;