<?php

cw_load('image', 'category', 'group_edit');
$categories_to_delete =& cw_session_register('categories_to_delete', array());
$search_data =& cw_session_register('search_data', array());
if ($action == 'search') {
    $search_data['categories'] = $posted_data;
    cw_header_location('index.php?target=' . $target . '&mode=search');
}
if ($action == 'list') {
    if (empty($delete_arr)) {
        $top_message = array('content' => cw_get_langvar_by_name('lbl_please_select_categories_for_editing'), 'type' => 'I');
        cw_header_location('index.php?target=' . $target);
    }
    $cat_ids = array_keys($delete_arr);
    $ge_id = cw_group_edit_add($cat_ids);
    $cat_id = $cat_ids[0];
    cw_header_location('index.php?target=' . $target . '&mode=edit&cat=' . $cat_id . '&ge_id=' . $ge_id);
}
if ($action == 'apply' && is_array($posted_data)) {
    foreach ($posted_data as $k => $v) {
        $query_data = array('order_by' => intval($v['order_by']));
        cw_array2update('categories', $query_data, "category_id='" . $k . "'");
        cw_category_update_status($k, $v['status']);
    }
    if ($cat) {
        $path = cw_category_get_subcategory_ids($cat);
        if (!empty($path)) {
            cw_recalc_subcat_count($path);
        }
    } else {
<?php

$top_message =& cw_session_register('top_message');
if ($action == 'list') {
    cw_load('group_edit');
    if (empty($product_ids)) {
        $top_message = array('content' => cw_get_langvar_by_name('lbl_please_select_products_for_editing'), 'type' => 'I');
        cw_header_location('index.php?target=' . $target . '&mode=search');
    } else {
        $product_ids = array_keys($product_ids);
        $ge_id = cw_group_edit_add($product_ids);
        $product_id = $product_ids[0];
        cw_header_location('index.php?target=products&mode=details&product_id=' . $product_id . '&ge_id=' . $ge_id);
    }
}
if ($action == 'update' && is_array($posted_data)) {
    cw_load('product');
    foreach ($posted_data as $k => $v) {
        if (isset($v['orderby']) && is_numeric($v['orderby'])) {
            $cat = intval($cat);
            db_query("update {$tables['products_categories']} set orderby='" . $v['orderby'] . "' where product_id='{$k}' and category_id='{$cat}'");
        }
        if (AREA_TYPE == 'P') {
            $wu = array('avail' => $v['avail'], 'product_id' => $k, 'warehouse_customer_id' => $customer_id, 'variant_id' => 0);
            cw_warehouse_insert_avail($wu, true);
        } elseif (!$addons['warehouse']) {
            cw_array2update('products', array('avail' => $v['avail']), "product_id='{$k}'");
        } else {
            cw_array2update('products_warehouses_amount', array('avail' => $v['avail']), "product_id='{$k}'");
        }
        // TODO, TOFIX: Does it set avail for all warehouses? Maybe use warehouses=0 as condition?