$versions{'widgets_agorascript.pl'} = '06.6.00.0000';

#######################################################################
#
# 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.
#
#######################################################################

#
#
# Contains the subroutines/functions for serving widgets that are found
# within AgoraScript that is parsed within headers, footers, product
# pages and other templates.
#
#

#######################################################################
# Cart Contents Snapshot Widget
# Allows cart contents snapshot display to be more flexible for column
# selection in templates: header and footer columns
sub display_cart_contents_for_templates {
    local (@my_cart_fields,$my_cart_row_number,$result);
    local ($count,$price,$product_id,$quantity,$total_cost,$total_qty) = 0;
    local $buysafe_temp_display = q{};
    local $formatted_total_cost = q{};
    local ($ppe_temp_display,$ppe_empty_temp_display,$pre_calc_shipping_display) = q{};
    if ( $sc_PayPalExpress_is_enabled =~ /yes/ ) {
        $ppe_temp_display = $PayPal_Express_checkout_display;
        $ppe_empty_temp_display = $PayPal_Express_empty_cart_contents_display;
        if ( $sc_PayPalExpress_BML_is_enabled =~ /yes/i ) {
            $ppe_temp_display .= $PayPal_Express_Billing_checkout_display;
            $ppe_empty_temp_display .= $PayPal_Express_Billing_empty_cart_contents_display;
        }
    }
    if ( $sc_buySafe_is_enabled =~ /yes/ ) {
        $buysafe_temp_display = qq|<br><br>$sc_buysafe_merchant_seal_complete_button_string <br>|;
    }
    if ( ( !( -f "$sc_cart_path" ) ) || ( $sc_processing_order =~ /yes/i ) ) {
        $result = $sc_widget_templates{'Empty Cart Contents Column Widget'};
        $ppe_temp_display = $ppe_empty_temp_display;
        $result =~ s/\[\[itemstext\]\]/$agora_cartitems_widget_itemstext/g;
        $result =~ s/\[\[total_qty\]\]/$total_qty/g;
        $result =~ s/\[\[subtotal_text\]\]/$agora_cartitems_widget_subtotal_text/g;
        $formatted_total_cost = display_price('0.00');
        $result =~ s/\[\[total_cost\]\]/$formatted_total_cost/g;
        if ( $sc_buySafe_is_enabled =~ /yes/ ) {
            $result =~ s/\[\[buysafetemp\]\]/$buysafe_temp_display/g;
        }
        else { $result =~ s/\[\[buysafetemp\]\]//g; }
        $result =~ s/\[\[ppe_temp_display\]\]/$ppe_temp_display/g;
        return $result;
    }
    open (CART, "$sc_cart_path") || file_open_error("$sc_cart_path",'display_cart_contents_for_templates',__FILE__, __LINE__);
    while (<CART>) {
       $count++;
       chop;
       @my_cart_fields = split (/\|/, $_);
       $my_cart_row_number = pop(@my_cart_fields);
       push (@my_cart_fields, $my_cart_row_number);
       $quantity = $my_cart_fields[0];
       $product_id = $my_cart_fields[1];
       $price = $my_cart_fields[$sc_cart_index_of_price_after_options];
       $total_cost = $total_cost + $quantity * $price;
       $total_qty = $total_qty + $quantity;
    }
    close (CART);

    if ( $count > 0 ) {
        $result = $sc_widget_templates{'Cart Contents Column Widget'};

         if ( $sc_pre_calc_shipping_installed =~ /yes/i ) {
             codehook('precheckoutshipButton');
             $result =~ s/\[\[precalcshipping\]\]/$sc_pre_calc_shipping_display/g;
         }
         else { $result =~ s/\[\[precalcshipping\]\]//g; }
    }
    else {
        $result = $sc_widget_templates{'Empty Cart Contents Column Widget'};
        $ppe_temp_display = $ppe_empty_temp_display;
    }

    $result =~ s/\[\[itemstext\]\]/$agora_cartitems_widget_itemstext/g;
    $result =~ s/\[\[total_qty\]\]/$total_qty/g;
    $result =~ s/\[\[subtotal_text\]\]/$agora_cartitems_widget_subtotal_text/g;
    $result =~ s/\[\[viewcart\]\]/$agora_cartitems_widget_viewcart/g;
    $result =~ s/\[\[checkout\]\]/$agora_cartitems_widget_checkout/g;
    $result =~ s/\[\[ppe_temp_display\]\]/$ppe_temp_display/g;
    if ( $count > 0 ) {
        $formatted_total_cost = display_price($total_cost);
        $result =~ s/\[\[total_cost\]\]/$formatted_total_cost/g;
    }
    else {
        $formatted_total_cost = display_price('0.00');
        $result =~ s/\[\[total_cost\]\]/$formatted_total_cost/g;
    }
    if ( $sc_buySafe_is_enabled =~ /yes/ ) {
        $result =~ s/\[\[buysafetemp\]\]/$buysafe_temp_display/g;
    }
    else { $result =~ s/\[\[buysafetemp\]\]//g; }

    return $result;
}

#######################################################################
# Horizontal Cart Contents Snapshot Widget
# Allows cart contents snapshot display to be more flexible for zone
# selection in templates: horizontal areas

sub display_cart_contents_horizontal_zones {
    local (@my_cart_fields,$my_cart_row_number,$result);
    local ($count,$price,$product_id,$quantity,$total_cost,$total_qty) = 0;
    local $buysafe_temp_display = q{};
    local $formatted_total_cost = q{};
    local ($ppe_temp_display,$ppe_empty_temp_display,$pre_calc_shipping_display) = q{};
    if ( $sc_PayPalExpress_is_enabled =~ /yes/ ) {
        $ppe_temp_display = $PayPal_Express_checkout_display;
        $ppe_empty_temp_display = $PayPal_Express_empty_cart_contents_display;
        if ( $sc_PayPalExpress_BML_is_enabled =~ /yes/i ) {
            $ppe_temp_display .= $PayPal_Express_Billing_checkout_display;
            $ppe_empty_temp_display .= $PayPal_Express_Billing_empty_cart_contents_display;
        }
    }
    if ( $sc_buySafe_is_enabled =~ /yes/ ) {
        $buysafe_temp_display = qq|<br><br>$sc_buysafe_merchant_seal_complete_button_string <br>|;
    }
    if ( ( !( -f "$sc_cart_path" ) ) || ( $sc_processing_order =~ /yes/i ) || ( $form_data{'dc'} ) || ( $form_data{'order_form_button.x'} ) ) {
        $result = '';
        return $result;
    }
    open (CART, "$sc_cart_path") || file_open_error("$sc_cart_path",'display_cart_contents_horizontal_zones',__FILE__, __LINE__);
    while (<CART>) {
       $count++;
       chop;
       @my_cart_fields = split (/\|/, $_);
       $my_cart_row_number = pop(@my_cart_fields);
       push (@my_cart_fields, $my_cart_row_number);
       $quantity = $my_cart_fields[0];
       $product_id = $my_cart_fields[1];
       $price = $my_cart_fields[$sc_cart_index_of_price_after_options];
       $total_cost = $total_cost + $quantity * $price;
       $total_qty = $total_qty + $quantity;
    }
    close (CART);
#title="Restart Checkout Process"><i class="fa fa-check fa-lg"></i> [[restartcheckout]]</a>
    if ( $count > 0 ) {
        $result = $sc_widget_templates{'Cart Contents Horizontal Widget'};

         if ( $sc_pre_calc_shipping_installed =~ /yes/i ) {
             codehook('precheckoutshipButton');
             $result =~ s/\[\[precalcshipping\]\]/$sc_pre_calc_shipping_display/g;
         }
         else { $result =~ s/\[\[precalcshipping\]\]//g; }
    }
    else {
        $result = '';
        return $result;
    }

    $result =~ s/\[\[itemstext\]\]/$agora_cartitems_widget_itemstext/g;
    $result =~ s/\[\[total_qty\]\]/$total_qty/g;
    $result =~ s/\[\[subtotal_text\]\]/$agora_cartitems_widget_subtotal_text/g;
    $result =~ s/\[\[viewcart\]\]/$agora_cartitems_widget_viewcart/g;
    if ( ( $form_data{'order_form_button.x'} ) || ( $form_data{'order_form_button'} ) ) {
        $result =~ s/\[\[checkout\]\]/$agora_productPage_restartcheckout_link/g;
    }
    else {
        $result =~ s/\[\[checkout\]\]/$agora_cartitems_widget_checkout/g;
    }
    $result =~ s/\[\[ppe_temp_display\]\]/$ppe_temp_display/g;
    if ( $count > 0 ) {
        $formatted_total_cost = display_price($total_cost);
        $result =~ s/\[\[total_cost\]\]/$formatted_total_cost/g;
    }
    else {
        $formatted_total_cost = display_price('0.00');
        $result =~ s/\[\[total_cost\]\]/$formatted_total_cost/g;
    }
    if ( $sc_buySafe_is_enabled =~ /yes/ ) {
        $result =~ s/\[\[buysafetemp\]\]/$buysafe_temp_display/g;
    }
    else { $result =~ s/\[\[buysafetemp\]\]//g; }

    return $result;
}

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

# Category List Widget
# Allows category list displays to be more flexible for column selection in templates: agoraPlates
sub display_category_list_for_templates {
    if ($sc_use_database_subcats =~ /yes/i) {
        if ($sc_global_bot_tracker ne '1') {
           return qq|\n$sc_categories_with_subcats\n|;
        }
        return qq|\n$sc_categories_bots_with_subcats\n|;
    } else {
        if ($sc_global_bot_tracker ne '1') {
           return qq|\n$sc_categories_no_subcats\n|;
        }
        return qq|\n$sc_categories_bots_no_subcats\n|;
    }
}

#######################################################################
#  Widget
# Allows Featured Product display to be more flexible for column selection in templates: agoraPlates
sub display_featured_product_for_templates {
    return $sc_featuredProduct;
}

#######################################################################
#  Widget
# Allows New Product list displays to be more flexible for column selection in templates: agoraPlates
sub display_new_product_for_templates {
    return $sc_tenNewProducts;
}

#######################################################################
#  Widget
# Allows Best Sellers list displays to be more flexible for column selection in templates: agoraPlates
sub display_best_sellers_list_for_templates {
    return $sc_tenBestSellers;
}

#######################################################################
# standard_column_routine
#
# runs widgets to be included in each column.
# widgets to be included in each zone are set in manager screens
# subroutine called from individual agoraPlate designs/templates
#
# widgets with no subroutine needs are left empty and skipped
#######################################################################

sub standard_column_routine {
    my ( $column_side ) = @_;
    $column_side .= '_column_widgets';
    foreach my $item (@$column_side) {
        ($key, $widget_title, $widget_template_name, $widget_routine_to_run) = split( /\|/, $item, 4 );
        my $temp_content = q{};
        my $template_holder = $sc_widget_templates{"$widget_template_name"};
        if ( $widget_routine_to_run ) { $temp_content = &$widget_routine_to_run; }
        $template_holder =~ s/\[\[column_box_title\]\]/$widget_title/i;
        $template_holder =~ s/\[\[column_box_content\]\]/$temp_content/i;
        print $template_holder;
    }
}

#######################################################################
# standard_horizontal_widget_routine
#
# runs widgets to be included in each horizontal oriented zone in templates.
# widgets to be included in each zone are set in manager screens
# subroutine called from individual agoraPlate designs/templates
#
# widgets with no subroutine needs are left empty and skipped
#######################################################################

sub standard_horizontal_widget_routine {
    my ( $zone ) = @_;
    $zone .= '_horizontal_widgets';
    foreach my $item (@$zone) {
        ($key, $widget_title, $widget_template_name, $widget_routine_to_run) = split( /\|/, $item, 4 );
        my $temp_content = q{};
        my $template_holder = $sc_widget_templates{"$widget_template_name"};
        if ( $widget_routine_to_run ) { $temp_content = &$widget_routine_to_run; }
        $template_holder =~ s/\[\[column_box_title\]\]/$widget_title/i;
        $template_holder =~ s/\[\[column_box_content\]\]/$temp_content/i;
        print $template_holder;
    }
}

#######################################################################
# Cart Links Sub Routine
#
# cartlinks added by Mister Ed of AgoraCart.com 09/2002
# Reads the links array instead of original .htm/.html files which allows admins to
# build link lists and store other metatag info for the pages, manually or via the online manager screens.
#
# Links can be altered via CSS for verticle or horizontal display.
#
# updated for version 6.6 & 7.x by Mister Ed of AgoraCart.com - 2013 & 2015 & 2018
# A more Responsive version of the older cartlinks routine
#
# Links as buttons subroutine added May 2019
#
#######################################################################
sub cartlinks {
    local (@clinknames);
    local ( $clinkfile, $clinkname, $temp_line );
    my $cartID_string = q|&amp;cart_id=[[cart_id]]|;

    if ( $cartlinks eq '' ) {

        # do we want to display a link to the Site's page?
        if ( $sc_display_cartlinksite =~ /yes/i ) {
            $temp_line = $sc_ul_link_list_generic;
            $temp_line =~ s/\[\[link_class\]\]/$sc_homesite_link_class/;
            $temp_line =~ s/\[\[link_url\]\]/$sc_display_cartlinksite_url/;
            $temp_line =~ s/\[\[title\]\]/$sc_display_cartlinksite_name/;
            $temp_line =~ s/\[\[link_display_name\]\]/$sc_display_cartlinksite_name/;
            $cartlinks .= $temp_line;
        }

        # do we want to display a link to the main store page?
        if ( $sc_display_cartlinkhome =~ /yes/i ) {
            $temp_line = $sc_ul_link_list_generic;
            if ( $sc_seo_friendly =~ /yes/i ) {
                $temp_line =~ s/\[\[link_url\]\]/$sc_store_base_URL/;
            }
            else {
                $temp_line =~ s/\[\[link_url\]\]/$sc_store_url/;
            }
            $temp_line =~ s/\[\[link_class\]\]/$sc_carthome_link_class/;
            $temp_line =~ s/\[\[title\]\]/$sc_display_cartlinkhome_name/;
            $temp_line =~ s/\[\[link_display_name\]\]/$sc_display_cartlinkhome_name/;
            $cartlinks .= $temp_line;
        }

        for $cartlink_page (@sc_active_cartlink_pages) {
            ( $clinkfile, $clinkname)  = ( split( /\|/, $cartlink_page, 2 ) );
            $temp_line = $sc_ul_link_list_normal;
            if ( $sc_seo_friendly =~ /yes/i ) {
                $temp_line = $sc_ul_link_list_seo;
            }
            $temp_line =~ s/\[\[link_class\]\]/$sc_cartlink_page_link_class/;
            $temp_line =~ s/\[\[seo_link_url\]\]/$sc_store_base_URL/;
            $temp_line =~ s/\[\[link_url\]\]/$sc_store_url/;
            $temp_line =~ s/\[\[clink_file\]\]/$clinkfile/;
            $temp_line =~ s/\[\[title\]\]/$clinkname/;
            $temp_line =~ s/\[\[link_display_name\]\]/$clinkname/;
            if ( $sc_global_bot_tracker ne '1' ) {
                # $temp_line =~ s/\[\[cartID_string\]\]/$cartID_string/;
            }
            $temp_line =~ s/\[\[cartID_string\]\]//;
            $cartlinks .= $temp_line;
        }
    }

    codehook('cartlinks_bottom');

}

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

sub cartlinks_as_buttons {
    local (@clinknames);
    local ( $clinkfile, $clinkname, $temp_line );
    my $cartID_string = q|&amp;cart_id=[[cart_id]]|;

    if ( $cartlinks eq '' ) {

        # do we want to display a link to the Site's page?
        if ( $sc_display_cartlinksite =~ /yes/i ) {
            $temp_line = $sc_ul_link_button_generic;
            $temp_line =~ s/\[\[link_class\]\]/$sc_homesite_link_class/;
            $temp_line =~ s/\[\[link_url\]\]/$sc_display_cartlinksite_url/;
            $temp_line =~ s/\[\[title\]\]/$sc_display_cartlinksite_name/;
            $temp_line =~ s/\[\[link_display_name\]\]/$sc_display_cartlinksite_name/;
            $cartlinks .= $temp_line;
        }

        # do we want to display a link to the main store page?
        if ( $sc_display_cartlinkhome =~ /yes/i ) {
            $temp_line = $sc_ul_link_button_generic;
            $temp_line =~ s/\[\[link_class\]\]/$sc_carthome_link_class/;
            $temp_line =~ s/\[\[link_url\]\]/$sc_store_url/;
            $temp_line =~ s/\[\[title\]\]/$sc_display_cartlinkhome_name/;
            $temp_line =~ s/\[\[link_display_name\]\]/$sc_display_cartlinkhome_name/;
            $cartlinks .= $temp_line;
        }

        for $cartlink_page (@sc_active_cartlink_pages) {
            ( $clinkfile, $clinkname)  = ( split( /\|/, $cartlink_page, 2 ) );

            if ( $sc_seo_friendly =~ /yes/i ) {
                $temp_line = $sc_ul_link_button_seo;
            }
            else {
                $temp_line = $sc_ul_link_button_normal;
            }
            $temp_line =~ s/\[\[link_class\]\]/$sc_cartlink_page_link_class/;
            $temp_line =~ s/\[\[seo_link_url\]\]/$sc_store_base_URL/;
            $temp_line =~ s/\[\[link_url\]\]/$sc_store_url/;
            $temp_line =~ s/\[\[clink_file\]\]/$clinkfile/;
            $temp_line =~ s/\[\[title\]\]/$clinkname/;
            $temp_line =~ s/\[\[link_display_name\]\]/$clinkname/;
            if ( $sc_global_bot_tracker ne '1' ) {
                # $temp_line =~ s/\[\[cartID_string\]\]/$cartID_string/;
            }
            $temp_line =~ s/\[\[cartID_string\]\]//;
            $cartlinks .= $temp_line;
        }
    }

    codehook('cartlinks_buttons_bottom');

}

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

1;
