Commit a07cf91e5622f96b870d6990f358299094801cc2
1 parent
4970e7ee
Exists in
master
and in
7 other branches
--no commit message
Showing
2 changed files
with
79 additions
and
25 deletions
Show diff stats
admin/js/estat_editor.js
| ... | ... | @@ -76,11 +76,12 @@ i3GEOadmin.editor = { |
| 76 | 76 | }, |
| 77 | 77 | tabela:{ |
| 78 | 78 | onde: "i3GEOadminEditorTabela", |
| 79 | + optionsTabela: "", | |
| 79 | 80 | lista: function(){ |
| 80 | 81 | var callback = { |
| 81 | 82 | success:function(o){ |
| 82 | 83 | try { |
| 83 | - var dados = YAHOO.lang.JSON.parse(o.responseText), | |
| 84 | + var opt,dados = YAHOO.lang.JSON.parse(o.responseText), | |
| 84 | 85 | temp = "<fieldset>" + |
| 85 | 86 | "<p class=paragrafo >Crie uma tabela no banco contendo limites ou localidades que poderá ser utilizada para espacializar os dados estatísticos existentes em outras tabelas<br>" + |
| 86 | 87 | "<input type=button value='Upload Shapefile' id='i3GEOadmin_botaoupload' /></p>" + |
| ... | ... | @@ -95,7 +96,9 @@ i3GEOadmin.editor = { |
| 95 | 96 | temp += "<fieldset>" + |
| 96 | 97 | "<p>Escolha uma tabela existente: " + |
| 97 | 98 | "<select id='i3GEOadmintabela' onchange='i3GEOadmin.editor.coluna.lista()'>"; |
| 98 | - temp += core_comboObjeto(dados,"tabela","tabela"); | |
| 99 | + opt = core_comboObjeto(dados,"tabela","tabela"); | |
| 100 | + i3GEOadmin.editor.tabela.optionsTabela = opt; | |
| 101 | + temp += opt; | |
| 99 | 102 | temp += "</select>" + |
| 100 | 103 | "<p class=paragrafo ><input type=button value='Mostrar dados' id='i3GEOadmintabelaMostrar' />" + |
| 101 | 104 | "<input type=button value='CSV' id='i3GEOadmintabelaCsv'/>" + |
| ... | ... | @@ -441,9 +444,18 @@ i3GEOadmin.editor = { |
| 441 | 444 | '<p class="paragrafo" >shp: <br><input class=digitar type="file" size=22 name="i3GEOuploadshp" style="top:0px;left:0px;cursor:pointer;"></p>' + |
| 442 | 445 | '<p class="paragrafo" >shx: <br><input class=digitar type="file" size=22 name="i3GEOuploadshx" style="top:0px;left:0px;cursor:pointer;"></p>' + |
| 443 | 446 | '<p class="paragrafo" >dbf: <br><input class=digitar type="file" size=22 name="i3GEOuploaddbf" style="top:0px;left:0px;cursor:pointer;"></p>' + |
| 444 | - '<p class="paragrafo" >Nome da nova tabela (não utilize caracteres incompatíveis com o banco de dados, como -, acentos ou espaços em branco):<br><input class=digitar type="text" size=20 id="tabelaDestino" name="tabelaDestino" style="top:0px;left:0px;cursor:pointer;"></p>' + | |
| 447 | + '<p class="paragrafo" >Nome da tabela (não utilize caracteres incompatíveis com o banco de dados, como -, acentos ou espaços em branco):<br><input class=digitar type="text" size=20 id="tabelaDestino" name="tabelaDestino" style="top:0px;left:0px;cursor:pointer;"> ' + | |
| 448 | + 'Ou escolha da lista: ' + | |
| 449 | + '<select onchange="javascript:$i(\'tabelaDestino\').value = this.value;">' + | |
| 450 | + i3GEOadmin.editor.tabela.optionsTabela + | |
| 451 | + '</select></p>' + | |
| 452 | + '<p class="paragrafo" >Tipo de operação:</p>' + | |
| 453 | + '<p class="paragrafo" ><select id=i3GEOtipoOperacao name=tipoOperacao >' + | |
| 454 | + '<option value=criar >Criar a tabela nova e incluir registros do SHP</option>' + | |
| 455 | + '<option value=incluir >Adicionar novos registros</option>' + | |
| 456 | + '<option value=apagar >Apagar dados atuais e incluir do SHP</option>' + | |
| 457 | + '</select></p>' + | |
| 445 | 458 | '<p class="paragrafo" >Código da projeção (SRID) Assegure-se que o shapefile esteja na projeção geográfica se você for usar com o sistema de metadados estatísticos :<br><input class=digitar type="text" value="4326" size=20 id="srid" name="srid" style="top:0px;left:0px;cursor:pointer;"></p>' + |
| 446 | - | |
| 447 | 459 | '<p class="paragrafo" ><input id=i3GEOuploadsubmit type="button" value="Enviar" size=12 />' + |
| 448 | 460 | '<input type="hidden" name="MAX_FILE_SIZE" value="1000000">' + |
| 449 | 461 | '<input type="hidden" id="i3GEOuploadcodigoconexao" name="i3GEOuploadcodigoconexao" value="">' + |
| ... | ... | @@ -457,6 +469,12 @@ i3GEOadmin.editor = { |
| 457 | 469 | return ins; |
| 458 | 470 | }, |
| 459 | 471 | submit: function(){ |
| 472 | + if($i("i3GEOtipoOperacao").value === "apagar"){ | |
| 473 | + var confirma = window.confirm("Apaga mesmo os registros? (nao pode ser revertido)"); | |
| 474 | + if(!confirma){ | |
| 475 | + return; | |
| 476 | + } | |
| 477 | + } | |
| 460 | 478 | if($i("tabelaDestino").value == ""){ |
| 461 | 479 | alert("Digite o nome da tabela a ser criada"); |
| 462 | 480 | return; |
| ... | ... | @@ -488,7 +506,6 @@ i3GEOadmin.editor = { |
| 488 | 506 | var ins = '<fieldset><form id=i3GEOuploadcsvf target="i3GEOuploadcsviframe" action="../php/metaestat_uploadcsv_submit.php" method="post" ENCTYPE="multipart/form-data">' + |
| 489 | 507 | '<p class="paragrafo" >CSV (utilize ponto como separador de valores decimais ou a importação poderá não ocorrer): <br><input class=digitar type="file" size=22 name="i3GEOuploadcsv" style="top:0px;left:0px;cursor:pointer;"></p>' + |
| 490 | 508 | '<p class="paragrafo" >Nome da nova tabela (não utilize caracteres incompatíveis com o banco de dados, como -, acentos ou espaços em branco):<br><input class=digitar type="text" size=20 id="tabelaDestinocsv" name="tabelaDestinocsv" style="top:0px;left:0px;cursor:pointer;"></p>' + |
| 491 | - | |
| 492 | 509 | '<p class="paragrafo" ><input id=i3GEOuploadcsvsubmit type="button" value="Enviar" size=12 />' + |
| 493 | 510 | '<input type="hidden" name="MAX_FILE_SIZE" value="1000000">' + |
| 494 | 511 | '<input type="hidden" id="i3GEOuploadcsvcodigoconexao" name="i3GEOuploadcsvcodigoconexao" value="">' + | ... | ... |
admin/php/metaestat_uploadshp_submit.php
| ... | ... | @@ -77,7 +77,6 @@ if (isset($_FILES['i3GEOuploadshp']['name'])){ |
| 77 | 77 | $colunas[] = $c; |
| 78 | 78 | } |
| 79 | 79 | } |
| 80 | - | |
| 81 | 80 | echo "<br>Numshapes: ". $numshapes; |
| 82 | 81 | $tipo = $shapefileObj->type; |
| 83 | 82 | echo "<br>Tipo: ". $tipo; |
| ... | ... | @@ -106,19 +105,61 @@ if (isset($_FILES['i3GEOuploadshp']['name'])){ |
| 106 | 105 | echo "<br>Tipos das colunas: <pre>"; |
| 107 | 106 | var_dump($tipoColuna); |
| 108 | 107 | echo "</pre>"; |
| 108 | + | |
| 109 | + try { | |
| 110 | + $dbh = new PDO('pgsql:dbname='.$conexao["bancodedados"].';user='.$conexao["usuario"].';password='.$conexao["senha"].';host='.$conexao["host"].';port='.$conexao["porta"]); | |
| 111 | + } catch (PDOException $e) { | |
| 112 | + echo '<span style=color:red >Connection failed: ' . $e->getMessage(); | |
| 113 | + exit; | |
| 114 | + } | |
| 115 | + | |
| 109 | 116 | //gera o script para criar a tabela |
| 117 | + //verifica se a tabela ja existe | |
| 118 | + $sql = "SELECT table_name FROM information_schema.tables where table_schema = '".$_POST["i3GEOuploadesquema"]."' AND table_name = '".$_POST["tabelaDestino"]."'"; | |
| 119 | + $res = $dbh->query($sql,PDO::FETCH_ASSOC); | |
| 120 | + if(count($res->fetchAll())>0){ | |
| 121 | + $tabelaExiste = true; | |
| 122 | + } | |
| 123 | + else{ | |
| 124 | + $tabelaExiste = false; | |
| 125 | + } | |
| 126 | + //encoding do banco de dados | |
| 127 | + $sql = "SELECT pg_encoding_to_char(encoding) FROM pg_database WHERE datname = '".$conexao["bancodedados"]."'"; | |
| 128 | + $res = $dbh->query($sql,PDO::FETCH_ASSOC); | |
| 129 | + $encodingdb = $res->fetchAll()[0]["pg_encoding_to_char"]; | |
| 130 | + if($encodingdb == "UTF8"){ | |
| 131 | + $encodingdb = "UTF-8"; | |
| 132 | + } | |
| 133 | + if($encodingdb == "LATIN1"){ | |
| 134 | + $encodingdb = "ISO-8859-1"; | |
| 135 | + } | |
| 136 | + //a tabela nao existe e e do tipo create | |
| 110 | 137 | $sqltabela = array(); |
| 111 | - $sql = "CREATE TABLE ".$_POST["i3GEOuploadesquema"].".".$_POST["tabelaDestino"]."(gid integer, the_geom geometry"; | |
| 112 | - foreach($colunas as $coluna){ | |
| 113 | - $sql .= ",".strtolower($coluna)." ".$tipoColuna[$coluna]; | |
| 114 | - } | |
| 115 | - $sql .= ")WITH(OIDS=FALSE)"; | |
| 116 | - $sqltabela[] = $sql; | |
| 117 | - $sqltabela[] = "ALTER TABLE ".$_POST["i3GEOuploadesquema"].".".$_POST["tabelaDestino"]." OWNER TO ".$conexao["usuario"]; | |
| 118 | - $sqltabela[] = "CREATE INDEX ".$_POST["tabelaDestino"]."_indx_thegeom ON ".$_POST["i3GEOuploadesquema"].".".$_POST["tabelaDestino"]." USING gist (the_geom )"; | |
| 119 | - echo "<br>Sql tabela: <pre>"; | |
| 120 | - var_dump($sqltabela); | |
| 121 | - echo "</pre>"; | |
| 138 | + if($tabelaExiste == false && $_POST["tipoOperacao"] == "criar"){ | |
| 139 | + $sql = "CREATE TABLE ".$_POST["i3GEOuploadesquema"].".".$_POST["tabelaDestino"]."(gid integer, the_geom geometry"; | |
| 140 | + foreach($colunas as $coluna){ | |
| 141 | + $sql .= ",".strtolower($coluna)." ".$tipoColuna[$coluna]; | |
| 142 | + } | |
| 143 | + $sql .= ")WITH(OIDS=FALSE)"; | |
| 144 | + $sqltabela[] = $sql; | |
| 145 | + $sqltabela[] = "ALTER TABLE ".$_POST["i3GEOuploadesquema"].".".$_POST["tabelaDestino"]." OWNER TO ".$conexao["usuario"]; | |
| 146 | + $sqltabela[] = "CREATE INDEX ".$_POST["tabelaDestino"]."_indx_thegeom ON ".$_POST["i3GEOuploadesquema"].".".$_POST["tabelaDestino"]." USING gist (the_geom )"; | |
| 147 | + echo "<br>Sql tabela: <pre>"; | |
| 148 | + var_dump($sqltabela); | |
| 149 | + echo "</pre>"; | |
| 150 | + } | |
| 151 | + if($tabelaExiste == true && $_POST["tipoOperacao"] == "criar"){ | |
| 152 | + echo "<span style=color:red >A tabela existe. Não pode ser criada.</span>"; | |
| 153 | + exit; | |
| 154 | + } | |
| 155 | + //se a tabela existe e e para remover os registros | |
| 156 | + if($tabelaExiste == true && $_POST["tipoOperacao"] == "apagar"){ | |
| 157 | + $sqltabela[] = "delete from ".$_POST["i3GEOuploadesquema"].".".$_POST["tabelaDestino"]; | |
| 158 | + } | |
| 159 | + if($tabelaExiste == true && $_POST["tipoOperacao"] == "apagar" && $_POST["i3GEOuploadesquema"] != "i3geo_metaestat"){ | |
| 160 | + echo "<span style=color:red >Não é possível executar essa operação nesse esquema.</span>"; | |
| 161 | + exit; | |
| 162 | + } | |
| 122 | 163 | //gera o script para inserir os dados |
| 123 | 164 | $linhas = array(); |
| 124 | 165 | $insert = "INSERT INTO ".$_POST["i3GEOuploadesquema"].".".$_POST["tabelaDestino"]."( gid,".strtolower(implode(",",$colunas)).",the_geom)"; |
| ... | ... | @@ -128,7 +169,9 @@ if (isset($_FILES['i3GEOuploadshp']['name'])){ |
| 128 | 169 | $vs[] = $i; |
| 129 | 170 | foreach($colunas as $coluna){ |
| 130 | 171 | if($tipoColuna[$coluna] == "varchar"){ |
| 131 | - $vs[] = "'".$s->getValue($layer,$coluna)."'"; | |
| 172 | + $texto = $s->getValue($layer,$coluna); | |
| 173 | + //echo mb_detect_encoding($texto); | |
| 174 | + $vs[] = "'".mb_convert_encoding($texto,$encodingdb,mb_detect_encoding($texto))."'"; | |
| 132 | 175 | } |
| 133 | 176 | else{ |
| 134 | 177 | $vs[] = $s->getValue($layer,$coluna); |
| ... | ... | @@ -138,12 +181,6 @@ if (isset($_FILES['i3GEOuploadshp']['name'])){ |
| 138 | 181 | $linhas[] = $insert."VALUES(".implode(",",$vs).")"; |
| 139 | 182 | } |
| 140 | 183 | $layer->close(); |
| 141 | - try { | |
| 142 | - $dbh = new PDO('pgsql:dbname='.$conexao["bancodedados"].';user='.$conexao["usuario"].';password='.$conexao["senha"].';host='.$conexao["host"].';port='.$conexao["porta"]); | |
| 143 | - } catch (PDOException $e) { | |
| 144 | - echo 'Connection failed: ' . $e->getMessage(); | |
| 145 | - } | |
| 146 | - | |
| 147 | 184 | foreach($sqltabela as $linha){ |
| 148 | 185 | try { |
| 149 | 186 | $dbh->query($linha); |
| ... | ... | @@ -174,4 +211,4 @@ function verificaNome($nome,$ext){ |
| 174 | 211 | } |
| 175 | 212 | ?> |
| 176 | 213 | </body> |
| 177 | -</html> | |
| 178 | 214 | \ No newline at end of file |
| 215 | +</html> | ... | ... |