function fn_update_ebay_template($data, $template_id = 0, $lang_code = CART_LANGUAGE) { if (empty($data['name'])) { return false; } unset($data['template_id']); if (fn_allowed_for('ULTIMATE')) { // check that template owner was not changed by store administrator if (Registry::get('runtime.company_id') || empty($data['company_id'])) { $template_company_id = db_get_field('SELECT company_id FROM ?:ebay_templates WHERE template_id = ?i', $template_id); if (!empty($template_company_id)) { $data['company_id'] = $template_company_id; } else { if (Registry::get('runtime.company_id')) { $template_company_id = $data['company_id'] = Registry::get('runtime.company_id'); } else { $template_company_id = $data['company_id'] = fn_get_default_company_id(); } } } else { $template_company_id = $data['company_id']; } } else { if (Registry::get('runtime.company_id')) { $template_company_id = Registry::get('runtime.company_id'); } else { $template_company_id = $data['company_id']; } } if (fn_allowed_for('ULTIMATE') && Registry::get('runtime.company_id') && !empty($template_company_id) && Registry::get('runtime.company_id') != $template_company_id) { $create = false; } else { $isUpdate = false; if (isset($data['payment_methods']) && is_array($data['payment_methods'])) { $data['payment_methods'] = implode(',', $data['payment_methods']); } if (empty($data['root_sec_category'])) { $data['sec_category'] = ''; } if (!empty($template_id)) { $isUpdate = true; db_query('UPDATE ?:ebay_templates SET ?u WHERE template_id = ?i', $data, $template_id); db_query('UPDATE ?:ebay_template_descriptions SET ?u WHERE template_id = ?i AND lang_code = ?s', $data, $template_id, $lang_code); if (isset($_REQUEST['share_objects']) && isset($_REQUEST['share_objects']['ebay_templates']) && isset($_REQUEST['share_objects']['ebay_templates'][$template_id])) { $_products = db_get_fields("SELECT product_id FROM ?:products WHERE company_id NOT IN (?n) AND ebay_template_id = ?i", $_REQUEST['share_objects']['ebay_templates'][$template_id], $template_id); if (!empty($_products)) { db_query("UPDATE ?:products SET ebay_template_id = 0 WHERE product_id IN (?n)", $_products); } } } else { $data['template_id'] = $template_id = db_query("INSERT INTO ?:ebay_templates ?e", $data); if (isset($data['name']) && empty($data['name'])) { unset($data['name']); } if (!empty($data['name'])) { foreach (fn_get_translation_languages() as $data['lang_code'] => $_v) { db_query("INSERT INTO ?:ebay_template_descriptions ?e", $data); } } } if ($data['use_as_default'] == 'Y') { db_query('UPDATE ?:ebay_templates SET use_as_default = ?s WHERE company_id = ?i AND NOT template_id = ?i', 'N', $template_company_id, $template_id); } if ($template_id && array_key_exists('product_ids', $data)) { if ($isUpdate) { $current_product_ids = \Ebay\Product::getTemplateProductIds($template_id); } else { $current_product_ids = array(); } $data['product_ids'] = (array) $data['product_ids']; $add_product_ids = array_diff($data['product_ids'], $current_product_ids); $delete_product_ids = array_diff($current_product_ids, $data['product_ids']); if (!empty($add_product_ids)) { foreach ($add_product_ids as $product_id) { \Ebay\Product::updateProductTemplateId($product_id, $template_id); } } if (!empty($delete_product_ids)) { foreach ($delete_product_ids as $product_id) { \Ebay\Product::updateProductTemplateId($product_id, 0); } } } } return $template_id; }