function fn_import($pattern, $import_data, $options) { $processed_data = array('E' => 0, 'N' => 0, 'S' => 0); if (defined('COMPANY_ID')) { $vendors_import_data = array(); if ($pattern['pattern_id'] == 'products') { // Importing products when in vendor mode: // Override company_id with current vendor's company_id // If product already exists but belongs to another vendor: skip record $product_codes = db_get_fields('SELECT product_code FROM ?:products'); foreach ($import_data as $v) { if (!in_array($v['product_code'], $product_codes) || $v['company_id'] == COMPANY_ID) { $v['company_id'] = COMPANY_ID; $vendors_import_data[] = $v; } else { $processed_data['S']++; } } } if ($pattern['pattern_id'] == 'product_images' || $pattern['pattern_id'] == 'qty_discounts') { // Importing images and qty discounts when in vendor mode: // Get a list of current vendor's products codes // Leave only current vendor's images and qty discounts $product_codes = db_get_fields('SELECT product_code FROM ?:products WHERE company_id = ' . COMPANY_ID); foreach ($import_data as $v) { if (in_array($v['product_code'], $product_codes)) { $vendors_import_data[] = $v; } else { $processed_data['S']++; } } } $import_data = $vendors_import_data; } $alt_keys = array(); $primary_fields = array(); $table_groups = array(); $processing_groups = array(); $default_groups = array(); $converting_groups = array(); $add_fields = array(); fn_start_scroller(); if (!empty($pattern['pre_processing'])) { $func = $pattern['pre_processing']; $function = array_shift($func); $args = $func; foreach ($args as $k => $v) { if (strpos($v, '@') !== false) { $_opt = str_replace('@', '', $v); $args[$k] = isset($options[$_opt]) ? $options[$_opt] : ''; } } call_user_func_array($function, $args); } fn_echo('<br />' . fn_get_lang_var('importing_data') . '<br />'); if (!empty($pattern['references'])) { foreach ($pattern['references'] as $table => $data) { $table_groups[$table] = $data; } } // Get keys to detect primary record foreach ($pattern['export_fields'] as $field => $data) { $_db_field = empty($data['db_field']) ? $field : $data['db_field']; // Collect fields with default values if (!empty($data['default'])) { if (is_array($data['default'])) { $default_groups[$_db_field] = call_user_func_array(array_shift($data['default']), $data['default']); } else { $default_groups[$_db_field] = $data['default']; } } // Get alt keys for primary table if (!empty($data['alt_key'])) { $alt_keys[$field] = $_db_field; } if (!isset($data['linked']) || $data['linked'] == true) { // Get fields for primary table if (empty($data['table']) || $data['table'] == $pattern['table']) { $primary_fields[$field] = $_db_field; } // Group fields by tables if (!empty($data['table'])) { $table_groups[$data['table']]['fields'][$_db_field] = true; } } // Create set with fields that must be added to data import if they are not exist // %'s are for compatibility with %% field type in "process_put" key if (!empty($data['use_put_from'])) { $_f = str_replace('%', '', $data['use_put_from']); $_f = !empty($pattern['export_fields'][$_f]['db_field']) ? $pattern['export_fields'][$_f]['db_field'] : $_f; $add_fields[$_f] = true; } // Generate processing groups if (!empty($data['process_put'])) { $args = $data['process_put']; $function = array_shift($args); $processing_groups[] = array('function' => $function, 'args' => $args, 'this_field' => $_db_field, 'table' => !empty($data['table']) ? $data['table'] : '', 'return_result' => !empty($data['return_result']) ? $data['return_result'] : false); } // Generate converting groups if (!empty($data['convert_put'])) { $args = $data['convert_put']; $function = array_shift($args); $converting_groups[] = array('function' => $function, 'this_field' => $_db_field, 'args' => $args); } } foreach ($import_data as $k => $v) { foreach ($add_fields as $_f => $_val) { if (!isset($v[$_f])) { $v[$_f] = ''; } } $_alt_keys = array(); $object_exists = true; // Check if converting groups exist and convert fields if it is so if (!empty($converting_groups)) { foreach ($converting_groups as $group) { if (!isset($v[$group['this_field']])) { continue; } $params = array(); $params[] = $v[$group['this_field']]; foreach ($group['args'] as $arg) { if (strpos($arg, '@') !== false) { $_opt = str_replace('@', '', $arg); $params[] = isset($options[$_opt]) ? $options[$_opt] : ''; } } $v[$group['this_field']] = call_user_func_array($group['function'], $params); } } foreach ($alt_keys as $import_field => $real_field) { if (!isset($v[$real_field])) { continue; } $_alt_keys[$real_field] = $v[$real_field]; } foreach ($primary_fields as $import_field => $real_field) { if (!isset($v[$real_field])) { continue; } $_primary_fields[$real_field] = $v[$real_field]; } $primary_object_id = db_get_row('SELECT ' . implode(', ', $pattern['key']) . ' FROM ?:' . $pattern['table'] . ' WHERE ?w', $_alt_keys); if (!(isset($pattern['import_skip_db_processing']) && $pattern['import_skip_db_processing'])) { if (empty($primary_object_id)) { // If scheme is used for update objects only, skip this record if (!empty($pattern['update_only'])) { fn_echo(fn_get_lang_var('object_does_not_exist') . ' ('); $_a = array(); foreach ($alt_keys as $_d => $_v) { if (!isset($v[$_v])) { continue; } $_a[] = $_d . ' = ' . $v[$_v]; } fn_echo(implode(', ', $_a) . ')...<br />'); $processed_data['S']++; continue; } $object_exists = false; fn_echo(fn_get_lang_var('creating') . ' ' . $pattern['name'] . '...'); $processed_data['N']++; // For new objects - fill the default values if (!empty($default_groups)) { foreach ($default_groups as $field => $value) { if (empty($v[$field])) { $v[$field] = $value; } } } } else { fn_echo(fn_get_lang_var('updating') . ' ' . $pattern['name'] . '...'); $processed_data['E']++; } $_data = fn_check_table_fields($v, $pattern['table']); if ($object_exists == true) { db_query('UPDATE ?:' . $pattern['table'] . ' SET ?u WHERE ?w', $_data, $primary_object_id); } else { $o_id = db_query('INSERT INTO ?:' . $pattern['table'] . ' ?e', $_data); if ($o_id !== true) { $primary_object_id = array(reset($pattern['key']) => $o_id); } else { foreach ($pattern['key'] as $_v) { $primary_object_id[$_v] = $_data[$_v]; } } } if ($pattern['table'] == 'products' && $object_exists == false) { require_once "products.php"; fn_add_to_new_items_block($primary_object_id['product_id']); } fn_echo('<b>' . implode(',', $primary_object_id) . '</b>. '); } if (!empty($processing_groups)) { foreach ($processing_groups as $group) { $args = array(); $use_this_group = true; $_refs = array(); foreach ($group['args'] as $ak => $av) { if ($av == '#key') { $args[$ak] = sizeof($primary_object_id) == 1 ? reset($primary_object_id) : $primary_object_id; } elseif ($av == '#this') { // If we do not have this field in the import data, do not apply the function if (!isset($v[$group['this_field']])) { $use_this_group = false; break; } $args[$ak] = $v[$group['this_field']]; } elseif ($av == '#counter') { $args[$ak] =& $processed_data; } elseif (strpos($av, '%') !== false) { $_ref = str_replace('%', '', $av); $_ref = !empty($pattern['export_fields'][$_ref]['db_field']) ? $pattern['export_fields'][$_ref]['db_field'] : $_ref; // FIXME!!! Move to code, which builds processing_groups $args[$ak] = isset($v[$_ref]) ? $v[$_ref] : ''; $_refs[] = $_ref; } elseif (strpos($av, '@') !== false) { $_opt = str_replace('@', '', $av); $args[$ak] = $options[$_opt]; } else { $args[$ak] = $av; } } if ($use_this_group == false) { continue; } $result = call_user_func_array($group['function'], $args); // FIXME - add checking for returned value if ($group['return_result'] == true) { $v[$group['this_field']] = $result; } else { // Remove processed fields from table groups if (!empty($group['table'])) { unset($table_groups[$group['table']]['fields'][$group['this_field']]); foreach ($_refs as $_ref) { unset($table_groups[$group['table']]['fields'][$_ref]); } } } } } if (!(isset($pattern['import_skip_db_processing']) && $pattern['import_skip_db_processing'])) { // Update referenced tables fn_echo(fn_get_lang_var('updating_links') . '... '); foreach ($table_groups as $table => $tdata) { if (isset($tdata['import_skip_db_processing']) && $tdata['import_skip_db_processing']) { break; } $_data = array(); // First, build condition $where_insert = array(); // If alternative key is defined, use it if (!empty($tdata['alt_key'])) { foreach ($tdata['alt_key'] as $akey) { if (strval($akey) == '#key') { $where_insert = fn_array_merge($where_insert, $primary_object_id); } elseif (strpos($akey, '@') !== false) { $_opt = str_replace('@', '', $akey); $where_insert[$akey] = $options[$_opt]; } else { $where_insert[$akey] = $v[$akey]; } } // Otherwise - link by reference fields } else { foreach ($tdata['reference_fields'] as $field => $value) { if (strval($value) == '#key') { $_val = sizeof($primary_object_id) == 1 ? reset($primary_object_id) : $primary_object_id; } elseif (strpos($value, '@') !== false) { $_opt = str_replace('@', '', $value); $_val = $options[$_opt]; } else { $_val = $value; } $where_insert[$field] = $_val; } } // Now, build update fields array foreach ($tdata['fields'] as $import_field => $set) { if (!isset($v[$import_field])) { continue; } $_data[$import_field] = $v[$import_field]; } // Check if object exists $is_exists = db_get_field("SELECT COUNT(*) FROM ?:{$table} WHERE ?w", $where_insert); if ($is_exists == true && !empty($_data)) { db_query("UPDATE ?:{$table} SET ?u WHERE ?w", $_data, $where_insert); } elseif (empty($is_exists)) { // if reference does not exist, we should insert it anyway to avoid inconsistency $_data = fn_array_merge($_data, $where_insert); if (substr($table, -13) == '_descriptions' && isset($_data['lang_code'])) { // add description for all cart languages when adding object data foreach ((array) Registry::get('languages') as $_data['lang_code'] => $lang_v) { db_query("REPLACE INTO ?:{$table} ?e", $_data); } } else { db_query("INSERT INTO ?:{$table} ?e", $_data); } } } fn_echo('<b>' . fn_get_lang_var('uc_ok') . '</b><br />'); } } $msg = fn_get_lang_var('text_exim_data_imported'); $msg = str_replace('[new]', $processed_data['N'], $msg); $msg = str_replace('[exist]', $processed_data['E'], $msg); $msg = str_replace('[skipped]', $processed_data['S'], $msg); $msg = str_replace('[total]', $processed_data['E'] + $processed_data['N'] + $processed_data['S'], $msg); fn_set_notification('N', fn_get_lang_var('notice'), $msg); fn_stop_scroller(); return true; }
$_data = array('product_id' => $product_id, 'link_type' => 'A'); $_add_categories = explode(',', $_REQUEST['product_data']['add_categories']); foreach ($_add_categories as $c_id) { // check if main category already exists if (is_numeric($c_id)) { $is_ex = db_get_field("SELECT COUNT(*) FROM ?:products_categories WHERE product_id = ?i AND category_id = ?i", $product_id, $c_id); if (!empty($is_ex)) { continue; } $_data['category_id'] = $c_id; db_query('INSERT INTO ?:products_categories ?e', $_data); } } fn_update_product_count($_add_categories); } fn_add_to_new_items_block($product_id); } // ----------------------- $suffix = ".update?product_id={$product_id}"; } else { $suffix = ".add"; } } // // Apply Global Option // if ($mode == 'apply_global_option') { if ($_REQUEST['global_option']['link'] == 'N') { fn_clone_product_options(0, $_REQUEST['product_id'], $_REQUEST['global_option']['id']); } else { db_query("REPLACE INTO ?:product_global_option_links (option_id, product_id) VALUES(?i, ?i)", $_REQUEST['global_option']['id'], $_REQUEST['product_id']);