$versions{'option_files.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 reading, parsing, displaying
# product options (from files) within catalog pages.
#
#

#######################################################################
sub load_opt_file {
    my ($files_to_load) = @_;
    my ( $very_first_part, $stuff_after, $arg, $newpath, $myans, $tack_on ) = q{};
    my ($path, $field) = q{};
    if ( $files_to_load ) {
        $field = $files_to_load;
        $field =~ s/ //g;
        $field =~ s/,/\[\[\nLoad_Option_File /g;
        $field = '%%Load_Option_File ' . $field . '%%';
        $field = load_opt_file_engine($field);
    }
    return $field;
}

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

sub load_opt_file_engine {    # do the actual loading of options file(s)
    my ($field) = @_;
    my ( $very_first_part, $stuff_after, $arg, $newpath, $myans, $tack_on );
    local (%file_list);

    # take only the part between <h3>--cut here--</h3>
    # tokens, so if there are any "load option file" tokens we must
    # perform special maint on them ... put the stuff between the cut tags
    # where the load token is and tack the rest on at the end (so all
    # agorascript is still there someplace.)

    while ( $field =~ /(%%Load_Option_File )([^%]+)(%%)/i ) {
        $arg = $2;
        $arg =~ s/ //g;
        if ( $arg =~ /,/ ) {    # there is a list, break it down
            $arg =~ s/,/%%Load_Option_File /g;
            $myans = "%%Load_Option_File $arg%%";
        }
        else {
            $newpath = "$sc_options_directory_path/$arg";
            if ( $file_list{$newpath} eq '' ) {    # prevent reloads
                 # allow tokens to be processed multiple times for efficiency in
                 # larger option matrices.  By default it is not enabled.
                if ( $sc_enable_multi_load_option_files =~ /yes/i ) {
                    $file_list{$newpath} = q{};
                }
                else {
                    $file_list{$newpath} = '1';
                }
                $myans = load_file_lines_to_str($newpath);
            }
            else {
                $myans = " (reload of option $arg attempted!) ";
            }
        }
        ( $very_first_part, $myans, $stuff_after ) = split( /<h3>--$cut_here_div--<\/h3>/i, $myans, 3 );
        if ( $myans eq '' ) {
            $myans   = $very_first_part;
            $tack_on = q{};
        }
        else {
            $tack_on = $very_first_part . $stuff_after;
        }
        if ( $myans eq '' ) {
            $myans = "(option file $arg not found)";
        }
        $field =~ s/(%%Load_Option_File )([^%]+)(%%)/$myans/i;
        if ( ( $tack_on ) && ( !( $field =~ /<h3>--$cut_here_div--<\/h3>/i ) ) )    {
            # there exists the cut tags in loaded files but not source ...
            # need to manually add them or we could be in trouble
            $field = "<h3>--$cut_here_div--</h3>" . $field;
            $field .= "<h3>--$cut_here_div--</h3>";
        }
        $field .= $tack_on;
    }
    return $field;
}

#######################################################################
#                    option_prep
#######################################################################
#
# Splits legacy option files that have a cut here divider:
#
# <h3>--Cut Here--</h3>
#
# Anything above the cut here divider(a variable) is thrown away, which allows
# for comments and/or instructions within the option files themselves.
#
#######################################################################

sub option_prep {
    local ( $field, $option_location, $product_id ) = @_;
    local ( $very_first_part, $junk );
    local ( $arg, $arg1, $arg2 );

    $field =
      agorascript( $field, 'optpre', "$option_location", __FILE__, __LINE__ );

    $field =~ s/%%ProductID%%/$product_id/ig;

   # need to remove this later... left just in case too many folks don't convert
    $field =~ s/\[\[PRODUCT_ID\]\]/$product_id/ig;

    $field =
      agorascript( $field, 'optpost', "$option_location", __FILE__, __LINE__ );

    # take only the part between <h3>--cut here--</h3> tokens
    ( $very_first_part, $field, $junk ) =
      split( /<h3>--$cut_here_div--<\/h3>/i, $field, 3 );
    if ( $field eq '' ) {
        $field = $very_first_part;
    }
    if ( $field eq '' ) {
        $field = "(file $option_location not found)";
    }

    return $field;

}

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

sub update_special_variable_options {
    local ($var_opt_action) = @_;
    local ( $cart_line, $code, $option_file, $script_type );
    local ( $options_list, $display_option_price );
    local ( $o_name, $o_price, $o_ship, $c_name, $c_price, $c_ship );
    local ( $options_list, $options, $zkey, $zdata, $change, %to_do_list );
    local ( @cart_row );
    my $temp = q{};
    my ($var_opt_used) = 'no';
    if ( $sc_disable_variable_options =~ /yes/i ) { return; }
    load_cart_copy();

    foreach my $inx_into_cart_copy ( sort( keys %cart_copy ) ) {
        $cart_line    = $cart_copy{$inx_into_cart_copy};
        @cart_row     = split( /\|/, $cart_line );
        $options_list = $cart_row[ $cart{'options_ids'} ];
        my @temp_data    = split( /$sc_opt_sep_marker/, $options_list );
        undef(%cart_row_options);
        undef(%orig_row_options);
        foreach my $inx (@temp_data) {
            ( $zkey, $zdata ) = split( /\*/, $inx, 2 );
            $zdata =~ s/\~/\|/g;
            $cart_row_options{$zkey} = $zdata;
            $orig_row_options{$zkey} = $zdata;
        }
        my $codelist = $cart_row[ $cart{'user1'} ];
        if ( $codelist ) {
            $var_opt_used = 'yes';
            my @code_items = split( /\;/, $codelist );
            foreach my $code (@code_items) {
                ( $option_file, $script_type ) = split( /,/, $code, 2 );
                $option_file = "$sc_options_directory_path/$option_file";
                if ( -f "$option_file" ) {
                    open( ZOPTFILE, "<$option_file" )
                      || file_open_error( "$option_file", 'Variable Options',__FILE__, __LINE__ );
                    read( ZOPTFILE, $code, 204800 );
                    close(ZOPTFILE);
                    $code = agorascript( $code, $script_type, 'Variable Options',__FILE__, __LINE__ );
                }
            }
        }

        # reconstruct the options data in the cart row based on 'current' data
        # also make adjustments to shipping and price fields
        $options_list = q{};
        $options      = q{};
        undef(%to_do_list);
        foreach my $zkey ( sort( keys %cart_row_options ) ) {
            $to_do_list{$zkey} = 1;
        }
        foreach my $zkey ( sort( keys %orig_row_options ) ) {
            $to_do_list{$zkey} = 1;
        }
        foreach my $zkey ( sort( keys %to_do_list ) ) {
            my $cart_option = $cart_row_options{$zkey};
            my $orig_option = $orig_row_options{$zkey};
            if ( $options_list ne '' ) { $options_list .= $sc_opt_sep_marker; }
            ( $o_name, $o_price, $o_ship ) = split( /\|/, $orig_option, 3 );
            ( $c_name, $c_price, $c_ship ) = split( /\|/, $cart_option, 3 );
            $change         = $c_price - $o_price;
            $inx            = $cart{'price_after_options'};
            $cart_row[$inx] = $cart_row[$inx] + $change;
            $change         = $c_ship - $o_ship;
            $inx            = $cart{'shipping'};
            $cart_row[$inx] = $cart_row[$inx] + $change;

            if ( $cart_option ) {
                $temp = $cart_option;
                $temp =~ s/\|/\~/g;
                $options_list .= $zkey . '*' . $temp;
                if ( ( 0 + $c_price ) == 0 ) {    #price zero, do not display it
                    $display_option_price = q{};
                }
                else {    # price non-zero, must format it
                    $display_option_price = " " . display_price($c_price);
                }
                if ( $options ) { $options .= "$sc_opt_sep_marker"; }
                $options .= "$c_name$display_option_price";
            }
        }
        $cart_row[ $cart{'options_ids'} ] = $options_list;
        $cart_row[ $cart{'options'} ]     = $options;

        # save the newly constructed cart row
        $cart_copy{$inx_into_cart_copy} = join( '|', @cart_row );
    }
    if ( $var_opt_used =~ /yes/i ) {
        save_cart_copy();
    }
}

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

sub cart_email_options {
    my ($a1) = @_;
    my ($a2, $jchar) = q{};
    $jchar = ' ' x $sc_opt_email_leading_spaces;
    my @xx = split( /$sc_opt_sep_marker/, $a1 );
    return join "\n" . $jchar, @xx;
}

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

1;
