$versions{'featured_product-ext_lib.pl'} = '06.6.00.0000';

# A Widget for vertical columns
#
# Contains sub routines (functions) for setting and saving  the featured product
# widget settings for column display in a template.
#
# Can be renamed in Widget Zone Manager
#
#######################################################################
#
# 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.
#
#######################################################################

$mc_featuredProductPath = "$mgrconfdir/featuredproduct.pl";

{
    local ($modname) = 'featured_product';
    register_extension( $modname, 'Featured Product Manager',
        $versions{$modname} );
    register_menu(
        'FeaturedProductSettings', 'write_featuredproduct_settings',
        $modname, 'Write Featured Product Settings'
    );
    register_menu(
        'change_featuredproduct_screen',
        'display_featuredproduct_screen',
        $modname, 'Display Featured Product Settings'
    );
    add_merchtools_settings_choice(
        'Featured Product',
        'Featured Product',
        "change_featuredproduct_screen"
    );
}

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

sub write_featuredproduct_settings {

    my ($myset, $subcat, $featuredURL, $featuredImage, $featuredPrice) = q{};
    my $temp_escaped_money_symbol = my_escape($sc_money_symbol);
    my $p1 = $in{'OptionA1p'};
    my $p2 = format_pricemgr($p1);
    my $tempName = $in{'OptionA1n'};
    $tempName =~ s/\-/_/g;
    $tempName =~ s/ /\-/g;
    if ( ( $in{'OptionA1'} ) && ( $in{'OptionA1t'} ) && ( $in{'OptionA1n'} ) && ( $in{'OptionA1p'} ) && ( $in{'OptionA1cat'} ) ) {
        if ( $in{'OptionA1subcat'} ) {
            $subcat = "&amp;[[subcatIndexField]]=$in{'OptionA1subcat'}";
        }

        if ( $sc_featuredProduct_secure_template eq '' ) {
            $sc_featuredProduct_secure_template = <<ENDOFTEXT;
<p><a href="[[featuredURL]]">
<img class="img-responsive img-vertical-widgets" src="[[imageURL]]" alt="[[OptionA1n]]" border="0"></a></p>
<p><a id="featured_link" href="[[featuredURL]]" title="[[OptionA1n]]">[[OptionA1n]]</a></p>
<p>[[OptionA1d]]</p>
<p class="ac_product_price">[[featuredPrice]]</p>
ENDOFTEXT
        }

        $sc_featuredProduct_secure = $sc_featuredProduct_secure_template;
        $featuredImage = qq|$sc_SSL_base_URL$in{'OptionA1t'}|;

        if ( $sc_seo_friendly =~ /yes/i ) {
            $featuredURL = qq|$sc_store_base_URL$tempName-p-$in{'OptionA1'}|;
        }
        else {
            $featuredURL = qq|$sc_store_url?p_id=$in{'OptionA1'}&amp;product=$in{'OptionA1cat'}$subcat&amp;name=$tempName&amp;xm=on|;
        }

        if ( $sc_money_symbol_placement eq 'front' ) {
            $featuredPrice = $temp_escaped_money_symbol . $sc_money_symbol_spaces . $p2;
        }
        else {
            $featuredPrice = $p2 . $sc_money_symbol_spaces . $temp_escaped_money_symbol;
        }

        $sc_featuredProduct_secure =~ s/\[\[OptionA1n\]\]/$in{'OptionA1n'}/g;
        $sc_featuredProduct_secure =~ s/\[\[featuredURL\]\]/$featuredURL/g;
        $sc_featuredProduct_secure =~ s/\[\[imageURL\]\]/$featuredImage/g;
        $sc_featuredProduct_secure =~ s/\[\[OptionA1d\]\]/$in{'OptionA1d'}/g;
        $sc_featuredProduct_secure =~ s/\[\[featuredPrice\]\]/$featuredPrice/g;
    }
    $sc_featuredProduct = $sc_featuredProduct_secure;
    $myset .= "\$sc_featuredProduct = qq|$sc_featuredProduct|;\n";
    $myset .= "\$sc_featuredProduct_secure = qq|$sc_featuredProduct_secure|;\n";
    $myset .=
      "\$sc_displayFeaturedProducts = \"$in{'sc_displayFeaturedProducts'}\";\n";
    $sc_displayFeaturedProducts = $in{'sc_displayFeaturedProducts'};
    update_store_settings( 'FEATUREDPRODUCT', $myset );
    $myset = q{|};    # css style
    $myset .= "$in{'OptionA1'}|";     # p_id
    $myset .= "$in{'OptionA1t'}|";    # thumbnail
    $myset .= "$in{'OptionA1n'}|";    # name
    $myset .= "$in{'OptionA1p'}|";    # price
    $myset .= "$in{'OptionA1cat'}|";   # product category
    $myset .= "$in{'OptionA1subcat'}|";    # product sub-category
    $myset .= "$in{'OptionA1d'}";     # description

    update_featuredproduct_file_settings( $mc_featuredProductPath, $myset );
    display_featuredproduct_screen();
}

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

