-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlines_picto.php
More file actions
80 lines (66 loc) · 2.39 KB
/
lines_picto.php
File metadata and controls
80 lines (66 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
// Parcourt récursivement le dossier `logo/` et concatène tous les
// fichiers nommés `lines_picto.csv` en une seule sortie CSV.
// Définir le répertoire de base (par défaut : dossier `logo` à côté du script)
$baseDir = __DIR__ . DIRECTORY_SEPARATOR . 'logo';
if (php_sapi_name() === 'cli') {
global $argv;
if (isset($argv[1]) && $argv[1] !== '') {
$baseDir = $argv[1];
}
}
if (!is_dir($baseDir)) {
$msg = "Directory not found: $baseDir\n";
if (php_sapi_name() === 'cli') {
fwrite(STDERR, $msg);
} else {
header('HTTP/1.1 500 Internal Server Error');
echo $msg;
}
exit(2);
}
// If run from a web server, send CSV headers for download
if (php_sapi_name() !== 'cli') {
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="lines_picto.csv"');
}
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($baseDir, RecursiveDirectoryIterator::SKIP_DOTS));
$firstHeaderPrinted = false;
$out = fopen('php://output', 'w');
foreach ($it as $file) {
if (!$file->isFile()) continue;
if (strtolower($file->getFilename()) !== 'lines_picto.csv') continue;
$path = $file->getPathname();
$handle = fopen($path, 'r');
if (!$handle) continue;
$header = fgetcsv($handle, 0, ';');
if ($header === false) {
fclose($handle);
continue;
}
// Enlever BOM UTF-8 si présent sur la première cellule de l'en-tête
if (isset($header[0])) {
$header[0] = preg_replace('/^\xEF\xBB\xBF/', '', $header[0]);
}
if (!$firstHeaderPrinted) {
fputcsv($out, $header, ';');
$firstHeaderPrinted = true;
}
// Compatibilité avec des en-têtes `line_id` ou `lineId`
$agencyIdIndex = array_search('agency_id', $header, true);
$lineIdIndex = array_search('line_id', $header, true);
if ($lineIdIndex === false) {
$lineIdIndex = array_search('lineId', $header, true);
}
while (($row = fgetcsv($handle, 0, ';')) !== false) {
if ($agencyIdIndex !== false && $lineIdIndex !== false && isset($row[$agencyIdIndex], $row[$lineIdIndex])) {
$prefix = $row[$agencyIdIndex] . '_';
if (strpos($row[$lineIdIndex], $prefix) !== 0) {
$row[$lineIdIndex] = $prefix . $row[$lineIdIndex];
}
}
fputcsv($out, $row, ';');
}
fclose($handle);
}
fclose($out);