<?php
    //
    // Product class - functions which act on a single product
    // $Id: Product.php,v 1.13 2004/12/18 17:32:07 doug Exp $
    // Doug Rinckes
    //

    class KCART_Product {

        // editProduct: modifies a product entry according to the action ("create" or "edit") 
        // the product is passed as an array
        // provides the form for displaying and editing a product
        function editProduct ($action,$product_code) {

            $GLOBALS["CNT_kcart_productmanager"]['title'] = "Kcart: ".$_SESSION['translate']->it("Product Editor");

            $hiddens['module'] = "kcart";

            // get the product info
            $product = KCART_Product::getProductInfo($product_code);

            // set different op for creating and editing a product 
            if ($action == "create") {
                $template['TITLE'] = $_SESSION['translate']->it ("Create A New Product");
                $hiddens['save_new_product'] = " ";
            } else if ($action == "edit") {
                $template['TITLE'] = $_SESSION['translate']->it ("Edit A Product");
                $hiddens['original_product_code'] = $product["product_code"];
            }

            $hiddens['KCART_manage'] = "modifyProductList";
            // the active state of the product is passed but not shown
            $hiddens['CART_product_active'] = $product["product_active"];

            // build the template contents
            $template['PRODUCT_CODE_TEXT'] = $_SESSION['translate']->it ("Product Code");
            $template['PRODUCT_CODE'] = PHPWS_Form::formTextField ("CART_product_code", $product["product_code"], 20);
            $template['PRODUCT_DESCRIPTION_TEXT'] = $_SESSION['translate']->it ("Description");
            $template['PRODUCT_DESCRIPTION'] = PHPWS_Form::formTextArea ("CART_product_description", $product["product_description"], 4, 60);
            $template['PRODUCT_URLBIG_TEXT'] = $_SESSION['translate']->it ("Large image URL - displayed as a popup");
            $template['PRODUCT_URLBIG'] = PHPWS_Form::formTextField ("CART_product_imageurl_big", $product["product_imageurl_big"], 60);
            $template['PRODUCT_URLSMALL_TEXT'] = $_SESSION['translate']->it ("Small image URL - displayed in the order box and cart summary");
            $template['PRODUCT_URLSMALL'] = PHPWS_Form::formTextField ("CART_product_imageurl_small", $product["product_imageurl_small"], 60);
            $template['PRODUCT_PRICE_TEXT'] = $_SESSION['translate']->it ("Price (do not include currency symbols)");
            $template['PRODUCT_PRICE'] = PHPWS_Form::formTextField ("CART_product_price", $product["product_price"], 20);
            $template['PRODUCT_ACTIVEPAGES_TEXT'] = $_SESSION['translate']->it ("List of Page ID's to activate this product on (page id's <b>must</b> be delimited with colons, the home page has page ID :home:)");
            $template['PRODUCT_ACTIVEPAGES'] = PHPWS_Form::formTextArea ("CART_product_activepages", $product["product_activepages"], 2, 60);

            // add the hidden elements
            $elements[0] = PHPWS_Form::formHidden ($hiddens);

            // display a different submit button for creating and editing a product 
            if ($action == "create") {
                $template['SUBMIT'] = PHPWS_Form::formSubmit ($_SESSION['translate']->it ("Create Product"), "save_new_product");
            } else if ($action == "edit") {
                $template['SUBMIT'] = PHPWS_Form::formSubmit ($_SESSION['translate']->it ("Save Changes"), "save_existing_product");
            }

            // produce the help button
            $template['SUBMIT_HELP'] = $_SESSION['OBJ_help']->show_link("kcart", "product_settings");
            // process the template
            $elements[0] .= KCART_kCart::processTemplate ($template, "kcart", "addcreateproduct.tpl");
            $elements[0] .= KCART_kCart::processTemplate ($template, "kcart", "form_submit.tpl");
            // make the form
            $content = PHPWS_Form::makeForm ("CART_product_addedit", "index.php", $elements, "post", NULL, NULL);

            // provide a "cancel" button
            $content .= KCART_kCart::managekCartButton($_SESSION['translate']->it("Cancel"));

            $GLOBALS["CNT_kcart_productmanager"]['content'] = $content;
        }


        // Deletes a product from the database
        // There must be a POST "CART_product_yes" or "CART_product_no" to take any action
        // otherwise it confirms with the user whether or not to delete the product
        function deleteProduct ($product_code) {

            // not sure what this does..
            $_SESSION["OBJ_layout"]->reorder_boxes ();

            // if the delete fails, produce an error box
            if (!$GLOBALS['core']->sqlDelete ("mod_kcart_products", "product_code", $product_code)) {
                KCART_kCart::error ($_SESSION['translate']->it ("The product [var1] could not be deleted from the database. This should not happen."));
                return;
            }
        }

        // Toggles whether the product is active ("on" or "off")
        function toggleProductActive ($product_code) {
            $product = KCART_Product::getProductInfo($product_code);
            if ($product) {
                PHPWS_WizardBag::toggle ($product["product_active"]);
                $update_array = array ("product_active" => $product["product_active"]);
                $GLOBALS['core']->sqlUpdate ($update_array, "mod_kcart_products", "product_code", $product_code);
            }
        }

        function saveProduct($product_array) {
            // save the passed product info into the database
            // the passed array must have the following keys:
            //  product_code
            //  product_description
            //  product_price
            //  product_activepages
            //  product_imageurl_big
            //  product_imageurl_small
            //  product_active

            // But since we don't trust outside code, we copy just those values into a new array

            // create an array with the passed values
            $save_array = array (
                "product_code" => $product_array["product_code"],
                "product_description" => $product_array["product_description"],
                "product_price" => $product_array["product_price"],
                "product_activepages" => $product_array["product_activepages"],
                "product_imageurl_big" => $product_array["product_imageurl_big"],
                "product_imageurl_small" => $product_array["product_imageurl_small"],
                "product_active" => $product_array["product_active"]
            );
            // check to see if the product code already exists
            if ($GLOBALS['core']->sqlSelect("mod_kcart_products","product_code",$product_array["product_code"])) {
                // it does, so do an update
                $GLOBALS['core']->sqlUpdate ($save_array, "mod_kcart_products", "product_code", $product_array["product_code"]);
            } else {
                // the product code doesn't exist, so do an insert
                $GLOBALS['core']->sqlInsert ($save_array, "mod_kcart_products", TRUE, TRUE, FALSE);
            }
        }

        // return an array of information for the passed product
        function getProductInfo($product_code) {
            $product_result = $GLOBALS['core']->sqlSelect ("mod_kcart_products", "product_code", $product_code);
            $info = array();
            if ($product_result) {
                $info = array(
                    "product_code" => $product_code,
                    "product_description" => $product_result[0]["product_description"],
                    "product_price" => $product_result[0]["product_price"],
                    "product_activepages" => $product_result[0]["product_activepages"],
                    "product_active" => $product_result[0]["product_active"],
                    "product_imageurl_big" => $product_result[0]["product_imageurl_big"],
                    "product_imageurl_small" => $product_result[0]["product_imageurl_small"]
                );
            }
            return($info);
        }


        // return a HTML string if passed up to two URLs.  If only the first one is defined, then just return an image
        // tag.  If both are defined, then return a link to the second and display the first
        function formatImage ($imageurl_small, $imageurl_big) {
            // initialise the HTML code
            $html = "";
            // strip out leading and trailing whitespace
            $imageurl_small = ltrim($imageurl_small);
            $imageurl_small = rtrim($imageurl_small);
            $imageurl_big = ltrim($imageurl_big);
            $imageurl_big = rtrim($imageurl_big);
            if (strlen($imageurl_small) >= 1) {
                $html = "<img src=\"$imageurl_small\">";
                if (strlen($imageurl_big) >= 1) {
                    $html = "<a href=\"$imageurl_big\">$html</a>";
                }
            }
            return($html);
        }

    }

?>
