Subversion Repositories eFlore/Applications.cel

Rev

Rev 2381 | Rev 2447 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1636 raphael 1
<?php
2
/**
1929 raphael 3
 * @category  PHP
4
 * @package   jrest
5
 * @author    Raphaël Droz <raphael@tela-botania.org>
6
 * @copyright 2013 Tela-Botanica
7
 * @license   http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
8
 * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
9
 */
1636 raphael 10
 
11
/**
12
 * Service d'import de données d'observation du CEL au format XLS
1649 raphael 13
 *
14
 * Sont define()'d commme n° de colonne tous les abbrevs retournés par
1656 raphael 15
 * FormateurGroupeColonne::nomEnsembleVersListeColonnes() préfixés par C_  cf: detectionEntete()
1649 raphael 16
 *
17
 * Exemple d'un test:
18
 * $ GET "/jrest/ExportXLS/22506?format=csv&range=*&limite=13" \
19
 *   | curl -F "upload=@-" -F utilisateur=22506 "/jrest/ImportXLS"
20
 * # 13 observations importées
21
 * + cf MySQL general_log = 1
22
 *
23
 **/
1636 raphael 24
 
25
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(dirname(realpath(__FILE__))) . '/lib');
26
// TERM
27
error_reporting(-1);
28
ini_set('html_errors', 0);
29
ini_set('xdebug.cli_color', 2);
30
require_once('lib/PHPExcel/Classes/PHPExcel.php');
1656 raphael 31
require_once('FormateurGroupeColonne.php');
1636 raphael 32
 
1640 raphael 33
 
34
date_default_timezone_set("Europe/Paris");
35
 
36
// nombre d'INSERT à cumuler par requête SQL
37
// (= nombre de lignes XLS à bufferiser)
1648 raphael 38
//define('NB_LIRE_LIGNE_SIMUL', 30);
39
define('NB_LIRE_LIGNE_SIMUL', 5);
1640 raphael 40
 
1933 raphael 41
// en cas d'import d'un fichier CSV, utilise fgetcsv() plutôt
42
// que PHPExcel ce qui se traduit par un gain de performances très substanciel
43
define('QUICK_CSV_IMPORT', TRUE);
44
 
1640 raphael 45
// Numbers of days between January 1, 1900 and 1970 (including 19 leap years)
46
// see traiterDateObs()
1675 raphael 47
// define("MIN_DATES_DIFF", 25569);
1640 raphael 48
 
49
 
1636 raphael 50
class MyReadFilter implements PHPExcel_Reader_IReadFilter {
1929 raphael 51
    // exclusion de colonnes
52
    public $exclues = array();
1640 raphael 53
 
1929 raphael 54
    // lecture par morceaux
1640 raphael 55
    public $ligne_debut = 0;
56
    public $ligne_fin = 0;
2055 aurelien 57
 
58
    public static $gestion_mots_cles = null;
1640 raphael 59
 
1929 raphael 60
    public function __construct() {}
61
    public function def_interval($debut, $nb) {
62
	$this->ligne_debut = $debut;
63
	$this->ligne_fin = $debut + $nb;
64
    }
1638 raphael 65
    public function readCell($colonne, $ligne, $worksheetName = '') {
1929 raphael 66
	if(@$this->exclues[$colonne]) return false;
67
	// si des n° de morceaux ont été initialisés, on filtre...
68
	if($this->ligne_debut && ($ligne < $this->ligne_debut || $ligne >= $this->ligne_fin)) return false;
69
	return true;
1636 raphael 70
    }
71
}
72
 
1675 raphael 73
// XXX: PHP 5.3
74
function __anonyme_1($v) {	return !$v['importable']; }
75
function __anonyme_2(&$v) {	$v = $v['nom']; }
76
function __anonyme_3($cell) { return !is_null($cell); };
77
function __anonyme_5($item) { return is_null($item) ? '?' : $item; }
78
function __anonyme_6() { return NULL; }
79
 
