function cw_gb_get_category_value($category_id, $default_value)
{
    global $tables;
    if (empty($category_id)) {
        return empty($default_value) ? FALSE : $default_value;
    }
    $attr_val_id = cw_query_first_cell("SELECT av.value\n\t\t                    FROM {$tables['attributes_values']} av, {$tables['attributes']} a\n\t\t                    WHERE av.item_id={$category_id}\n\t\t                    \tAND av.item_type='C'\n\t\t                    \tAND a.attribute_id=av.attribute_id\n\t\t                    \tAND a.field='g:google_product_category'");
    if (empty($attr_val_id)) {
        // Get parent categories
        $parent_categories = array();
        cw_category_generate_path($category_id, $parent_categories);
        if (count($parent_categories)) {
            foreach ($parent_categories as $parent_category_id) {
                $attr_val_id = cw_query_first_cell("SELECT av.value\n\t\t                    FROM {$tables['attributes_values']} av, {$tables['attributes']} a\n\t\t                    WHERE av.item_id={$parent_category_id}\n\t\t                    \tAND av.item_type='C'\n\t\t                    \tAND a.attribute_id=av.attribute_id\n\t\t                    \tAND a.field='g:google_product_category'");
                if (!empty($attr_val_id)) {
                    break;
                }
            }
        }
    }
    if ($attr_val_id) {
        $attr_val = cw_query_first_cell("SELECT value FROM {$tables['attributes_default']} WHERE attribute_value_id = " . $attr_val_id);
        if (empty($attr_val)) {
            return empty($default_value) ? FALSE : $default_value;
        }
        return $attr_val;
    }
    return empty($default_value) ? FALSE : $default_value;
}
function cw_category_update_path($cat, $rec = false)
{
    global $tables;
    $path = array();
    $level = 0;
    cw_category_generate_path($cat, $path);
    $path[] = $cat;
    db_query("delete from {$tables['categories_parents']} where category_id='{$cat}'");
    foreach ($path as $val) {
        cw_array2insert('categories_parents', array('category_id' => $cat, 'parent_id' => $val, 'level' => $level++));
    }
    if ($rec) {
        $subcats = cw_category_get_subcategory_ids($cat);
        if (count($subcats)) {
            foreach ($subcats as $scat_id) {
                cw_category_update_path($scat_id);
            }
        }
    }
}
function cw_ebay_get_category_value($category_id, $default_value)
{
    global $tables;
    if (empty($category_id)) {
        return empty($default_value) ? FALSE : $default_value;
    }
    $attr = cw_query_hash("SELECT a.field, av.value\n\t\t                    FROM {$tables['attributes_values']} av, {$tables['attributes']} a\n\t\t                    WHERE av.item_id={$category_id} \n\t\t                    \tAND av.item_type='C' \n\t\t                    \tAND a.attribute_id=av.attribute_id\n\t\t                    \tAND a.field='ebay_category'", 'field', false, true);
    $ebay_category = FALSE;
    if (isset($attr['ebay_category']) && !empty($attr['ebay_category'])) {
        $ebay_category = $attr['ebay_category'];
    } else {
        // Get parent categories
        $parent_categories = array();
        cw_category_generate_path($category_id, $parent_categories);
        if (count($parent_categories)) {
            foreach ($parent_categories as $parent_category_id) {
                $attr = cw_query_hash("SELECT a.field, av.value\n\t\t\t\t\t                    FROM {$tables['attributes_values']} av, {$tables['attributes']} a\n\t\t\t\t\t                    WHERE av.item_id={$parent_category_id} \n\t\t\t\t\t                    \tAND av.item_type='C' \n\t\t\t\t\t                    \tAND a.attribute_id=av.attribute_id\n\t\t\t\t\t                    \tAND a.field='ebay_category'", 'field', false, true);
                if (isset($attr['ebay_category']) && !empty($attr['ebay_category'])) {
                    $ebay_category = $attr['ebay_category'];
                    break;
                }
            }
        }
    }
    if ($ebay_category) {
        $category_value = cw_query_hash("SELECT attribute_value_id, value_key FROM {$tables['attributes_default']} WHERE attribute_value_id = " . $ebay_category, 'attribute_value_id', false, true);
        if (empty($category_value[$ebay_category])) {
            return empty($default_value) ? FALSE : $default_value;
        }
        return $category_value[$ebay_category];
    }
    return empty($default_value) ? FALSE : $default_value;
}