-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathimport_yellow_pages.php
441 lines (393 loc) · 17 KB
/
import_yellow_pages.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
<?php
require_once dirname(__FILE__).'/../www/config.inc.php';
error_reporting(E_ALL | E_STRICT);
$db = new mysqli('localhost', UNL_Officefinder::$db_user, UNL_Officefinder::$db_pass, UNL_Officefinder::$db_name);
echo '<pre>';
// OK Wipe the DB
$db->query('TRUNCATE departments');
$db->query('TRUNCATE department_aliases');
$db->query('TRUNCATE department_permissions');
$db->query('TRUNCATE telecom_unidaslt_to_departments');
$sap_dept = new UNL_Peoplefinder_Department(array('d'=>'University of Nebraska - Lincoln'));
if ($root = UNL_Officefinder_Department::getByID(1)) {
// Found an existing root
} else {
// Add a new root entry
// Import OFFICIAL departments into the database
$root = new UNL_Officefinder_Department();
$root->setAsRoot();
updateFields($root, $sap_dept);
}
// Now crawl through all the official departments and update the data.
updateOfficialDepartment($sap_dept);
// Get root again, because the left and right values have changed!
$root = UNL_Officefinder_Department::getById(1);
foreach (array(
'Buildings',
'Copy Centers',
'Religious Groups',
//'Fax Access Numbers',
//'Vice Chancellors',
//'UNOPA (University of Nebraska Office Professionals Association)',
//'Operated By Follett Higher Education Group University Bookstore',
//'UN Computing Services Network (University of Nebraska Central Administration)',
//'UAAD Officers & Non-Committee Chair Executive Board',
//'Administration',
//'Colleges',
//'Graduate Assistants',
) as $semi_official_dept_name) {
if (!($semi_official = UNL_Officefinder_Department::getByname($semi_official_dept_name))) {
$semi_official = new UNL_Officefinder_Department();
$semi_official->name = $semi_official_dept_name;
$semi_official->org_unit = '_'.md5($semi_official_dept_name);
$semi_official->save();
$root->addChild($semi_official);
}
}
$cleanup_file = new SplFileObject(dirname(__FILE__).'/Centrex Cleanup.csv');
$cleanup_file->setFlags(SplFileObject::READ_CSV);
//checkCleanupFile($cleanup_file);
//exit();
// Now rename a couple departments that are really mis-named:
foreach (array(
'50001186'=>'IANR Information Services'
) as $old=>$new) {
$dept = UNL_Officefinder_Department::getByOrg_unit($old);
if ($dept === false) {
$dept = UNL_Officefinder_Department::getByName($old);
}
$dept->name = $new;
$dept->save();
}
function updateOfficialDepartment(UNL_Peoplefinder_Department $sap_dept, UNL_Officefinder_Department &$parent = null)
{
if (!($dept = UNL_Officefinder_Department::getByorg_unit($sap_dept->org_unit))) {
$dept = new UNL_Officefinder_Department();
}
// Now update all fields with the official data from SAP
updateFields($dept, $sap_dept);
echo '.';
flush();
if ($parent) {
if ($dept->isChildOf($parent)) {
// All OK!
} else {
if ($dept->hasChildren()) {
throw new Exception('Err, hmm. This is an existing department with children has moved, I can\'t handle that yet! The department name is '.$dept->name.' with org_unit id = '.$dept->org_unit);
} else {
$parent->addChild($dept, true);
}
}
}
if ($sap_dept->hasChildren()) {
foreach ($sap_dept->getChildren() as $sap_sub_dept) {
updateOfficialDepartment($sap_sub_dept, $dept);
}
}
}
function updateFields(UNL_Officefinder_Department $old, UNL_Peoplefinder_Department $new) {
foreach ($old as $key=>$val) {
if (isset($new->$key)
&& $key != 'options') {
$old->$key = $new->$key;
}
// Save it
$old->save();
if (!empty($new->org_abbr)) {
$old->addAlias($new->org_abbr);
}
}
}
$known_as = array();
if ($result = $db->query('SELECT * FROM telecom_departments WHERE sLstTyp=1 AND iSeqNbr=0;')) {
printf("Select returned %d rows.\n", $result->num_rows);
// Official department search
$dept_search = new SimpleXMLElement(file_get_contents(UNL_Peoplefinder::getDataDir().'/hr_tree.xml'));
while($obj = $result->fetch_object()){
if (preg_match('/(.*)\(see (.*)\)/', $obj->szLname.' '.$obj->szFname.' '.$obj->szAddtText, $matches)) {
// known-as listing
echo 'known as listing!!'.$obj->szLname.' '.$obj->szFname.' '.$obj->szAddtText.PHP_EOL;
$known_as[] = array(cleanField($matches[2]), cleanField($matches[1]));
continue;
}
$clean_name = cleanField($obj->szLname.' '.$obj->szFname.' '.$obj->szAddtText.' '.$obj->mFreeFormText);
echo $clean_name.', '.$obj->lMaster_id.'<br>'.PHP_EOL;
$dept = false;
$official_dept = false;
$parent_dept = false;
try {
$official_dept = new UNL_Peoplefinder_Department(array('d'=>$clean_name));
// Found an official match
$dept = UNL_Officefinder_Department::getByorg_unit($official_dept->org_unit);
if (!$dept) {
echo 'error finding official org record';
exit();
}
} catch (Exception $e) {
if (isset($postal_code)) {
$results = $dept_search->xpath('//attribute[@name="org_unit"][@value="50000003"]/..//attribute[@name="postal_code"][@value="'.$postal_code.'"]/..');
if (count($results)) {
// Found a match on the zip code
$official_dept = new UNL_Peoplefinder_Department(array('d'=>(string)$results[0][0]->attribute['value']));
$dept = UNL_Officefinder_Department::getByorg_unit($official_dept->org_unit);
echo $name.'=>'.$official_dept->name.PHP_EOL;
}
}
if (!$dept) {
// Both those failed, check the cleanup file
// Not an official department, no clue where this goes, check the cleanup file
foreach ($cleanup_file as $row) {
if ($clean_name == $row[0]) {
// Found an entry in the cleanup file
if (isset($row[3])) {
switch(strtolower($row[3])) {
case 'delete':
case 'delete?':
case 'remove?':
case 'remove':
continue 2;
break;
default:
break;
}
}
if (!empty($row[1])) {
// THis maps to an official entry
if (substr($row[1], 0, 1) == '5') {
$dept = UNL_Officefinder_Department::getByorg_unit($row[1]);
} else {
try {
$official_dept = new UNL_Peoplefinder_Department(array('d'=>$row[1]));
$dept = UNL_Officefinder_Department::getByorg_unit($official_dept->org_unit);
} catch(Exception $e) {
$dept = UNL_Officefinder_Department::getByname($row[1], 'org_unit IS NOT NULL');
}
}
} elseif (!empty($row[2])) {
// Found a parent
if (substr($row[2], 0, 1) == '5') {
$parent_dept = UNL_Officefinder_Department::getByorg_unit((int)$row[2]);
if ($parent_dept == false) {
throw new Exception($row[2].' is an invalid offical org number');
}
} else {
$parent_dept = UNL_Officefinder_Department::getByname($row[2], 'org_unit IS NOT NULL');
}
if (!$parent_dept) {
// Don't know about this department yet, let's add it
$parent_dept = new UNL_Officefinder_Department();
$parent_dept->name = $row[2];
$parent_dept->save();
UNL_Officefinder_Department::getByID(1)->addChild($parent_dept, true);
}
}
break;
}
}
}
}
if (false === $dept) {
// New department, no clue where this goes
$dept = new UNL_Officefinder_Department();
$dept->name = $clean_name;
}
// Existing SAP Fields
// $dept->org_unit = NULL;
// $dept->building = NULL;
// $dept->room = NULL;
// $dept->city = NULL;
// $dept->state = NULL;
// $dept->postal_code = NULL;
if (trim($obj->sZipCd5) != '' || trim($obj->sZipCd4) != '') {
if (trim($obj->sZipCd5) == '') {
// Assume 68588
$obj->sZipCd5 = '68588';
}
if (strlen(trim($obj->sZipCd4)) == 3) {
$obj->sZipCd4 = '0'.trim($obj->sZipCd4);
}
$dept->postal_code = trim($obj->sZipCd5).'-'.trim($obj->sZipCd4);
}
// Added fields
$dept->address = trim($obj->szAddress);
$dept->phone = '';
if (trim($obj->sNPA1) !== '') {
$dept->phone = trim($obj->sNPA1).'-'.preg_replace('/([\d]{3})([\d]{4})/', '$1-$2', trim($obj->sPhoneNbr1));
}
// $dept->fax = NULL;
// $dept->email = NUll;
// $dept->website = NULL;
// $dept->acronym = NULL;
// $dept->known_as = NULL;
$dept->save();
$db->query('INSERT INTO telecom_unidaslt_to_departments VALUES ('.$obj->lMaster_id.','.$dept->id.');');
if (false === $official_dept
&& !isset($dept->org_unit)) {
// Find where the parent is
if ($parent_dept
&& !$dept->isChildOf($parent_dept)) {
$parent_dept->addChild($dept, true);
} else {
if (!$dept->isChildOf(UNL_Officefinder_Department::getById(1))) {
UNL_Officefinder_Department::getById(1)->addChild($dept, true);
}
}
}
$sql = "SELECT * FROM telecom_departments WHERE lGroup_id={$obj->lGroup_id} AND iSeqNbr !=0 ORDER BY iSeqNbr ASC;";
$listings = $db->query($sql);
if ($listings->num_rows) {
$k = 0;
$indentation_levels = array();
$indentation_levels[0] = $dept;
$last_added = $dept;
while ($listing = $listings->fetch_object()) {
$child_clean_name = cleanField($listing->szDirLname.' '.$listing->szDirFname.' '.$listing->szDirAddText.' '.$listing->mFreeFormText, false);
$child = UNL_Officefinder_Department::getByname($child_clean_name, 'org_unit IS NULL AND parent_id = '.$dept->id);
if ($child instanceof UNL_Officefinder_Department) {
continue;
}
echo str_repeat('-', $listing->tiIndDrg).$child_clean_name.PHP_EOL;
$child = new UNL_Officefinder_Department();
$child->name = $child_clean_name;
if (trim($listing->sNPA1) !== '') {
$child->phone = trim($listing->sNPA1).'-'.preg_replace('/([\d]{3})([\d]{4})/', '$1-$2', trim($listing->sPhoneNbr1));
}
$child->address = trim($listing->szAddress);
// $child->email = NULL;
// $child->uid = NULL;
$child->save();
$db->query('INSERT INTO telecom_unidaslt_to_departments VALUES ('.$listing->lMaster_id.','.$child->id.');');
$i = $listing->tiIndDrg-1;
if (!isset($indentation_levels[$i])) {
$last_added->reload();
$last_added->addChild($child, true);
} else {
$indentation_levels[$i]->reload();
$indentation_levels[$i]->addChild($child, true);
}
// Store the last child at this level
$indentation_levels[$listing->tiIndDrg] = $child;
$last_added = $child;
}
}
$listings->close();
// echo PHP_EOL;
}
/* free result set */
$result->close();
include(dirname(__FILE__).'/minor_data_cleanups.php');
}
$db->close();
foreach ($known_as as $data) {
addKnownAs($data[0], $data[1]);
}
function cleanField($text, $correct_case = true)
{
if ($correct_case) {
$text = strtolower($text);
$text = ucwords($text);
}
$text = preg_replace('/Of([\s]|$)/', ' of$1', $text);
$text = str_replace('And ', '& ', $text);
$text = str_replace('For ', 'for ', $text);
$text = preg_replace('/Unl([\s]|$)/', 'UNL$1', $text);
$text = preg_replace('/Uno([\s]|$)/', 'UNO$1', $text);
$text = preg_replace('/Unk([\s]|$)/', 'UNK$1', $text);
$text = preg_replace('/Unmc([\s]|$)/', 'UNMC$1', $text);
$text = preg_replace('/Uaad([\s]|$)/', 'UAAD$1', $text);
$text = preg_replace('/Unopa([\s]|$)/', 'UNOPA$1', $text);
while(preg_match('/"([a-z])"/', $text, $matches)) {
$text = str_replace($matches[0], strtoupper($matches[0]), $text);
}
if (preg_match('/, (.*)[\s]+?$/', $text, $matches)) {
switch ($matches[1]) {
case 'Office of':
case 'Office of':
case 'Dept. of':
case 'Department of':
case 'Dept of':
case 'Dept of':
case 'Department of':
case 'Division of':
case 'Institute of':
case 'Institute for':
case 'School of':
case 'School of':
case 'The':
case 'College of':
case 'College of':
case 'Office of ':
case 'University':
case 'University ':
$text = $matches[1].' '.str_replace($matches[0], '', $text);
break;
default:
echo PHP_EOL,'UNKKKK'.$matches[1].PHP_EOL;
}
}
$text = preg_replace_callback('/\(([a-z])/', function($matches) {return '('.ucfirst($matches[1]);}, $text);
$text = preg_replace_callback('/\-([a-z])/', function($matches) {return '-'.ucfirst($matches[1]);}, $text);
$text = preg_replace('/\(ec\)/i', '(EC)', $text);
$text = str_replace('(Apc)', '(APC)', $text);
$text = str_replace('Ianr', 'IANR', $text);
$text = str_replace(' ', ' ', trim($text));
$text = str_replace(' ', ' ', trim($text));
$text = preg_replace('/^Dept\.? of /', '', $text);
//echo $text.PHP_EOL;
return trim($text);
}
function checkCleanupFile($cleanup_file)
{
foreach ($cleanup_file as $row) {
// Found an entry in the cleanup file
if (!empty($row[1])) {
if ($row[1] == 'Official SAP #/Name') {
continue;
}
// THis maps to an official entry
if (substr($row[1], 0, 1) == '5') {
$official_dept = UNL_Officefinder_Department::getByorg_unit($row[1]);
} else {
$official_dept = UNL_Officefinder_Department::getByname($row[1], 'org_unit IS NOT NULL');
}
if (!$official_dept) {
echo 'I Dunno who this OFFICIAL dept is '.$row[0].'=>'.$row[1].'<br />';
}
continue;
} elseif (!empty($row[2])) {
// Found a parent
if (substr($row[2], 0, 1) == '5') {
$parent_dept = UNL_Officefinder_Department::getByorg_unit($row[2]);
} else {
$parent_dept = UNL_Officefinder_Department::getByname($row[2]);
if (!$parent_dept) {
// Don't know about this department yet, let's add it
// $parent_dept = new UNL_Officefinder_Department();
// $parent_dept->name = $row[2];
// $parent_dept->save();
//$root->addChild($parent_dept);
}
}
if (!$parent_dept) {
echo 'I Dunno who this PARENT dept is '.$row[0].'=>'.$row[2].'<br />';
}
continue;
}
}
}
function addKnownAs($official_dept_name, $alias) {
switch($official_dept_name) {
case 'University Communications':
$official_dept_name = 'Office of University Communications';
break;
case 'Residence Halls':
$official_dept_name = 'University Housing';
break;
}
$parent_dept = UNL_Officefinder_Department::getByname($official_dept_name, 'org_unit IS NOT NULL');
if (!$parent_dept) {
echo 'Could not find the alias!'.$official_dept_name.'<br>'.PHP_EOL;
} else {
$parent_dept->addAlias($alias);
}
}