<?php
    // $Id: Template.php,v 1.2 2004/08/11 12:32:16 doug Exp $
    //
    // Template Class
    // Supplies routines dealing with www.template.com
    // Template payment processor for KCart

    // To customise this, replace template with your payment processor name, and Template with the name
    // you wish to use in the class and text.
    // Then edit the methods formatItem and formatForm according to the instructions from the
    // payment processor.  
    // Some modules only support submitting in one currency, in this case you will need additional
    // changes - see the Kagi processor for an example of only submitting prices in USD.

    // Payment processor class for template
    class KCART_PP_Template {


        // present the table to allow the administrator to configure the Template settings
        function configure(&$settings) {
            $template = array();
            $template['HELP'] = $_SESSION['OBJ_help']->show_link("kcart_template", "template");

            $template['SHOP_ID_TEXT'] = $_SESSION['translate']->it ("Template Shop ID");
            $template['SHOP_URL_TEXT'] = $_SESSION['translate']->it ("Template Processing URL");
            $template['CHECKOUT_IMG_TEXT'] = $_SESSION['translate']->it ("Template Checkout Image URL");
            $template['CHECKOUT_LABEL_TEXT'] = $_SESSION['translate']->it ("Template Checkout Button Label");
            $template['BUYNOW_IMG_TEXT'] = $_SESSION['translate']->it ("Template BuyNow Image URL");
            $template['BUYNOW_LABEL_TEXT'] = $_SESSION['translate']->it ("Template BuyNow Button Label");
            $template['CURRENCY_CODE_TEXT'] = $_SESSION['translate']->it ("Template Currency Code"); 
            $template['CURRENCY_SYMBOL_TEXT'] = $_SESSION['translate']->it ("Template Currency Symbol");
            $template['ADJUSTMENT_TEXT'] = $_SESSION['translate']->it ("Template Percentage Price Adjustment");
            $template['EXCHANGE_RATE_TEXT'] = $_SESSION['translate']->it ("Template Exchange Rate");
            $template['NOTE_TEXT'] = $_SESSION['translate']->it ("Template Explanatory Note");

            $template['ACTIVE'] = PHPWS_Form::formRadio (
                "CART_pp_template_active", 0,
                $settings["pp_template_active"], NULL, 
                $_SESSION['translate']->it("Template not active")
            );
            $template['ACTIVE'] .= PHPWS_Form::formRadio (
                "CART_pp_template_active", 1,
                $settings["pp_template_active"], NULL, 
                $_SESSION['translate']->it("Template active")
            );
            $template['SHOP_ID'] = PHPWS_Form::formTextField (
                "CART_pp_template_shop_id", 
                $settings["pp_template_shop_id"], 40
            );
            $template['SHOP_URL'] = PHPWS_Form::formTextField (
                "CART_pp_template_shop_url", 
                $settings["pp_template_shop_url"], 40
            );
            $template['CHECKOUT_IMG_URL'] = PHPWS_Form::formTextField (
                "CART_pp_template_checkout_img_url", 
                $settings["pp_template_checkout_img_url"], 40
            );
            $template['CHECKOUT_LABEL'] = PHPWS_Form::formTextField (
                "CART_pp_template_checkout_label", 
                $settings["pp_template_checkout_label"], 40
            );
            $template['BUYNOW_IMG_URL'] = PHPWS_Form::formTextField (
                "CART_pp_template_buynow_img_url", 
                $settings["pp_template_buynow_img_url"], 40
            );
            $template['BUYNOW_LABEL'] = PHPWS_Form::formTextField (
                "CART_pp_template_buynow_label", 
                $settings["pp_template_buynow_label"], 40
            );
            $template['CURRENCY_CODE'] = PHPWS_Form::formTextField (
                "CART_pp_template_currency_code", 
                $settings["pp_template_currency_code"], 5
            );
            $template['CURRENCY_SYMBOL'] = PHPWS_Form::formTextField (
                "CART_pp_template_currency_symbol", 
                $settings["pp_template_currency_symbol"], 5
            );
            $template['ADJUSTMENT'] = PHPWS_Form::formTextField (
                "CART_pp_template_adjustment", 
                $settings["pp_template_adjustment"], 5
            );
            $template['NOTE'] = PHPWS_Form::formTextArea (
                "CART_pp_template_note", 
                $settings["pp_template_note"],4, 40
            );

            // get the exchange rate and put it into the template as text and a hidden input item
            $exchange_rate = KCART_ExchangeRates::getExchangeRate($settings["currency_code"], $settings["pp_template_currency_code"]);
            if ($exchange_rate > 0) { 
                $settings["pp_template_exchange_rate"] = $exchange_rate;
            }
            $template["EXCHANGE_RATE"] = $settings["pp_template_exchange_rate"];
            return(KCART_kCart::processTemplate ($template, "kcart_template", "settings.tpl"));
        }


        // set the default values, checking if they are already set
        function defaultValues($cartSettings) {
            $defaults = array(
                "pp_template_active" => 0,
                "pp_template_shop_id" => PHPWS_Text::parseInput("template"),
                "pp_template_shop_url" => PHPWS_Text::parseInput("https://order.template.com/cgi-bin/store.cgi"),
                "pp_template_checkout_img_url" => "",
                "pp_template_checkout_label" => "Checkout with Template",
                "pp_template_buynow_img_url" => "",
                "pp_template_buynow_label" => "Buy Now with Template",
                "pp_template_currency_code" => "USD",
                "pp_template_currency_symbol" => "$",
                "pp_template_adjustment" => "0",
                "pp_template_note" => "Template will add VAT to orders delivered within the European Union.",
                "pp_template_exchange_rate" => "1"
            );
            // step through each of the default settings
            // check if it is set in the cartSettings array
            // if not, set it - but don't overwrite them
            foreach ($defaults as $key => $value) {
                if (!isset($cartSettings[$key])) {
                    $cartSettings[$key] = $value;
                }
            }
            return($cartSettings);
        }


        // formats a products data to be suitable to pass to template.com
        // note that the price can only be specified in USD, but you can also pass a currency
        // code which is used to convert it to something else.
        function formatItem($cartSettings, $code, $desc, $price, $qty = 1, $item_counter = 1) {
            return(
                PHPWS_Form::formHidden (
                    array(
                        "Product" => sprintf(
                            "%s %.2f - %d * %s", 
                            $cartSettings["pp_template_currency_code"],
                            $price * $cartSettings["pp_template_exchange_rate"] * $qty, 
                            $qty, $code
                        )
                    )
                )
            );
        }


        // the form consists of a hidden input item for each thing in the cart, a
        // number of hidden input items for the form and a submit button.
        // this function creates the form hidden items and the submit button,
        // and uses the passed hidden items to create the form.
        function formatForm($cartSettings, $form_hiddens, $button_label, $image = "") {

            // these hidden items inform template who is to get the money, what currency it is in,
            // and to give the user the option of changing currency.
            $form_hiddens[0] .= PHPWS_Form::formHidden (
                array(
                    "action" => "add",
                    "view" => "cart",
                    "storeID" => $cartSettings["pp_template_shop_id"],
                    "order/javascript" => "YES",
                    "currency" => $cartSettings["pp_template_currency_code"]
                )
            );
            $checkout_url = $cartSettings["pp_template_shop_url"];
            // create the submit button
            $form_hiddens[0] .= KCART_PaymentProcessors::formSubmitImage ($button_label,$image);
            // create the form
            return(PHPWS_Form::makeForm ("KCART_user", $checkout_url, $form_hiddens, "post", NULL, NULL));
        }

        function buildBuyNowForm($cartSettings, $code, $desc, $price, $qty = 1) {
            // build a form for a single item, to generate a buynow button

            // if this processor is not active...
            if (!KCART_PP_Template::isActive($cartSettings)) {
                return("");
            }

            // adjust the price
            $price = $price * $cartSettings["pp_template_exchange_rate"] * (1 + ($cartSettings["pp_template_adjustment"] / 100));

            // format a single item
            $item[0] = KCART_PP_Template::formatItem(
                $cartSettings,
                $code,
                $desc,
                $price,
                $qty
            );
            // format the form, setting the button label and image url
            return(
                KCART_PP_Template::formatForm(
                    $cartSettings,
                    $item,
                    $cartSettings["pp_template_buynow_label"],
                    $cartSettings["pp_template_buynow_img_url"]
                )
            );
        }

        function buildCheckoutForm($cartSettings) {
            // build the checkout form for multiple items in the cart
            // returns a HTML string to create the form submission

            // if this processor is not active...
            if (!KCART_PP_Template::isActive($cartSettings)) {
                return("");
            }

            $item[0] = "";
            $item_counter = 1;
            // step through everything in the cart
            $cart_contents = KCART_UserCart::getCart();
            foreach ($cart_contents as $product_code => $quantity) {
                if ($quantity > 0) {
                    $product_info = KCART_Product::getProductInfo($product_code);

                    // adjust the price
                    $product_info["product_price"] = $product_info["product_price"] * 
                    $cartSettings["pp_template_exchange_rate"] * 
                    (1 + ($cartSettings["pp_template_adjustment"] / 100));

                    // format the item to create the hidden fields
                    $item[0] .= KCART_PP_Template::formatItem(
                        $cartSettings,
                        $product_code,
                        $product_info["product_description"],
                        $product_info["product_price"],
                        $quantity,
                        $item_counter
                    );
                    $item_counter ++;
                }
            }
            // format the form, setting the button label and image url
            $form = KCART_PP_Template::formatForm(
                $cartSettings,
                $item,
                $cartSettings["pp_template_checkout_label"],
                $cartSettings["pp_template_checkout_img_url"]
            );
            $card_images = "http://".KCART_kCart::returnSourceHttp()."mod/kcart_template/img/template.png";
            return(array(0 => $form, 1 => $cartSettings["pp_template_note"], 2 => $card_images));
        }

        // check if this payment processor is active
        // you can add additional logic to this if you only want to present this choice
        // to certain users, etc.
        function isActive($cartSettings) {
            if ($cartSettings["pp_template_active"] == 1) {
                return(TRUE);
            } else {
                return(FALSE);
            }
        }
    }

?>
