$versions{'plug_ins_mgr-ext_lib'} = '06.6.00.0000';

# Contains all sub routines (functions) for enabling and disabling plug-ins that
# are installed in the store system.

#######################################################################
#
# AgoraCart and all associated files, except where noted, are
# Copyright 2001 to Present jointly by K-Factor Technologies, Inc.
# and by C E Mayo (aka Mister Ed) at AgoraCart.com & K-Factor.net
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This copyright notice may not be removed or altered in any way.
#
#######################################################################

{
    local ($modname) = 'plugin_activator_mgr';
    register_extension( $modname, 'Plug-in Module Selector', $versions{$modname} );

    add_settings_choice(
        'Plug-in / Add-on Selector',
        ' Plug-in / Add-on Selector ',
        'select_plugins_screen'
    );
    register_menu( 'select_plugins_screen', 'select_plugins_screen',
        $modname, 'Display Which Plug-ins to Activate' );
    register_menu( 'SavePluginList', 'save_plugin_choices',
        $modname,  'Choose Plug-ins to Activate' );
}

#######################################################################

sub select_plugins_screen {

    local (@plist, @myPlugins, $mylist_of_plugins_installed) = q{};
    local (%pluginsAvailableList);
    my ( $name, $name_lc, $is_plugin_checked ) = q{};

    print &$manager_page_header( 'Select Plug-ins / Add-on Modules to Activate', '', '', '', '' );


    opendir( PLUGINS, "$sc_add_on_modules_dir" );
    @myPlugins = sort( grep( /_plugin_manifest\.inc\b/i, readdir(PLUGINS) ) );
    closedir(PLUGINS);

    foreach $plugMan (@myPlugins) {
        $plugMan =~ /([\w\-\=\+]+)(\.inc)/i;
        $plugMan = "$1$2";
        require_supporting_libraries( __FILE__, __LINE__, "$sc_add_on_modules_dir/$plugMan" );
        $plugin_id = $sc_plugin_id{"$plugMan"};
        $plugin_id =~ tr/A-Z/a-z/;

        if ( $mc_mgr_plugins_enabled =~ /$plugin_id/ ) {
            $is_plugin_checked = ' checked';
        }
        else {
            $is_plugin_checked = '';
        }


        $pluginsAvailableList{"$plugin_id"} = qq{
<div class="col-sm-6" style="margin-bottom: 10px;">
<div class="input-group">
  <span class="input-group-addon">
    <input type="checkbox" name="plugins_enabled" value="$plugin_id"$is_plugin_checked>
    <input type="hidden" name="plugin_$plugin_id" value="$sc_plugin_file_name{$plugin_id}">

  </span>
  <label class="form-control">$sc_plugin_mgr_name{$plugin_id} <span style="padding-left: 20px;"></span><i class="fa fa-info-circle" data-toggle="tooltip" data-placement="right" title="$sc_plugin_mgr_tooltip{$plugin_id}" aria-hidden="true"></i></label>
</div>
</div>
};

    }


    foreach $item ( sort( keys %pluginsAvailableList ) ) {
        $mylist_of_plugins_installed .= $pluginsAvailableList{$item};
    }


    print <<ENDOFTEXT;
<h1 class="mgr-page-title"><b>Plug-in / Add-on Modules</b></h1>
<p><br></p>

<p class="well">This is a list of plug-ins (aka add-ons) installed in your store. Select all the plug-in modules you wish to activate from the list below. Hover on the <i class="fa fa-info-circle"></i> icon next to each plug-in name for information about that plug-in module.</p>
<p><br></p>

ENDOFTEXT
    if ( $in{'system_edit_success'} ) {
        print <<ENDOFTEXT;
<div class="row col-xs-12 col-sm-12 col-md-12 col-lg-12 hidden-xs">
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 bg-success">
        <h4 class="text-primary mgr-center">The Active Plug-ins Enabled List has been successfully updated.</h4>
    </div>
</div>
<div class="row col-xs-12 col-sm-12"><p><br></p></div>
ENDOFTEXT
    }

    print <<ENDOFTEXT;
<form action="manager.cgi" method="post">
<input type="hidden" name="mgr_seshun" value="$in{'mgr_seshun'}">

<div class="row">
$mylist_of_plugins_installed
</div>

<p><br><br></p>
<input name="SavePluginList" type="hidden" value="Submit">
<input type="hidden" name="system_edit_success" value="yes">
<button class="btn btn-primary btn-lg mgr-center" style="display: block;">Update Active Plug-in List</button>
</form>
<p><br><br><br></p>
ENDOFTEXT
    print &$manager_page_footer;
}

#######################################################################

sub save_plugin_choices {

    local ($myset, @values, $pluginlist_string, $mc_mgr_plugins_enabled ) = q{};
    my $plugin_count = 0;

    @values = param('plugins_enabled');
    foreach $value ( @values ) {
        $pluginfilename = param("plugin_$value");
        if ( $plugin_count == 0 ) {
            $pluginlist_string .= "'$pluginfilename'";
            $mc_mgr_plugins_enabled .= "$value";
        }
        else {
            $pluginlist_string .= ",'$pluginfilename'";
            $mc_mgr_plugins_enabled .= "|$value";
        }
        $plugin_count++;
    }

    $myset .= "\@sc_active_plugins = ($pluginlist_string);\n";
    $myset .= "\$mc_mgr_plugins_enabled = '$mc_mgr_plugins_enabled';\n";

    update_store_settings( 'PLUGINS', $myset );
    select_plugins_screen();
}

#######################################################################

1;
