Commit c6302b245380ecf2ea78c229451ef1d10c292e7b

Authored by Edmar Moretti
1 parent 9023b4c4
Exists in master

--no commit message

Showing 1 changed file with 44 additions and 15 deletions   Show diff stats
admin/php/metaestat_uploadcsv_submit.php
... ... @@ -67,7 +67,6 @@ if (ob_get_level() == 0) ob_start();
67 67 }
68 68 echo "<p class='paragrafo' >Separador de colunas identificado: <b>".$separador."</b></p>";
69 69 echo "<p class='paragrafo' >Total de colunas: <b>".count($colunas)."</b></p>";
70   - //var_dump($colunas);
71 70 fclose ($handle);
72 71 //le o csv em um array
73 72 $handle = fopen ($arqcsv, "r");
... ... @@ -77,7 +76,7 @@ if (ob_get_level() == 0) ob_start();
77 76 while (!feof($handle)) {
78 77 $buffer = fgets($handle);
79 78 if($buffer != $cabecalho){
80   - $buffer = str_replace('"','',$buffer);
  79 + //$buffer = str_replace('"','',$buffer);
81 80 $buffer = str_replace("'",'',$buffer);
82 81 $buffer = str_replace("\n",'',$buffer);
83 82 $buffer = str_replace("\r",'',$buffer);
... ... @@ -96,7 +95,7 @@ if (ob_get_level() == 0) ob_start();
96 95 }
97 96 fclose ($handle);
98 97 //decobre o tipo de coluna
99   - $testar = 500;
  98 + $testar = 50;
100 99 if(count($linhas) < $testar){
101 100 $testar = count($linhas);
102 101 }
... ... @@ -198,25 +197,36 @@ if (ob_get_level() == 0) ob_start();
198 197 $escapar = "',<,>,%,#,@,(,)";
199 198 for ($i=0; $i<$nlinhas;$i++){
200 199 $s = $linhas[$i];
  200 + $s = str_replace('"','',$s);
201 201 $enc = mb_detect_encoding($s);
202 202 if($enc != ""){
203 203 $s = mb_convert_encoding($s,$encodingdb,$enc);
204 204 }
205 205 $vs = array();
206 206 for ($j=0; $j<$ncolunas;$j++){
207   - if($tipoColuna[$colunas[$j]] == "varchar"){
  207 + $escape = "";
  208 + if($tipoColuna[$coluna] == "varchar"){
208 209 $texto = $s[$j];
209   - $texto = str_replace("'","",$texto);
210   - $texto = "'".addcslashes($texto,$escapar)."'";
211   - if($texto == "''"){
212   - $texto = 'null';
  210 + $enc = mb_detect_encoding($texto);
  211 + $textosl = addcslashes($texto,$escapar);
  212 + if($textosl != $texto){
  213 + $escape = "E";
213 214 }
214   - $vs[] = $texto;
  215 + if($enc != "" && $enc != $encodingdb){
  216 + $textosl = "$escape'".mb_convert_encoding($textosl,$encodingdb,$enc)."'";
  217 + }
  218 + else{
  219 + $textosl = "$escape'".$textosl."'";
  220 + }
  221 + if($textosl == "''"){
  222 + $textosl = 'null';
  223 + }
  224 + $vs[] = $textosl;
215 225 }
216 226 else{
217 227 $valor = $s[$j];
218   - if($valor == ""){
219   - $valor = "nulo";
  228 + if($valor == "" || (empty($valor) && $valor != 0)){
  229 + $valor = 'nulo';
220 230 }
221 231 $vs[] = $valor;
222 232 }
... ... @@ -253,20 +263,39 @@ if (ob_get_level() == 0) ob_start();
253 263 echo 'Erro: ' . $e->getMessage();
254 264 }
255 265 }
  266 + $bdcon = pg_connect('dbname='.$conexao["bancodedados"].' user='.$conexao["usuario"].' password='.$conexao["senha"].' host='.$conexao["host"].' port='.$conexao["porta"]."options='-c client_encoding=LATIN1'");
  267 +
256 268 foreach($linhasql as $linha){
257 269 try {
258 270 $res = $dbh->query($linha);
259 271 if($res == false){
260   - echo "<br><br><span style=color:red >Erro em: </span>".$linha;
  272 + $res = pg_query($bdcon,$linha);
  273 + if($res == false){
  274 + $linha = remove_accents($linha);
  275 + $res = $dbh->query($linha);
  276 + if($res == false){
  277 + $res = pg_query($bdcon,$linha);
  278 + if($res == false){
  279 + echo "<br><br><span style=color:red >Erro em: </span>".$linha;
  280 + }
  281 + }
  282 + else{
  283 + echo "<br><br><span style=color:red >Linha com acentos removidos: </span>".$linha;
  284 + }
  285 + }
261 286 }
262 287 } catch (PDOException $e) {
263 288 echo 'Erro: ' . $e->getMessage();
264 289 }
265 290 }
266   - echo "<br>Registros existentes no CSV: ". $nlinhas;
267 291 $sql = "select * from ".$_POST["i3GEOuploadcsvesquema"].".".$_POST["tabelaDestinocsv"];
268 292 $q = $dbh->query($sql,PDO::FETCH_ASSOC);
269 293 $r = $q->fetchAll();
  294 + if($nlinhas != count($r)){
  295 + echo "<span style='color:red'>";
  296 + }
  297 + echo "<br>Registros existentes no CSV: ". $nlinhas;
  298 +
270 299 echo "<br>Registros na tabela final: ". count($r);
271 300 echo "<b><br>Feito!!!<br>Fa&ccedil;a o reload da p&aacute;gina";
272 301 }
... ... @@ -274,8 +303,8 @@ if (ob_get_level() == 0) ob_start();
274 303 echo "<p class='paragrafo' >Erro ao enviar o arquivo. Talvez o tamanho do arquivo seja maior do que o permitido.</p>";
275 304 }
276 305  
277   - ?>
278   - <script>window.scrollTo(0,10000);</script>
  306 + ?>
  307 + <script>window.scrollTo(0,10000);</script>
279 308  
280 309 </body>
281 310 </html>
... ...