| Line 11... |
Line 11... |
| 11 |
|
11 |
|
| 12 |
/**
|
12 |
/**
|
| 13 |
* Service d'import de données d'observation du CEL au format XLS
|
13 |
* Service d'import de données d'observation du CEL au format XLS
|
| Line 14... |
Line 14... |
| 14 |
*/
|
14 |
*/
|
| 15 |
|
15 |
|
| Line 16... |
Line 16... |
| 16 |
// sont define()'d commme n° de colonne tous les slugs retournés par ExportXLS::fieldSetsToColumns()
|
16 |
// sont define()'d commme n° de colonne tous les abbrevs retournés par ExportXLS::nom_d_ensemble_vers_liste_de_colonnes()
|
| 17 |
// préfixés par C_ cf: detectionEntete()
|
17 |
// préfixés par C_ cf: detectionEntete()
|
| 18 |
|
18 |
|
| Line 23... |
Line 23... |
| 23 |
ini_set('xdebug.cli_color', 2);
|
23 |
ini_set('xdebug.cli_color', 2);
|
| 24 |
require_once('lib/PHPExcel/Classes/PHPExcel.php');
|
24 |
require_once('lib/PHPExcel/Classes/PHPExcel.php');
|
| 25 |
require_once('ExportXLS.php');
|
25 |
require_once('ExportXLS.php');
|
| Line 26... |
Line 26... |
| 26 |
|
26 |
|
| 27 |
class MyReadFilter implements PHPExcel_Reader_IReadFilter {
|
27 |
class MyReadFilter implements PHPExcel_Reader_IReadFilter {
|
| 28 |
public $excludes = array();
|
28 |
public $exclues = array();
|
| 29 |
public function __construct() {}
|
29 |
public function __construct() {}
|
| 30 |
public function readCell($column, $row, $worksheetName = '') {
|
30 |
public function readCell($colonne, $ligne, $worksheetName = '') {
|
| 31 |
if(@$this->excludes[$column]) return false;
|
31 |
if(@$this->exclues[$colonne]) return false;
|
| 32 |
if($row == 1) return false;
|
32 |
if($ligne == 1) return false;
|
| 33 |
return true;
|
33 |
return true;
|
| 34 |
}
|
34 |
}
|
| Line 35... |
Line 35... |
| 35 |
}
|
35 |
}
|
| Line 76... |
Line 76... |
| 76 |
$this->utilisateur = $this->getInfosComplementairesUtilisateur($pairs['utilisateur']);
|
76 |
$this->utilisateur = $this->getInfosComplementairesUtilisateur($pairs['utilisateur']);
|
| Line 77... |
Line 77... |
| 77 |
|
77 |
|
| Line 78... |
Line 78... |
| 78 |
$infos_fichier = array_pop($_FILES);
|
78 |
$infos_fichier = array_pop($_FILES);
|
| 79 |
|
79 |
|
| Line 80... |
Line 80... |
| 80 |
/*$objPHPExcel = PHPExcel_IOFactory::load($infos_fichier['tmp_name']);
|
80 |
/*$objPHPExcel = PHPExcel_IOFactory::load($infos_fichier['tmp_name']);
|
| 81 |
$sheetData = $objPHPExcel->getActiveSheet()->toArray(NULL,FALSE,FALSE,TRUE);*/
|
81 |
$donnees = $objPHPExcel->getActiveSheet()->toArray(NULL,FALSE,FALSE,TRUE);*/
|
| 82 |
|
82 |
|
| Line 83... |
Line 83... |
| 83 |
/*$objReader = PHPExcel_IOFactory::createReader("Excel5");
|
83 |
/*$objReader = PHPExcel_IOFactory::createReader("Excel5");
|
| Line 84... |
Line 84... |
| 84 |
$objReader->setReadDataOnly(true);
|
84 |
$objReader->setReadDataOnly(true);
|
| 85 |
$objPHPExcel = $objReader->load($infos_fichier['tmp_name']);*/
|
85 |
$objPHPExcel = $objReader->load($infos_fichier['tmp_name']);*/
|
| 86 |
|
86 |
|
| 87 |
//var_dump($sheetData);
|
87 |
//var_dump($donnees);
|
| Line 88... |
Line 88... |
| 88 |
|
88 |
|
| 89 |
$objReader = PHPExcel_IOFactory::createReader("Excel5");
|
89 |
$objReader = PHPExcel_IOFactory::createReader("Excel5");
|
| 90 |
$objReader->setReadDataOnly(true);
|
90 |
$objReader->setReadDataOnly(true);
|
| Line 91... |
Line 91... |
| 91 |
$objPHPExcel = $objReader->load($infos_fichier['tmp_name']);
|
91 |
$objPHPExcel = $objReader->load($infos_fichier['tmp_name']);
|
| 92 |
$sheetData = $objPHPExcel->getActiveSheet()->toArray(NULL,FALSE,FALSE,TRUE);
|
92 |
$donnees = $objPHPExcel->getActiveSheet()->toArray(NULL,FALSE,FALSE,TRUE);
|
| 93 |
|
93 |
|
| 94 |
$filterSubset = new MyReadFilter();
|
94 |
$filtre_colonnes = new MyReadFilter();
|
| 95 |
$filterSubset->excludes = self::detectionEntete($sheetData[1]);
|
95 |
$filtre_colonnes->exclues = self::detectionEntete($donnees[1]);
|
| 96 |
$objReader->setReadFilter($filterSubset);
|
96 |
$objReader->setReadFilter($filtre_colonnes);
|
| Line 97... |
Line 97... |
| 97 |
|
97 |
|
| 98 |
// recharge avec le filtre sur colonnes actif
|
98 |
// recharge avec le filtre sur colonnes actif
|
| 99 |
// (exclue les colonnes inutiles/inutilisables)
|
99 |
// (exclue les colonnes inutiles/inutilisables)
|
| 100 |
$objPHPExcel = $objReader->load($infos_fichier['tmp_name']);
|
100 |
$objPHPExcel = $objReader->load($infos_fichier['tmp_name']);
|
| 101 |
$sheetData = $objPHPExcel->getActiveSheet()->toArray(NULL,FALSE,FALSE,TRUE);
|
101 |
$donnees = $objPHPExcel->getActiveSheet()->toArray(NULL,FALSE,FALSE,TRUE);
|
| 102 |
self::charger($sheetData);
|
102 |
self::charger($donnees);
|
| 103 |
}
|
103 |
}
|
| 104 |
|
104 |
|
| 105 |
static function detectionEntete($entete) {
|
105 |
static function detectionEntete($entete) {
|
| 106 |
$colonnes_reconnues = Array();
|
106 |
$colonnes_reconnues = Array();
|
| 107 |
$cols = ExportXLS::fieldSetsToColumns('standard');
|
107 |
$cols = ExportXLS::nom_d_ensemble_vers_liste_de_colonnes('standard');
|
| 108 |
foreach($entete as $k => $v) {
|
108 |
foreach($entete as $k => $v) {
|
| 109 |
$entete_simple = iconv('UTF-8', 'ASCII//TRANSLIT', strtolower(trim($v)));
|
109 |
$entete_simple = iconv('UTF-8', 'ASCII//TRANSLIT', strtolower(trim($v)));
|
| 110 |
foreach($cols as $col) {
|
110 |
foreach($cols as $col) {
|
| 111 |
$entete_officiel_simple = iconv('UTF-8', 'ASCII//TRANSLIT', strtolower(trim($col['nom'])));
|
111 |
$entete_officiel_simple = iconv('UTF-8', 'ASCII//TRANSLIT', strtolower(trim($col['nom'])));
|
| Line 123... |
Line 123... |
| 123 |
|
123 |
|
| 124 |
$colonnes_automatiques = array_filter($cols, function($v) { return !$v['importable']; });
|
124 |
$colonnes_automatiques = array_filter($cols, function($v) { return !$v['importable']; });
|
| 125 |
// ne conserve que le nom long pour matcher avec la ligne XLS d'entête
|
125 |
// ne conserve que le nom long pour matcher avec la ligne XLS d'entête
|
| 126 |
array_walk($colonnes_automatiques, function(&$v) { $v = $v['nom']; });
|
126 |
array_walk($colonnes_automatiques, function(&$v) { $v = $v['nom']; });
|
| 127 |
$colonnesID_a_exclure = array_intersect($entete, $colonnes_automatiques);
|
127 |
$colonnesID_a_exclure = array_intersect($entete, $colonnes_automatiques);
|
| Line 128... |
Line 128... |
| 128 |
// TODO: pourquoi ne pas comparer avec les slugs aussi
|
128 |
// TODO: pourquoi ne pas comparer avec les abbrevs aussi
|
| 129 |
|
129 |
|
| Line 130... |
Line 130... |
| 130 |
return array_merge($colonnesID_non_reconnues, $colonnesID_a_exclure);
|
130 |
return array_merge($colonnesID_non_reconnues, $colonnesID_a_exclure);
|
| 131 |
}
|
131 |
}
|
| 132 |
|
132 |
|
| 133 |
static function charger($donnees) {
|
133 |
static function charger($donnees) {
|
| 134 |
// TODO: chunkfilter
|
134 |
// TODO: chunkfilter
|
| 135 |
$chunks = 30;
|
135 |
$morceaux = 30;
|
| 136 |
$i = 2;
|
136 |
$i = 2;
|
| 137 |
$end = $i + $chunks;
|
137 |
$fin = $i + $morceaux;
|
| 138 |
while($i < $end && isset($donnees[$i])) {
|
138 |
while($i < $fin && isset($donnees[$i])) {
|
| 139 |
$record = self::chargerLigne($donnees[$i]);
|
139 |
$enregistrement = self::chargerLigne($donnees[$i]);
|
| 140 |
$req = implode(', ', self::sortArrayByArray($record, self::$ordre_BDD));
|
140 |
$req = implode(', ', self::sortArrayByArray($enregistrement, self::$ordre_BDD));
|
| Line 141... |
Line 141... |
| 141 |
$i++;
|
141 |
$i++;
|
| Line 172... |
Line 172... |
| 172 |
"station" => trim($ligne[C_STATION]),
|
172 |
"station" => trim($ligne[C_STATION]),
|
| 173 |
"milieu" => trim($ligne[C_MILIEU]),
|
173 |
"milieu" => trim($ligne[C_MILIEU]),
|
| 174 |
"commentaire" => trim($ligne[C_COMMENTAIRE]),
|
174 |
"commentaire" => trim($ligne[C_COMMENTAIRE]),
|
| Line 175... |
Line 175... |
| 175 |
|
175 |
|
| 176 |
// XXX: fixe
|
176 |
// XXX: fixe
|
| 177 |
"transmission" => 0, // cf commentaire dans ExportXLS::fieldSetsToColumns()
|
177 |
"transmission" => 0, // cf commentaire dans ExportXLS::nom_d_ensemble_vers_liste_de_colonnes()
|
| 178 |
"date_creation" => date("Y-m-d H:i:s"),
|
178 |
"date_creation" => date("Y-m-d H:i:s"),
|
| Line 179... |
Line 179... |
| 179 |
"date_modification" => date("Y-m-d H:i:s"),
|
179 |
"date_modification" => date("Y-m-d H:i:s"),
|
| 180 |
|
180 |
|