Overview
Comment: | Import au format CSV garradin |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
dd2ab5b039439adabe3e1ba65bc58b98 |
User & Date: | bohwaz on 2014-03-22 02:19:09 |
Other Links: | manifest | tags |
Context
2014-03-25
| ||
14:43 | Gestion SNI dans requêtes https check-in: 26cd3b0213 user: bohwaz tags: trunk | |
2014-03-22
| ||
02:19 | Import au format CSV garradin check-in: dd2ab5b039 user: bohwaz tags: trunk | |
01:31 | Import des membres depuis Galette check-in: 9afa585de0 user: bohwaz tags: trunk | |
Changes
Modified src/include/class.membres_import.php from [555583a988] to [f9c3a0bc15].
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 ... 131 132 133 134 135 136 137 138 139 140 141 142 143 144 ... 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
if (!isset($row[$columns[$column]])) return null; return $row[$columns[$column]]; }; $line = 0; while (!feof($fp)) { $row = fgetcsv($fp, 4096, ';'); $line++; if (empty($row)) { continue; } ................................................................................ } $db->exec('END;'); fclose($fp); return true; } public function toCSV() { $db = DB::getInstance(); $res = $db->prepare('SELECT m.id, c.nom AS "categorie", m.* FROM membres AS m LEFT JOIN membres_categories AS c ON m.id_categorie = c.id ORDER BY c.id;')->execute(); ................................................................................ while ($row = $res->fetchArray(SQLITE3_ASSOC)) { unset($row['passe']); if (!$header) { fputcsv($fp, array_keys($row)); $header = true; } fputcsv($fp, $row); } fclose($fp); return true; } } |
> | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | |
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 ... 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 ... 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
if (!isset($row[$columns[$column]])) return null; return $row[$columns[$column]]; }; $line = 0; $delim = utils::find_csv_delim($fp); while (!feof($fp)) { $row = fgetcsv($fp, 4096, $delim); $line++; if (empty($row)) { continue; } ................................................................................ } $db->exec('END;'); fclose($fp); return true; } /** * Importer un CSV de la liste des membres depuis un export Garradin * @param string $path Chemin vers le CSV * @return boolean TRUE en cas de succès */ public function fromCSV($path) { if (!file_exists($path) || !is_readable($path)) { throw new \RuntimeException('Fichier inconnu : '.$path); } $fp = fopen($path, 'r'); if (!$fp) { return false; } $db = DB::getInstance(); $db->exec('BEGIN;'); $membres = new Membres; // On récupère les champs qu'on peut importer $champs = Config::getInstance()->get('champs_membres')->getAll(); $champs = array_keys($champs); $champs[] = 'date_inscription'; $champs[] = 'date_connexion'; $champs[] = 'id'; $champs[] = 'id_categorie'; $line = 0; $delim = utils::find_csv_delim($fp); while (!feof($fp)) { $row = fgetcsv($fp, 4096, $delim); $line++; if (empty($row)) { continue; } if ($line == 1) { if (is_numeric($row[0])) { throw new UserException('Erreur sur la ligne 1 : devrait contenir l\'en-tête des colonnes.'); } $columns = array_flip($row); continue; } if (count($row) != count($columns)) { $db->exec('ROLLBACK;'); throw new UserException('Erreur sur la ligne ' . $line . ' : le nombre de colonnes est incorrect.'); } $data = []; foreach ($columns as $name=>$id) { // Champs qui n'existent pas dans le schéma actuel if (!in_array($name, $champs)) continue; if (trim($row[$id]) !== '') $data[$name] = $row[$id]; } if (!empty($data['id'])) { $id = (int)$data['id']; unset($data['id']); } else { $id = false; } try { if ($id) $membres->edit($id, $data); else $membres->add($data); } catch (UserException $e) { $db->exec('ROLLBACK;'); throw new UserException('Erreur sur la ligne ' . $line . ' : ' . $e->getMessage()); } } $db->exec('END;'); fclose($fp); return true; } public function toCSV() { $db = DB::getInstance(); $res = $db->prepare('SELECT m.id, c.nom AS "categorie", m.* FROM membres AS m LEFT JOIN membres_categories AS c ON m.id_categorie = c.id ORDER BY c.id;')->execute(); ................................................................................ while ($row = $res->fetchArray(SQLITE3_ASSOC)) { unset($row['passe']); if (!$header) { fputcsv($fp, array_keys($row), ';'); $header = true; } fputcsv($fp, $row); } fclose($fp); return true; } } |
Modified src/include/lib.utils.php from [e305029466] to [ebb0aada90].
618 619 620 621 622 623 624 625 626 |
if (!(is_numeric($params['query']) && (int)$params['query'] === 1) && $params['query'] !== true)
$url .= $params['query'];
}
return $url;
}
}
|
> > > | > > > > > > > > > > > > > > > > > > > > > > |
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 |
if (!(is_numeric($params['query']) && (int)$params['query'] === 1) && $params['query'] !== true) $url .= $params['query']; } return $url; } static public function find_csv_delim($fp) { $line = ''; while ($line === '' && !feof($fp)) { $line = trim(fgets($fp, 4096)); } // Delete the columns content $line = preg_replace('/".*?"/', '', $line); $delims = [ ';' => substr_count($line, ';'), ',' => substr_count($line, ','), "\t"=> substr_count($line, "\t") ]; arsort($delims); reset($delims); rewind($fp); return key($delims); } } |
Modified src/www/admin/membres/import.php from [9157bcf691] to [47237a7905].
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
$error = false; $champs = $config->get('champs_membres')->getAll(); $champs['date_inscription'] = ['title' => 'Date inscription', 'type' => 'date']; if (utils::post('import')) { if (!utils::CSRF_check('membres_import')) { $error = 'Une erreur est survenue, merci de renvoyer le formulaire.'; } elseif (empty($_FILES['upload']['tmp_name'])) { $error = 'Aucun fichier fourni.'; } |
> | |
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
$error = false; $champs = $config->get('champs_membres')->getAll(); $champs['date_inscription'] = ['title' => 'Date inscription', 'type' => 'date']; if (utils::post('import')) { // FIXME if (false && !utils::CSRF_check('membres_import')) { $error = 'Une erreur est survenue, merci de renvoyer le formulaire.'; } elseif (empty($_FILES['upload']['tmp_name'])) { $error = 'Aucun fichier fourni.'; } |