} else { $response->addError('ERROR_WRONG_OBJECT_DATA', str_replace('[object]', 'products', __('twgadmin_wrong_api_object_data'))); } } } } if ($object == 'categories') { if ($_REQUEST['action'] == 'update') { foreach ($data as $category) { // allow to update only order status if (!empty($category['category_id'])) { if (!fn_update_category($category, $category['category_id'], $lang_code)) { $msg = str_replace('[object_id]', $category['category_id'], __('twgadmin_wrong_api_object_data')); $response->addError('ERROR_OBJECT_UPDATE', str_replace('[object]', 'categories', __('twgadmin_wrong_api_object_data'))); } elseif (!empty($category['icon'])) { TwigmoImage::updateIconsByApiData($category['icon'], $category['category_id'], 'category'); } } else { $response->addError('ERROR_WRONG_OBJECT_DATA', str_replace('[object]', 'categories', __('twgadmin_wrong_api_object_data'))); } } } } if ($object == 'images') { if ($_REQUEST['action'] == 'delete') { foreach ($data as $image) { if (empty($image['image_id'])) { $response->addError('ERROR_WRONG_OBJECT_DATA', str_replace('[object]', 'images', __('twgadmin_wrong_api_object_data'))); continue; } $image_info = db_get_row("SELECT pair_id, object_type FROM ?:images_links WHERE image_id = ?i", $image['image_id']);
function fn_twg_api_update_product($product_data, $product_id = 0, $lang_code = CART_LANGUAGE) { $_data = $product_data; if (!empty($product_data['timestamp'])) { $_data['timestamp'] = fn_parse_date($product_data['timestamp']); // Minimal data for product record } if (!empty($product_data['avail_since'])) { $_data['avail_since'] = fn_parse_date($product_data['avail_since']); } if (Registry::get('settings.General.allow_negative_amount') == 'N' && isset($_data['amount'])) { $_data['amount'] = abs($_data['amount']); } // add new product if (empty($product_id)) { $create = true; // product title can't be empty if (empty($product_data['product'])) { return false; } $product_id = db_query("INSERT INTO ?:products ?e", $_data); if (empty($product_id)) { return false; } // // Adding same product descriptions for all cart languages // $_data = $product_data; $_data['product_id'] = $product_id; $_data['product'] = trim($_data['product'], " -"); $langs = (array) Registry::get('languages'); foreach (array_keys($langs) as $_data['lang_code']) { db_query("INSERT INTO ?:product_descriptions ?e", $_data); } // update product } else { if (isset($product_data['product']) && empty($product_data['product'])) { unset($product_data['product']); } db_query("UPDATE ?:products SET ?u WHERE product_id = ?i", $_data, $product_id); $_data = $product_data; if (!empty($_data['product'])) { $_data['product'] = trim($_data['product'], " -"); } db_query("UPDATE ?:product_descriptions SET ?u WHERE product_id = ?i AND lang_code = ?s", $_data, $product_id, $lang_code); } // Log product add/update fn_log_event('products', !empty($create) ? 'create' : 'update', array('product_id' => $product_id)); // Update product prices if (isset($product_data['price'])) { if (!isset($product_data['prices'])) { $product_data['prices'] = array(); $skip_price_delete = true; } $_price = array('price' => abs($product_data['price']), 'lower_limit' => 1); array_unshift($product_data['prices'], $_price); } if (!empty($product_data['prices'])) { if (empty($skip_price_delete)) { db_query("DELETE FROM ?:product_prices WHERE product_id = ?i", $product_id); } foreach ($product_data['prices'] as $v) { if (!empty($v['lower_limit'])) { $v['product_id'] = $product_id; db_query("REPLACE INTO ?:product_prices ?e", $v); } } } // Update main icon if (!empty($product_data['icon'])) { TwigmoImage::updateIconsByApiData($product_data['icon'], $product_id); } // Update additional images if (!empty($product_data['images'])) { TwigmoImage::updateImagesByApiData($product_data['images'], $product_id); } return $product_id; }