$suffix = ".inventory?product_id={$_REQUEST['product_id']}";
 }
 // Apply global options to the selected products
 if ($mode == 'apply') {
     if (!empty($_REQUEST['apply_options']['options'])) {
         $_data = $_REQUEST['apply_options'];
         foreach ($_data['options'] as $key => $value) {
             $products_ids = empty($_data['product_ids']) ? array() : explode(',', $_data['product_ids']);
             foreach ($products_ids as $k) {
                 $updated_products[$k] = db_get_row("SELECT a.product_id, a.company_id, b.product FROM ?:products as a" . " LEFT JOIN ?:product_descriptions as b ON a.product_id = b.product_id" . " AND lang_code = ?s" . " WHERE a.product_id = ?i", CART_LANGUAGE, $k);
                 if ($_data['link'] == 'N') {
                     fn_clone_product_options(0, $k, $value);
                 } else {
                     db_query("REPLACE INTO ?:product_global_option_links (option_id, product_id) VALUES (?i, ?i)", $value, $k);
                     if (fn_allowed_for('ULTIMATE')) {
                         fn_ult_share_product_option($value, $k);
                     }
                 }
             }
         }
         if (!empty($updated_products)) {
             fn_set_notification('N', __('notice'), __('options_have_been_applied_to_products'));
         }
     }
     $suffix = ".apply";
 }
 if ($mode == 'update') {
     fn_trusted_vars('option_data', 'regexp');
     if (fn_allowed_for('MULTIVENDOR')) {
         $option_data = array();
         if (!empty($_REQUEST['option_id'])) {
/**
 * Updates product option
 *
 * @param array $option_data option data array
 * @param int $option_id option ID (empty if we're adding the option)
 * @param string $lang_code language code to add/update option for
 * @return int ID of the added/updated option
 */
function fn_update_product_option($option_data, $option_id = 0, $lang_code = DESCR_SL)
{
    /**
     * Changes parameters before update option data
     *
     * @param array  $option_data Option data
     * @param int    $option_id   Option identifier
     * @param string $lang_code   Two-letter language code (e.g. 'en', 'ru', etc.)
     */
    fn_set_hook('update_product_option_pre', $option_data, $option_id, $lang_code);
    // Add option
    if (empty($option_id)) {
        if (empty($option_data['product_id'])) {
            $option_data['product_id'] = 0;
        }
        $option_data['option_id'] = $option_id = db_query('INSERT INTO ?:product_options ?e', $option_data);
        foreach (fn_get_translation_languages() as $option_data['lang_code'] => $_v) {
            db_query("INSERT INTO ?:product_options_descriptions ?e", $option_data);
        }
        $create = true;
        // Update option
    } else {
        // if option inventory changed from Y to N, we should clear option combinations
        if (!empty($option_data['product_id']) && !empty($option_data['inventory']) && $option_data['inventory'] == 'N') {
            $condition = fn_get_company_condition('?:product_options.company_id');
            $old_option_inventory = db_get_field("SELECT inventory FROM ?:product_options WHERE option_id = ?i {$condition}", $option_id);
            if ($old_option_inventory == 'Y') {
                $inventory_filled = db_get_field('SELECT COUNT(*) FROM ?:product_options_inventory WHERE product_id = ?i', $option_data['product_id']);
                if ($inventory_filled) {
                    fn_delete_product_option_combinations($option_data['product_id']);
                }
            }
        }
        if (fn_allowed_for('ULTIMATE') && !empty($option_data['product_id']) && fn_ult_is_shared_product($option_data['product_id']) == 'Y') {
            $product_company_id = db_get_field('SELECT company_id FROM ?:products WHERE product_id = ?i', $option_data['product_id']);
            $option_id = fn_ult_update_shared_product_option($option_data, $option_id, Registry::ifGet('runtime.company_id', $product_company_id), $lang_code);
            if (Registry::get('runtime.company_id') && Registry::get('runtime.company_id') != $product_company_id) {
                $deleted_variants = array();
                fn_set_hook('update_product_option_post', $option_data, $option_id, $deleted_variants, $lang_code);
                return $option_id;
            }
        }
        db_query("UPDATE ?:product_options SET ?u WHERE option_id = ?i", $option_data, $option_id);
        db_query("UPDATE ?:product_options_descriptions SET ?u WHERE option_id = ?i AND lang_code = ?s", $option_data, $option_id, $lang_code);
    }
    if (fn_allowed_for('ULTIMATE')) {
        // options of shared product under the shared store hasn't a company_id. No necessary for updating.
        if (!empty($option_data['company_id'])) {
            fn_ult_update_share_object($option_id, 'product_options', $option_data['company_id']);
        }
        if (!empty($option_data['product_id'])) {
            fn_ult_share_product_option($option_id, $option_data['product_id']);
        }
    }
    if (!empty($option_data['variants'])) {
        $var_ids = array();
        // Generate special variants structure for checkbox (2 variants, 1 hidden)
        if ($option_data['option_type'] == 'C') {
            $option_data['variants'] = array_slice($option_data['variants'], 0, 1);
            // only 1 variant should be here
            reset($option_data['variants']);
            $_k = key($option_data['variants']);
            $option_data['variants'][$_k]['position'] = 1;
            // checked variant
            $v_id = db_get_field("SELECT variant_id FROM ?:product_option_variants WHERE option_id = ?i AND position = 0", $option_id);
            $option_data['variants'][] = array('position' => 0, 'variant_id' => $v_id);
        }
        $variant_images = array();
        foreach ($option_data['variants'] as $k => $v) {
            if ((!isset($v['variant_name']) || $v['variant_name'] == '') && $option_data['option_type'] != 'C') {
                continue;
            }
            // Update product options variants
            if (isset($v['modifier'])) {
                $v['modifier'] = floatval($v['modifier']);
                if (floatval($v['modifier']) > 0) {
                    $v['modifier'] = '+' . $v['modifier'];
                }
            }
            if (isset($v['weight_modifier'])) {
                $v['weight_modifier'] = floatval($v['weight_modifier']);
                if (floatval($v['weight_modifier']) > 0) {
                    $v['weight_modifier'] = '+' . $v['weight_modifier'];
                }
            }
            //            if (isset($v['set_by_user'])) {
            //
            //            }
            $v['option_id'] = $option_id;
            if (empty($v['variant_id']) || !empty($v['variant_id']) && !db_get_field("SELECT variant_id FROM ?:product_option_variants WHERE variant_id = ?i", $v['variant_id'])) {
                $v['variant_id'] = db_query("INSERT INTO ?:product_option_variants ?e", $v);
                foreach (fn_get_translation_languages() as $v['lang_code'] => $_v) {
                    db_query("INSERT INTO ?:product_option_variants_descriptions ?e", $v);
                }
            } else {
                db_query("DELETE FROM ?:product_option_variants_disabled WHERE product_id=?i AND option_id =?i AND variant_id=?i", $option_data['extraProductId'], $option_id, $v['variant_id']);
                if ($v['status'] == 'Y') {
                    $vd = array('product_id' => $option_data['extraProductId'], 'option_id' => $option_id, 'variant_id' => $v['variant_id']);
                    db_query("INSERT INTO ?:product_option_variants_disabled ?e", $vd);
                    unset($v['status']);
                }
                db_query("UPDATE ?:product_option_variants SET ?u WHERE variant_id = ?i", $v, $v['variant_id']);
                db_query("UPDATE ?:product_option_variants_descriptions SET ?u WHERE variant_id = ?i AND lang_code = ?s", $v, $v['variant_id'], $lang_code);
            }
            $var_ids[] = $v['variant_id'];
            if ($option_data['option_type'] == 'C') {
                fn_delete_image_pairs($v['variant_id'], 'variant_image');
                // force deletion of variant image for "checkbox" option
            } else {
                $variant_images[$k] = $v['variant_id'];
            }
        }
        if ($option_data['option_type'] != 'C' && !empty($variant_images)) {
            fn_attach_image_pairs('variant_image', 'variant_image', 0, $lang_code, $variant_images);
        }
        // Delete obsolete variants
        $condition = !empty($var_ids) ? db_quote('AND variant_id NOT IN (?n)', $var_ids) : '';
        $deleted_variants = db_get_fields("SELECT variant_id FROM ?:product_option_variants WHERE option_id = ?i {$condition}", $option_id, $var_ids);
        if (!empty($deleted_variants)) {
            db_query("DELETE FROM ?:product_option_variants WHERE variant_id IN (?n)", $deleted_variants);
            db_query("DELETE FROM ?:product_option_variants_descriptions WHERE variant_id IN (?n)", $deleted_variants);
            foreach ($deleted_variants as $v_id) {
                fn_delete_image_pairs($v_id, 'variant_image');
            }
        }
    }
    if (!fn_allowed_for('ULTIMATE:FREE')) {
        // Rebuild exceptions
        if (!empty($create) && !empty($option_data['product_id'])) {
            fn_update_exceptions($option_data['product_id']);
        }
    }
    /**
     * Update product option (running after fn_update_product_option() function)
     *
     * @param array  $option_data      Array with option data
     * @param int    $option_id        Option identifier
     * @param array  $deleted_variants Array with deleted variants ids
     * @param string $lang_code        Language code to add/update option for
     */
    fn_set_hook('update_product_option_post', $option_data, $option_id, $deleted_variants, $lang_code);
    return $option_id;
}
Exemple #3
0
    }
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $suffix = '';
    // Define trusted variables that shouldn't be stripped
    fn_trusted_vars('product_data', 'override_products_data', 'product_files_descriptions', 'add_product_files_descriptions', 'products_data', 'product_file');
    //
    // Apply Global Option
    //
    if ($mode == 'apply_global_option') {
        if ($_REQUEST['global_option']['link'] == 'N') {
            fn_clone_product_options(0, $_REQUEST['product_id'], $_REQUEST['global_option']['id']);
        } else {
            db_query("REPLACE INTO ?:product_global_option_links (option_id, product_id) VALUES(?i, ?i)", $_REQUEST['global_option']['id'], $_REQUEST['product_id']);
            if (fn_allowed_for('ULTIMATE')) {
                fn_ult_share_product_option($_REQUEST['global_option']['id'], $_REQUEST['product_id']);
            }
        }
        $suffix = ".update?product_id={$_REQUEST['product_id']}";
    }
    //
    // Create/update product
    //
    if ($mode == 'update') {
        if (!empty($_REQUEST['product_data']['product'])) {
            fn_companies_filter_company_product_categories($_REQUEST, $_REQUEST['product_data']);
            if (empty($_REQUEST['product_data']['category_ids'])) {
                fn_set_notification('E', __('error'), __('category_is_empty'));
                return array(CONTROLLER_STATUS_REDIRECT, !empty($_REQUEST['product_id']) ? 'products.update?product_id=' . $_REQUEST['product_id'] : 'products.add');
            } else {
                $_REQUEST['product_data']['category_ids'] = explode(',', $_REQUEST['product_data']['category_ids']);
/**
 * Function checks and changes shared product data
 *
 * @param int $product_id Product ID
 */
function fn_check_and_update_product_sharing($product_id)
{
    $shared = false;
    $new_categories_company_ids = db_get_fields("SELECT DISTINCT c.company_id" . " FROM ?:products_categories pc" . " LEFT JOIN ?:categories c ON c.category_id = pc.category_id" . " WHERE pc.product_id = ?i", $product_id);
    $shared_categories_company_ids = db_get_fields("SELECT DISTINCT company_id FROM ?:ult_product_descriptions WHERE product_id = ?i", $product_id);
    $count = count($new_categories_company_ids);
    $company_id = reset($new_categories_company_ids);
    $product_company_id = db_get_field('SELECT company_id FROM ?:products WHERE product_id = ?i', $product_id);
    if ($count == 1 && $company_id == $product_company_id) {
        // product belongs to one store. It is not shared now
        // check that products.company_id == category.company_id
        /* Do not touch product.company_id.
           if ($company_id != $product_company_id) {
               db_query('UPDATE ?:products SET company_id = ?i WHERE product_id = ?i', $company_id, $product_id);
           }*/
        db_query('DELETE FROM ?:ult_product_descriptions WHERE product_id = ?i', $product_id);
        db_query('DELETE FROM ?:ult_product_prices WHERE product_id = ?i', $product_id);
        $product_options = fn_get_product_options($product_id, DESCR_SL, false, false, false, true);
        $product_options = array_keys($product_options);
        if (!empty($product_options)) {
            db_query('DELETE FROM ?:ult_product_option_variants WHERE option_id IN (?a)', $product_options);
        }
    } else {
        $shared = true;
        $new_company_ids = array_diff($new_categories_company_ids, $shared_categories_company_ids);
        if (!empty($new_company_ids)) {
            $product_options = fn_get_product_options($product_id, DESCR_SL, false, false, false, true);
            $product_options = array_keys($product_options);
        }
        foreach ($new_company_ids as $new_cid) {
            // coping owner data to tables with shared data
            db_query('REPLACE INTO ?:ult_product_descriptions (' . ' product_id, lang_code, company_id, product, shortname, short_description,' . ' full_description, meta_keywords, meta_description, search_words, page_title,' . ' age_warning_message, promo_text)' . ' SELECT' . ' product_id, lang_code, ?i, product, shortname, short_description,' . ' full_description, meta_keywords, meta_description, search_words,' . ' page_title, age_warning_message, promo_text' . ' FROM ?:product_descriptions' . ' WHERE product_id = ?i', $new_cid, $product_id);
            db_query('REPLACE INTO ?:ult_product_prices (' . ' product_id, price, percentage_discount, lower_limit, company_id, usergroup_id)' . ' SELECT product_id, price, percentage_discount, lower_limit, ?i, usergroup_id' . ' FROM ?:product_prices' . ' WHERE product_id = ?i', $new_cid, $product_id);
            if (!empty($product_options)) {
                db_query('REPLACE INTO ?:ult_product_option_variants (' . ' variant_id, option_id, company_id, modifier, modifier_type)' . ' SELECT variant_id, option_id, ?i, modifier, modifier_type' . ' FROM ?:product_option_variants' . ' WHERE option_id IN (?a)', $new_cid, $product_options);
            }
        }
        $deleted_company_ids = array_diff($shared_categories_company_ids, $new_categories_company_ids);
        if (!empty($deleted_company_ids)) {
            // deleting data from shared tables
            db_query('DELETE FROM ?:ult_product_descriptions' . ' WHERE product_id = ?i AND company_id IN (?a)', $product_id, $deleted_company_ids);
            db_query('DELETE FROM ?:ult_product_prices' . ' WHERE product_id = ?i AND company_id IN (?a)', $product_id, $deleted_company_ids);
            if (!isset($product_options)) {
                $product_options = fn_get_product_options($product_id, DESCR_SL, false, false, false, true);
                $product_options = array_keys($product_options);
            }
            if (!empty($product_options)) {
                db_query('DELETE FROM ?:ult_product_option_variants' . ' WHERE option_id IN (?a) AND company_id IN (?a)', $product_options, $deleted_company_ids);
            }
        }
        $global_option_links = db_get_fields("SELECT option_id FROM ?:product_global_option_links WHERE product_id = ?i", $product_id);
        $product_options = array_merge(isset($product_options) ? $product_options : array(), $global_option_links);
        if (!empty($product_options)) {
            foreach ($product_options as $po_id) {
                fn_ult_share_product_option($po_id, $product_id);
            }
        }
    }
    $cids = db_get_fields('SELECT category_id FROM ?:products_categories WHERE product_id = ?i', $product_id);
    fn_update_product_count($cids);
    /**
     * Processed addition/deletion of companies product is shared between
     *
     * @param int     $product_id
     * @param boolean $shared
     * @param array   $shared_categories_company_ids
     * @param array   $new_categories_company_ids
     */
    fn_set_hook('check_and_update_product_sharing', $product_id, $shared, $shared_categories_company_ids, $new_categories_company_ids);
}