# ==================================================================
# Gossamer List - enhanced mailing list management system
#
#   Website  : http://gossamer-threads.com/
#   Support  : http://gossamer-threads.com/scripts/support/
#   CVS Info :                          
#   Revision : $Id: Plugins.pm,v 1.9 2004/01/13 01:21:56 jagerman 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 GList::Plugins;
# ==================================================================
use strict;
use GList qw/$IN $CFG $USER/;

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

sub get_plugin_user_cfg {
# --------------------------------------------------------------
# Returns the user config hash for a given plugin.
#
    my $class       = ($_[0] eq 'GList::Plugins') ? shift : '';
    my $plugin_name = shift || return;
    my $cfg = GT::Plugins->load_cfg ( $CFG->{priv_path} . '/lib/GList/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 'GList::Plugins') ? shift : '';
    my $plugin_name = shift || return;
    my $hash        = shift || return;

    my $cfg = GT::Plugins->load_cfg ( $CFG->{priv_path} . '/lib/GList/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->{priv_path} . '/lib/GList/Plugins', $cfg );
}

sub get_plugin_registry {
# --------------------------------------------------------------
# Returns the user config hash for a given plugin.
#
    my $class       = ($_[0] eq 'GList::Plugins') ? shift : '';
    my $plugin_name = shift || return;
    my $cfg = GT::Plugins->load_cfg ( $CFG->{priv_path} . '/lib/GList/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 'GList::Plugins') ? shift : '';
    my $plugin_name = shift || return;
    my $hash        = shift || return;

    my $cfg = GT::Plugins->load_cfg ( $CFG->{priv_path} . '/lib/GList/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->{priv_path} . '/lib/GList/Plugins', $cfg );
}

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

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 = new GT::Plugins::Manager (
        cgi          => $IN,
        tpl_root     => "$CFG->{priv_path}/templates/$CFG->{template_set}",
        plugin_dir   => $CFG->{priv_path} . "/lib/GList/Plugins",
        prog_name    => 'mlist',
        prog_ver     => $CFG->{version},
        prog_reg     => $CFG->{reg_number},
        prefix       => 'GList::Plugins::',
        base_url     => "glist.cgi?do=admin_page&pg=plugin_manager.html".(( $USER->{use_cookie} ) ? '' : "&sid=$USER->{session_id}"),
        path_to_perl => $CFG->{path_to_perl},
        perl_args    => "-cw -I$CFG->{priv_path}"
    ) or return "Error loading plugin manager: $GT::Plugins::error";
    return $man->process;
}

# ------------------------------------------------------------------------------------------------- #
# 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 = new GT::Plugins::Wizard (
        cgi            => $IN,
        tpl_root       => "$CFG->{priv_path}/templates/$CFG->{template_set}",
        plugin_dir     => $CFG->{priv_path} . "/lib/GList/Plugins",
        prog_ver       => $CFG->{version},
        install_header => 'use GList qw/$IN $DB $CFG/;',
        initial_indent => '',
        prefix         => 'GList::Plugins::',
        dirs           => {
            user_cgi   => '$CFG->{cgi_path}',
            admin_cgi  => '$CFG->{cgi_path}'
        }
    );
    return $wiz->process;
}

# ------------------------------------------------------------------------------------------------- #
# 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->{priv_path}/templates/$CFG->{template_set}",
        plugin_dir   => $CFG->{priv_path} . "/lib/GList/Plugins",
        prefix       => 'GList::Plugins::',
        prog_name    => 'glist',
        prog_ver     => $CFG->{version},
        prog_reg     => $CFG->{reg_number},
        base_url     => 'glist.cgi?do=admin_page&pg=plugin_manager.html'.(( $USER->{use_cookie} ) ? '' : "&sid=$USER->{session_id}"),
        path_to_perl => $CFG->{path_to_perl},
        perl_args    => "-cw -I$CFG->{priv_path}"
    );
    return { menu => $man->admin_menu, cgi_url => $CFG->{cgi_url} };
}
1;

