} else { $company_countries = $company_data['countries_list']; } $_countries = array(); foreach ($company_countries as $code) { if (isset($countries_list[$code])) { $_countries[$code] = $countries_list[$code]; unset($countries_list[$code]); } } $company_data['countries_list'] = $_countries; unset($_countries, $company_countries); } Tygh::$app['view']->assign('countries_list', $countries_list); if ($mode == 'add') { $schema = fn_init_clone_schemas(); Tygh::$app['view']->assign('clone_schema', $schema); } // Get "Company" settings from the DB $settings_values = fn_restore_post_data('update'); $section = Settings::instance()->getSectionByName('Company'); $settings_data = Settings::instance()->getList($section['section_id'], 0, false, $company_id, CART_LANGUAGE); foreach ($settings_data['main'] as $field_id => &$field_data) { unset($field_data['update_for_all']); if (!empty($settings_values) && !empty($settings_values[$field_id])) { $field_data['value'] = $settings_values[$field_id]; } elseif ($mode == 'add') { unset($field_data['value']); } } Tygh::$app['view']->assign('company_settings', $settings_data['main']);
function fn_clone_table_data($table_data, $clone_data, $start, $from, $to, $extra = array()) { static $schema; static $cloned_ids = array(); $clone_id = $table_data['name']; if (!isset($cloned_ids[$clone_id])) { $cloned_ids[$clone_id] = array(); } if (empty($schema)) { $schema = fn_init_clone_schemas(); } $limit = 50; // Clone 50 lines per one iteration $return = array(); $condition = ''; if (!empty($table_data['condition'])) { $condition = ' AND ' . implode(' AND ', $table_data['condition']); preg_match_all('/%(.*?)%/', $condition, $variables); foreach ($variables[1] as $variable) { $variable = fn_strtolower($variable); $var = ${$variable}; if (is_array($var)) { $var = implode(', ', $var); } $condition = preg_replace('/%(.*?)%/', $var, $condition, 1); } } if (!empty($table_data['dependence_tree'])) { $ids = fn_build_dependence_tree($table_data['name'], $table_data['key'], $parent = 'parent_id', $from); $data = $_data = array(); if (!empty($ids)) { $_data = db_get_hash_array('SELECT * FROM ?:' . $table_data['name'] . ' WHERE company_id = ?i ' . $condition . 'AND ' . $table_data['key'] . ' IN (?a)', $table_data['key'], $from, $ids); } foreach ($ids as $id) { if (isset($_data[$id])) { $data[] = $_data[$id]; } } unset($_data, $ids); $start = db_get_field('SELECT COUNT(*) FROM ?:' . $table_data['name'] . ' WHERE company_id = ?i', $from); } elseif (empty($clone_data)) { $data = db_get_array('SELECT * FROM ?:' . $table_data['name'] . ' WHERE company_id = ?i ' . $condition . ' LIMIT ?i, ?i', $from, $start, $limit); } else { $data = db_get_array('SELECT * FROM ?:' . $table_data['name'] . ' WHERE ' . $table_data['key'] . ' IN (?a)' . $condition, array_keys($clone_data)); } if (!empty($data)) { // We using sharing. So do not use "quick" insert schema... if (false && empty($table_data['children']) && empty($table_data['pre_process']) && empty($table_data['post_process']) && empty($table_data['return_clone_data'])) { $exclude = array(empty($clone_data) ? $table_data['key'] : ''); if (!empty($table_data['exclude'])) { $exclude = array_merge($exclude, $table_data['exclude']); } $fields = fn_get_table_fields($table_data['name'], $exclude, true); $query = 'REPLACE INTO ?:' . $table_data['name'] . ' (' . implode(',', $fields) . ') VALUES '; $rows = array(); foreach ($data as $row) { if (empty($clone_data)) { unset($row[$table_data['key']]); } else { $row[$table_data['key']] = $clone_data[$row[$table_data['key']]]; } if (!empty($extra)) { foreach ($extra as $field => $field_data) { if (isset($field_data[$row[$field]])) { $row[$field] = $field_data[$row[$field]]; } } } if (isset($row['company_id'])) { $row['company_id'] = $to; } if (!empty($table_data['exclude'])) { foreach ($table_data['exclude'] as $exclude_field) { unset($row[$exclude_field]); } } $row = explode('(###)', addslashes(implode('(###)', $row))); $rows[] = "('" . implode("', '", $row) . "')"; } $query .= implode(', ', $rows); db_query($query); } else { foreach ($data as $id => $row) { if (!empty($table_data['key'])) { $key = $row[$table_data['key']]; if (empty($clone_data)) { unset($row[$table_data['key']]); } else { $row[$table_data['key']] = $clone_data[$row[$table_data['key']]]; } } if (isset($row['company_id'])) { $row['company_id'] = $to; } if (!empty($extra)) { foreach ($extra as $field => $field_data) { if (isset($field_data[$row[$field]])) { $row[$field] = $field_data[$row[$field]]; } } } if (!empty($table_data['exclude'])) { foreach ($table_data['exclude'] as $exclude_field) { unset($row[$exclude_field]); } } if (!empty($table_data['pre_process']) && function_exists($table_data['pre_process'])) { call_user_func($table_data['pre_process'], $table_data, $row, $clone_data, $cloned_ids[$clone_id], $extra); } $new_key = db_query('REPLACE INTO ?:' . $table_data['name'] . ' ?e', $row); if (!empty($key)) { $cloned_ids[$clone_id][$key] = $new_key; } if (!empty($table_data['return_clone_data'])) { if (count($table_data['return_clone_data']) == 1 && reset($table_data['return_clone_data']) == $table_data['key']) { $return[$table_data['key']][$key] = $new_key; } else { $_key = !empty($table_data['return_clone_data']) ? reset($table_data['return_clone_data']) : $table_data['key']; $new_data = db_get_row('SELECT ' . implode(', ', $table_data['return_clone_data']) . ' FROM ?:' . $table_data['name'] . ' WHERE `' . $_key . '` = ?s', $new_key); foreach ($table_data['return_clone_data'] as $field) { $return[$field][$data[$id][$field]] = $new_data[$field]; } } } if (!empty($table_data['post_process']) && function_exists($table_data['post_process'])) { call_user_func($table_data['post_process'], $new_key, $table_data, $row, $clone_data, $cloned_ids[$clone_id], $extra); } } if (!empty($table_data['children'])) { $__data = !empty($table_data['return_clone_data']) ? reset($return) : $cloned_ids[$clone_id]; foreach ($table_data['children'] as $child_data) { if (!empty($child_data['data_from'])) { if (Registry::get('clone_data.' . $child_data['data_from']) == 'Y') { $data_from = $schema[$child_data['data_from']]; if (!empty($tables['tables'])) { foreach ($tables['tables'] as $_table_data) { fn_clone_table_data($_table_data, $__data, 0, $from, $to); } } elseif (!empty($data_from['function']) && function_exists($data_from['function'])) { call_user_func($data_from['function'], $table_data, $cloned_ids[$clone_id], $start, $from, $to, $extra); } } } else { fn_clone_table_data($child_data, $__data, 0, $from, $to); } } } } } if (empty($clone_data)) { $total = db_get_field('SELECT COUNT(*) FROM ?:' . $table_data['name'] . ' WHERE company_id = ?i', $from); if ($total >= $start + $limit) { $start += $limit; fn_clone_table_data($table_data, array(), $start, $from, $to); } } return array($return, $cloned_ids[$clone_id]); }