Overview
Comment:Correction import CSV: ignorer le BOM UTF-8 au début du CSV (signalé par @Jean-jaques et @Nicolas)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | dev
Files: files | file ages | folders
SHA1: 06f494acb38a2d537293b0d2ac47a1ca00e4df04
User & Date: bohwaz on 2017-09-21 01:55:51
Other Links: branch diff | manifest | tags
References
2020-12-08
23:25 Wiki page "Changelog/0.9" artifact: b9c916b4d1 user: bohwaz
Context
2017-09-21
07:08
Créer une sauvegarde avant mise à jour check-in: d3c235bbd3 user: bohwaz tags: dev
01:55
Correction import CSV: ignorer le BOM UTF-8 au début du CSV (signalé par @Jean-jaques et @Nicolas) check-in: 06f494acb3 user: bohwaz tags: dev
2017-09-20
06:33
@count n'est plus une syntaxe valide check-in: b4e9e4804f user: bohwaz tags: dev
Changes

Modified src/include/lib/Garradin/Compta/Import.php from [60189afd5d] to [5efe3ab736].

102
103
104
105
106
107
108

109
110
111
112
113
114
115
				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))







>







102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
				return null;

			return $row[$columns[$column]];
		};

		$line = 0;
		$delim = Utils::find_csv_delim($fp);
		Utils::skip_bom($fp);

		while (!feof($fp))
		{
			$row = fgetcsv($fp, 4096, $delim);
			$line++;

			if (empty($row))
281
282
283
284
285
286
287

288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306

307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
				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;
			}

			if (empty($columns))
			{
				$columns = $row;
				$columns = array_flip($columns);
				continue;
			}

			$date = $col('Date');


			if (!preg_match('!^\d{2}/\d{2}/\d{4}$!', $date))
			{
				$db->rollback();
				throw new UserException('Erreur sur la ligne ' . $line . ' : la date n\'est pas au format jj/mm/aaaa.');
			}

			$date = explode('/', $date);
			$date = $date[2] . '-' . $date[1] . '-' . $date[0];

			if ($db->test('compta_exercices', '(? < debut OR ? > fin) AND cloture = 0', $date, $date))
			{
				continue;
			}

			$debit = $get_compte($col('Compte débité - Numéro'), $col('Compte débité - Intitulé'));







>



















>

|


|


<
|







282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316

317
318
319
320
321
322
323
324
				return null;

			return $row[$columns[$column]];
		};

		$line = 0;
		$delim = Utils::find_csv_delim($fp);
		Utils::skip_bom($fp);

		while (!feof($fp))
		{
			$row = fgetcsv($fp, 4096, $delim);
			$line++;

			if (empty($row))
			{
				continue;
			}

			if (empty($columns))
			{
				$columns = $row;
				$columns = array_flip($columns);
				continue;
			}

			$date = $col('Date');
			$date = \DateTime::createFromFormat('d/m/Y', $date);

			if (!$date)
			{
				$db->rollback();
				throw new UserException(sprintf('Erreur sur la ligne %d : la date "%s" n\'est pas au format jj/mm/aaaa.', $line, $col('Date')));
			}


			$date = $date->format('Y-m-d');

			if ($db->test('compta_exercices', '(? < debut OR ? > fin) AND cloture = 0', $date, $date))
			{
				continue;
			}

			$debit = $get_compte($col('Compte débité - Numéro'), $col('Compte débité - Intitulé'));

Modified src/include/lib/Garradin/Membres/Import.php from [c8e1a71b74] to [eefd4b70a3].

91
92
93
94
95
96
97

98
99
100
101
102
103
104
				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))







>







91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
				return null;

			return $row[$columns[$column]];
		};

		$line = 0;
		$delim = Utils::find_csv_delim($fp);
		Utils::skip_bom($fp);

		while (!feof($fp))
		{
			$row = fgetcsv($fp, 4096, $delim);
			$line++;

			if (empty($row))
172
173
174
175
176
177
178

179
180
181
182
183
184
185
		$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++;








>







173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
		$champs[] = 'date_inscription';
		//$champs[] = 'date_connexion';
		//$champs[] = 'id';
		//$champs[] = 'id_categorie';

		$line = 0;
		$delim = Utils::find_csv_delim($fp);
		Utils::skip_bom($fp);

		while (!feof($fp))
		{
			$row = fgetcsv($fp, 4096, $delim);

			$line++;

Modified src/include/lib/Garradin/Utils.php from [06255be23d] to [2bf872f126].

646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
            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));
        }







|







646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
            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));
        }
668
669
670
671
672
673
674

675
676









677
678
679
680
681
682
683
684
685
686
687
688
            "\t"=> substr_count($line, "\t")
        ];

        arsort($delims);
        reset($delims);

        rewind($fp);

        return key($delims);
    }










    static public function row_to_csv($row)
    {
        $row = (array) $row;

        array_walk($row, function ($field) {
            return str_replace('"', '""', $field);
        });

        return '"' . implode('", "', $row) . '"';
    }
}







>


>
>
>
>
>
>
>
>
>












668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
            "\t"=> substr_count($line, "\t")
        ];

        arsort($delims);
        reset($delims);

        rewind($fp);

        return key($delims);
    }

    static public function skip_bom(&$fp)
    {
        // Skip BOM
        if (fgets($fp, 4) !== chr(0xEF) . chr(0xBB) . chr(0xBF))
        {
            fseek($fp, 0);
        }
    }

    static public function row_to_csv($row)
    {
        $row = (array) $row;

        array_walk($row, function ($field) {
            return str_replace('"', '""', $field);
        });

        return '"' . implode('", "', $row) . '"';
    }
}

Modified src/www/admin/compta/import.php from [8dcdde991d] to [9b4e3790c6].

45
46
47
48
49
50
51
52
53
54
        catch (UserException $e)
        {
            $form->addError($e->getMessage());
        }
    }
}

$tpl->assign('ok', qg('ok'));

$tpl->display('admin/compta/import.tpl');







|


45
46
47
48
49
50
51
52
53
54
        catch (UserException $e)
        {
            $form->addError($e->getMessage());
        }
    }
}

$tpl->assign('ok', qg('ok') !== null);

$tpl->display('admin/compta/import.tpl');