Пример #1
0
    }
    if ($cat) {
        $path = cw_category_get_subcategory_ids($cat);
        if (!empty($path)) {
            cw_recalc_subcat_count($path);
        }
    } else {
        cw_recalc_subcat_count();
    }
    $top_message = array('content' => cw_get_langvar_by_name('msg_adm_categories_upd'), 'type' => 'I');
    cw_header_location("index.php?target={$target}" . ($mode ? '&mode=' . $mode : '') . "&cat={$cat}&js_tab={$js_tab}");
}
if ($action == 'delete') {
    if ($confirmed == "Y") {
        # kronev, for the big amount of products - the
        cw_display_service_header('lbl_delete_categories') . '<br/>';
        if (is_array($categories_to_delete)) {
            foreach ($categories_to_delete as $cat) {
                $parent_category_id = cw_call('cw_category_delete', array($cat, true));
            }
        }
        $top_message = array('content' => cw_get_langvar_by_name('msg_adm_category_del'), 'type' => 'I');
        cw_header_location('index.php?target=categories&cat=' . $parent_category_id);
    } else {
        $categories_to_delete = is_array($delete_arr) ? array_keys($delete_arr) : null;
        cw_header_location("index.php?target=categories&cat={$cat}&mode=delete");
    }
}
if (empty($featured_type)) {
    $featured_type = 'featured_products';
}
global $csvxc_field_types;
if ($mode == 'flexible_import') {
    if ($action == 'import_file') {
        mb_internal_encoding('UTF-8');
        setlocale(LC_ALL, "en_US.UTF-8");
        $rules = array('profile_id' => '', 'import_type' => '');
        $data = array('profile_id' => $profile_id, 'import_type' => $import_type, 'import_file' => $_FILES['import_file']);
        $fill_error = cw_call('cw_error_check', array(&$data, $rules));
        if ($import_type == 'server' && !$server_filenames) {
            $index = substr_count($fill_error, "\n") + ($fill_error != '' ? 1 : 0);
            $fill_error .= "\n" . ++$index . 'a . ' . cw_get_langvar_by_name('err_field_select_file');
        }
        if ($fill_error) {
            cw_add_top_message($fill_error, 'E');
        } else {
            cw_display_service_header("Running flexible import");
            $profile = cw_call('cw_flexible_import_get_profile', array('params' => array('id' => intval($profile_id))));
            if (!empty($profile['mapping_data'])) {
                $mapping_data = unserialize($profile['mapping_data']);
            }
            foreach ($mapping_data as $tbl_name => $tbl_fields) {
                foreach ($tbl_fields as $fld_name => $fld_mapping) {
                    if (!empty($fld_mapping['custom_sql'])) {
                        $mapping_data[$tbl_name][$fld_name]['custom_sql'] = stripslashes(base64_decode($fld_mapping['custom_sql']));
                    }
                }
            }
            if ($server_filenames) {
                foreach ($server_filenames as $s_file) {
                    $profile['file_name'] = fi_files_path . $s_file;
                    $parsed_file = cw_call('cw_flexible_import_parse_file', array($profile, false));
function cw_product_build_flat($params, $return)
{
    extract($params);
    global $tables, $addons;
    $where = "";
    if ($product_id) {
        if (!is_array($product_id)) {
            $product_id = array($product_id);
        }
        $where = "product_id in ('" . implode("', '", $product_id) . "')";
        db_query("delete from {$tables['products_flat']} where {$where}");
    } else {
        db_query("delete from {$tables['products_flat']}");
    }
    $fields = $from_tbls = $query_joins = $where = $groupbys = $having = $orderbys = array();
    $from_tbls[] = 'products';
    $fields[] = "{$tables['products']}.product_id";
    $where[] = $tables['products'] . '.' . $where;
    # kornev, get the query from the addons
    if ($return) {
        foreach ($return as $saname => $sadata) {
            if (isset(${$saname}) && is_array(${$saname}) && empty(${$saname})) {
                ${$saname} = $sadata;
            }
        }
    }
    # kornev, there are nothing to do if the fields are empty (by default)
    if (count($fields) == 1) {
        return;
    }
    $groupbys[] = "{$tables['products']}.product_id";
    # kornev, generate it;
    $search_query = cw_db_generate_query($fields, $from_tbls, $query_joins, $where, $groupbys, $having, $orderbys);
    if ($tick > 0) {
        cw_display_service_header('lbl_rebuild_products_flat');
    }
    $sd = db_query($search_query);
    $updated = 0;
    if ($sd) {
        while ($row = db_fetch_array($sd)) {
            cw_array2insert('products_flat', cw_addslashes($row), true);
            $updated++;
            if ($tick > 0 && $updated % $tick == 0) {
                cw_flush('.');
            }
            if ($tick > 0 && $updated / $tick % 100 == 0) {
                cw_flush('<br/>');
            }
        }
    }
    db_free_result($sd);
    return $updated;
}
<?php

# kornev, TOFIX
if (!isset($tables['cached_images'])) {
    $tables['cached_images'] = 'cw_xcm_cached_images';
}
$cache_files = cw_query("SELECT * FROM {$tables['cached_images']}");
if (empty($cache_files) || !is_array($cache_files)) {
    cw_header_location("index.php?target=settings&js_tab=Smarty_plugins");
}
cw_display_service_header();
cw_flush('Removing cached images: ...');
$cnt = 0;
if (count($cache_files)) {
    foreach ($cache_files as $file) {
        @unlink($file['image_path']);
        $cnt++;
        if ($cnt > 5) {
            cw_flush('.');
        }
    }
}
db_query("TRUNCATE TABLE {$tables['cached_images']}");
cw_flush('<br />cached images are deleted.');
cw_header_location("index.php?target=settings&js_tab=Smarty_plugins");
exit;
function cw_recalc_subcat_count($category_id = 0, $tick = 0)
{
    global $tables, $config;
    if ($tick > 0) {
        cw_display_service_header('lbl_recalc_subcat_count') . '<br/>';
    } elseif ($tick > 0 && $start % $tick == 0) {
        cw_flush('.');
    }
    if (!$category_id) {
        $where = '';
    } elseif (is_array($category_id)) {
        if (is_array(current($category_id))) {
            foreach ($categoryid as $k => $v) {
                $categoryid[$k] = $v['category_id'];
            }
        }
        $where = "where category_id in ('" . implode("', '", $category_id) . "')";
    } else {
        $where = "where parent_id='{$category_id}' or category_id='{$category_id}'";
    }
    $subcategories = cw_query_column("select category_id from {$tables['categories']} {$where}");
    db_query("delete from {$tables['categories_subcount']} where category_id in ('" . implode("', '", $subcategories) . "')");
    $subcategories_list = implode("','", $subcategories);
    // Calculates memberships subcategories/products' counts as '0-membership' count plus only unique subcategories/products
    // for that membership (vs 0-mbrs) excluding all common with '0-membership' scats/prods
    // Status 1 -- only enabled scats/prods
    // Status 0 -- All scats/prods
    $counts = "select cm.category_id, cm.membership_id,\n\t\tcount(DISTINCT if(ifnull(cms.membership_id,-1)=-1,NULL,sc.category_id)) as subcategory_count,\n\t\tcount(DISTINCT if(ifnull(cms.membership_id,-1)=-1,NULL,if(ifnull(sc.status,0)=0,NULL,sc.category_id))) as subcategory_count_avail,\n\t\tcount(DISTINCT if(ifnull(pm.membership_id,-1)=-1,NULL,p.product_id)) as product_count,\n\t\tcount(DISTINCT if(ifnull(pm.membership_id,-1)=-1,NULL,if(ifnull(pr.status,0)=0,NULL,p.product_id))) as product_count_avail\n\t\tfrom {$tables['categories_memberships']} cm\n\t\tleft join {$tables['categories']} sc on cm.category_id = sc.parent_id\n\t\tleft join {$tables['categories_memberships']} cms on sc.category_id=cms.category_id and cms.membership_id in (0,cm.membership_id)\n\t\tleft Join  {$tables['products_categories']} p on p.category_id=cm.category_id\n\t\tleft Join  {$tables['products']} pr on p.product_id=pr.product_id\n\t\tleft Join  {$tables['products_memberships']} pm on pm.product_id=p.product_id and pm.membership_id in (0,cm.membership_id)\n\t\twhere cm.category_id in ('{$subcategories_list}')\n\t\tand cm.membership_id is not null and cm.category_id is not null\n\t\tgroup by cm.category_id, cm.membership_id";
    $counts = cw_query_hash($counts, array('category_id', 'membership_id'), false);
    $i = 0;
    if (!empty($counts)) {
        foreach ($counts as $category_id => $data) {
            foreach ($data as $membership_id => $cnts) {
                extract($cnts);
                $status = 0;
                $arr = compact('category_id', 'membership_id', 'status', 'subcategory_count', 'product_count');
                cw_array2insert('categories_subcount', $arr, true);
                $status = 1;
                $subcategory_count = $subcategory_count_avail;
                $product_count = $product_count_avail;
                $arr = compact('category_id', 'membership_id', 'status', 'subcategory_count', 'product_count');
                cw_array2insert('categories_subcount', $arr, true);
                if ($tick > 0 && $i % $tick == 0) {
                    cw_flush(". ");
                }
                $i++;
            }
        }
    }
    # kornev, used only in the shop - so only the shop amount is calculated
    # general products count
    cw_load('product');
    $count_products = cw_query_first("select group_concat(distinct membership_id) as ind from {$tables['products_memberships']}");
    $count_products = array_fill_keys(explode(',', $count_products['ind']), 0);
    db_query("delete from {$tables['categories_subcount']} where category_id = 0");
    if (is_array($count_products)) {
        foreach ($count_products as $k => $v) {
            $product_count = intval(cw_func_call('cw_product_search', array('data' => array('count' => 1), 'user_info' => array('membership_id' => $k), 'current_area' => 'C')));
            cw_array2insert('categories_subcount', array('category_id' => 0, 'membership_id' => $k, 'status' => 2, 'product_count' => $product_count), true);
        }
    }
}
function cw_rebuild_variants($product_id, $force_rebuild = false, $tick = 1)
{
    global $tables;
    if (!$force_rebuild) {
        # Check variant's matrix
        $options_count = cw_query_first_cell("SELECT COUNT(*) FROM {$tables['product_options']}, {$tables['product_options_values']} WHERE {$tables['product_options']}.product_option_id = {$tables['product_options_values']}.product_option_id AND {$tables['product_options']}.product_id = '{$product_id}' AND {$tables['product_options']}.type = '' AND {$tables['product_options']}.avail = 1 AND {$tables['product_options_values']}.avail = 1");
        $variants_count = count(cw_query_column("SELECT COUNT(*) FROM {$tables['product_variant_items']}, {$tables['product_variants']} WHERE {$tables['product_variants']}.product_id = '{$product_id}' AND {$tables['product_variants']}.variant_id = {$tables['product_variant_items']}.variant_id GROUP BY {$tables['product_variant_items']}.option_id"));
        if ($options_count == $variants_count && $options_count > 0) {
            return true;
        }
    }
    if ($tick > 0) {
        cw_display_service_header("lbl_rebuild_variants");
    }
    $ids = cw_query_column("SELECT variant_id FROM {$tables['product_variants']} WHERE product_id = '{$product_id}'");
    if (!empty($ids)) {
        # Save old data
        $vars = cw_query_hash("SELECT pv.*, pwa.avail  FROM {$tables['product_variants']} as pv LEFT JOIN {$tables['products_warehouses_amount']} as pwa ON pv.variant_id = pwa.variant_id WHERE pv.product_id = '{$product_id}'", "variant_id", false);
        $prices = db_query("select pp.* from {$tables['products_prices']} as pp where pp.product_id = '{$product_id}' AND variant_id != 0");
        if ($prices) {
            while ($v = db_fetch_array($prices)) {
                if (!isset($vars[$v['variant_id']])) {
                    continue;
                }
                $key = $v['quantity'] . "|" . $v['membership_id'];
                if (!isset($vars[$v['variant_id']]['prices'])) {
                    $vars[$v['variant_id']]['prices'] = array();
                }
                if (!isset($vars[$v['variant_id']]['prices'][$key]) || $vars[$v['variant_id']]['prices'][$key]['price'] > $v['price']) {
                    $vars[$v['variant_id']]['prices'][$key] = $v;
                }
            }
            db_free_result($prices);
        }
        unset($prices);
        $items = cw_query_hash("SELECT {$tables['product_variant_items']}.*, {$tables['product_options_values']}.product_option_id FROM {$tables['product_variant_items']}, {$tables['product_options_values']}, {$tables['product_variants']} WHERE {$tables['product_variant_items']}.option_id = {$tables['product_options_values']}.option_id AND {$tables['product_variant_items']}.variant_id = {$tables['product_variants']}.variant_id AND {$tables['product_variants']}.product_id = '{$product_id}'", array('product_option_id', "option_id"), true, true);
        # Delete old variants
        /*
                $tmp = cw_query_first("SELECT MIN(avail) as avail, MIN(weight) as weight FROM $tables[product_variants] WHERE product_id = '$product_id'");
                db_query("UPDATE $tables[products] SET avail = '$tmp[avail]', weight = '$tmp[weight]' WHERE product_id = '$product_id'");
                unset($tmp);
        */
        db_query("DELETE FROM {$tables['products_prices']} WHERE product_id = '{$product_id}' AND variant_id != 0");
        db_query("DELETE FROM {$tables['product_variant_items']} WHERE variant_id IN ('" . implode("','", $ids) . "')");
        db_query("DELETE FROM {$tables['products_warehouses_amount']} WHERE product_id = '{$product_id}' AND variant_id != 0");
    }
    unset($ids);
    db_query("DELETE FROM {$tables['product_variants']} WHERE product_id = '{$product_id}'");
    # Get modifier-classes
    $classes = cw_query($sql = "SELECT product_option_id FROM {$tables['product_options']} WHERE product_id = '{$product_id}' AND type = '' AND avail = 1 ORDER BY orderby");
    if (empty($classes)) {
        return false;
    }
    foreach ($classes as $k => $v) {
        $classes[$k]['cnt'] = 0;
        $classes[$k]['options'] = cw_query_column("SELECT option_id FROM {$tables['product_options_values']} WHERE product_option_id = '{$v['product_option_id']}' AND avail = 1 ORDER BY orderby, option_id ASC ");
        if (!@count($classes[$k]['options']) || !is_array($classes[$k]['options'])) {
            unset($classes[$k]);
        }
    }
    if (empty($classes)) {
        return false;
    }
    $classes = array_values($classes);
    $classes[0]['cnt'] = -1;
    # Build variant's matrix
    $variants = array();
    # Write variants to DB
    $product = cw_query_first("SELECT {$tables['products']}.eancode, {$tables['products']}.productcode, {$tables['products']}.weight,  {$tables['products']}.cost, {$tables['products_prices']}.price FROM {$tables['products']}, {$tables['products_prices']}  WHERE  {$tables['products_prices']}.variant_id = 0 AND {$tables['products_prices']}.quantity = '1' AND {$tables['products_prices']}.membership_id = 0 AND {$tables['products']}.product_id = '{$product_id}' GROUP BY {$tables['products']}.product_id");
    $cnt_row = $cnt = $cnd_ean = 0;
    do {
        $is_end = false;
        $options = array();
        $old_variants = array();
        foreach ($classes as $k => $c) {
            $option_id = 0;
            if (!$is_end) {
                if ($c['cnt'] >= count($c['options']) - 1) {
                    $c['cnt'] = 0;
                } else {
                    $c['cnt']++;
                    $is_end = true;
                }
                $classes[$k] = $c;
            }
            $option_id = $c['options'][$c['cnt']];
            if (empty($option_id)) {
                continue;
            }
            $options[] = $option_id;
            if (isset($items[$c['product_option_id']][$option_id])) {
                if (empty($old_variants)) {
                    $old_variants = $items[$c['product_option_id']][$option_id];
                } else {
                    $old_variants = array_intersect($old_variants, $items[$c['product_option_id']][$option_id]);
                }
            }
        }
        if (!$is_end || empty($options)) {
            break;
        }
        $_product = $product;
        # Restore old data
        $old_variant_id = false;
        if (is_array($old_variants) && !empty($old_variants)) {
            $old_variant_id = array_shift($old_variants);
            if (isset($vars[$old_variant_id])) {
                $_product = cw_array_merge($_product, $vars[$old_variant_id]);
            }
        }
        unset($old_variants);
        # Get unique SKU
        $sku = $_product['productcode'];
        while (cw_query_first_cell("SELECT COUNT(*) FROM {$tables['product_variants']} WHERE productcode = '{$sku}'") > 0) {
            $sku = $_product['productcode'] . ++$cnt;
        }
        $eancode = $_product['eancode'];
        while (cw_query_first_cell("SELECT COUNT(*) FROM {$tables['product_variants']} WHERE eancode = '{$eancode}'") > 0) {
            $eancode = $_product['eancode'] . ++$cnd_ean;
        }
        $data = array("product_id" => $product_id, "weight" => $_product['weight'], "cost" => $_product['cost'], "productcode" => $sku, 'eancode' => $eancode);
        # Check variant_id
        if (!empty($old_variant_id) && cw_query_first_cell("SELECT COUNT(*) FROM {$tables['product_variants']} WHERE variant_id = '{$old_variant_id}'") == 0) {
            $data['variant_id'] = $old_variant_id;
        }
        # Insert variant info
        $variant_id = cw_array2insert('product_variants', $data);
        if (empty($variant_id)) {
            continue;
        }
        if ($_product['avail'] == NULL) {
            $_product['avail'] = 0;
        }
        if (cw_query_first_cell("SELECT COUNT(*) FROM {$tables['products_warehouses_amount']} WHERE variant_id = '{$variant_id}'") == 0) {
            cw_array2insert('products_warehouses_amount', array('product_id' => $product_id, 'avail' => $_product['avail'], 'avail_ordered' => 0, 'avail_sold' => 0, 'avail_reserved' => '0', 'variant_id' => $variant_id, 'warehouse_customer_id' => '0'), false);
        }
        # Write products_prices
        if (empty($_product['prices'])) {
            cw_price_lists_replace_price($product_id, $_product['price'], $variant_id, true, true);
        } else {
            foreach ($_product['prices'] as $p) {
                cw_price_lists_replace_price($product_id, $p['price'], $variant_id, true, $p['is_manual']);
            }
        }
        # Restore image
        if (!empty($old_variant_id) && $variant_id != $old_variant_id) {
            cw_image_delete($variant_id, "W");
            db_query("UPDATE {$tables['products_images_var']} SET id = '{$variant_id}' WHERE id = '{$old_variant_id}'");
        }
        # Write matrix
        foreach ($options as $i) {
            db_query("INSERT INTO {$tables['product_variant_items']} (variant_id, option_id) VALUES ('{$variant_id}','{$i}')");
        }
        if ($tick > 0 && $cnt_row++ % $tick == 0) {
            cw_flush(". ");
        }
    } while ($is_end);
    # Clean old variants images
    $images = cw_query_column("SELECT {$tables['products_images_var']}.id FROM {$tables['product_variants']} LEFT JOIN {$tables['products_images_var']} ON {$tables['product_variants']}.variant_id = {$tables['products_images_var']}.id WHERE {$tables['products_images_var']}.id IS NULL");
    if (!empty($images)) {
        cw_image_delete($images, "W");
    }
    return true;
}