-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport.php
More file actions
112 lines (99 loc) · 3.8 KB
/
import.php
File metadata and controls
112 lines (99 loc) · 3.8 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/******** INPUT VARIABLES ********/
// Originally created using https://github.com/PHPOffice/PHPExcel
require_once '/path/to/PHPExcel/Classes/PHPExcel/IOFactory.php';
// xlsx from https://www.data.gouv.fr/en/datasets/repertoire-operationnel-des-metiers-et-des-emplois-rome/
$tmpfname = "/path/to/ROME_ArboPrincipale.xlsx";
// Your JSON and SQL outputs
$outputJSONPath = "/path/to/ROME_ArboPrincipale.json";
$outputSQLPath = "/path/to/ROME_ArboPrincipale.sql";
/****** END INPUT VARIABLES ******/
$outputJSON = [];
$outputSQL = '
CREATE TABLE rome_taxonomy_category (
rome_prefix varchar(10) PRIMARY KEY,
parent_rome_prefix varchar(10),
label varchar(255) NOT NULL
);
CREATE TABLE rome_taxonomy_ogr (
ogr int PRIMARY KEY,
label varchar(255) NOT NULL,
lvl_1_rome_prefix varchar(10) NOT NULL,
lvl_2_rome_prefix varchar(10) NOT NULL,
lvl_3_rome varchar(10) NOT NULL
);
';
$excelReader = PHPExcel_IOFactory::createReaderForFile($tmpfname);
$excelObj = $excelReader->load($tmpfname);
// ROME codes are in the second sheet :
$sheets = $excelObj->getAllSheets();
$worksheet = $excelObj->getSheet(1);
$lastRow = $worksheet->getHighestRow();
$ROMECategory = [];
for ($row = 2; $row <= $lastRow; $row++) {
$A = trim($worksheet->getCell('A' . $row)->getValue(), " ");
if (empty($A)) {
continue;
}
$B = trim($worksheet->getCell('B' . $row)->getValue(), " ");
$C = trim($worksheet->getCell('C' . $row)->getValue(), " ");
$D = trim($worksheet->getCell('D' . $row)->getValue(), " ");
$E = trim($worksheet->getCell('E' . $row)->getValue(), " ");
if ($A && $B && $C && !empty($E)) {
// OGR occupation
$ROMECategory[$A][1][$B][1][$C][1][$E] = $D;
} else {
// ROME Category
if ($C != "") {
$ROMECategory[$A][1][$B][1][$C] = [
$D,
[]
];
} else if ($B != "") {
$ROMECategory[$A][1][$B] = [
$D,
[]
];
} else {
$ROMECategory[$A] = [
$D,
[]
];
}
}
}
foreach ($ROMECategory as $catLvl1 => $lvl1) {
$outputSQL .= 'INSERT INTO rome_taxonomy_category (rome_prefix, parent_rome_prefix, label) VALUES (' .
'"' . $catLvl1 . '",' .
'NULL,' .
'"' . $lvl1[0] . '");' . "\n";
foreach ($lvl1[1] as $catLvl2 => $lvl2) {
$outputSQL .= 'INSERT INTO rome_taxonomy_category (rome_prefix, parent_rome_prefix, label) VALUES (' .
'"' . $catLvl1 . $catLvl2 . '",' .
'"' . $catLvl1 . '",' .
'"' . $lvl2[0] . '");' . "\n";
foreach ($lvl2[1] as $catLvl3 => $lvl3) {
$outputSQL .= 'INSERT INTO rome_taxonomy_category (rome_prefix, parent_rome_prefix, label) VALUES (' .
'"' . $catLvl1 . $catLvl2 . $catLvl3 . '",' .
'"' . $catLvl1 . $catLvl2 . '",' .
'"' . $lvl3[0] . '");' . "\n";
foreach ($lvl3[1] as $ogr => $occupation) {
$outputSQL .= 'INSERT INTO rome_taxonomy_ogr (ogr, label, lvl_1_rome_prefix, lvl_2_rome_prefix, lvl_3_rome) VALUES (' .
$ogr . ',' .
'"' . $occupation . '",' .
'"' . $catLvl1 . '",' .
'"' . $catLvl1 . $catLvl2 . '",' .
'"' . $catLvl1 . $catLvl2 . $catLvl3 . '");' . "\n";
$outputJSON[] = [
'ogr' => $ogr,
'label' => $occupation,
'lvl_1_rome_prefix' => $catLvl1,
'lvl_2_rome_prefix' => $catLvl2,
'lvl_3_rome' => $catLvl3,
];
}
}
}
}
file_put_contents($outputJSONPath, json_encode($outputJSON, JSON_PRETTY_PRINT));
file_put_contents($outputSQLPath, $outputSQL);