sub display_featuredproduct_screen {

    my ($lines);
    my ( $junk, $OptionA1, $OptionA1t, $OptionA1n, $OptionA1p, $OptionA1d, $OptionA1cat, $Option1Asubcat );

    open( DATA, "$mc_featuredProductPath" );
    @indata = <DATA>;
    close(DATA);

    foreach $lines (@indata) {
        ( $csstyle, $OptionA1, $OptionA1t, $OptionA1n, $OptionA1p, $OptionA1cat, $OptionA1subcat, $OptionA1d ) = split( /\|/, $lines );
    }

    print &$manager_page_header( 'Featured Product', '', '', '', '' );

    print <<ENDOFTEXT;
<h1 class='mgr-page-title'>Featured Product Manager</h1>
<p><br></p>

<p class="well">This is a veritical column widget that allows you to enter a product from your store database that will be displayed as a <i>Featured Product</i> on your store pages. Please only include a short description and a Catalog/Database or thumbnail sized image of the product in this widget. You may rename the title of this widget (such as: "Deal of the Week", etc.) in the <b>Widget Zone Manager</b> as well as arrange the order and placement of the widget within a selected column in your chosen template.</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">Featured Product File has been successfully updated.</h4>
        </div>
    </div>
<div class="row col-xs-12 col-sm-12"><p><br></p></div>
ENDOFTEXT
    }

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

<p><b>Display Featured Product Widget:</b><span style="padding-left: 20px;"></span><select name=sc_displayFeaturedProducts>
<option selected>$sc_displayFeaturedProducts</option>
<option>yes</option>
<option>no</option>
</select></p>
<p><hr></p>

<p><b>Price:</b> <small>(no currency sign)</small> <span style="padding-left: 20px;"></span><input type="text" name="OptionA1p" size="10" value="$OptionA1p" maxlength="25" required></p>
<p><hr></p>

<p><b>Product ID:</b><span style="padding-left: 20px;"></span><input type="text" name="OptionA1" size="8" value="$OptionA1" maxlength="15" required></p>
<p><hr></p>

<p><b>Product Category:</b><span style="padding-left: 20px;"></span><input type="text" name="OptionA1cat" size="18" value="$OptionA1cat" maxlength="60" required></p>
<p><br></p>

<p><b>Product Sub-Category:</b> <small>(if any)</small><span style="padding-left: 20px;"></span><input type="text" name="OptionA1subcat" size="18" value="$OptionA1subcat" maxlength="60"></p>
<p><hr></p>

<p><b>Product Name:</b> <small>(to display)</small><span style="padding-left: 20px;"></span><input type="text" name="OptionA1n" size="18" value="$OptionA1n" maxlength="45 required"></p>
<p><hr></p>

<p><b>Short Description:</b> <small>(125 Chars. max.)</small>
<textarea name="OptionA1d" rows="2" class="form-control" maxlength="125" wrap="soft" required>$OptionA1d</textarea></p>
<p><hr></p>

<p><b>Image:</b><br>
<small>( enter path and image name. Can use the URL token for images such as: [[URLofImages]]/00001/image_name_cat.jpg )</small>
<input type="text" name="OptionA1t" class="form-control" value="$OptionA1t" maxlength="125" required></p>
<p><br><br></p>

<input type="hidden" name="system_edit_success" value="yes">
<button name="FeaturedProductSettings" class="btn btn-primary btn-lg btn-block mgr-center" value="Submit">Update Featured Product</button>

</form>
<p><br><br><br><br><br></p>

~;
    print &$manager_page_footer;

}

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

sub update_featuredproduct_file_settings {

    local ( $item, $stuff ) = @_;

    get_file_lock("$mc_featuredProductPath.lockfile");

    open( BESTFILE, ">$mc_featuredProductPath" )
      || my_die("Can't Open $mc_featuredProductPath");
    print( BESTFILE $stuff );
    close(BESTFILE);

    release_file_lock("$mc_featuredProductPath.lockfile");

}

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

1;
