# ==================================================================
# 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: Plugins.pm,v 1.48 2005/04/14 01:08:49 jagerman 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.
# ==================================================================

package Links::Plugins;
# ==================================================================
    use strict;
    use Links qw/$IN $CFG/;

# ------------------------------------------------------------------------------------------------- #
# Plugin config                                                                                     #
# ------------------------------------------------------------------------------------------------- #

sub get_plugin_user_cfg {
# --------------------------------------------------------------
# Returns the user config hash for a given plugin.
#
    my $class       = ($_[0] eq 'Links::Plugins') ? shift : '';
    my $plugin_name = shift || return;
    my $cfg = GT::Plugins->load_cfg ( $CFG->{admin_root_path} . '/Plugins' );
    exists $cfg->{$plugin_name}                     or return {};
    (ref $cfg->{$plugin_name}->{user} eq 'ARRAY')   or return {};

    my $opts = {};
    foreach my $opt (@{$cfg->{$plugin_name}->{user}}) {
        $opts->{$opt->[0]} = $opt->[1];
    }
    return $opts;
}

sub set_plugin_user_cfg {
# --------------------------------------------------------------
# Takes a plugin name and config hash and saves it.
#
    my $class       = ($_[0] eq 'Links::Plugins') ? shift : '';
    my $plugin_name = shift || return;
    my $hash        = shift || return;

    my $cfg = GT::Plugins->load_cfg ( $CFG->{admin_root_path} . '/Plugins' );
    exists $cfg->{$plugin_name}     or return;
    (ref $cfg->{$plugin_name}->{user} eq 'ARRAY')   or return {};

    foreach my $opt (@{$cfg->{$plugin_name}->{user}}) {
        $opt->[1] = $hash->{$opt->[0]};
    }
    return GT::Plugins->save_cfg ( $CFG->{admin_root_path} . '/Plugins', $cfg );
}

sub get_plugin_registry {
# --------------------------------------------------------------
# Returns the user config hash for a given plugin.
#
    my $class       = ($_[0] eq 'Links::Plugins') ? shift : '';
    my $plugin_name = shift || return;
    my $cfg = GT::Plugins->load_cfg ( $CFG->{admin_root_path} . '/Plugins' );
    exists $cfg->{$plugin_name}                       or return {};

    return ( $cfg->{$plugin_name}->{registry} || {} );
}

sub set_plugin_registry {
# --------------------------------------------------------------
# Takes a plugin name and config hash and saves it.
#
    my $class       = ($_[0] eq 'Links::Plugins') ? shift : '';
    my $plugin_name = shift || return;
    my $hash        = shift || return;

    my $cfg = GT::Plugins->load_cfg ( $CFG->{admin_root_path} . '/Plugins' );

    exists $cfg->{$plugin_name}                         or return;

    my $registry    = ( $cfg->{$plugin_name}->{registry} ||= {} );
    foreach my $opt ( keys %{$hash} ) {
        $registry->{$opt} = $hash->{$opt};
    }

    return GT::Plugins->save_cfg ( $CFG->{admin_root_path} . '/Plugins', $cfg );
}


# ------------------------------------------------------------------------------------------------- #
# Displaying                                                                                        #
# ------------------------------------------------------------------------------------------------- #

sub admin_menu {
# -----------------------------------------------------------------
# Displays the admin menu with the plugin options shown.
#
    require GT::Plugins::Manager;
    my $man = new GT::Plugins::Manager ( 
                                            cgi => $IN, 
                                            tpl_root => $CFG->{admin_root_path} . "/templates/admin",
                                            plugin_dir => $CFG->{admin_root_path} . "/Plugins", 
                                            prog_name => 'lsql',
                                            prog_ver => $CFG->{version},
                                            prog_reg => $CFG->{reg_number},
                                            base_url => 'admin.cgi?do=page&page=plugin_manager.html',
                                            path_to_perl => $CFG->{path_to_perl},
                                            perl_args => "-cw -I$CFG->{admin_root_path}"
                                        );
    return $man->admin_menu;
}

# ------------------------------------------------------------------------------------------------- #
# Wizard                                                                                            #
# ------------------------------------------------------------------------------------------------- #

sub wizard {
# -------------------------------------------------------------------
# Manages the plugin wizard, basically just creates a wizard object,
# and returns the output. Real work is done in GT::Plugins::Wizard.
#
    require GT::Plugins::Wizard;
    my $wiz = GT::Plugins::Wizard->new(
        cgi => $IN,
        tpl_root => $CFG->{admin_root_path} . "/templates/admin",
        plugin_dir => $CFG->{admin_root_path} . "/Plugins",
        prog_ver => $CFG->{version},
        install_header => 'use Links qw/:objects/;',
        initial_indent => '',
        dirs => {
            user_cgi => '$CFG->{admin_root_path}/..',
            admin_cgi => '$CFG->{admin_root_path}'
        },
        oo => '$PLG'
    );
    return $wiz->process;
}

# ------------------------------------------------------------------------------------------------- #
# Manager                                                                                           #
# ------------------------------------------------------------------------------------------------- #

sub manager {
# -------------------------------------------------------------------
# Manages the plugin installer, basically just creates an installerobject,
# and returns the output. Real work is done in GT::Plugins::Installer
#

    require GT::Plugins::Manager;
    my $man = GT::Plugins::Manager->new(
        cgi => $IN,
        tpl_root => $CFG->{admin_root_path} . "/templates/admin",
        plugin_dir => $CFG->{admin_root_path} . "/Plugins",
        prog_name => 'lsql',
        prog_ver => $CFG->{version},
        prog_init => $CFG->{admin_root_path},
        prog_reg => $CFG->{reg_number},
        base_url => 'admin.cgi?do=page&page=plugin_manager.html',
        path_to_perl => $CFG->{path_to_perl},
        perl_args => "-cw -I$CFG->{admin_root_path}"
    ) or return "Error loading plugin manager: $GT::Plugins::error";
    return $man->process;
}

1;
