public static function territorio($postID, $taxonomy, $col1, $uf_row = false) { $uf = trim($uf_row); $cidade = trim($col1); $cidade_slug = sanitize_title($cidade); if ($uf !== false) { $uf_slug = sanitize_title($uf); $term_obj = get_term_by('slug', $uf_slug, $taxonomy); if ($term_obj !== false) { if ($postID > 0) { wp_set_object_terms($postID, intval($term_obj->term_id), $taxonomy, true); } else { FluxoSettingsPage::log("wp_set_object_terms( {$postID}, intval({$term_obj->term_id}), {$taxonomy}, true );"); FluxoSettingsPage::log(" Term name: {$term_obj->name}<br/>"); } } else { $estados = get_terms($taxonomy, array('hide_empty' => 0, 'parent' => 0)); $descs = array(); foreach ($estados as $estado) { $descs[trim($estado->description)] = $estado->term_id; $descs[self::FormatText($estado->description)] = $estado->term_id; } if (array_key_exists($uf, $descs)) { wp_set_object_terms($postID, intval($descs[$uf]), $taxonomy, true); } elseif (array_key_exists(self::FormatText($uf), $descs)) { wp_set_object_terms($postID, intval($descs[self::FormatText($uf)]), $taxonomy, true); } else { FluxoSettingsPage::log("Term: UF: {$uf_slug}, não encontrado, tax: {$taxonomy}, Post: {$postID}, cols: {$col1}, {$uf_row}<br/>"); } } } if (!empty($cidade_slug)) { $term_obj = get_term_by('slug', $cidade_slug, $taxonomy); if ($term_obj !== false) { if ($postID > 0) { wp_set_object_terms($postID, intval($term_obj->term_id), $taxonomy, true); } else { FluxoSettingsPage::log("wp_set_object_terms( {$postID}, intval({$term_obj->term_id}), {$taxonomy}, true );"); FluxoSettingsPage::log(" Term name: {$term_obj->name}<br/>"); } } else { FluxoSettingsPage::log("Term: Cidade: {$cidade_slug}, não encontrada, tax: {$taxonomy}, Post: {$postID}, cols: {$col1}, {$uf}, cidade: {$cidade}, cidade_slug: {$cidade_slug}<br/>"); } } }
public function ImportarCsv_callback() { FluxoSettingsPage::newLog(); echo '<div id="result">'; if (function_exists('mapasdevista_get_coords')) { include_once dirname(__FILE__) . '/Tratar.php'; $debug = false; $getLocation = true; $begin = 0; $limit = 20; $ids = array(); $pins_args = array('post_type' => 'attachment', 'meta_key' => '_pin_anchor', 'posts_per_page' => '-1'); $pinsTodos = get_posts($pins_args); $pins = array(); foreach ($pinsTodos as $pin) { $pins[] = $pin->ID; } FluxoSettingsPage::log(print_r($pins, true)); ini_set("memory_limit", "2048M"); set_time_limit(0); $names = array(); $file = fopen('/tmp/import.csv', 'r'); $coords = array(); if (file_exists('/tmp/coords.csv')) { $coords_file = fopen('/tmp/coords.csv', 'r'); $coords_row = fgetcsv($coords_file, 0, ';'); while ($coords_row !== false) { $coords[$coords_row[0]] = array('lat' => $coords_row[1], 'lon' => $coords_row[2]); $coords_row = fgetcsv($coords_file, 0, ';'); } } //cabeçalho da planilha for ($i = 0; $i < 3; $i++) { $row = fgetcsv($file, 0, ';'); $names[$i] = array_map('trim', $row); } for ($i = 0; $i < $begin; $i++) { $row = fgetcsv($file, 0, ';'); } FluxoSettingsPage::log('<pre>'); $row = fgetcsv($file, 0, ';'); $i = 0; do { if (count($ids) > 0) { while ($row !== false && !in_array($row[3], $ids)) { $row = fgetcsv($file, 0, ';'); } if ($row === false) { break; } } $row[0] = trim($row[0]); $row[1] = trim($row[1]); if (empty($row[0]) && empty($row[1]) || strcasecmp($row[0], 'Inexistente') == 0) { $row = fgetcsv($file, 0, ';'); $i++; continue; } if (empty($row[0])) { $row[0] = $row[1]; } // definir titulo e descrição $post = array('post_author' => 1, 'post_content' => trim($row[6]), 'post_title' => trim($row[0]), 'post_type' => 'emrede', 'post_status' => 'publish'); $post_id = 0; if (!$debug) { $post_id = wp_insert_post($post); } //colunas que eu não quero importar como texto livre (custom fields) $no_import = array(7, 8, 10); $location = false; if (count($coords) > 0) { $location = $coords[$row[1]]; } $row[19] = trim($row[19]); $row[20] = trim($row[20]); $row[21] = trim($row[21]); $row[22] = trim($row[22]); $row[19] = trim(array_shift(explode(';', $row[19]))); $row[19] = trim(array_shift(explode(',', $row[19]))); $row[19] = trim(array_shift(explode('-', $row[19]))); if ($getLocation && $location === false) { //lets try address first $uf = $row[20]; if (!empty($row[21])) { // país $uf .= "," . $row[21]; } if (!empty($row[22])) { $row[22] = array_shift(explode(';', $row[22])); $location = mapasdevista_get_coords($row[22] . "," . $row[19] . ',' . $uf); // Endereço if ($location == false) { $location = mapasdevista_get_coords($row[19] . ',' . $uf); // Município e estado } } else { //setar coluna do municipio $location = mapasdevista_get_coords($row[19] . ',' . $uf); // Município e estado } } if ($debug) { FluxoSettingsPage::log($post, true); if ($location !== false) { FluxoSettingsPage::log("{$row[0]};{$location['lat']};{$location['lon']}"); } elseif ($getLocation) { FluxoSettingsPage::log("{$row['0']} -> debug ponto em rede não encontrado"); } FluxoSettingsPage::log('<br/>'); } else { if ($location !== false) { //setar coluna id FluxoSettingsPage::log("{$row[0]};{$location['lat']};{$location['lon']}"); // exportar lat e lon FluxoSettingsPage::log('<br/>'); update_post_meta($post_id, '_mpv_location', $location); } elseif ($getLocation) { FluxoSettingsPage::log("{$row['0']} -> ponto em rede não encontrado"); FluxoSettingsPage::log('<br/>'); } } $pin = $pins[rand(0, count($pins) - 1)]; if (!$debug && is_int($post_id)) { update_post_meta($post_id, '_mpv_pin', $pin); delete_post_meta($post_id, '_mpv_inmap'); delete_post_meta($post_id, '_mpv_in_img_map'); add_post_meta($post_id, "_mpv_inmap", 1); } else { FluxoSettingsPage::log("Pin: {$pin}"); FluxoSettingsPage::log('<br/>'); } global $EmRede_global; $fields = $EmRede_global->getFields(); foreach ($row as $key => $custom_field) { $custom_field = trim($custom_field); if (in_array($key, array(0, 1, 2, 3, 5, 6, 11, 12, 13, 14, 19, 20, 24, 26, 28, 29, 36, 38, 42))) { continue; } if ($key > 44) { break; } if (!in_array($key, $no_import)) { if (array_key_exists($names[0][$key], $fields)) { if ($debug) { FluxoSettingsPage::log("update_post_meta({$post_id}, {$fields[$names[0][$key]]['slug']}, {$custom_field});<br/>"); } else { update_post_meta($post_id, $fields[$names[0][$key]]['slug'], $custom_field); } } else { FluxoSettingsPage::log("{$row['0']} -> campo não encontrado: {$names[0][$key]}"); FluxoSettingsPage::log('<br/>'); } } } /** * Taxonomies */ Tratar::territorio($post_id, 'territorio', $row[19], $row[20]); Tratar::tags($post_id, 'category', $row[14]); Tratar::publicoalvo($post_id, 'publico-alvo', $row[11]); $row = fgetcsv($file, 0, ';'); $i++; } while ($row !== false && (!$debug || $i < $limit)); FluxoSettingsPage::log('</pre>'); fclose($file); } echo '</div>'; die; }