function fn_get_csv($pattern, $file, $options) { $max_line_size = 65536; // 64 Кб $result = array(); if ($options['delimiter'] == 'C') { $delimiter = ','; } elseif ($options['delimiter'] == 'T') { $delimiter = "\t"; } else { $delimiter = ';'; } if (!empty($file) && file_exists($file)) { $encoding = fn_detect_encoding($file, 'F', !empty($options['lang_code']) ? $options['lang_code'] : CART_LANGUAGE); if (!empty($encoding)) { $file = fn_convert_encoding($encoding, 'UTF-8', $file, 'F'); } else { fn_set_notification('W', __('warning'), __('text_exim_utf8_file_format')); } $f = false; if ($file !== false) { $f = fopen($file, 'rb'); } if ($f) { // Get import schema $import_schema = fgetcsv($f, $max_line_size, $delimiter); if (empty($import_schema)) { fn_set_notification('E', __('error'), __('error_exim_cant_read_file')); return false; } // Check if we selected correct delimiter // If line was read without delimition, array size will be == 1. if (sizeof($import_schema) == 1) { // we could export one column if it is correct, otherwise show error if (!in_array($import_schema[0], array_keys($pattern['export_fields']))) { fn_set_notification('E', __('error'), __('error_exim_incorrent_delimiter')); return false; } } // Analyze schema - check for required fields if (fn_analyze_schema($import_schema, $pattern) == false) { return false; } // Collect data $schema_size = sizeof($import_schema); $skipped_lines = array(); $line_it = 1; while (($data = fn_fgetcsv($f, $max_line_size, $delimiter)) !== false) { $line_it++; if (fn_is_empty($data)) { continue; } if (sizeof($data) != $schema_size) { $skipped_lines[] = $line_it; continue; } $result[] = array_combine($import_schema, Bootstrap::stripSlashes($data)); } if (!empty($skipped_lines)) { fn_set_notification('W', __('warning'), __('error_exim_incorrect_lines', array('[lines]' => implode(', ', $skipped_lines)))); } return $result; } else { fn_set_notification('E', __('error'), __('error_exim_cant_open_file')); return false; } } else { fn_set_notification('E', __('error'), __('error_exim_file_doesnt_exist')); return false; } }
public function _addDellinCities($url_cities, $post) { $file_dir = fn_get_files_dir_path() . "dellin/"; fn_mkdir($file_dir); @chmod($file_dir, 0777); $file_path = $file_dir . date("Y-m-d", TIME) . '_cities.csv'; if (!file_exists($file_path)) { $response = Http::post($url_cities, json_encode($post), $this->url_params); $result = (array) json_decode($response); file_put_contents($file_path, file_get_contents($result['url'])); if (!empty($result['url'])) { $max_line_size = 65536; // 64 Кб $data_city = array(); $delimiter = ','; $encoding = fn_detect_encoding($result['url'], 'F', CART_LANGUAGE); if (!empty($encoding)) { $result['url'] = fn_convert_encoding($encoding, 'UTF-8', $result['url'], 'F'); } else { fn_set_notification('W', __('warning'), __('text_exim_utf8_file_format')); } $f = false; if ($result['url'] !== false) { $f = fopen($result['url'], 'rb'); } if ($f) { $import_schema = fgetcsv($f, $max_line_size, $delimiter); $schema_size = sizeof($import_schema); $skipped_lines = array(); $line_it = 1; while (($data = fn_fgetcsv($f, $max_line_size, $delimiter)) !== false) { $line_it++; if (fn_is_empty($data)) { continue; } if (sizeof($data) != $schema_size) { $skipped_lines[] = $line_it; continue; } $data = str_replace(array('\\r', '\\n', '\\t', '"'), '', $data); $data_city = array_combine($import_schema, Bootstrap::stripSlashes($data)); if (!empty($data_city)) { $dellin_city = array('number_city' => $data_city['id'], 'code_kladr' => str_replace(' ', '', $data_city['codeKLADR']), 'is_terminal' => $data_city['isTerminal']); $first_pos = strpos($data_city['name'], '('); $end_pos = strpos($data_city['name'], ')') - $first_pos; if (!empty($first_pos)) { $dellin_city['state'] = str_replace(array("(", ")"), "", substr($data_city['name'], $first_pos, $end_pos)); $dellin_city['city'] = str_replace(array('(' . $dellin_city['state'] . ')', '"'), "", $data_city['name']); } else { $dellin_city['state'] = str_replace(array('г.', 'г', 'г. ', 'г '), '', $data_city['name']); $dellin_city['city'] = $data_city['name']; } $dellin_city['city_id'] = db_get_field("SELECT city_id FROM ?:rus_dellin_cities WHERE code_kladr = ?s", $dellin_city['code_kladr']); db_query("REPLACE INTO ?:rus_dellin_cities ?e", $dellin_city); } } } } } }