-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTIScsv2array.php
124 lines (93 loc) · 3.34 KB
/
TIScsv2array.php
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
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/*************************** APORIA WorksRegistration Class ***************************
APORIA Works Registration
Copyright © 2016, 2017 Gord Dimitrieff <[email protected]>
This file is part of APORIA Works Registration, a PHP library for reading,
writing and manipulating CISAC Common Works Registration (CWR) files.
APORIA Works Registration is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
APORIA Works Registration is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with APORIA Works Registration. If not, see <http://www.gnu.org/licenses/>.
*/
/*
TIScsv2array.php
This script generates PHP code from CISAC's TIS excel file (saved as a csv), which is then included by the WorksRegistration class.
You only need to use this script if you need to regenerate the TIS-data.php file for any reason.
*/
ini_set('auto_detect_line_endings', TRUE);
function buildTree(array $elements, $parentId = 0) {
$branch = array();
foreach ($elements as $element) {
if ($element['parent_id'] == $parentId) {
$children = buildTree($elements, $element['TIS-N']);
if ($children) {
$element['children'] = $children;
}
$branch[] = $element;
}
}
return $branch;
}
printf("Reading file...\n");
$rows = array_map('str_getcsv', file('TIS.csv'));
$header = array_shift($rows);
$csv = array();
foreach ($rows as $row)
$csv[] = array_combine($header, $row);
$position = 0;
$lastTIS = array();
$level = 1;
$TIS_List = array();
while($position < count($csv))
{
$row =& $csv[$position];
$previous_level = $level;
$level = $row['Level'];
$TIS = $row['TIS-N'];
$row['Exists from'] = null;
$row['Exists until'] = null;
$row['Belongs to from'] = null;
$row['Belongs to until'] = null;
unset($row['Exists from']);
unset($row['Exists until']);
unset($row['Belongs to from']);
unset($row['Belongs to until']);
$row['Type'] = null;
unset($row['Type']);
$row['Level'] = null;
unset($row['Level']);
$TIS_List[$TIS] = $row['Name'];
if($level == $previous_level && $level > 1)
{
$row['parent_id'] = $last_level_position[$level-1];
}
if($level > $previous_level && $level > 1)
{
$row['parent_id'] = $last_level_position[$previous_level];
}
if($level < $previous_level)
{
$row['parent_id'] = $last_level_position[$level-1];
}
$last_level_position[$level] = $row['TIS-N'];
$position++;
}
printf("Exporting CSV...\n");
$fp = fopen('TIS-keyed.csv', 'w');
fputcsv($fp, array_keys($row));
foreach ($csv as $row) {
fputcsv($fp, $row);
}
fclose($fp);
printf("Building data tree...\n");
$tree = buildTree($csv);
printf("Writing php code...\n");
file_put_contents ("TIS-data.php" , "<?php\n/*\tThis file is an integral part of APORIA Works Registration, and was automatically generated by TIScsv2array.php\n\tVisit http://www.aporia-records.com/aws for more information.\n*/\n\$this->TISdata = ".var_export($tree, true).";\n?>" );
printf("Done.\n");
?>