/
libimport.php
201 lines (158 loc) · 4.9 KB
/
libimport.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
125
126
127
128
129
130
131
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
<?PHP
class import
{
//------------------------------------------------------------------------------
// analyse imported file
// $path ... path to file for analysing
// $title ... if true, don't use first line
// $parseId ... if true, first entry of line is id
function analyse($path,$parent,$title,$root,$parseId,$action,$levelCnt)
{
//echoall($path);
$importArray = import::fetch_file_to_array($path);
$delimiter = ";";
// echoarray($importArray);
?>
<style>
table {
border:1px;
padding:0px;
margin:0px;
}
th {
color:black;
border-width:1px;
font-size:10pt;
}
td {
color:black;
border-width:1px;
font-size:10pt;
}
</style>
<?PHP
if (is_array($importArray))
{
echo "<h2>Analyse $path</h2>";
// $levelCnt = 7;
$linePos = 0;
echo "<table border='1' cellpadding='0' cellspacing='0'>";
if ($parseId) echo "<th>ID</th>";
for ($li=0;$li<$levelCnt;$li++)
{
echo "<th>Level $li</th>";
}
echo "<th>Herkunft</th>";
echo "<th>Bemerkung</th>";
foreach($importArray as $entry)
{
if ($linePos != 0 or !$title) // filter title line
{
$lineArray = explode($delimiter,$entry);
$colPos = 0;
if ($parseId) $entryid = $lineArray[0];
// loop for entries
if ($parseId) $start = 1; else $start = 0; // filter id
for($i = $start;$i<count($lineArray);$i++)
{
$lineEntry = str_replace("\n",'',$lineArray[$i]);
$lineEntry = str_replace("\r",'',$lineEntry);
$source = $lineArray[$li+1];
$comment = $lineArray[$li+2];
if ($lineEntry) // value found
{
$parentArray[$colPos] = $lineEntry; // utf8_encode($lineEntry);
// insert into database
if ($action == "insert")
{
$parentName = $parentArray[$colPos-1];
if (!$parentName) $parentName = thesaurus::get_name($parent); // no parent name -> use thesaurus
if ($root) $source = $root;
$newId = import::insert($lineEntry,$parentName,"$idName=$entryid;Root=$source;Bemerkung=$comment");
}
break;
}
$colPos++;
}
// remove unused array items
for ($i = $colPos + 1;$i < 7;$i++)
{
$parentArray[$i] = "";
}
// display line
echo "<tr>";
if ($parseId) echo "<td>$entryid</td>";
else echo "<td> </td>";
foreach ($parentArray as $entry)
echo "<td>" . $entry . "</td>";
if ($source) echo "<td>$source</td>";
else echo "<td> </td>";
if ($comment) echo "<td>$comment</td>";
else echo "<td> </td>";
if ($newId) echo "<td><i><b>($newId)</b></i></td>";
echo "</tr>";
}
else
{
// header
$lineArray = explode($delimiter,$entry);
$idName = $lineArray[0];
$sourceName = $lineArray[7];
$commentName = $lineArray[8];
}
$linePos++; // next line of file
}
echo "</table>";
}
}
//------------------------------------------------------------------------------
// fetch file from path
function fetch_file_to_array($path)
{
if (file_exists($path))
{
return(file($path));
}
}
//------------------------------------------------------------------------------
// insert new entry and create link
function insert($name,$parent,$comment)
{
//debug
//echo "$name - $parent - $comment<br>";
$owner = user::id(); // user id
$status = 2; // descriptor
$linktype = 1; // hyrarchic link
$parentArray = search::get($parent,"exact");
$entryArray = search::get($name,"exact");
$parentId = $parentArray[0][ID];
$entryId = $entryArray[0][ID];
// entry already exists - create polyseme
if ($entryId)
{
$name = "$name ($parent)";
}
// create new entry
$entryString = "INSERT INTO entry SET
name='$name',
owner='$owner',
status='$status',
comment='$comment'";
mysql_query($entryString);
// get new id
$newId = mysql_insert_id();
//debug
//echo "$parent ($parentId) > $name ($newId)<br>";
// create link to parent
if ($newId)
{
$linkString = "INSERT INTO parent SET
parent='$parentId',
child='$newId',
type='$linktype'";
mysql_query($linkString);
}
return ($name);
}
}
?>