#!/usr/local/bin/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: jump.cgi,v 1.32 2005/03/30 09:20:49 brewt Exp $
#
# Copyright (c) 2001 Gossamer Threads Inc.  All Rights Reserved.
# Redistribution in part or in whole strictly prohibited. Please
# see LICENSE file for full details.
# ==================================================================

use strict;
use lib '/var/home/slowtwitch/slowtwitch.com/cgi-bin/articles/admin';
use Links qw/$PLG $DB $IN $USER $CFG/;
use Links::User::Page;

local $SIG{__DIE__} = \&Links::fatal;

Links::init('/var/home/slowtwitch/slowtwitch.com/cgi-bin/articles/admin');
Links::init_user();

if ($USER->{Status} eq 'Administrator') {
    print $IN->header;
    my $error = _update() if $IN->param('update');
    print Links::user_page('featured_links.html', { error => $error, message => $error ? "" : "Featured links are updated" });
}
else {
    print $IN->redirect('/');
}

sub _update {
    my $cgi = $IN->get_hash();
    my @articles = map $cgi->{"article_$_"}, grep { $cgi->{"article_$_"} } 1..4;
    my @photos   = map $cgi->{"photo_$_"}, grep { $cgi->{"photo_$_"} } 1..2;

    return "All featured links are required" if scalar @articles < 4 or scalar @photos < 2;
    
    my $tab = $DB->table('Links');
    my $articles = $tab->count({ ID => \@articles, isValidated => 'Yes' });
    return "All 4 featured articles are required and not duplicate" if scalar $articles < 4;
    
    my $photos = $tab->count({ ID => \@photos, isValidated => 'Yes' });
    return "All 2 featured photos/videos are required and not duplicate" if scalar $photos < 2;
    
    $CFG->{featured_articles} = \@articles;
    $CFG->{featured_photos}   = \@photos;
    $CFG->save;
    return;
}
