function cw_copy_class($product_option_id, $product_id = false)
{
    global $tables, $ge_id;
    $data['class'] = cw_query_first("SELECT * FROM {$tables['product_options']} WHERE product_option_id = '{$product_option_id}'");
    $data['product_options_values'] = cw_query("SELECT * FROM {$tables['product_options_values']} WHERE product_option_id = '{$product_option_id}'");
    $data['product_options_lng'] = cw_query("select * from {$tables['product_options_lng']} where product_option_id = '{$product_option_id}'");
    $data['product_options_values_lng'] = cw_query("SELECT {$tables['product_options_values_lng']}.* FROM {$tables['product_options_values_lng']}, {$tables['product_options_values']} WHERE {$tables['product_options_values_lng']}.option_id = {$tables['product_options_values']}.option_id AND {$tables['product_options_values']}.product_option_id = '{$product_option_id}'");
    if (empty($product_id)) {
        while ($pid = cw_group_edit_each($ge_id, 1, $product_id)) {
            cw_add_class_data($data, $pid);
        }
    } else {
        if (!is_array($product_id)) {
            $product_id = array($product_id);
        }
        foreach ($product_id as $pid) {
            cw_add_class_data($data, $pid);
        }
    }
}
            if (count($subcats)) {
                foreach ($subcats as $scat_id) {
                    cw_category_update_path($scat_id);
                }
            }
            $path = array($parent_id, $cat_location);
            cw_recalc_subcat_count($path);
        }
        $top_message = array('content' => cw_get_langvar_by_name('msg_adm_category_move'), 'type' => 'I');
        cw_header_location("index.php?target={$target}&mode=edit&cat={$cat}&ge_id={$ge_id}");
    }
}
if ($action == "delete_icon" && !empty($cat)) {
    cw_image_delete($cat, 'categories_images_thumb');
    if ($ge_id && $fields['image']) {
        while ($id = cw_group_edit_each($ge_id, 100, $cat)) {
            cw_image_delete($id, 'categories_images_thumb');
        }
    }
    $top_message = array('content' => cw_get_langvar_by_name('msg_adm_category_icon_del'), 'type' => 'I');
    cw_header_location("index.php?target={$target}&mode={$mode}&cat={$cat}&ge_id={$ge_id}");
}
$smarty->assign('memberships', cw_user_get_memberships(array('C', 'R')));
$current_category = cw_func_call('cw_category_get', array('cat' => $cat, 'from_category' => 0, 'location_target' => '', 'lang' => $edited_language));
if ($mode == 'add') {
    $current_category['category'] = '';
    $current_category['image'] = array();
}
if ($saved_category) {
    if (!is_array($current_category)) {
        $current_category = array();
            unset($data['image_id']);
            $data = cw_array_map("addslashes", $data);
            while ($pid = cw_group_edit_each($ge_id, 1, $product_id)) {
                $id = cw_query_first_cell("SELECT image_id FROM {$tables['products_detailed_images']} WHERE id = '{$pid}' AND md5 = '{$data['md5']}'");
                if (!empty($id)) {
                    cw_image_delete($id, "D", true);
                }
                $data['id'] = $pid;
                cw_array2insert("products_detailed_images", $data);
            }
        }
    }
    $top_message = array('content' => cw_get_langvar_by_name('msg_adm_product_images_upd'), 'type' => 'I');
    cw_dpi_refresh($product_id, 'dpi');
}
if ($action == 'product_images_delete' && is_array($iids)) {
    foreach ($iids as $image_id => $tmp) {
        $md5 = cw_query_first_cell("SELECT md5 FROM {$tables['products_detailed_images']} WHERE image_id = '{$image_id}'");
        cw_image_delete($image_id, 'products_detailed_images', true);
        if ($ge_id && $fields['d_image'][$image_id]) {
            while ($pid = cw_group_edit_each($ge_id, 1, $product_id)) {
                $id = cw_query_first_cell("SELECT image_id FROM {$tables['products_detailed_images']} WHERE id = '{$pid}' AND md5 = '{$md5}'");
                if (!empty($id)) {
                    cw_image_delete($id, "D", true);
                }
            }
        }
    }
    $top_message = array('content' => cw_get_langvar_by_name('msg_adm_product_images_del'), 'type' => 'I');
    cw_dpi_refresh($product_id, 'dpi');
}
function cw_group_edit_delete($tbl, $obj_id)
{
    global $ge_id, $tables;
    if (!$ge_id) {
        return;
    }
    while ($id = cw_group_edit_each($ge_id, 100, $obj_id)) {
        db_query("delete from " . $tables[$tbl] . " where obj_id in ('" . implode("','", $id) . "')");
    }
}
                $vid = cw_variants_get_same($k, $pid);
                if (!empty($vid)) {
                    cw_image_delete($vid, 'products_images_var');
                }
            }
        }
    }
    $refresh = $rebuild_quick = true;
} elseif ($action == 'product_variants_search') {
    $search_variants[$product_id] = empty($search) ? array() : $search;
    $refresh = true;
}
if ($rebuild_quick) {
    cw_func_call('cw_product_build_flat', array('product_id' => $product_id));
    if ($ge_id) {
        while ($pid = cw_group_edit_each($ge_id, 100)) {
            cw_func_call('cw_product_build_flat', array('product_id' => $pid));
        }
    }
}
if ($refresh) {
    cw_refresh($product_id, 'product_variants');
}
# Get the product options list
$product_options = cw_call('cw_get_product_classes', array($product_id));
if (!empty($product_options)) {
    $smarty->assign('product_options', $product_options);
}
$variants = cw_call('cw_get_product_variants', array($product_id));
$svariants = $search_variants[$product_id];
if ($svariants && !empty($variants)) {
function cw_attributes_group_update($ge_id, $item_id, $item_type, $fields)
{
    global $tables;
    $attributes = cw_func_call('cw_attributes_get', array('item_id' => $item_id, 'item_type' => $item_type));
    $att = $att_ids = array();
    if ($attributes) {
        foreach ($attributes as $attribute) {
            if ($fields[$attribute['field']]) {
                $att[] = $attribute;
                $att_ids[] = $attribute['attribute_id'];
            }
        }
    }
    if (count($att)) {
        while ($id = cw_group_edit_each($ge_id, 1, $item_id)) {
            db_query("delete from {$tables['attributes_values']} where item_id = '{$id}' and item_type='{$item_type}' and attribute_id in ('" . implode("', '", $att_ids) . "')");
            foreach ($att as $v) {
                foreach ($v['values'] as $value) {
                    cw_array2insert('attributes_values', array('item_id' => $id, 'item_type' => $item_type, 'attribute_id' => $v['attribute_id'], 'value' => $value));
                }
            }
        }
    }
}