8 * Champs du CSV de Galette
9 * les lignes vides ('') ne seront pas proposées à l'import
12 public $galette_fields = [
34 'Infos (réservé administrateur)',
39 'Date création fiche',
40 'Date modification fiche',
44 7, // bool_display_info
53 * Importer un CSV de la liste des membres depuis Galette
54 * @param string $path Chemin vers le CSV
55 * @param array $translation_table Tableau indiquant la correspondance à effectuer entre les champs
56 * de Galette et ceux de Garradin. Par exemple : ['Date création fiche' => 'date_inscription']
57 * @return boolean TRUE en cas de succès
59 public function fromGalette($path, $translation_table)
61 if (!file_exists($path) ||
!is_readable($path))
63 throw new \
RuntimeException('Fichier inconnu : '.$path);
66 $fp = fopen($path, 'r');
73 $db = DB
::getInstance();
75 $membres = new Membres
;
77 $columns = array_flip($this->galette_fields
);
79 $col = function($column) use (&$row, &$columns)
81 if (!isset($columns[$column]))
84 if (!isset($row[$columns[$column]]))
87 return $row[$columns[$column]];
91 $delim = utils
::find_csv_delim($fp);
95 $row = fgetcsv($fp, 4096, $delim);
103 if (count($row) != count($columns))
105 $db->exec('ROLLBACK;');
106 throw new UserException('Erreur sur la ligne ' . $line . ' : le nombre de colonnes est incorrect.');
111 foreach ($translation_table as $galette=>$garradin)
113 // Champs qu'on ne veut pas importer
114 if (empty($garradin))
117 // Concaténer plusieurs champs
118 if (isset($data[$garradin]))
119 $data[$garradin] .= "\n" . $col($galette);
121 $data[$garradin] = $col($galette);
125 $membres->add($data);
127 catch (UserException
$e)
129 $db->exec('ROLLBACK;');
130 throw new UserException('Erreur sur la ligne ' . $line . ' : ' . $e->getMessage());
141 * Importer un CSV de la liste des membres depuis un export Garradin
142 * @param string $path Chemin vers le CSV
143 * @return boolean TRUE en cas de succès
145 public function fromCSV($path)
147 if (!file_exists($path) ||
!is_readable($path))
149 throw new \
RuntimeException('Fichier inconnu : '.$path);
152 $fp = fopen($path, 'r');
159 $db = DB
::getInstance();
161 $membres = new Membres
;
163 // On récupère les champs qu'on peut importer
164 $champs = Config
::getInstance()->get('champs_membres')->getAll();
165 $champs = array_keys($champs);
166 $champs[] = 'date_inscription';
167 $champs[] = 'date_connexion';
169 $champs[] = 'id_categorie';
172 $delim = utils
::find_csv_delim($fp);
176 $row = fgetcsv($fp, 4096, $delim);
187 if (is_numeric($row[0]))
189 throw new UserException('Erreur sur la ligne 1 : devrait contenir l\'en-tête des colonnes.');
192 $columns = array_flip($row);
196 if (count($row) != count($columns))
198 $db->exec('ROLLBACK;');
199 throw new UserException('Erreur sur la ligne ' . $line . ' : le nombre de colonnes est incorrect.');
204 foreach ($columns as $name=>$id)
208 // Champs qui n'existent pas dans le schéma actuel
209 if (!in_array($name, $champs))
212 if (trim($row[$id]) !== '')
213 $data[$name] = $row[$id];
216 if (!empty($data['id']))
218 $id = (int)$data['id'];
228 $membres->edit($id, $data);
230 $membres->add($data);
232 catch (UserException
$e)
234 $db->exec('ROLLBACK;');
235 throw new UserException('Erreur sur la ligne ' . $line . ' : ' . $e->getMessage());
245 public function toCSV()
247 $db = DB
::getInstance();
249 $res = $db->prepare('SELECT m.id, c.nom AS categorie, m.* FROM membres AS m
250 LEFT JOIN membres_categories AS c ON m.id_categorie = c.id ORDER BY c.id;')->execute();
252 $fp = fopen('php://output', 'w');
255 while ($row = $res->fetchArray(SQLITE3_ASSOC
))
257 unset($row['passe']);
261 fputcsv($fp, array_keys($row));