1636 raphael 80
class ImportXLS extends Cel  {
1929 raphael 81
    static function __anonyme_4(&$item, $key) { $item = self::quoteNonNull(trim($item)); }
1636 raphael 82
 
1929 raphael 83
    static $ordre_BDD = Array(
84
	"ce_utilisateur",
85
	"prenom_utilisateur",
86
	"nom_utilisateur",
87
	"courriel_utilisateur",
88
	"ordre",
89
	"nom_sel",
90
	"nom_sel_nn",
91
	"nom_ret",
92
	"nom_ret_nn",
93
	"nt",
94
	"famille",
95
	"nom_referentiel",
96
	"zone_geo",
97
	"ce_zone_geo",
98
	"date_observation",
99
	"lieudit",
100
	"station",
101
	"milieu",
102
	"mots_cles_texte",
103
	"commentaire",
104
	"transmission",
105
	"date_creation",
106
	"date_modification",
107
	"date_transmission",
108
	"latitude",
109
	"longitude",
110
	"altitude",
111
	"abondance",
112
	"certitude",
113
	"phenologie",
114
	"code_insee_calcule"
115
    );
1636 raphael 116
 
1929 raphael 117
    // cf: initialiser_pdo_ordered_statements()
118
    // eg: "INSERT INTO cel_obs (ce_utilisateur, ..., phenologie, code_insee_calcule) VALUES"
119
    // colonnes statiques d'abord, les autres ensuite, dans l'ordre de $ordre_BDD
120
    static $insert_prefix_ordre;
121
    // eg: "(<id>, <prenom>, <nom>, <email>, now(), now(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
122
    // dont le nombre de placeholder dépend du nombre de colonnes non-statiques
123
    // colonnes statiques d'abord, les autres ensuite, dans l'ordre de $ordre_BDD
124
    static $insert_ligne_pattern_ordre;
1648 raphael 125
 
1929 raphael 126
    // seconde (meilleure) possibilité
127
    // cf: initialiser_pdo_statements()
128
    // eg: "INSERT INTO cel_obs (ce_utilisateur, ..., date_creation, ...phenologie, code_insee_calcule) VALUES"
129
    static $insert_prefix;
130
    // eg: "(<id>, <prenom>, <nom>, <email>, ?, ?, ?, now(), now(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
131
    // dont le nombre de placeholder dépend du nombre de colonnes non-statiques
132
    static $insert_ligne_pattern;
1648 raphael 133
 
1929 raphael 134
    /*
135
      Ces colonnes:
136
      - sont propres à l'ensemble des enregistrements uploadés
137
      - sont indépendantes du numéro de lignes
138
      - n'ont pas de valeur par défaut dans la structure de la table
139
      - nécessitent une initialisation dans le cadre de l'upload
1649 raphael 140
 
1929 raphael 141
      initialiser_colonnes_statiques() y merge les données d'identification utilisateur
142
    */
143
    public $colonnes_statiques = Array(
144
	"ce_utilisateur" => NULL,
145
	"prenom_utilisateur" => NULL,
146
	"nom_utilisateur" => NULL,
147
	"courriel_utilisateur" => NULL,
1640 raphael 148
 
1929 raphael 149
	// fixes (fonction SQL)
150
	// XXX future: mais pourraient varier dans le futur si la mise-à-jour
151
	// d'observation est implémentée
152
	"date_creation" => "now()",
153
	"date_modification" => "now()",
154
    );
2381 aurelien 155
 
156
    public static $prefixe_colonnes_etendues = 'ext:';
157
    public static $indexes_colonnes_etendues = Array();
158
    public static $gestion_champs_etendus = null;
1640 raphael 159
 
1929 raphael 160
    public $id_utilisateur = NULL;
1649 raphael 161
 
1929 raphael 162
    // erreurs d'import
163
    public $bilan = Array();
1642 raphael 164
 
1929 raphael 165
    // cache (pour traiterLocalisation() pour l'instant)
166
    static $cache = Array('geo' => array());
1649 raphael 167
 
1929 raphael 168
    function ImportXLS($config) {
169
	parent::__construct($config);
170
    }
171
 
172
    function createElement($pairs) {
173
	if(!isset($pairs['utilisateur']) || trim($pairs['utilisateur']) == '') {
174
	    exit('0');
1636 raphael 175
	}
176
 
1929 raphael 177
	$id_utilisateur = intval($pairs['utilisateur']);
178
	$this->id_utilisateur = $id_utilisateur; // pour traiterImage();
1649 raphael 179
 
1929 raphael 180
	if(!isset($_SESSION)) session_start();
181
	$this->controleUtilisateur($id_utilisateur);
1640 raphael 182
 
1929 raphael 183
	$this->utilisateur = $this->getInfosComplementairesUtilisateur($id_utilisateur);
2034 aurelien 184
 
1636 raphael 185
 
2034 aurelien 186
 
1929 raphael 187
	$this->initialiser_colonnes_statiques($id_utilisateur);
1649 raphael 188
 
1929 raphael 189
	// initialisation du statement PDO/MySQL
190
	// première version, pattern de requête pas génial
191
	/* list(self;;$insert_prefix_ordre, self::$insert_ligne_pattern_ordre) =
192
	   $this->initialiser_pdo_ordered_statements($this->colonnes_statiques); */
193
	list(self::$insert_prefix, self::$insert_ligne_pattern) =
194
	    $this->initialiser_pdo_statements($this->colonnes_statiques);
1636 raphael 195
 
1929 raphael 196
	$infos_fichier = array_pop($_FILES);
1636 raphael 197
 
1929 raphael 198
	/*$objPHPExcel = PHPExcel_IOFactory::load($infos_fichier['tmp_name']);
199
	  $donnees = $objPHPExcel->getActiveSheet()->toArray(NULL,FALSE,FALSE,TRUE);*/
1636 raphael 200
 
1929 raphael 201
	/*$objReader = PHPExcel_IOFactory::createReader("Excel5");
202
	  $objReader->setReadDataOnly(true);
203
	  $objPHPExcel = $objReader->load($infos_fichier['tmp_name']);*/
1636 raphael 204
 
1929 raphael 205
	//var_dump($donnees);
1636 raphael 206
 
1929 raphael 207
	// renomme le fichier pour lui ajouter son extension initiale, ce qui
208
	// permet (une sorte) d'autodétection du format.
209
	$fichier = $infos_fichier['tmp_name'];
210
	$extension = pathinfo($infos_fichier['name'], PATHINFO_EXTENSION);
211
	if( (strlen($extension) == 3 || strlen($extension) == 4) &&
212
	    (@rename($fichier, $fichier . '.' . $extension))) { // XXX: @ safe-mode
213
	    $fichier = $fichier . '.' . $extension;
214
	}
1642 raphael 215
 
1929 raphael 216
	$objReader = PHPExcel_IOFactory::createReaderForFile($fichier);
217
	// TODO: check if compatible with toArray(<1>,<2>,TRUE,<4>)
218
	$objReader->setReadDataOnly(true);
1640 raphael 219
 
1929 raphael 220
	// TODO: is_a obsolete entre 5.0 et 5.3, retirer le @ à terme
1933 raphael 221
	$IS_CSV = @is_a($objReader, 'PHPExcel_Reader_CSV') && QUICK_CSV_IMPORT;
222
	// en cas d'usage de fgetcsv, testons que nous pouvons compter les lignes
223
	if($IS_CSV) $nb_lignes = intval(exec("wc -l $fichier"));
224
	// et, le cas échéant, fallback sur PHPExcel à nouveau. La raison de ce test ici est
225
	// l'instabilité du serveur (safe_mode, safe_mode_exec_dir, symlink vers binaires pour exec(), ... multiples points-of-failure)
226
	if($IS_CSV && !$nb_lignes) $IS_CSV = FALSE;
227
 
228
	if($IS_CSV) {
1929 raphael 229
	    $objReader->setDelimiter(',')
230
		->setEnclosure('"')
231
		->setLineEnding("\n")
232
		->setSheetIndex(0);
233
	}
1642 raphael 234
 
1929 raphael 235
	// on ne conserve que l'en-tête
236
	$filtre = new MyReadFilter();
237
	$filtre->def_interval(1, 2);
238
	$objReader->setReadFilter($filtre);
1640 raphael 239
 
1929 raphael 240
	$objPHPExcel = $objReader->load($fichier);
241
	$obj_infos = $objReader->listWorksheetInfo($fichier);
1636 raphael 242
 
1933 raphael 243
	if($IS_CSV) {
244
	    // $nb_lignes est déjà défini ci-dessus
245
	    $csvFileHandler = fopen($fichier, 'r');
246
	    // nous utilisons la valeur de retour dans un but informatif de l'utilisateur à la
247
	    // fin de l'import, *mais aussi* dans un array_diff_key() ci-dessous car bien que dans le
248
	    // fond le "parser" fgetcsv() n'ait pas d'intérêt à connaître les colonnes à ignorer,
249
	    // il se trouve que celles-ci peuvent interférer sur des fonctions comme traiterEspece()
250
	    // cf test "ref-nom-num.test.php" pour lequel l'élément C_NOM_SEL vaudrait 3 et $ligne serait array(3 => -42)
251
	    $filtre->exclues = self::detectionEntete(fgetcsv($csvFileHandler), TRUE);
252
	} else {
253
	    // XXX: indépendant du readFilter ?
254
	    $nb_lignes = $obj_infos[0]['totalRows'];
255
	    $donnees = $objPHPExcel->getActiveSheet()->toArray(NULL, FALSE, TRUE, TRUE);
256
	    $filtre->exclues = self::detectionEntete($donnees[1]);
257
	}
1636 raphael 258
 
1929 raphael 259
	$obs_ajouts = 0;
260
	$obs_maj = 0;
261
	$nb_images_ajoutees = 0;
262
	$nb_mots_cle_ajoutes = 0;
2381 aurelien 263
	$nb_champs_etendus_inseres = 0;
1677 raphael 264
 
1929 raphael 265
	$dernier_ordre = Cel::db()->requeter("SELECT MAX(ordre) AS ordre FROM cel_obs WHERE ce_utilisateur = $id_utilisateur");
266
	$dernier_ordre = intval($dernier_ordre[0]['ordre']) + 1;
267
	if(! $dernier_ordre) $dernier_ordre = 0;
1640 raphael 268
 
1929 raphael 269
	// on catch to les trigger_error(E_USER_NOTICE);
270
	set_error_handler(array($this, 'erreurs_stock'), E_USER_NOTICE);
1933 raphael 271
	$this->taxon_info_webservice = new RechercheInfosTaxonBeta($this->config, NULL);
1642 raphael 272
 
1933 raphael 273
 
1929 raphael 274
	// lecture par morceaux (chunks), NB_LIRE_LIGNE_SIMUL lignes à fois
275
	// pour aboutir des requêtes SQL d'insert groupés.
276
	for($ligne = 2; $ligne < $nb_lignes + NB_LIRE_LIGNE_SIMUL; $ligne += NB_LIRE_LIGNE_SIMUL) {
1933 raphael 277
	    if(!$IS_CSV) {
278
		$filtre->def_interval($ligne, NB_LIRE_LIGNE_SIMUL);
279
		$objReader->setReadFilter($filtre);
1640 raphael 280
 
1933 raphael 281
		/* recharge avec $filtre actif (filtre sur lignes colonnes):
282
		   - exclue les colonnes inutiles/inutilisables)
283
		   - ne selectionne que les lignes dans le range [$ligne - $ligne + NB_LIRE_LIGNE_SIMUL] */
284
		$objPHPExcel = $objReader->load($fichier)->getActiveSheet();
1640 raphael 285
 
1933 raphael 286
		// set col typing
287
		if(C_CE_ZONE_GEO != 'C_CE_ZONE_GEO')
288
		    $objPHPExcel->getStyle(C_CE_ZONE_GEO . '2:' . C_CE_ZONE_GEO . $objPHPExcel->getHighestRow())->getNumberFormat()->setFormatCode('00000');
1818 raphael 289
 
1933 raphael 290
		// TODO: set to string type
291
		if(C_ZONE_GEO != 'C_ZONE_GEO')
292
		    $objPHPExcel->getStyle(C_ZONE_GEO . '2:' . C_ZONE_GEO . $objPHPExcel->getHighestRow())->getNumberFormat()->setFormatCode('00000');
1818 raphael 293
 
1933 raphael 294
		$donnees = $objPHPExcel->toArray(NULL, FALSE, TRUE, TRUE);
295
	    }
296
	    else {
297
		$i = NB_LIRE_LIGNE_SIMUL;
298
		$donnees = array();
299
		while($i--) {
300
		    $tab = fgetcsv($csvFileHandler);
301
		    if(!$tab) continue;
302
		    $donnees[] = array_diff_key($tab, $filtre->exclues);
303
		}
1818 raphael 304
 
1933 raphael 305
	    }
306
 
307
	    // var_dump($donnees, get_defined_constants(true)['user']);die;
1929 raphael 308
	    // ici on appel la fonction qui fera effectivement l'insertion multiple
309
	    // à partir des (au plus) NB_LIRE_LIGNE_SIMUL lignes
1640 raphael 310
 
1929 raphael 311
	    // TODO: passer $this, ne sert que pour appeler des méthodes publiques qui pourraient être statiques
2381 aurelien 312
	    list($enregistrements, $images, $mots_cle, $champs_etendus) =
1929 raphael 313
		self::chargerLignes($this, $donnees, $this->colonnes_statiques, $dernier_ordre);
314
	    if(! $enregistrements) break;
1642 raphael 315
 
1929 raphael 316
	    self::trierColonnes($enregistrements);
317
	    // normalement: NB_LIRE_LIGNE_SIMUL, sauf si une enregistrement ne semble pas valide
318
	    // ou bien lors du dernier chunk
1648 raphael 319
 
1929 raphael 320
	    $nb_rec = count($enregistrements);
321
	    $sql_pattern = self::$insert_prefix .
322
		str_repeat(self::$insert_ligne_pattern_ordre . ', ', $nb_rec - 1) .
323
		self::$insert_ligne_pattern_ordre;
1648 raphael 324
 
1929 raphael 325
	    $sql_pattern = self::$insert_prefix .
326
		str_repeat(self::$insert_ligne_pattern . ', ', $nb_rec - 1) .
327
		self::$insert_ligne_pattern;
1648 raphael 328
 
1929 raphael 329
	    Cel::db()->beginTransaction();
330
	    $stmt = Cel::db()->prepare($sql_pattern);
331
	    $donnees = array();
332
	    foreach($enregistrements as $e) $donnees = array_merge($donnees, array_values($e));
1648 raphael 333
 
2034 aurelien 334
	     // echo $sql_pattern . "\n"; var_dump($enregistrements, $donnees); die; // debug ici
1648 raphael 335
 
1929 raphael 336
	    $stmt->execute($donnees);
1648 raphael 337
 
1929 raphael 338
	    // $stmt->debugDumpParams(); // https://bugs.php.net/bug.php?id=52384
339
	    $dernier_autoinc = Cel::db()->lastInsertId();
340
	    Cel::db()->commit();
1648 raphael 341
 
1929 raphael 342
	    if(! $dernier_autoinc) trigger_error("l'insertion semble avoir échoué", E_USER_NOTICE);
1648 raphael 343
 
1929 raphael 344
	    $obs_ajouts += count($enregistrements);
345
	    // $obs_ajouts += count($enregistrements['insert']);
346
	    // $obs_maj += count($enregistrements['update']);
2034 aurelien 347
 
348
	    $ordre_ids = self::chargerCorrespondancesIdOrdre($this, $enregistrements);
349
 
350
	    $nb_images_ajoutees += self::stockerImages($enregistrements, $images, $ordre_ids);
1929 raphael 351
	    $nb_mots_cle_ajoutes += self::stockerMotsCle($enregistrements, $mots_cle, $dernier_autoinc);
2381 aurelien 352
	    $nb_champs_etendus_inseres += self::stockerChampsEtendus($champs_etendus, $ordre_ids, $this->config);
1929 raphael 353
	}
1642 raphael 354
 
1929 raphael 355
	restore_error_handler();
1642 raphael 356
 
1929 raphael 357
	if($this->bilan) echo implode("\n", $this->bilan) . "\n";
358
	printf('%1$d observation%2$s ajoutée%2$s' . "\n" .
359
	       '%3$d image%4$s attachée%4$s' . "\n" .
360
	       // '%5$d mot%6$c-clef ajouté%6$c [TODO]' . "\n" . // TODO
361
	       (count($filtre->exclues) > 0 ? 'colonne%7$s non-traitée%7$s: %8$s' . "\n" : ''),
1792 raphael 362
 
1929 raphael 363
	       $obs_ajouts,
364
	       $obs_ajouts > 1 ? 's' : '',
365
	       $nb_images_ajoutees,
366
	       $nb_images_ajoutees > 1 ? 's' : '',
367
	       $nb_mots_cle_ajoutes,
368
	       $nb_mots_cle_ajoutes > 1 ? 's' : '',
369
	       count($filtre->exclues) > 1 ? 's' : '',
370
	       implode(', ', $filtre->exclues));
371
	die();
372
    }
1636 raphael 373
 
1933 raphael 374
    /* detectionEntete() sert deux rôles:
375
       1) détecter le type de colonne attendu à partir des textes de la ligne d'en-tête afin de define()
376
       2) permet d'identifier les colonnes non-supportées/inutiles afin d'alléger le processus de parsing de PHPExcel
377
       grace au ReadFilter (C'est le rôle de la valeur de retour)
378
 
379
       La raison de la présence du paramètre $numeric_keys est que pour réussir à identifier les colonnes à exclure nous
380
       devons traiter un tableau représentant la ligne d'en-tête aussi bien:
381
       - sous forme associative pour PHPExcel (les clefs sont les lettres de l'alphabet)
382
       - sous forme de clefs numériques (fgetcsv())
383
       Le détecter après coup est difficile et pourtant cette distinction est importante car le comportement
384
       d'array_merge() (réordonnancement des clefs numérique) n'est pas souhaitable dans le second cas. */
385
    static function detectionEntete($entete, $numeric_keys = FALSE) {
1929 raphael 386
	$colonnes_reconnues = Array();
387
	$cols = FormateurGroupeColonne::nomEnsembleVersListeColonnes('standard,avance');
2381 aurelien 388
 
1929 raphael 389
	foreach($entete as $k => $v) {
390
	    // traite les colonnes en faisant fi de la casse et des accents
391
	    $entete_simple = iconv('UTF-8', 'ASCII//TRANSLIT', strtolower(trim($v)));
392
	    foreach($cols as $col) {
2381 aurelien 393
			$entete_officiel_simple = iconv('UTF-8', 'ASCII//TRANSLIT', strtolower(trim($col['nom'])));
394
			$entete_officiel_abbrev = $col['abbrev'];
395
			if($entete_simple == $entete_officiel_simple || $entete_simple == $entete_officiel_abbrev) {
396
			    // debug echo "define C_" . strtoupper($entete_officiel_abbrev) . ", $k ($v)\n";
397
			    define("C_" . strtoupper($entete_officiel_abbrev), $k);
398
			    $colonnes_reconnues[$k] = 1;
399
			    break;
400
			}
401
 
402
			if(strpos($v, self::$prefixe_colonnes_etendues) === 0) {
403
				$colonnes_reconnues[$k] = 1;
404
				self::$indexes_colonnes_etendues[$k] = $v;
405
				break;
406
			}
1929 raphael 407
	    }
408
	}
2381 aurelien 409
 
1929 raphael 410
	// défini tous les index que nous utilisons à une valeur d'index de colonne Excel qui n'existe pas dans
411
	// le tableau renvoyé par PHPExcel
412
	// Attention cependant d'utiliser des indexes différenciés car traiterLonLat() et traiterEspece()
413
	// les utilisent
414
	foreach($cols as $col) {
415
	    if(!defined("C_" . strtoupper($col['abbrev'])))
416
		define("C_" . strtoupper($col['abbrev']), "C_" . strtoupper($col['abbrev']));
417
	}
1636 raphael 418
 
1929 raphael 419
	// prépare le filtre de PHPExcel qui évitera le traitement de toutes les colonnes superflues
1640 raphael 420
 
1929 raphael 421
	// eg: diff ( Array( H => Commune, I => rien ) , Array( H => 1, K => 1 )
422
	// ==> Array( I => rien )
423
	$colonnesID_non_reconnues = array_diff_key($entete, $colonnes_reconnues);
1636 raphael 424
 
1929 raphael 425
	// des colonnes de FormateurGroupeColonne::nomEnsembleVersListeColonnes()
426
	// ne retient que celles marquées "importables"
427
	$colonnes_automatiques = array_filter($cols, '__anonyme_1');
1640 raphael 428
 
1929 raphael 429
	// ne conserve que le nom long pour matcher avec la ligne XLS d'entête
430
	array_walk($colonnes_automatiques, '__anonyme_2');
1640 raphael 431
 
1929 raphael 432
	// intersect ( Array ( N => Milieu, S => Ordre ), Array ( ordre => Ordre, phenologie => Phénologie ) )
433
	// ==> Array ( S => Ordre, AA => Phénologie )
434
	$colonnesID_a_exclure = array_intersect($entete, $colonnes_automatiques);
1636 raphael 435
 
1933 raphael 436
	if($numeric_keys) {
437
	    return $colonnesID_non_reconnues + $colonnesID_a_exclure;
438
	}
1929 raphael 439
	// TODO: pourquoi ne pas comparer avec les abbrevs aussi ?
440
	// merge ( Array( I => rien ) , Array ( S => Ordre, AA => Phénologie ) )
441
	// ==> Array ( I => rien, AA => Phénologie )
442
	return array_merge($colonnesID_non_reconnues, $colonnesID_a_exclure);
443
    }
2034 aurelien 444
 
445
    static function chargerCorrespondancesIdOrdre($cel, $lignes) {
446
 
447
    	$ordre_ids = array();
448
 
449
    	$requete_obs_ids = "SELECT id_observation, ordre FROM cel_obs WHERE ordre IN (";
450
    	foreach($lignes as &$ligne) {
451
    		$requete_obs_ids .= $ligne['ordre'].',';
452
    	}
453
    	$requete_obs_ids = rtrim($requete_obs_ids, ',');
454
    	$requete_obs_ids .= ") AND ce_utilisateur = ".Cel::db()->proteger($cel->id_utilisateur);
455
 
456
 
457
    	$obs_ids = Cel::db()->requeter($requete_obs_ids);
458
    	foreach($obs_ids as &$obs) {
459
    		$ordre_ids[$obs['ordre']] = $obs['id_observation'];
460
    	}
461
    	return $ordre_ids;
462
    }
1636 raphael 463
 
1929 raphael 464
    /*
465
     * charge un groupe de lignes
466
     */
467
    static function chargerLignes($cel, $lignes, $colonnes_statiques, &$dernier_ordre) {
468
	$enregistrement = NULL;
469
	$enregistrements = Array();
470
	$toutes_images = Array();
471
	$tous_mots_cle = Array();
2381 aurelien 472
	$tous_champs_etendus = array();
1640 raphael 473
 
1929 raphael 474
	foreach($lignes as $ligne) {
2381 aurelien 475
 
1933 raphael 476
	    // dans le cas de fgetcsv, on peut avoir des false additionnel (cf do/while l. 279)
477
	    if($ligne === false) continue;
478
 
1929 raphael 479
	    //$ligne = array_filter($ligne, function($cell) { return !is_null($cell); });
480
	    //if(!$ligne) continue;
481
	    // on a besoin des NULL pour éviter des notice d'index indéfini
482
	    if(! array_filter($ligne, '__anonyme_3')) continue;
1640 raphael 483
 
1929 raphael 484
	    if( ($enregistrement = self::chargerLigne($ligne, $dernier_ordre, $cel)) ) {
485
		// $enregistrements[] = array_merge($colonnes_statiques, $enregistrement);
486
		$enregistrements[] = $enregistrement;
487
		$pos = count($enregistrements) - 1;
488
		$last = &$enregistrements[$pos];
1640 raphael 489
 
1929 raphael 490
		if(isset($enregistrement['_images'])) {
491
		    // ne dépend pas de cel_obs, et seront insérées *après* les enregistrements
492
		    // mais nous ne voulons pas nous priver de faire des INSERT multiples pour autant
493
		    $toutes_images[] = Array("images" => $last['_images'],
494
					     "obs_pos" => $pos);
495
		    // ce champ n'a pas à faire partie de l'insertion dans cel_obs,
496
		    // mais est utile pour cel_obs_images
497
		    unset($last['_images']);
498
		}
1640 raphael 499
 
1929 raphael 500
		if(isset($enregistrement['_mots_cle'])) {
501
		    // ne dépend pas de cel_obs, et seront insérés *après* les enregistrements
502
		    // mais nous ne voulons pas nous priver de faire des INSERT multiples pour autant
503
		    $tous_mots_cle[] = Array("mots_cle" => $last['_mots_cle'],
504
					     "obs_pos" => $pos);
505
		    // la version inlinée des mots est enregistrées dans cel_obs
506
		    // mais cel_mots_cles_obs fait foi.
507
		    // XXX: postponer l'ajout de ces informations dans cel_obs *après* l'insertion effective
508
		    // des records dans cel_mots_cles_obs ?
509
		    unset($last['_mots_cle']);
1636 raphael 510
		}
1640 raphael 511
 
2381 aurelien 512
		if(isset($enregistrement['_champs_etendus'])) {
513
			$tous_champs_etendus[] = Array("champs_etendus" => $last['_champs_etendus'],
514
					"ordre" => $dernier_ordre);
515
			unset($last['_champs_etendus']);
516
		}
1929 raphael 517
		$dernier_ordre++;
518
	    }
1642 raphael 519
	}
1640 raphael 520
 
1929 raphael 521
	// XXX future: return Array($enregistrements_a_inserer, $enregistrements_a_MAJ, $toutes_images);
2381 aurelien 522
	return Array($enregistrements, $toutes_images, $tous_mots_cle, $tous_champs_etendus);
1929 raphael 523
    }
1642 raphael 524
 
525
 
1929 raphael 526
    static function trierColonnes(&$enregistrements) {
527
	foreach($enregistrements as &$enregistrement) {
528
	    $enregistrement = self::sortArrayByArray($enregistrement, self::$ordre_BDD);
529
	    //array_walk($enregistrement, function(&$item, $k) { $item = is_null($item) ? "NULL" : $item; });
530
	    //$req .= implode(', ', $enregistrement) . "\n";
1677 raphael 531
	}
1929 raphael 532
    }
1677 raphael 533
 
1642 raphael 534
 
1929 raphael 535
    static function stockerMotsCle($enregistrements, $tous_mots_cle, $lastid) {
536
	$c = 0;
537
	// debug: var_dump($tous_mots_cle);die;
538
	foreach($tous_mots_cle as $v) $c += count($v['mots_cle']['to_insert']);
539
	return $c;
540
    }
1642 raphael 541
 
2034 aurelien 542
    static function stockerImages($enregistrements, $toutes_images, $ordre_ids) {
1929 raphael 543
	$images_insert = 'INSERT INTO cel_obs_images (id_image, id_observation) VALUES %s ON DUPLICATE KEY UPDATE id_image = id_image';
544
	$images_obs_assoc = Array();
1650 raphael 545
 
1929 raphael 546
	foreach($toutes_images as $images_pour_obs) {
547
	    $obs = $enregistrements[$images_pour_obs["obs_pos"]];
2034 aurelien 548
	    $id_obs = $ordre_ids[$obs['ordre']]; // id réel de l'observation correspondant à l'ordre
1929 raphael 549
	    foreach($images_pour_obs['images'] as $image) {
550
		$images_obs_assoc[] = sprintf('(%d,%d)',
551
					      $image['id_image'], // intval() useless
552
					      $id_obs); // intval() useless
553
	    }
1636 raphael 554
	}
555
 
1929 raphael 556
	if($images_obs_assoc) {
557
	    $requete = sprintf($images_insert, implode(', ', $images_obs_assoc));
558
	    // debug echo "$requete\n";
559
	    Cel::db()->requeter($requete);
560
	}
1770 raphael 561
 
1929 raphael 562
	return count($images_obs_assoc);
563
    }
1636 raphael 564
 
1929 raphael 565
    /*
566
      Aucune des valeurs présentes dans $enregistrement n'est quotée
567
      cad aucune des valeurs retournée par traiter{Espece|Localisation}()
568
      car ce tableau est passé à un PDO::preparedStatement() qui applique
569
      proprement les règle d'échappement.
570
    */
571
    static function chargerLigne($ligne, $dernier_ordre, $cel) {
572
	// évite des notices d'index lors des trigger_error()
573
	$ref_ligne = !empty($ligne[C_NOM_SEL]) ? trim($ligne[C_NOM_SEL]) : '';
1636 raphael 574
 
1929 raphael 575
	// en premier car le résultat est utile pour
576
	// * traiter espèce (traiterEspece())
577
	// * traiter longitude et latitude (traiterLonLat())
578
	$referentiel = self::identReferentiel(trim(strtolower(@$ligne[C_NOM_REFERENTIEL])), $ligne, $ref_ligne);
1852 raphael 579
 
1929 raphael 580
	// $espece est rempli de plusieurs informations
581
	$espece = Array(C_NOM_SEL => NULL, C_NOM_SEL_NN => NULL, C_NOM_RET => NULL,
582
			C_NOM_RET_NN => NULL, C_NT => NULL, C_FAMILLE => NULL);
583
	self::traiterEspece($ligne, $espece, $referentiel, $cel->taxon_info_webservice);
1636 raphael 584
 
1929 raphael 585
	if(!$espece[C_NOM_SEL]) $referentiel = Cel::$fallback_referentiel;
586
	if($espece[C_NOM_SEL] && !$espece[C_NOM_SEL_NN]) $referentiel = Cel::$fallback_referentiel;
1649 raphael 587
 
1929 raphael 588
	// $localisation est rempli à partir de plusieurs champs: C_ZONE_GEO et C_CE_ZONE_GEO
589
	$localisation = Array(C_ZONE_GEO => NULL, C_CE_ZONE_GEO => NULL);
590
	self::traiterLocalisation($ligne, $localisation);
1649 raphael 591
 
1929 raphael 592
	// $transmission est utilisé pour date_transmission
593
	// XXX: @ contre "Undefined index"
594
	@$transmission = in_array(strtolower(trim($ligne[C_TRANSMISSION])), array(1, 'oui')) ? 1 : 0;
1640 raphael 595
 
596
 
1929 raphael 597
	// Dans ce tableau, seules devraient apparaître les données variable pour chaque ligne.
598
	// Dans ce tableau, l'ordre des clefs n'importe pas (cf: self::sortArrayByArray())
599
	$enregistrement = Array(
600
	    "ordre" => $dernier_ordre,
1640 raphael 601
 
1929 raphael 602
	    "nom_sel" => $espece[C_NOM_SEL],
603
	    "nom_sel_nn" => $espece[C_NOM_SEL_NN],
604
	    "nom_ret" => $espece[C_NOM_RET],
605
	    "nom_ret_nn" => $espece[C_NOM_RET_NN],
606
	    "nt" => $espece[C_NT],
607
	    "famille" => $espece[C_FAMILLE],
1640 raphael 608
 
1929 raphael 609
	    "nom_referentiel" => $referentiel,
1640 raphael 610
 
1929 raphael 611
	    "zone_geo" => $localisation[C_ZONE_GEO],
612
	    "ce_zone_geo" => $localisation[C_CE_ZONE_GEO],
1642 raphael 613
 
1929 raphael 614
	    // $ligne: uniquement pour les infos en cas de gestion d'erreurs (date incompréhensible)
615
	    "date_observation" => isset($ligne[C_DATE_OBSERVATION]) ? self::traiterDateObs($ligne[C_DATE_OBSERVATION], $ref_ligne) : NULL,
1642 raphael 616
 
1929 raphael 617
	    "lieudit" => isset($ligne[C_LIEUDIT]) ? trim($ligne[C_LIEUDIT]) : NULL,
618
	    "station" => isset($ligne[C_STATION]) ? trim($ligne[C_STATION]) : NULL,
619
	    "milieu" => isset($ligne[C_MILIEU]) ? trim($ligne[C_MILIEU]) : NULL,
1642 raphael 620
 
1929 raphael 621
	    "mots_cles_texte" => NULL, // TODO: foreign-key
622
	    // XXX: @ contre "Undefined index"
623
	    "commentaire" => isset($ligne[C_COMMENTAIRE]) ? trim($ligne[C_COMMENTAIRE]) : NULL,
1648 raphael 624
 
1929 raphael 625
	    "transmission" => $transmission,
626
	    "date_transmission" => $transmission ? date("Y-m-d H:i:s") : NULL, // pas de fonction SQL dans un PDO statement, <=> now()
1648 raphael 627
 
1929 raphael 628
	    // $ligne: uniquement pour les infos en cas de gestion d'erreurs (lon/lat incompréhensible)
629
	    "latitude" => isset($ligne[C_LATITUDE]) ? self::traiterLonLat(NULL, $ligne[C_LATITUDE], $referentiel, $ref_ligne) : NULL,
630
	    "longitude" => isset($ligne[C_LONGITUDE]) ? self::traiterLonLat($ligne[C_LONGITUDE], NULL, $referentiel, $ref_ligne) : NULL,
631
	    "altitude" => isset($ligne[C_ALTITUDE]) ? intval($ligne[C_ALTITUDE]) : NULL, // TODO: guess alt from lon/lat
1642 raphael 632
 
1929 raphael 633
	    // @ car potentiellement optionnelles ou toutes vides => pas d'index dans PHPExcel (tableau optimisé)
634
	    "abondance" => @$ligne[C_ABONDANCE],
635
	    "certitude" => @$ligne[C_CERTITUDE],
636
	    "phenologie" => @$ligne[C_PHENOLOGIE],
1642 raphael 637
 
1929 raphael 638
	    "code_insee_calcule" => substr($localisation[C_CE_ZONE_GEO], -5) // varchar(5)
639
	);
1677 raphael 640
 
1929 raphael 641
	// passage de $enregistrement par référence, ainsi ['_images'] n'est défini
642
	// que si des résultats sont trouvés
643
	// "@" car PHPExcel supprime les colonnes null sur toute la feuille (ou tout le chunk)
644
	if(@$ligne[C_IMAGES]) self::traiterImage($ligne[C_IMAGES], $cel->id_utilisateur, $enregistrement);
1636 raphael 645
 
1929 raphael 646
	if(@$ligne[C_MOTS_CLES_TEXTE]) self::traiterMotsCle($ligne[C_MOTS_CLES_TEXTE], $cel->id_utilisateur, $enregistrement);
2381 aurelien 647
 
648
	$champs_etendus = self::traiterChampsEtendus($ligne, self::$indexes_colonnes_etendues);
649
	if(!empty($champs_etendus)) {
650
		$enregistrement["_champs_etendus"] = $champs_etendus;
651
	}
1675 raphael 652
 
1929 raphael 653
	return $enregistrement;
654
    }
2381 aurelien 655
 
656
    static function traiterChampsEtendus(&$ligne, &$indexes_colonnes_etendues) {
657
    	$champs_etendus_indexes = array();
658
    	foreach($indexes_colonnes_etendues as $index_num => $label) {
659
    		if(isset($ligne[$index_num])) {
660
    			$champs_etendus_indexes[str_replace(self::$prefixe_colonnes_etendues, '', $label)] = $ligne[$index_num];
661
    		}
662
    	}
663
    	return $champs_etendus_indexes;
664
    }
1640 raphael 665
 
1929 raphael 666
    static function traiterImage($str, $id_utilisateur, &$enregistrement) {
667
	$liste_images = array_filter(explode("/", $str));
1640 raphael 668
 
1929 raphael 669
	//array_walk($liste_images, '__anonyme_4');
670
	array_walk($liste_images, array(__CLASS__, '__anonyme_4'));
671
	$requete = sprintf(
672
	    "SELECT id_image, nom_original FROM cel_images WHERE ce_utilisateur = %d AND nom_original IN (%s)",
673
	    $id_utilisateur,
674
	    implode(',', $liste_images));
1642 raphael 675
 
1929 raphael 676
	$resultat = Cel::db()->requeter($requete);
1678 raphael 677
 
1929 raphael 678
	if($resultat) $enregistrement['_images'] = $resultat;
679
    }
1642 raphael 680
 
1929 raphael 681
    static function traiterMotsCle($str, $id_utilisateur, &$enregistrement) {
682
	$liste_mots_cle = $liste_mots_cle_recherche = array_map("trim", array_unique(array_filter(explode(",", $str))));
683
	array_walk($liste_mots_cle_recherche, array(__CLASS__, '__anonyme_4'));
1677 raphael 684
 
2055 aurelien 685
	if(self::$gestion_mots_cles == null) {
686
		$gestion_mots_cles = new GestionMotsCles($this->config, 'obs');
687
	}
688
	$mots_cles_ids = $gestion_mots_cles->obtenirIdsMotClesPourMotsCles($liste_mots_cle, $id_utilisateur);
689
	foreach($mots_cles_ids as $mot_cle) {
690
		$resultat[$mot_cle['id_mot_cle']] = $mot_cle['mot_cle'];
691
	}
1677 raphael 692
 
1929 raphael 693
	$enregistrement['mots_cles_texte'] = implode(',', $liste_mots_cle);
694
	$enregistrement['_mots_cle'] = array("existing" => $resultat,
695
					     "to_insert" => array_diff($liste_mots_cle, $resultat));
696
    }
1640 raphael 697
 
1770 raphael 698
 
1929 raphael 699
    /* FONCTIONS de TRANSFORMATION de VALEUR DE CELLULE */
1640 raphael 700
 
1929 raphael 701
    // TODO: PHP 5.3, utiliser date_parse_from_format()
702
    // TODO: parser les heures (cf product-owner)
703
    // TODO: passer par le timestamp pour s'assurer de la validité
704
    static function traiterDateObs($date, $ref_ligne) {
705
	// TODO: see https://github.com/PHPOffice/PHPExcel/issues/208
706
	// TODO: PHPExcel_Shared_Date::ExcelToPHP()
707
	if(is_double($date)) {
708
	    if($date > 0)
709
		return PHPExcel_Style_NumberFormat::toFormattedString($date, PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2) . " 00:00:00";
710
	    trigger_error("ligne \"{$ref_ligne}\": " .
711
			  "Attention: date antérieure à 1970 et format de cellule \"DATE\" utilisés ensemble",
712
			  E_USER_NOTICE);
1640 raphael 713
 
1929 raphael 714
	    // throw new Exception("erreur: date antérieure à 1970 et format de cellule \"DATE\" utilisés ensemble");
1640 raphael 715
 
1929 raphael 716
	    // attention, UNIX timestamp, car Excel les décompte depuis 1900
717
	    // cf http://fczaja.blogspot.fr/2011/06/convert-excel-date-into-timestamp.html
718
	    // $timestamp = ($date - MIN_DATES_DIFF) * 60 * 60 * 24 - time(); // NON
1636 raphael 719
 
1929 raphael 720
	    // $timestamp = PHPExcel_Calculation::getInstance()->calculateFormula("=" . $date . "-DATE(1970,1,1)*60*60*24"); // NON
1642 raphael 721
 
1929 raphael 722
	    // echo strftime("%Y/%m/%d 00:00:00", $timestamp); // NON
723
	}
724
	else {
725
	    // attend l'un des formats de
726
	    // http://www.php.net/manual/fr/datetime.formats.date.php
727
	    // le plus simple: YYYY/MM/DD (utilisé à l'export), mais DD-MM-YYYY est aussi supporté
728
	    $matches = NULL;
729
	    // et on essaie d'être sympa et supporter aussi DD/MM/YYYY
730
	    if(preg_match(';^([0-3]?\d)/([01]\d)/([12]\d\d\d)$;', $date, $matches)) {
731
		$date = $matches[3] . '/' . $matches[2] . '/' . $matches[1];
732
	    }
733
	    $timestamp = strtotime($date);
734
	    if(! $timestamp || $timestamp > time() + 3600 * 24 * 1) { // une journée d'avance maxi autorisée (décallage horaire ?)
735
		if($date) trigger_error("ligne \"{$ref_ligne}\": Attention: date erronée ($date)", E_USER_NOTICE);
1640 raphael 736
		return NULL;
1929 raphael 737
	    }
738
	    return strftime("%Y-%m-%d 00:00:00", $timestamp);
1636 raphael 739
	}
1929 raphael 740
    }
1636 raphael 741
 
1929 raphael 742
    static function identReferentiel($referentiel, $ligne, $ref_ligne) {
743
	// SELECT DISTINCT nom_referentiel, COUNT(id_observation) AS count FROM cel_obs GROUP BY nom_referentiel ORDER BY count DESC;
744
	if(strpos($referentiel, 'bdtfx') !== FALSE) return 'bdtfx'; //:v1.01';
745
	if(strpos($referentiel, 'bdtxa') !== FALSE) return 'bdtxa'; //:v1.00';
746
	//if(strpos($referentiel, 'bdnff') !== FALSE) return 'bdnff'; //:4.02';
747
	if(strpos($referentiel, 'bdnff') !== FALSE) return 'bdtfx';
748
	if(strpos($referentiel, 'isfan') !== FALSE) return 'isfan'; //:v1.00';
2155 mathias 749
	if(strpos($referentiel, 'apd') !== FALSE) return 'apd'; //:v1.00';
1929 raphael 750
	if(strpos($referentiel, 'autre') !== FALSE) return 'autre';
1640 raphael 751
 
1929 raphael 752
	if($referentiel && isset($ligne[C_NOM_SEL]) && $ligne[C_NOM_SEL]) {
753
	    trigger_error("ligne \"{$ref_ligne}\": Attention: référentiel \"{$referentiel}\" inconnu", E_USER_NOTICE);
754
	    return 'autre';
755
	}
1642 raphael 756
 
1929 raphael 757
	// pas de référentiel ou pas de NOM_SEL: NULL
758
	return NULL;
759
	/* TODO: cf story,
760
	   En cas de NULL faire une seconde passe de détection à partir du nom saisi
761
	   + accepter les n° de version */
762
    }
1642 raphael 763
 
1929 raphael 764
    static function traiterLonLat($lon = NULL, $lat = NULL, $referentiel = 'bdtfx', $ref_ligne) {
765
	// en CSV ces valeurs sont des string, avec séparateur en français (","; cf défauts dans ExportXLS)
766
	if($lon && is_string($lon)) $lon = str_replace(',', '.', $lon);
767
	if($lat && is_string($lat)) $lat = str_replace(',', '.', $lat);
1640 raphael 768
 
1929 raphael 769
	// sprintf applique une précision à 5 décimale (comme le ferait MySQL)
770
	// tout en uniformisant le format de séparateur des décimales (le ".")
771
	if($lon && is_numeric($lon) && $lon >= -180 && $lon <= 180) return sprintf('%.5F', $lon);
772
	if($lat && is_numeric($lat) && $lat >= -90 && $lat <= 90) return sprintf('%.5F', $lat);
773
 
774
	if($lon || $lat) {
775
	    trigger_error("ligne \"{$ref_ligne}\": " .
776
			  "Attention: longitude ou latitude erronée",
777
			  E_USER_NOTICE);
1636 raphael 778
	}
1929 raphael 779
	return NULL;
1636 raphael 780
 
1929 raphael 781
	/* limite france métropole si bdtfx ? ou bdtxa ? ...
782
	   NON!
783
	   Un taxon d'un référentiel donné peut être théoriquement observé n'importe où sur le globe.
784
	   Il n'y a pas lieu d'effectuer des restriction ici.
785
	   Cependant des erreurs fréquentes (0,0 ou lon/lat inversées) peuvent être détectés ici.
786
	   TODO */
787
	$bbox = self::getReferentielBBox($referentiel);
788
	if(!$bbox) return NULL;
1642 raphael 789
 
1929 raphael 790
	if($lon) {
791
	    if($lon < $bbox['EST'] && $lon > $bbox['OUEST']) return is_numeric($lon) ? $lon : NULL;
792
	    else return NULL;
793
	}
794
	if($lat) {
795
	    if($lat < $bbox['NORD'] && $lat > $bbox['SUD']) return is_numeric($lat) ? $lat : NULL;
796
	    return NULL;
797
	}
798
    }
1651 raphael 799
 
1929 raphael 800
    /*
801
      TODO: s'affranchir du webservice pour la détermination du nom scientifique en s'appuyant sur cel_references,
802
      pour des questions de performances
803
    */
804
    static function traiterEspece($ligne, Array &$espece, &$referentiel, $taxon_info_webservice) {
805
	if(empty($ligne[C_NOM_SEL])) {
806
	    // TODO: nous ne déclarons pas "Numéro nomenclatural" comme colonne importable
807
	    // Nous ne pouvons donc pas tenter d'être sympa sur la détermination par num_nom
808
	    /* if(!empty($ligne[C_NOM_SEL_NN]) && $referentiel != Cel::$fallback_referentiel)
809
	       $ligne[C_NOM_SEL] = $referentiel . ':nn:' . $ligne[C_NOM_SEL_NN];
810
	       else */
811
	    return;
812
	}
1651 raphael 813
 
1929 raphael 814
	// nom_sel reste toujours celui de l'utilisateur
815
	$espece[C_NOM_SEL] = trim($ligne[C_NOM_SEL]);
1640 raphael 816
 
1929 raphael 817
	// XXX/attention, nous ne devrions pas accepter un référentiel absent !
818
	if(!$referentiel) $referentiel = 'bdtfx';
819
	$taxon_info_webservice->setReferentiel($referentiel);
820
	$ascii = iconv('UTF-8', 'ASCII//TRANSLIT', $ligne[C_NOM_SEL]);
1688 raphael 821
 
1929 raphael 822
	// TODO: si empty(C_NOM_SEL) et !empty(C_NOM_SEL_NN) : recherche info à partir de C_NOM_SEL_NN
823
	// echo "rechercherInformationsComplementairesSurNom()\n";
824
	/*
825
	  SELECT num_nom, nom_sci, num_nom_retenu ,auteur, annee, biblio_origine, nom_sci,auteur  FROM bdtfx_v1_01  WHERE (nom_sci LIKE 'Heliotropium europaeum')  ORDER BY nom_sci ASC	  LIMIT 0, 1
826
	  #
827
	  SELECT num_nom, nom_sci, num_nom_retenu ,auteur, annee, biblio_origine, nom_sci,auteur  FROM bdtfx_v1_01  WHERE (nom_sci LIKE 'eliotropium euro')  ORDER BY nom_sci ASC   LIMIT 0, 1
828
	  SELECT num_nom, nom_sci, num_nom_retenu ,auteur, annee, biblio_origine, nom_sci,auteur  FROM bdtfx_v1_01  WHERE (nom_sci LIKE 'eliotropium')	ORDER BY nom_sci ASC   LIMIT 0, 1
829
	  SELECT num_nom, nom_sci, num_nom_retenu ,auteur, annee, biblio_origine, nom_sci,auteur  FROM bdtfx_v1_01  WHERE (nom_sci LIKE 'eliotropium% euro%')  ORDER BY nom_sci ASC   LIMIT 0, 1
830
	  #
1640 raphael 831
 
1929 raphael 832
	  SELECT nom_sci, num_nom_retenu, nom_sci_html, auteur, annee, biblio_origine FROM bdtfx_v1_01 WHERE num_nom = 31468
833
	*/
834
	// $determ = $taxon_info_webservice->rechercherInformationsComplementairesSurNom($ligne[C_NOM_SEL]);
835
	// permet une reconnaissance de bdtfx:nn:XXXX
836
	$determ = $taxon_info_webservice->rechercherInfosSurTexteCodeOuNumTax(trim($ligne[C_NOM_SEL]));
1640 raphael 837
 
1929 raphael 838
	// note: rechercherInfosSurTexteCodeOuNumTax peut ne retourner qu'une seule clef "nom_sel"
839
	if (! $determ) {
840
	    // on supprime les noms retenus et renvoi tel quel
841
	    // on réutilise les define pour les noms d'indexes, tant qu'à faire
842
	    // XXX; tout à NULL sauf C_NOM_SEL ci-dessus ?
843
	    $espece[C_NOM_SEL_NN] = @$ligne[C_NOM_SEL_NN];
844
	    $espece[C_NOM_RET] = @$ligne[C_NOM_RET];
845
	    $espece[C_NOM_RET_NN] = @$ligne[C_NOM_RET_NN];
846
	    $espece[C_NT] = @$ligne[C_NT];
847
	    $espece[C_FAMILLE] = @$ligne[C_FAMILLE];
1781 raphael 848
 
1929 raphael 849
	    return;
850
	}
1781 raphael 851
 
1929 raphael 852
	// succès de la détection, mais résultat partiel
853
	if(!isset($determ->id))
854
	    $determ = $taxon_info_webservice->effectuerRequeteInfosComplementairesSurNumNom($determ->{"nom_retenu.id"});
1784 raphael 855
 
1929 raphael 856
	// ne devrait jamais arriver !
857
	if(!$determ) die("erreur critique: " . __FILE__ . ':' . __LINE__);
1784 raphael 858
 
1929 raphael 859
	// un schéma <ref>:(nt|nn):<num> (ie: bdtfx:nt:8503) a été passé
860
	// dans ce cas on met à jour le référentiel avec celui passé dans le champ espèce
861
	if(isset($determ->ref)) {
862
	    $referentiel = $determ->ref;
1640 raphael 863
	}
864
 
1929 raphael 865
	// succès de la détection
866
	// nom_sel est remplacé, mais seulement si un motif spécial à été utilisé (bdtfx:nn:4567)
867
	if($taxon_info_webservice->is_notation_spe) {
868
	    $espece[C_NOM_SEL] = $determ->nom_sci;
1688 raphael 869
	}
870
 
1929 raphael 871
	// écrasement des numéros (nomenclatural, taxonomique) saisis...
872
	$espece[C_NOM_SEL_NN] = $determ->id;
873
	$espece[C_NOM_RET] = RechercheInfosTaxonBeta::supprimerBiblio($determ->nom_retenu_complet);
874
	$espece[C_NOM_RET_NN] = $determ->{"nom_retenu.id"};
875
	$espece[C_NT] = $determ->num_taxonomique;
876
	$espece[C_FAMILLE] = $determ->famille;
877
	return;
878
	// et des info complémentaires
1697 raphael 879
 
1797 raphael 880
	/*
1929 raphael 881
	// GET /service:eflore:0.1/bdtfx/noms/31468?retour.champs=nom_sci,auteur,id,nom_retenu_complet,nom_retenu.id,num_taxonomique,famille
882
	/home/raphael/eflore/projets/services/modules/0.1/bdtfx/Noms.php:280
883
	SELECT  *, nom_sci   FROM bdtfx_v1_01	 WHERE num_nom = '31468'
884
	SELECT nom_sci, num_nom_retenu, nom_sci_html, auteur, annee, biblio_origine FROM bdtfx_v1_01 WHERE num_nom = 31468
885
	SELECT nom_sci, num_nom_retenu, nom_sci_html, auteur, annee, biblio_origine FROM bdtfx_v1_01 WHERE num_nom = 86535
1797 raphael 886
	*/
1770 raphael 887
 
1697 raphael 888
 
1929 raphael 889
	//var_dump($complement, $espece);die;
890
    }
1697 raphael 891
 
1929 raphael 892
    static function detectFromNom($nom) {
893
	$r = Cel::db()->requeter(sprintf("SELECT num_nom, num_tax_sup FROM bdtfx_v1_01 WHERE (nom_sci LIKE '%s') ".
894
					 "ORDER BY nom_sci ASC LIMIT 0, 1",
895
					 Cel::db()->proteger($nom)));
896
	if($r) return $r;
1697 raphael 897
 
1929 raphael 898
	Cel::db()->requeter(sprintf("SELECT num_nom, num_tax_sup FROM bdtfx_v1_01 WHERE (nom_sci LIKE '%s' OR nom LIKE '%s') ".
899
				    "ORDER BY nom_sci ASC LIMIT 0, 1",
900
				    Cel::db()->proteger($nom),
901
				    Cel::db()->proteger(str_replace(' ', '% ', $nom))));
902
	return $r;
903
    }
1697 raphael 904
 
905
 
1929 raphael 906
    /*
907
     * TODO: analyse rigoureuse:
908
     * == Identifiant Commune
909
     * - INSEE-C:\d{5}
910
     * - \d{5}
911
     * - \d{2}
912
     * == Commune
913
     * - \w+ (\d{2})
914
     * - \w+ (\d{5})
915
     * - \w+
916
     *
917
     */
918
    static function traiterLocalisation($ligne, Array &$localisation) {
919
	if(empty($ligne[C_ZONE_GEO])) $ligne[C_ZONE_GEO] = NULL;
920
	if(empty($ligne[C_CE_ZONE_GEO])) $ligne[C_CE_ZONE_GEO] = NULL;
1697 raphael 921
 
1929 raphael 922
	$identifiant_commune = trim($ligne[C_ZONE_GEO]);
923
	if(!$identifiant_commune) {
924
	    $departement = trim($ligne[C_CE_ZONE_GEO]);
925
 
926
	    if(strpos($departement, "INSEE-C:", 0) === 0) {
927
		$localisation[C_CE_ZONE_GEO] = trim($ligne[C_CE_ZONE_GEO]);
928
		if(array_key_exists($localisation[C_CE_ZONE_GEO], self::$cache['geo'])) {
929
		    $localisation[C_ZONE_GEO] = self::$cache['geo'][$localisation[C_CE_ZONE_GEO]];
1697 raphael 930
		}
931
		else {
1929 raphael 932
		    $nom = Cel::db()->requeter(sprintf("SELECT nom FROM cel_zones_geo WHERE code = %s LIMIT 1",
933
						       self::quoteNonNull(substr($localisation[C_CE_ZONE_GEO], strlen("INSEE-C:")))));
934
		    if($nom) $localisation[C_ZONE_GEO] = $nom[0]['nom'];
935
		    self::$cache['geo'][$localisation[C_CE_ZONE_GEO]] = @$nom[0]['nom'];
1697 raphael 936
		}
1929 raphael 937
		return;
938
	    }
1697 raphael 939
 
1929 raphael 940
	    if(!is_numeric($departement)) {
941
		$localisation[C_CE_ZONE_GEO] = $ligne[C_CE_ZONE_GEO];
942
		return;
943
	    }
944
 
945
	    $cache_attempted = FALSE;
946
	    if(array_key_exists($departement, self::$cache['geo'])) {
947
		$cache_attempted = TRUE;
948
		if(self::$cache['geo'][$departement][0] && self::$cache['geo'][$departement][1]) {
949
		    $localisation[C_ZONE_GEO] = self::$cache['geo'][$departement][0];
950
		    $localisation[C_CE_ZONE_GEO] = self::$cache['geo'][$departement][1];
951
		    return;
1697 raphael 952
		}
1929 raphael 953
	    }
1697 raphael 954
 
1929 raphael 955
	    if(! $cache_attempted && ($resultat_commune = Cel::db()->requeter(sprintf("SELECT DISTINCT nom, CONCAT('INSEE-C:', code) AS code FROM cel_zones_geo WHERE code = %s LIMIT 1",
956
										      self::quoteNonNull($departement)))) ) {
957
		$localisation[C_ZONE_GEO] = $resultat_commune[0]['nom'];
958
		$localisation[C_CE_ZONE_GEO] = $resultat_commune[0]['code'];
959
		self::$cache['geo'][$departement] = array($resultat_commune[0]['nom'], $resultat_commune[0]['code']);
960
		return;
961
	    }
962
	    ;
963
	    // if(strlen($departement) == 4) $departement = "INSEE-C:0" . $departement;
964
	    // if(strlen($departement) == 5) $departement = "INSEE-C:" . $departement;
965
	    // if(strlen($departement) <= 9) return "INSEE-C:0" . $departement; // ? ... TODO
1697 raphael 966
 
1929 raphael 967
	    $departement = trim($departement); // TODO
1697 raphael 968
 
1929 raphael 969
	    $localisation[C_CE_ZONE_GEO] = $ligne[C_CE_ZONE_GEO];
970
	    return;
971
	}
1697 raphael 972
 
973
 
1929 raphael 974
	$select = "SELECT DISTINCT nom, code FROM cel_zones_geo";
975
 
976
	if (preg_match('/(.+) \((\d{1,5})\)/', $identifiant_commune, $elements)) {
977
	    // commune + departement : montpellier (34)
978
	    $nom_commune=$elements[1];
979
	    $code_commune=$elements[2];
980
	    if(strlen($code_commune) <= 2) {
981
		$requete = sprintf("%s WHERE nom = %s AND code LIKE %s",
982
				   $select, self::quoteNonNull($nom_commune),
983
				   self::quoteNonNull($code_commune.'%'));
984
	    }
985
	    else {
986
		$requete = sprintf("%s WHERE nom = %s AND code = %d",
987
				   $select, self::quoteNonNull($nom_commune),
988
				   $code_commune);
989
	    }
990
	}
991
	elseif (preg_match('/^(\d+|(2[ab]\d+))$/i', $identifiant_commune, $elements)) {
992
	    // Code insee seul
993
	    $code_insee_commune=$elements[1];
994
	    $requete = sprintf("%s WHERE code = %s", $select, self::quoteNonNull($code_insee_commune));
995
	}
996
	else {
997
	    // Commune seule (le departement sera recupere dans la colonne departement si elle est presente)
998
	    // on prend le risque ici de retourner une mauvaise Commune
999
	    $nom_commune = str_replace(" ", "%", iconv('UTF-8', 'ASCII//TRANSLIT', $identifiant_commune));
1000
	    $requete = sprintf("%s WHERE nom LIKE %s", $select, self::quoteNonNull($nom_commune.'%'));
1001
	}
1697 raphael 1002
 
1003
 
1929 raphael 1004
	if(array_key_exists($identifiant_commune, self::$cache['geo'])) {
1005
	    $resultat_commune = self::$cache['geo'][$identifiant_commune];
1697 raphael 1006
	}
1929 raphael 1007
	else {
1008
	    $resultat_commune = Cel::db()->requeter($requete);
1009
	    self::$cache['geo'][$identifiant_commune] = $resultat_commune;
1010
	}
1011
	// TODO: levenstein sort ?
1012
	// TODO: count résultat !
1697 raphael 1013
 
1929 raphael 1014
	// cas de la commune introuvable dans le référentiel
1015
	// réinitialisation aux valeurs du fichier XLS
1016
	if(! $resultat_commune) {
1017
	    $localisation[C_ZONE_GEO] = trim($ligne[C_ZONE_GEO]);
1018
	    $localisation[C_CE_ZONE_GEO] = trim($ligne[C_CE_ZONE_GEO]);
1019
	} else {
1020
	    $localisation[C_ZONE_GEO] = $resultat_commune[0]['nom'];
1021
	    $localisation[C_CE_ZONE_GEO] = "INSEE-C:" . $resultat_commune[0]['code'];
1022
	    return;
1023
	}
1642 raphael 1024
 
1929 raphael 1025
	$departement = &$localisation[C_CE_ZONE_GEO];
1642 raphael 1026
 
1929 raphael 1027
	if(strpos($departement, "INSEE-C:", 0) === 0) {
1028
	    $localisation[C_ZONE_GEO] = $localisation[C_ZONE_GEO];
1029
	    $localisation[C_CE_ZONE_GEO] = $localisation[C_CE_ZONE_GEO];
1030
	}
1031
 
1032
 
1033
	if(!is_numeric($departement)) {
1034
	    $localisation[C_ZONE_GEO] = $localisation[C_ZONE_GEO];
1035
	    $localisation[C_CE_ZONE_GEO] = $localisation[C_CE_ZONE_GEO];
1036
	}
1037
 
1038
	if(strlen($departement) == 4) $departement = "INSEE-C:0" . $departement;
1039
	if(strlen($departement) == 5) $departement = "INSEE-C:" . $departement;
1040
	// if(strlen($departement) <= 9) return "INSEE-C:0" . $departement; // ? ... TODO
1041
 
1042
	$departement = trim($departement); // TODO
1043
 
1044
	$localisation[C_ZONE_GEO] = $localisation[C_ZONE_GEO];
1045
	$localisation[C_CE_ZONE_GEO] = $localisation[C_CE_ZONE_GEO];
1046
    }
2381 aurelien 1047
 
1048
 
1049
    public static function stockerChampsEtendus($champs_etendus, $ordre_ids, $config) {
1050
    	// singleton du pauvre mais l'export est suffisamment inefficace pour s'en priver
1051
    	self::$gestion_champs_etendus = self::$gestion_champs_etendus == null ?
1052
    										new GestionChampsEtendus($config, 'obs') :
1053
    										self::$gestion_champs_etendus;
1054
 
1055
    	$champs_etendus_obs = array();
1056
    	foreach($champs_etendus as $champ_etendu_a_obs) {
1057
    		$id_obs = $ordre_ids[$champ_etendu_a_obs['ordre']]; // id réel de l'observation correspondant à l'ordre
1058
    		foreach($champ_etendu_a_obs['champs_etendus'] as $label => $champ) {
1059
				// XXX: insère t'on des valeurs vides ?
1060
    			$valeur = $champ;
2394 aurelien 1061
    			$cle = $label;
2381 aurelien 1062
 
1063
    			if (!empty($cle) && !empty($valeur)) {
1064
    				$champ_etendu_a_inserer = new ChampEtendu();
1065
    				$champ_etendu_a_inserer->id = $id_obs;
1066
    				$champ_etendu_a_inserer->cle = $cle;
1067
    				$champ_etendu_a_inserer->valeur = $valeur;
1068
 
1069
    				$champs_etendus_obs[] = $champ_etendu_a_inserer;
1070
    			}
1071
    		}
1072
    	}
1073
 
1074
    	$insertion = self::$gestion_champs_etendus->ajouterParLots($champs_etendus_obs);
1075
    	//TODO: que faire si l'insertion des champs étendus échoue ?
1076
    	return count($champs_etendus_obs);
1077
    }
1929 raphael 1078
 
1079
    /* HELPERS */
1640 raphael 1080
 
1929 raphael 1081
    // http://stackoverflow.com/questions/348410/sort-an-array-based-on-another-array
1082
    // XXX; utilisé aussi (temporairement ?) par FormateurGroupeColonne.
1083
    static function sortArrayByArray($array, $orderArray) {
1084
	$ordered = array();
1085
	foreach($orderArray as $key) {
1086
	    if(array_key_exists($key, $array)) {
1087
		$ordered[$key] = $array[$key];
1088
		unset($array[$key]);
1089
	    }
1636 raphael 1090
	}
1929 raphael 1091
	return $ordered + $array;
1092
    }
1640 raphael 1093
 
1929 raphael 1094
    // retourne une BBox [N,S,E,O) pour un référentiel donné
1095
    static function getReferentielBBox($referentiel) {
1096
	if($referentiel == 'bdtfx') return Array(
1097
	    'NORD' => 51.2, // Dunkerque
1098
	    'SUD' => 41.3, // Bonifacio
1099
	    'EST' => 9.7, // Corse
1100
	    'OUEST' => -5.2); // Ouessan
1101
	return FALSE;
1102
    }
1640 raphael 1103
 
1929 raphael 1104
    // ces valeurs ne sont pas inséré via les placeholders du PDO::preparedStatement
1105
    // et doivent donc être échappées correctement.
1106
    public function initialiser_colonnes_statiques() {
1107
	$this->colonnes_statiques = array_merge($this->colonnes_statiques,
1108
						Array(
1109
						    "ce_utilisateur" => self::quoteNonNull($this->id_utilisateur), // peut-être un hash ou un id
1110
						    "prenom_utilisateur" => self::quoteNonNull($this->utilisateur['prenom']),
1111
						    "nom_utilisateur" => self::quoteNonNull($this->utilisateur['nom']),
1112
						    "courriel_utilisateur" => self::quoteNonNull($this->utilisateur['courriel']),
1113
						));
1640 raphael 1114
 
1929 raphael 1115
    }
1642 raphael 1116
 
1929 raphael 1117
    static function initialiser_pdo_ordered_statements($colonnes_statiques) {
1118
	return Array(
1119
	    // insert_ligne_pattern_ordre
1120
	    sprintf('INSERT INTO cel_obs (%s, %s) VALUES',
1121
		    implode(', ', array_keys($colonnes_statiques)),
1122
		    implode(', ', array_diff(self::$ordre_BDD, array_keys($colonnes_statiques)))),
1649 raphael 1123
 
1929 raphael 1124
	    // insert_ligne_pattern_ordre
1125
	    sprintf('(%s, %s ?)',
1126
		    implode(', ', $colonnes_statiques),
1127
		    str_repeat('?, ', count(self::$ordre_BDD) - count($colonnes_statiques) - 1))
1128
	);
1129
    }
1648 raphael 1130
 
1929 raphael 1131
    static function initialiser_pdo_statements($colonnes_statiques) {
1132
	return Array(
1133
	    // insert_prefix
1134
	    sprintf('INSERT INTO cel_obs (%s) VALUES ',
1135
		    implode(', ', self::$ordre_BDD)),
1649 raphael 1136
 
1650 raphael 1137
 
1929 raphael 1138
	    // insert_ligne_pattern, cf: self::$insert_ligne_pattern
1139
	    '(' .
1140
	    // 3) créé une chaîne de liste de champ à inséré en DB
1141
	    implode(', ', array_values(
1142
		// 2) garde les valeurs fixes (de $colonnes_statiques),
1143
		// mais remplace les NULL par des "?"
1144
		array_map('__anonyme_5',
1145
			  // 1) créé un tableau genre (nom_sel_nn => NULL) depuis self::$ordre_BDD
1146
			  // et écrase certaines valeurs avec $colonnes_statiques (initilisé avec les données utilisateur)
1147
			  array_merge(array_map('__anonyme_6', array_flip(self::$ordre_BDD)), $colonnes_statiques
1148
			  )))) .
1149
	    ')'
1150
	);
1151
    }
1649 raphael 1152
 
1929 raphael 1153
    // équivalent à Bdd2->proteger() (qui wrap PDO::quote),
1154
    // sans transformer NULL en ""
1155
    static function quoteNonNull($chaine) {
1156
	if(is_null($chaine)) return "NULL";
1157
	if(!is_string($chaine) && !is_integer($chaine)) {
1158
	    die("erreur: " . __FILE__ . ':' . __LINE__);
1642 raphael 1159
	}
1929 raphael 1160
	return Cel::db()->quote($chaine);
1161
    }
1642 raphael 1162
 
1929 raphael 1163
    public function erreurs_stock($errno, $errstr) {
1164
	$this->bilan[] = $errstr;
1165
    }
1636 raphael 1166
}