Changeset 50174 in spip-zone
- Timestamp:
- Aug 2, 2011, 3:04:35 PM (10 years ago)
- Location:
- _plugins_/csv_import
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
_plugins_/csv_import/exec/csvimport_admin.php
r45742 r50174 112 112 // champs de la table 113 113 //$s = ""; 114 $s .= "<table >";114 $s .= "<table class='csv_import' >"; 115 115 $col=0; 116 116 foreach($desc['field'] as $field=>$type){ -
_plugins_/csv_import/exec/csvimport_import.php
r42523 r50174 15 15 include_spip('public/assembler'); 16 16 17 function csvimport_visu_extrait($nombre,$import_mode,$table,$id_form){ 17 function csvimport_visu_extrait($nb_lignes, $import_mode, $nom_table, $id_form){ 18 18 19 // Extrait de la table en commençant par les dernieres maj 19 if ($import_mode!='form') 20 return csvimport_table_visu_extrait($table,$nombre); 21 else { 22 $contexte = array('id_form'=>$id_form,'total'=>$nombre); 23 $out = recuperer_fond("fonds/tables_visu_extrait",$contexte); 24 return $out; 25 } 20 if ( $import_mode != 'form' ) { 21 22 $out = csvimport_table_visu_extrait($nom_table, $nb_lignes); 23 24 } else { 25 26 // Utilisation du 'fond' "tables_visu_extrait" pour afficher un extrait de la table de départ 27 // (PB : Affichage d'une colonne "Choix multiple" là où on voudrait plûtot une colonne pour chaque choix) 28 $contexte = array( 'id_form'=>$id_form, 'total'=>$nb_lignes ); 29 $out = recuperer_fond("fonds/tables_visu_extrait", $contexte); 30 } 31 32 return $out; 26 33 } 27 34 28 function csvimport_table_fields($mode, $table,$id_form){35 function csvimport_table_fields($mode, $table, $id_form){ 29 36 $table_fields = array(); 30 if ($mode=='table'){ 37 38 if ($mode=='table') 39 { 31 40 $csvimport_tables_auth = csvimport_tables_auth(); 32 if (isset($csvimport_tables_auth[$table]['field'])) 41 42 if (isset($csvimport_tables_auth[$table]['field'])) 33 43 $table_fields=$csvimport_tables_auth[$table]['field']; 34 44 else 35 45 $table_fields=array_keys($GLOBALS['tables_principales'][$table]['field']); 46 36 47 $table_fields=array_flip($table_fields); 48 37 49 foreach ($table_fields as $key=>$value) { 38 50 $table_fields[$key] = $key; 39 51 } 40 return $table_fields; 41 } 42 if ($mode=='form' && $id_form){ 52 53 return $table_fields; 54 55 } //FIN ($mode=='table') 56 57 58 if ($mode=='form' && $id_form) 59 { 43 60 include_spip('inc/forms'); 44 61 $structure = Forms_structure($id_form); 45 62 $table_fields['id_donnee'] = 'id_donnee'; 63 46 64 foreach ($structure as $champ=>$info){ 47 if ($info['type']!='multiple') 65 66 if ($info['type']!='multiple') { 48 67 $table_fields[$champ] = $info['titre']; 49 else68 } else { 50 69 foreach ($info['choix'] as $choix=>$value) { 51 70 $table_fields[$choix] = $value; 52 71 } 53 } 72 } 73 74 } 75 54 76 return $table_fields; 55 } 77 78 } //FIN ($mode=='form') 79 56 80 return $table_fields; 57 81 } 82 58 83 59 84 function csvimport_import_step3(&$step, &$erreur, $import_link, $import_form_link, $csvimport_replace_actif, $csvimport_add_actif){ … … 72 97 $apercu = _request('apercu'); 73 98 74 $titre = _T("csvimport:import_csv",array('table'=>$table)); 75 76 if ($table===NULL && $id_form) 99 100 // SI $table est vide... 101 if( $table == "" ){ 102 $nom_table = sql_getfetsel( 'titre', 'spip_forms', 'id_form ='.intval($id_form) ); 103 } else { 104 $nom_table = $table; 105 } 106 107 $titre = _T("csvimport:import_csv",array('table'=>$nom_table)); 108 109 if ( $table===NULL && $id_form ) 77 110 $import_mode='form'; 78 111 else … … 93 126 94 127 if ($step==3){ 95 if ( (!$file_name) ||(!$tmp_name)||(!$size)||(!$type) )128 if ( (!$file_name) || (!$tmp_name) || (!$size) || (!$type) ) 96 129 $erreur[$step][] = _T("csvimport:fichier_absent"); 97 130 … … 108 141 if (!$head) $head = false; 109 142 $data = csvimport_importcsv($tmp_name, $head, $delim); 143 110 144 if ($data==false) { 111 145 $erreur[$step][] = _T("csvimport:fichier_vide"); 112 146 } 113 $table_fields = csvimport_table_fields($import_mode,$table,$id_form); 114 $new_assoc=csvimport_field_associate($data, $table_fields, $assoc_field); 115 $test=array_diff($new_assoc,$assoc_field); 147 148 $table_fields = csvimport_table_fields($import_mode, $table, $id_form); 149 $new_assoc = csvimport_field_associate($data, $table_fields, $assoc_field); 150 $test = array_diff($new_assoc, $assoc_field); //( array_diff() : Calcule la différence entre deux tableaux ) 151 116 152 if (count($test)>0){ 117 153 $erreur[$step][] = _T("csvimport:correspondance_incomplete"); 118 154 } 155 119 156 if (isset($erreur[$step])) $step--; 120 157 } 158 121 159 if ($step==3){ 160 122 161 $hidden['file_name'] = $file_name; 123 162 $hidden['tmp_name'] = $tmp_name; … … 139 178 }*/ 140 179 141 if (($remplacer)&&(!_request('confirme_remplace'))){ 180 181 if ( ($remplacer) && (!_request('confirme_remplace')) ){ 182 142 183 $hidden['remplacer'] = 'oui'; 143 184 $step3 .= $import_form_link; 144 foreach($hidden as $key=>$value) 185 186 foreach($hidden as $key=>$value) { 145 187 $step3 .= "<input type='hidden' name='$key' value='$value' />"; 188 } 189 146 190 // Extrait de la table en commencant par les dernieres maj 147 $step3 .= "<ul><li class='editer_texte'>"; 148 $step3 .= csvimport_visu_extrait(5,$import_mode,$table,$id_form); 191 $step3 .= "<ul><li class='editer_texte' style='overflow:auto;'>"; 192 193 $step3 .= csvimport_visu_extrait(5, $import_mode, $table, $id_form); 194 149 195 $step3 .= "</li><li class='editer_texte'>"; 150 196 $step3 .= "<div class='explication'>"._T('csvimport:avertissement_remplacement')."</div>"; … … 159 205 $step3 .= "</div></form>"; 160 206 } 161 else if (($ajouter)&&(!_request('confirme_ajoute'))){ 207 else if ( ($ajouter) && (!_request('confirme_ajoute')) ){ 208 162 209 $hidden['ajouter'] = 'oui'; 163 210 $step3 .= $import_form_link; 211 212 // (Ajout d'un div pour pouvoir avoir un 'overflow: auto;' sur l'extrait de la table) 213 $step3 .= "<div style='margin:10px 5px; border:1px dashed; overflow: auto;' >"; 214 164 215 // Extrait de la table en commencant par les dernieres maj 165 $step3 .= csvimport_visu_extrait(5,$import_mode,$table,$id_form); 166 167 $step3 .= csvimport_array_visu_assoc($data, $table_fields, $assoc_field, 5); 168 if ($import_mode=='form') 216 $step3 .= csvimport_visu_extrait(5, $import_mode, $table, $id_form); 217 218 $step3 .= "<br />"; 219 220 $step3 .= csvimport_array_visu_assoc($data, $table_fields, $assoc_field, 10); 221 222 $step3 .= "<br />"; 223 $step3 .= "</div>"; 224 225 226 if ($import_mode=='form') { 169 227 if (include_spip('inc/forms')){ 170 228 Forms_csvimport_ajoute_table_csv($data, $id_form, $assoc_field, $err, true); 171 229 $step3 .= csvimport_show_erreurs($err); 172 230 } 231 } 173 232 174 233 $step3 .= "<div style='padding: 2px; color: black;'> "; 175 $step3 .= _T("csvimport:avertissement_ajout",array('table'=>$table)); 176 foreach($hidden as $key=>$value) 234 235 $step3 .= "<h4 style='margin:5px 5px 20px;'>"; 236 $step3 .= _T("csvimport:avertissement_ajout",array('table'=>$nom_table)); 237 $step3 .= "</h4>"; 238 239 foreach($hidden as $key=>$value) { 177 240 $step3 .= "<input type='hidden' name='$key' value='$value' />"; 241 } 242 178 243 $step3 .= "<p class='boutons'><input type='submit' name='annule_ajoute' value='"._T('annuler')."' class='submit' /> "; 179 244 $step3 .= "<input type='submit' name='confirme_ajoute' value='"._T('csvimport:ajouter_donnees')."' class='submit' /></p>"; 180 245 $step3 .= "</div></form>"; 246 181 247 } 182 248 else { 249 183 250 // vidange de la table 184 if ( ($remplacer)&&(_request('confirme_remplace'))){251 if ( ($remplacer) && (_request('confirme_remplace')) ){ 185 252 if ($import_mode=='table') 186 253 csvimport_vidange_table($table); … … 189 256 Forms_donnees_vide($id_form); 190 257 } 258 191 259 // le reste est identique que ce soit un ajout ou un remplace 192 if (($remplacer)||($ajouter)){ 260 if ( ($remplacer) || ($ajouter) ){ 261 193 262 $err = array(); 194 if ($import_mode=='table') 195 $out = csvimport_ajoute_table_csv($data, $table, $assoc_field,$err); 196 elseif ($import_mode=='form') 263 264 if ($import_mode=='table') { 265 266 $out = csvimport_ajoute_table_csv($data, $table, $assoc_field, $err); 267 268 } elseif ($import_mode=='form') { 269 197 270 if (include_spip('inc/forms')) 198 271 Forms_csvimport_ajoute_table_csv($data, $id_form, $assoc_field, $err); 199 272 273 } 274 275 $step3 .= "<br />"; 276 200 277 // Extrait de la table en commencant par les dernieres maj 201 $step3 .= csvimport_visu_extrait(10,$import_mode,$table,$id_form); 202 203 if (count($err)){ 204 $step3 .= bouton_block_invisible("erreurs"); 205 $step3 .= _T("csvimport:erreurs_ajout_base",array('nb'=>count($err))); 206 $step3 .= debut_block_invisible("erreurs"); 278 $step3 .= "<div style='margin: 0 5px; overflow: auto;' >"; 279 $step3 .= csvimport_visu_extrait(10, $import_mode, $table, $id_form); 280 $step3 .= "</div>"; 281 282 283 // SI il y a des erreurs .... 284 if( count($err) ) 285 { 286 $step3 .= "<br />"; 287 288 $step3 .= "<h3 style='margin-left:5px; color:red;' >" . 289 _T("csvimport:erreurs_ajout_base", array('nb'=>count($err))) . 290 "</h3>"; 291 292 $step3 .= "<br />"; 293 294 $step3 .= "<div style='overflow: auto; border:1px dashed;'>"; 295 if (version_compare($GLOBALS['spip_version_code'],'1.9300','<')) // SPIP <= 1.9.2x 296 { 297 $step3 .= bouton_block_invisible("Erreur(s)"); 298 $step3 .= debut_block_invisible("Erreur(s)"); 299 300 } elseif (version_compare($GLOBALS['spip_version_code'],'2','>=')) // SPIP >= 2.x 301 { 302 $step3 .= bouton_block_depliable ("Erreur(s)",true); 303 $step3 .= debut_block_depliable("Erreur(s)"); 304 } 305 207 306 $step3 .= csvimport_show_erreurs($err); 307 208 308 $step3 .= fin_block(); 309 $step3 .= "<br />"; 310 $step3 .= "</div>"; 311 312 313 // SI aucune erreur ... 314 } else { 315 $step3 .= "<br />"; 316 $step3 .= "<h3 style='margin:10px 5px; color:green;'>"._T("csvimport:confirmation_ajout_base")."</h3>"; 317 $step3 .= "<br />"; 318 319 $step3 .= csvimport_show_erreurs($err); // ??? (Normalement, pas d'erreur ici!?) 209 320 } 210 else 211 $step3 .= csvimport_show_erreurs($err); 321 322 $step3 .= "<br /><br />"; 323 212 324 } 213 325 } 214 326 } 327 215 328 return $step3; 216 329 } 330 217 331 function csvimport_import_step2(&$step, &$erreur, $import_link, $import_form_link, $csvimport_replace_actif, $csvimport_add_actif){ 218 332 $table = _request('table'); … … 282 396 } 283 397 } 398 284 399 $table_fields = csvimport_table_fields($import_mode,$table,$id_form); 285 if ($data && ($step==2)) 286 $assoc_field=csvimport_field_associate($data, $table_fields, $assoc_field); 400 401 if ($data && ($step==2)) { 402 403 // Essaye d'associer les noms des colonnes importés du CSV avec les noms des colonnes de la table : 404 // ("$assoc_field" vide au départ) 405 $assoc_field = csvimport_field_associate($data, $table_fields, $assoc_field); 406 407 } 408 409 287 410 if ($step==2){ 288 411 $hidden['file_name'] = $file_name; … … 291 414 $hidden['type'] = $type; 292 415 $hidden['step'] = 3; 416 417 // Ajout d'une barre de séparation visuelle 418 $step2 .= "<hr style='border:1.5px dotted;' />"; 293 419 294 420 $step2 .= csvimport_show_erreurs($erreur); 295 421 $step2 .= $import_form_link; 422 296 423 foreach($hidden as $key=>$value) 297 424 $step2 .= "<input type='hidden' name='$key' value='$value' />"; 298 299 $step2 .= "<ul><li class='editer_texte'>"; 300 $step2 .= "<div class='explication'>"._T('csvimport:premieres_lignes',array('nb'=>5))."</div>"; 301 $step2 .= csvimport_array_visu_extrait($data, /*$head*/true, 5); 425 426 $step2 .= "<br />"; 427 $step2 .= "<h3 style='margin-left:10px; margin-bottom:0px;'>" . _T('csvimport:extrait_CSV_importe') . "</h3>"; 428 429 $step2 .= "<ul><li class='editer_texte extrait_table' style='overflow: auto;'>"; 430 $step2 .= "<div class='explication' style='font-size: 12px; font-weight:bold;' >" . _T('csvimport:premieres_lignes', array('nb'=>5)) . "</div>"; 431 432 $step2 .= "<div style='overflow: auto;' >"; 433 $step2 .= csvimport_array_visu_extrait($data, $head, 5); 434 $step2 .= "</div>"; 302 435 303 436 $step2 .= "</li><li class='editer_texte'>"; … … 309 442 $step2 .= "<li><label for='entete'>"._T("csvimport:ligne_entete")."</label>"; 310 443 $step2 .= "<input type='checkbox' name='head' id='entete' class='fondl' style='width:2em;' value='true'"; 444 311 445 if ($head==true) 312 446 $step2 .= " checked='checked'"; 447 313 448 $step2 .= " /></li><li class='editer_texte'>"; 314 449 … … 318 453 319 454 $step2 .= "<li class='editer_texte'>"; 320 $step2 .= csvimport_array_visu_assoc($data, $table_fields, $assoc_field, 5); 455 456 $step2 .= "<br />"; 457 458 459 $nb_lignes_previ = 5; 460 461 $step2 .= "<div class='explication' style='font-size: 12px; font-weight:bold;' >" . 462 _T('csvimport:previsualisation_CSV_integre', array('nb'=>$nb_lignes_previ)) . "</div>"; 463 464 $step2 .= "<div style='overflow:auto; margin-bottom:10px;' >"; 465 $step2 .= csvimport_array_visu_assoc($data, $table_fields, $assoc_field, $nb_lignes_previ); 466 $step2 .= "</div>"; 467 321 468 $step2 .= "</li></ul>\n"; 322 469 … … 338 485 } 339 486 } 487 340 488 function csvimport_import_step1(&$step, &$erreur, $import_link, $import_form_link, $csvimport_replace_actif, $csvimport_add_actif){ 341 489 $table = _request('table'); … … 352 500 $assoc_field = _request('assoc_field'); 353 501 $apercu = _request('apercu'); 502 354 503 if ($table===NULL && $id_form) 355 504 $import_mode='form'; … … 369 518 $step1 .= "<ul><li>"; 370 519 $step1 .= "<label for='file_name'>"._T("csvimport:fichier_choisir")."</label>"; 371 $step1 .= "<input type='file' name='csvfile' id='file_name' class='fichier' />"; 520 521 // (Agrandissement du <input>) 522 $step1 .= "<input type='file' name='csvfile' id='file_name' class='fichier' size='30' />"; 523 372 524 $step1 .= "</li></ul>"; 373 525 $step1 .= "<p class='boutons'><input type='submit' name='Valider' value='"._T('bouton_valider')."' class='submit' /></p>"; 374 526 $step1 .= "</div></form></div>\n"; 375 527 } 528 376 529 return $step1; 377 530 } 378 531 532 533 // ##### FONCTION PRINCIPALE ##### // 379 534 function exec_csvimport_import(){ 380 535 global $spip_lang_right; 381 $assoc_field=array(); 536 537 $assoc_field = array(); 538 382 539 $table = _request('table'); 383 540 $id_form = intval(_request('id_form')); … … 400 557 $retour = generer_url_ecrire('csvimport_tous'); 401 558 402 $titre = _T("csvimport:import_csv",array('table'=>$table)); 559 //// INFO / RAPPEL : 560 /* sql_fetsel(...) = Retourne la première ligne d’une selection sql. 561 La fonction est identique à sql_fetch(sql_select(...))*/ 562 /* sql_getfetsel(...) = Retourne l'unique champ demandé dans une requête Select à résultat unique. 563 Retourne le premier champ de la première ligne d’une sélection sql. 564 La fonction est équivalente à $r = sql_fetsel(...)); return $r ? $r[0] : false; */ 565 /* '_q()' est remplacé par 'sql_quote()' pour échapper les champs non numeriques dans les syntaxes de requêtes SQL 566 intval() est utilisé pour les champs numeriques */ 567 568 // SI $table est vide... 569 if( $table == "" ){ 570 $nom_table = sql_getfetsel( 'titre', 'spip_forms', 'id_form ='.intval($id_form) ); 571 } else { 572 $nom_table = $table; 573 } 574 575 $titre = _T( "csvimport:import_csv", array('table'=>$nom_table) ); 403 576 $icone = _DIR_PLUGIN_CSVIMPORT."img_pack/csvimport-24.png"; 577 404 578 $operations = array(); 405 579 … … 454 628 455 629 $hidden = array(); 456 457 630 $milieu = ''; 458 631 … … 464 637 $milieu .= icone_inline(_T('icone_retour'), $retour, $icone, "rien.gif",$GLOBALS['spip_lang_left']); 465 638 } 639 466 640 $milieu .= gros_titre($titre,'', false); 641 642 // Affichge de l'étape courante pour savoir où on en est : 643 $milieu.= "<h4>" . _T("csvimport:etape", array('step'=>$step)) . "</h4>"; 644 467 645 $milieu .= "</div>"; 468 646 469 647 $milieu .= "<div class='formulaire_spip'>"; 470 648 649 471 650 if($step<3){ 472 $milieu .= csvimport_visu_extrait(5,$import_mode,$table,$id_form); 473 } 651 652 // Affichage du nom de la table avec la nouvelle variable '$nom_table' 653 $milieu .= "<br /><h3 style='margin-left:10px'>" . _T("csvimport:extrait_table", array('nom_table'=>$nom_table)) . "</h3>"; 654 655 // (Ajout d'un div pour pouvoir avoir un 'overflow: auto;' sur l'extrait de la table) 656 $milieu .= "<div style='margin: 0 5px; overflow: auto;' >"; 657 658 // Affichage d'un extrait de la table actuelle : 659 // (PB : Affichage d'une colonne "Choix multiple" là où on voudrait plûtot une colonne pour chaque choix! ("A", "B" et "C") ) 660 $milieu .= csvimport_visu_extrait(5, $import_mode, $table, $id_form); 661 $milieu .= "</div>"; 662 663 // Récupération des noms des colonnes possibles/attendues du fichier CSV à importer : 664 // (juste pour information, aucune vérification à posteriori) 665 $table_fields = csvimport_table_fields($import_mode, $table, $id_form); 666 667 // Affichage des colonnes du fichier CSV à importer : 668 $milieu .= "<br /><br />"; 669 $milieu .= "<h3 style='margin-left:10px'>" . _T("csvimport:noms_colonnes_CSV") . "</h3>"; 670 671 // Ajout d'un div pour pouvoir avoir un 'overflow: auto;' 672 $milieu .= "<div style='margin: 0 5px; overflow: auto;' >"; 673 674 $milieu .= "<table border='1' cellpadding='4' cellspacing='0'>"; 675 //$output .= "<caption> Voici le titre du tableau </caption>"; 676 $milieu .= "<tr>"; 677 foreach($table_fields as $tablekey => $libelle){ 678 $milieu .= "<th>" . $libelle . "</th>\n"; 679 } 680 $milieu .= "</tr> </table>"; 681 682 $milieu .= "</div>"; 683 684 $milieu .= "<br />"; 685 686 } 687 688 474 689 // --- STEP 3 475 690 $milieu .= csvimport_import_step3($step, $erreur, $import_link, $import_form_link, $csvimport_replace_actif, $csvimport_add_actif); … … 480 695 // --- STEP 1 481 696 $milieu .= csvimport_import_step1($step, $erreur, $import_link, $import_form_link, $csvimport_replace_actif, $csvimport_add_actif); 697 482 698 483 699 $milieu .= "</div>"; -
_plugins_/csv_import/exec/csvimport_telecharger.php
r42523 r50174 118 118 119 119 // Excel ? 120 if ($delim == ',') 120 if ($delim == ',') { 121 121 122 $extension = 'csv'; 122 else { 123 $extension = 'xls'; 123 124 } else { 125 126 // Extension 'csv' si delim = ';' (et pas forcément 'xls' !) 127 if ($delim == ';') { $extension = 'csv'; } 128 else { $extension = 'xls'; } 129 124 130 # Excel n'accepte pas l'utf-8 ni les entites html... on fait quoi? 125 131 include_spip('inc/charsets'); -
_plugins_/csv_import/inc/csvimport.php
r45742 r50174 166 166 echo "</div>\n"; 167 167 } 168 $out = "<br />"; 168 169 $out = "<br /> <br />"; 170 169 171 if (defined('_DIR_PLUGIN_FORMS')&&($GLOBALS['meta']['forms_base_version']>0.17)){ 170 172 if (include_spip('inc/forms_tables_affichage')) … … 204 206 function csvimport_show_erreurs($erreur){ 205 207 $output = ""; 208 206 209 if (count($erreur)>0){ 207 210 $output .= "<div class='messages'>"; 208 foreach($erreur as $steper=>$desc) 211 212 foreach($erreur as $line=>$desc) 209 213 foreach($desc as $key=>$val) 210 $output .= "<strong>$steper::$key:$val</strong><br />"; 214 $output .= " Ligne $line :: $val <br />"; 215 211 216 $output .= "</div>\n"; 212 217 } 218 213 219 return $output; 214 220 } 215 221 216 function csvimport_table_visu_extrait($nom_table, $nombre_lignes = 0){217 222 function csvimport_table_visu_extrait($nom_table, $nombre_lignes = 0){ 223 218 224 $limit = ""; 219 225 if ($nombre_lignes > 0) … … 222 228 $trouver_table = charger_fonction('trouver_table', 'base'); 223 229 $desc = $trouver_table($nom_table); 224 if (in_array('maj', $desc['field'])) { 230 231 //if ( in_array('maj', $desc['field']) ) // (Ne marchait pas...) 232 if ( isset($desc['field']['maj']) ) 233 { 225 234 $maj_exist = true; 226 $result = sql_select("*",$nom_table,"","","maj DESC",$limit); 227 } 228 else { 235 $result = sql_select("*", $nom_table, "", "", "maj DESC", $limit); 236 237 } elseif( isset($desc['field']['id_article']) ) 238 { 229 239 $maj_exist = false; 230 $result = sql_select("*",$nom_table,"","","",$limit); 240 $result = sql_select("*", $nom_table, "", "", "id_article DESC", $limit); 241 242 } else { 243 $maj_exist = false; 244 $result = sql_select("*", $nom_table, "", "", "", $limit); 231 245 } 232 246 233 $nb_data=sql_count($result); 247 $nb_data = sql_count($result); 248 234 249 if ($nombre_lignes==0) 235 250 $nombre_lignes = $nb_data; 251 236 252 $data_count = 0; 237 253 $head_set = false; 238 254 $nb_col = 0; 255 239 256 if($nb_data>0){ 240 $ret .= "<table width='100%'>"; 241 while (($row = sql_fetch($result))&&($data_count++<$nombre_lignes)){ 257 258 $ret .= "<table class='csv_import' width='100%' border='1' cellspacing='0' cellpadding='2'>"; 259 260 while ( ($row = sql_fetch($result)) && ($data_count++<$nombre_lignes) ) 261 { 242 262 if (!$head_set){ 243 263 $ret .= "<tr>"; … … 249 269 $head_set = true; 250 270 } 271 251 272 $ret .= "<tr>"; 273 252 274 foreach($row as $key=>$value) 253 $ret .= "<td>" . $value . "</td>"; 275 $ret .= "<td>" . couper($value,50) . "</td>"; // (Couper le texte aprés 50 caractères) 276 254 277 $ret .= "</tr>\n"; 255 278 } 279 256 280 if ($nb_data>$nombre_lignes){ 257 281 $num_rows = sql_count(sql_select("*",$nom_table)); 258 $ret .= "<tr><td colspan='$nb_col' style='border-top:1px dotted;'>"._T("csvimport:lignes_table",array('table'=>$nom_table,'nb_resultats'=>$num_rows))."</td></tr>\n"; 259 } 282 283 $ret .= "<tr><td colspan='$nb_col' style='border-top:1px dotted; font-weight:bold;'> (" . 284 _T("csvimport:lignes_table", array('table'=>$nom_table,'nb_resultats'=>$num_rows) ) . 285 ") </td></tr>\n"; 286 } 287 260 288 $ret .= "</table>\n"; 261 }262 else289 290 } else { 263 291 $ret = "<p>"._T("csvimport:table_vide", array('table'=>$nom_table))."</p>"; 292 } 264 293 265 294 return $ret; 266 295 } 267 296 297 298 // ### Affichage des @nombre_lignes@ premières lignes du fichier CSV importé ### // 268 299 function csvimport_array_visu_extrait($data, $head, $nombre_lignes = 0){ 269 300 $output = ""; … … 271 302 $head_set = false; 272 303 $nb_col = 0; 304 273 305 if ($data!=false){ 274 $output .= "<table width='100%'>"; 306 307 $output .= "<table width='100%' border='1' cellspacing='0' cellpadding='2'>"; 275 308 foreach($data as $key=>$ligne) { 309 310 // SI c'est la 1ère itération ALORS on affiche d'abord la ligne des noms de colonne (<th>) 276 311 if (($head==true)&&($head_set==false)){ 277 312 $output .= "<tr>"; … … 281 316 } 282 317 $output .= "</tr>\n"; 283 $head_set = true; 284 } 285 else{ 318 $head_set = true; //(à ne faire qu'une seule fois!) 319 } 320 321 // Changement ici : Suppression du "else" pour qu'on ne saute pas à chaque fois la 1ère ligne! 322 //else{ 286 323 $output .= "<tr>"; 287 324 foreach($ligne as $value){ … … 289 326 } 290 327 $output .= "</tr>\n"; 291 } 328 //} 329 292 330 if (($nombre_lignes>0)&&($data_count++>=$nombre_lignes)) 293 331 break; 294 } 332 333 } 334 295 335 if ($data_count>0) 296 $output .= '<tr><td style="border-top:1px dotted " colspan="'.$nb_col.'">'._T("csvimport:lignes_totales_csv",array("nb"=>count($data))).'</td></tr>';336 $output .= '<tr><td style="border-top:1px dotted; font-weight:bold;" colspan="'.$nb_col.'"> ('._T("csvimport:lignes_totales_csv",array("nb"=>count($data))).') </td></tr>'; 297 337 $output .= "</table>\n"; 298 338 } 339 299 340 if ($data_count==0) 300 341 $output .= _T("csvimport:aucune_donnee"); 342 301 343 return $output; 302 344 } 303 345 346 347 // ### Affichage du résultat des $nombre_lignes premières lignes du fichier CSV ajoutées à la table ### // 348 // (selon les choix "Champs du fichier CSV" - "Champs de la table") 304 349 function csvimport_array_visu_assoc($data, $table_fields, $assoc_field, $nombre_lignes = 0){ 305 350 $assoc=array_flip($assoc_field); … … 307 352 $output = ""; 308 353 $data_count = 0; 309 $output .= "<table width='100%' >";354 $output .= "<table width='100%' border='1' cellspacing='0' cellpadding='2'>"; 310 355 $output .= "<tr>"; 311 356 foreach($table_fields as $key=>$value){ … … 316 361 $nb_col = 0; 317 362 if ($data!=false){ 363 318 364 foreach($data as $key=>$ligne) { 365 319 366 $nb_col = 0; 320 367 $output .= "<tr>"; 368 321 369 foreach($table_fields as $key=>$value){ 322 370 $nb_col++; … … 330 378 $output .= "</td>"; 331 379 } 380 332 381 $output .= "</tr>\n"; 382 333 383 if (($nombre_lignes>0)&&(++$data_count>=$nombre_lignes)) 334 384 break; 335 385 } 336 386 } 387 337 388 if ($data_count>0) 338 $output .= '<tr><td style="border-top:1px dotted" colspan="'.$nb_col.'">'._T("csvimport:lignes_totales_csv",array("nb"=>count($data))).'</td></tr>'; 389 $output .= '<tr><td style="border-top:1px dotted; font-weight:bold;" colspan="'.$nb_col.'"> ('._T("csvimport:lignes_totales_csv",array("nb"=>count($data))).') </td></tr>'; 390 339 391 $output .= "</table>"; 392 340 393 return $output; 341 394 } 342 395 396 343 397 function csvimport_nettoie_key($key){ 398 399 /* 400 Fonction "string importer_csv_nettoie_key(string $key)" : 401 Plugin Spip-Bonux 2 (/inc/importer_csv.php) 402 Enlever les accents des cles presentes dans le head, sinon ca pose des problemes ... 403 (cf. http://spip-plugins.quesaco.org/phpdoc/_plugins_/spip-bonux-2/spip-bonux-2/_inc---importer_csv.php.html ) 404 */ 344 405 return importer_csv_nettoie_key($key); 345 } 406 407 } 408 346 409 347 410 function csvimport_field_associate($data, $table_fields, $assoc_field){ 348 411 global $tables_principales; 349 $assoc=$assoc_field; 412 413 $assoc = $assoc_field; 350 414 if (!is_array($assoc)) $assoc = array(); 351 $csvfield=array_keys($data{0}); 415 416 $csvfield = array_keys($data{0}); 352 417 foreach($csvfield as $k=>$v){ 353 418 $csvfield[$k] = csvimport_nettoie_key($v); 354 419 } 355 $csvfield=array_flip($csvfield); 356 357 // on enleve toutes les associations dont 358 // la cle n'est pas un csvfield 359 // la valeur n'est pas un tablefield 360 // l'un des deux est deja affecte 420 //(Retourne le tableau : Remplace les clés par les valeurs, et les valeurs par les clés) 421 $csvfield = array_flip($csvfield); 422 423 // On enleve toutes les associations dont : 424 // - la cle n'est pas un csvfield 425 // - la valeur n'est pas un tablefield 426 // - l'un des deux est deja affecte 361 427 foreach ($assoc as $key=>$value){ 362 428 $good_key = false; 363 429 $good_value = false; 430 364 431 if (isset($csvfield[$key])){ 365 432 $good_key = true; 366 433 } 367 if ((isset($table_fields[$value]))||($value==-1)){ 434 435 if ( (isset($table_fields[$value])) || ($value==-1) ){ 368 436 $good_value = true; 369 437 } 370 if (($good_key==false)||($good_value==false)) 438 439 if ( ($good_key==false) || ($good_value==false) ) { 371 440 unset($assoc[$key]); 372 else{441 }else{ 373 442 unset($csvfield[$key]); 374 if ($value!=-1) unset($table_fields[$value]); 375 } 376 } 377 378 //assoc auto des cles qui portent le meme nom 443 if ($value != -1) unset($table_fields[$value]); 444 } 445 } 446 447 448 //// Modification ici : Il y avait un mélange entre les clés et les valeurs... 449 //// Association automatique des clés qui portent le meme nom (même valeur/libellé) : 450 foreach($csvfield as $csvkey=>$csvval ){ 451 foreach($table_fields as $tablekey=>$tableval ) 452 if (strcasecmp($csvkey, $tableval)==0){ // (Comparaison insensible à la casse de chaînes binaires. 0 si égales) 453 $assoc[$csvkey] = $tablekey; 454 unset($csvfield[$csvkey]); // ("unset()" : efface l'élément du tableau) 455 unset($table_fields[$tablekey]); 456 } 457 } 458 //// 459 460 461 //// Changement de comportement : Association des autres avec "Ne pas importer!" (-1) 462 //// (AU LIEU DE : Association des autres dans l'ordre qui vient) 463 //$table_fields = array_keys($table_fields); 379 464 foreach(array_keys($csvfield) as $csvkey){ 380 foreach(array_keys($table_fields) as $tablekey) 381 if (strcasecmp($csvkey,$tablekey)==0){ 382 $assoc[$csvkey]=$tablekey; 383 unset($csvfield[$csvkey]); 384 unset($table_fields[$tablekey]); 385 } 386 } 387 //assoc des autres dans l'ordre qui vient 388 $table_fields=array_keys($table_fields); 389 foreach(array_keys($csvfield) as $csvkey){ 390 $assoc[$csvkey]=array_shift($table_fields); 391 if ($assoc[$csvkey]==NULL) $assoc[$csvkey]="-1"; 465 //$assoc[$csvkey] = array_shift($table_fields); 466 //if ($assoc[$csvkey] == NULL) { 467 $assoc[$csvkey] = "-1"; 468 //} 392 469 unset($csvfield[$csvkey]); 393 470 } 471 394 472 return $assoc; 395 473 } … … 397 475 function csvimport_field_configure($data, $table_fields, $assoc){ 398 476 $output = ""; 399 $csvfield=array_keys($data{0}); 400 401 $output .= "<table><tr><td>"._T("csvimport:champs_csv")."</td><td>"._T("csvimport:champs_table")."</td></tr>"; 402 foreach($csvfield as $csvkey){ 403 $csvkey = csvimport_nettoie_key($csvkey); 404 $output .= "<tr>"; 405 $output .= "<td>$csvkey</td>"; 477 $csvfield = array_keys($data{0}); 478 479 $output .= "<table border='0' cellpadding='4' cellspacing='2'>"; 480 $output .= "<tr> <th>"._T("csvimport:champs_csv")."</th>"; 481 $output .= "<th>" . _T("csvimport:champs_table") . "</th> </tr>"; 482 483 foreach($csvfield as $csvkey){ //(ATTENTION : "$csvkey" n'est en fait pas une CLE du tableau, c'est une VALEUR du tableau!) 484 $csvkey = csvimport_nettoie_key($csvkey); // (Supprime les accents des clés) 485 486 $output .= "<tr>"; 487 $output .= "<td>$csvkey</td>"; 406 488 $output .= "<td><select name='assoc_field[$csvkey]'>\n"; 407 $output .= "<option value='-1'>"._T("csvimport:pas_importer")."</option>\n"; 489 $output .= "<option value='-1'>" . _T("csvimport:pas_importer") . "</option>\n"; 490 408 491 foreach($table_fields as $tablekey => $libelle){ 409 492 $output .= "<option value='$tablekey'"; 410 if ($assoc[$csvkey]==$tablekey) 411 $output .= " selected='selected'";412 $output .= ">$libelle</option>\n";413 }493 if ($assoc[$csvkey]==$tablekey) { $output .= " selected='selected' "; } 494 $output .= ">". $libelle . " (" . $tablekey . ")</option>\n"; 495 } 496 414 497 $output .= "</select></td></tr>"; 415 498 } 499 416 500 $output .= "</table>"; 501 417 502 return $output; 418 503 } … … 430 515 function csvimport_ajoute_table_csv($data, $table, $assoc_field, &$erreur){ 431 516 global $tables_principales; 517 432 518 $csvimport_tables_auth = csvimport_tables_auth(); 433 519 $assoc = array_flip($assoc_field); 434 520 $desc = sql_showtable($table); 435 if (!isset($desc['field']) || count($desc['field'])==0){ 521 522 if ( !isset($desc['field']) || count($desc['field'])==0 ){ 436 523 $erreur[0][] = _T("csvimport:description_table_introuvable"); 437 524 return; 438 525 } 439 if ($GLOBALS['mysql_rappel_nom_base'] AND $db = $GLOBALS['spip_mysql_db']) 526 527 // Supression du prefixe du nom de la table (Par exemple : "spip1_*" --> "spip_*") 528 if( isset($GLOBALS['db_ok']['prefixe']) && ($GLOBALS['db_ok']['prefixe'] != 'spip') ) 529 { 530 $prefixe_tables = $GLOBALS['db_ok']['prefixe']; 531 $table = str_replace( $prefixe_tables , 'spip' , $table ); 532 } 533 534 if ( $GLOBALS['mysql_rappel_nom_base'] AND $db = $GLOBALS['spip_mysql_db'] ) 440 535 $table = '`'.$db.'`.'.$table; 441 536 442 443 537 $tablefield=array_keys($desc['field']); 444 $output = ""; 538 //$output = ""; //??? 539 445 540 // y a-t-il une cle primaire ? 446 if (isset($desc['key']["PRIMARY KEY"])){ 541 if( isset($desc['key']["PRIMARY KEY"]) ) 542 { 447 543 $primaire = $desc['key']["PRIMARY KEY"]; 448 544 // la cle primaire est-elle importee ? … … 450 546 unset($primaire); 451 547 } 548 452 549 // y a-t-il un champ TIMESTAMP ? 453 $test =array_flip($desc['field']);454 if (isset($test['TIMESTAMP']))550 $test = array_flip($desc['field']); 551 if( isset($test['TIMESTAMP']) ) 455 552 $stamp = $test['TIMESTAMP']; 456 553 457 if ($data!=false){ 554 if( $data != false ) 555 { 458 556 $count_lignes = 0; 459 foreach($data as $key=>$ligne) { 460 $count_lignes ++; 557 558 foreach($data as $key=>$ligne) 559 { 560 $count_lignes ++; 561 461 562 $check = array_flip($tablefield); 563 462 564 foreach($check as $key=>$value){ 463 565 $kc = csvimport_nettoie_key($key); 464 if ((isset($assoc[$kc]))&&(isset($ligne[$assoc[$kc]]))){ 566 if ( (isset($assoc[$kc])) && (isset($ligne[$assoc[$kc]])) ) 567 { 465 568 $what[$key] = addslashes($ligne[$assoc[$kc]]); 466 569 unset($check[$key]); 467 570 } 468 } 469 if ((isset($stamp))&&isset($check[$stamp])){ 571 } // (FIN foreach) 572 573 if ( (isset($stamp)) && isset($check[$stamp]) ){ 470 574 $what[$stamp] = date('Y-m-d H:i:s'); 471 575 } 472 if (is_array($what)) { 576 577 if (is_array($what)) 578 { 473 579 $id_primary = sql_insertq($table, $what); 474 if ($id_primary==0) 475 $erreur[$count_lignes][] = "ajout impossible ::$what::$with::<br />"; 476 } 477 else 478 $erreur[$count_lignes][] = "rien à ajouter<br />"; 479 } 480 } 580 581 // SI id = 0 (Erreur d'insertion) ... 582 if ($id_primary==0) { 583 $apercu = couper(print_r($what,true), 150, ' ...'); // Petit aperçu de la valeur (150 caractères) 584 $erreur[$count_lignes][] = "Ajout impossible ! :: $apercu "; 585 } 586 587 } else { 588 $erreur[$count_lignes][] = "Rien à ajouter ! "; 589 } 590 591 }// (FIN foreach) 592 593 }// (FIN if) 594 481 595 } 482 596 -
_plugins_/csv_import/lang/csvimport_fr.php
r45777 r50174 12 12 'aucune_donnee' => 'Il n\'y a aucune donnée dans le fichier.', 13 13 'aucune_table_declaree' => 'Aucune table n\'est déclarée pour l\'import CSV', 14 'avertissement_ajout' => 'Les données du fichier CSV vont être ajoutées à la table "@table@" comme illustré ci-dess ous.',14 'avertissement_ajout' => 'Les données du fichier CSV vont être ajoutées à la table "@table@" comme illustré ci-dessus.', 15 15 'avertissement_remplacement' => 'Cette opération va entraîner la suppression de toutes les données présentes dans la table. Les données du fichier seront insérées comme ci-dessous :', 16 16 … … 18 18 'caracteres_separation' => 'Caractère de séparation', 19 19 'champs_csv' => 'Champs du fichier CSV', 20 'champs_table' => 'Champs de la table ',20 'champs_table' => 'Champs de la table : "libellé (clé)"', 21 21 'correspondance_incomplete' => 'Correspondances CSV-Table incomplètes', 22 22 'correspondance_indefinie' => 'Correspondances CSV-Table non définies', 23 23 'csvimport' => 'Import CSV', 24 'confirmation_ajout_base' => 'Le CSV a été correctement ajouté à la base !', 25 24 26 25 27 // D … … 35 37 'export_tabulation' => 'CSV avec tabulations', 36 38 'exporter' => 'Exporter', 39 'extrait_table' => 'Extrait de la table "@nom_table@" : ', 40 'etape' => '(Étape @step@ sur 3)', 41 'extrait_CSV_importe' => 'Extrait du fichier CSV importé : ', 37 42 38 43 // F … … 42 47 43 48 // I 44 'import_csv' => 'Import CSV : @table@',49 'import_csv' => 'Import CSV : "@table@"', 45 50 'import_export_tables' => 'Import / Export dans les tables', 46 51 … … 53 58 // N 54 59 'nb_enregistrements' => '@nb@ enregistrements', 60 'noms_colonnes_CSV' => 'Noms des colonnes (attendus) du fichier CSV à importer : ', 55 61 56 62 // P … … 60 66 'probleme_chargement_fichier_erreur' => 'Problème lors du chargement du fichier (erreur @erreur@).', 61 67 'probleme_inextricable' => 'Problème inextricable...', 68 'previsualisation_CSV_integre' => 'Prévisualisation des @nb@ premières lignes du fichier CSV intégrées à la table : ', 62 69 63 70 // R -
_plugins_/csv_import/plugin.xml
r47358 r50174 9 9 </auteur> 10 10 <version> 11 0.2. 111 0.2.2 12 12 </version> 13 13 <icon>img_pack/csvimport-24.png</icon>
Note: See TracChangeset
for help on using the changeset viewer.