Skip to content

Commit 60ec49e

Browse files
authored
Update update_hr_tree.php script to update departments (#139)
1 parent 20ffc93 commit 60ec49e

File tree

6 files changed

+225
-125
lines changed

6 files changed

+225
-125
lines changed

data/add_business_center.sql

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ALTER TABLE `departments`
2+
ADD `bc_org_unit` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL AFTER `org_unit`,
3+
ADD `bc_name` varchar(500) COLLATE utf8_unicode_ci DEFAULT NULL AFTER `bc_org_unit`,
4+
ADD INDEX ( `bc_org_unit` ),
5+
ADD INDEX ( `bc_name`(255) );

data/officefinder.sql

+90-112
Original file line numberDiff line numberDiff line change
@@ -1,137 +1,115 @@
1-
-- phpMyAdmin SQL Dump
2-
-- version 2.11.6
3-
-- http://www.phpmyadmin.net
1+
-- MySQL dump 10.13 Distrib 5.7.28, for macos10.14 (x86_64)
42
--
5-
-- Host: localhost
6-
-- Generation Time: Jul 09, 2010 at 04:50 PM
7-
-- Server version: 5.1.48
8-
-- PHP Version: 5.3.1
9-
10-
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
11-
3+
-- Host: localhost Database: officefinder
4+
-- ------------------------------------------------------
5+
-- Server version 5.7.28
126

137
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
148
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
159
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
1610
/*!40101 SET NAMES utf8 */;
11+
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
12+
/*!40103 SET TIME_ZONE='+00:00' */;
13+
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
14+
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
15+
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
16+
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
1717

1818
--
19-
-- Database: 'officefinder'
20-
--
21-
22-
-- --------------------------------------------------------
23-
24-
--
25-
-- Table structure for table 'departments'
26-
--
27-
28-
CREATE TABLE IF NOT EXISTS departments (
29-
id int(11) NOT NULL AUTO_INCREMENT,
30-
`name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
31-
org_unit varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
32-
building varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
33-
room varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
34-
city varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
35-
state varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
36-
postal_code varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
37-
address varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
38-
phone varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
39-
fax varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
40-
email varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
41-
website varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
42-
acronym varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
43-
alternate varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
44-
PRIMARY KEY (id),
45-
UNIQUE KEY name_UNIQUE (`name`)
46-
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
47-
48-
-- --------------------------------------------------------
49-
50-
--
51-
-- Table structure for table 'department_permissions'
19+
-- Table structure for table `department_aliases`
5220
--
5321

54-
CREATE TABLE IF NOT EXISTS department_permissions (
55-
department_id int(11) NOT NULL,
56-
uid varchar(45) COLLATE utf8_unicode_ci NOT NULL,
57-
PRIMARY KEY (department_id,uid)
22+
DROP TABLE IF EXISTS `department_aliases`;
23+
/*!40101 SET @saved_cs_client = @@character_set_client */;
24+
/*!40101 SET character_set_client = utf8 */;
25+
CREATE TABLE `department_aliases` (
26+
`department_id` int(11) NOT NULL,
27+
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
28+
PRIMARY KEY (`department_id`,`name`),
29+
CONSTRAINT `FK_DEPT_ALIAS_DEPT` FOREIGN KEY (`department_id`) REFERENCES `departments` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
5830
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
59-
60-
-- --------------------------------------------------------
61-
62-
--
63-
-- Table structure for table 'listings'
64-
--
65-
66-
CREATE TABLE IF NOT EXISTS listings (
67-
id int(11) NOT NULL AUTO_INCREMENT,
68-
department_id int(11) NOT NULL,
69-
`name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
70-
phone varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
71-
sort int(11) DEFAULT NULL,
72-
address varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
73-
email varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
74-
uid varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
75-
PRIMARY KEY (id)
76-
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
77-
78-
-- --------------------------------------------------------
31+
/*!40101 SET character_set_client = @saved_cs_client */;
7932

8033
--
81-
-- Table structure for table 'listing_permissions'
34+
-- Table structure for table `department_permissions`
8235
--
8336

84-
CREATE TABLE IF NOT EXISTS listing_permissions (
85-
listing_id int(11) NOT NULL,
86-
uid varchar(45) COLLATE utf8_unicode_ci NOT NULL,
87-
PRIMARY KEY (listing_id,uid)
37+
DROP TABLE IF EXISTS `department_permissions`;
38+
/*!40101 SET @saved_cs_client = @@character_set_client */;
39+
/*!40101 SET character_set_client = utf8 */;
40+
CREATE TABLE `department_permissions` (
41+
`department_id` int(11) NOT NULL,
42+
`uid` varchar(45) COLLATE utf8_unicode_ci NOT NULL,
43+
PRIMARY KEY (`department_id`,`uid`),
44+
CONSTRAINT `FK_DEPT_PERM_DEPT` FOREIGN KEY (`department_id`) REFERENCES `departments` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
8845
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
89-
90-
-- --------------------------------------------------------
46+
/*!40101 SET character_set_client = @saved_cs_client */;
9147

9248
--
93-
-- Table structure for table 'listing_uid'
49+
-- Table structure for table `departments`
9450
--
9551

96-
CREATE TABLE IF NOT EXISTS listing_uid (
97-
listing_id int(11) NOT NULL,
98-
uid varchar(45) COLLATE utf8_unicode_ci NOT NULL,
99-
PRIMARY KEY (listing_id,uid)
100-
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
101-
102-
-- --------------------------------------------------------
52+
DROP TABLE IF EXISTS `departments`;
53+
/*!40101 SET @saved_cs_client = @@character_set_client */;
54+
/*!40101 SET character_set_client = utf8 */;
55+
CREATE TABLE `departments` (
56+
`id` int(11) NOT NULL AUTO_INCREMENT,
57+
`name` varchar(500) COLLATE utf8_unicode_ci DEFAULT NULL,
58+
`org_unit` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'Corresponds to official HR Org number, eg: 5000XXX',
59+
`bc_org_unit` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
60+
`bc_name` varchar(500) COLLATE utf8_unicode_ci DEFAULT NULL,
61+
`building` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
62+
`room` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
63+
`city` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
64+
`state` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
65+
`postal_code` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
66+
`address` varchar(250) COLLATE utf8_unicode_ci DEFAULT NULL,
67+
`phone` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
68+
`fax` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
69+
`email` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
70+
`website` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
71+
`academic` tinyint(1) NOT NULL DEFAULT '0' COMMENT 'Flag to indicate if department is academic',
72+
`suppress` tinyint(1) NOT NULL DEFAULT '0' COMMENT 'Suppress displaying this department? Typically used to hide official SAP orgs with no appointments and children.',
73+
`parent_id` int(11) DEFAULT NULL,
74+
`sort_order` int(11) DEFAULT NULL,
75+
`uid` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'UID corresponding to related IDM user',
76+
`uidlastupdated` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
77+
`dateupdated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
78+
`rgt` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
79+
`lft` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
80+
`level` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
81+
PRIMARY KEY (`id`),
82+
UNIQUE KEY `org_unit` (`org_unit`),
83+
KEY `name` (`name`(255)),
84+
KEY `parent_id` (`parent_id`),
85+
KEY `academic` (`academic`),
86+
KEY `suppress` (`suppress`),
87+
KEY `bc_org_unit` (`bc_org_unit`),
88+
KEY `bc_name` (`bc_name`(255))
89+
) ENGINE=InnoDB AUTO_INCREMENT=6131 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
90+
/*!40101 SET character_set_client = @saved_cs_client */;
10391

10492
--
105-
-- Table structure for table 'telecom_departments'
93+
-- Table structure for table `telecom_unidaslt_to_departments`
10694
--
10795

108-
CREATE TABLE IF NOT EXISTS telecom_departments (
109-
lGroup_id bigint(20) DEFAULT NULL,
110-
tiSplit varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
111-
sLstTyp varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
112-
sLstSty varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
113-
iSeqNbr int(11) DEFAULT NULL,
114-
tiIndDrg varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
115-
sNPA1 varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
116-
sNPA2 varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
117-
sNPA3 varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
118-
sPhoneNbr1 varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
119-
sPhoneNbr2 varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
120-
sPhoneNbr3 varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
121-
sExtension1 varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
122-
sExtension2 varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
123-
szLname varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
124-
szFname varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
125-
szAddtText varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
126-
szAddress varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
127-
szDepartmentalAddress varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
128-
szLocName varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
129-
szStateName varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
130-
sZipCd5 varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
131-
sZipCd4 varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
132-
szDirLname varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
133-
szDirFname varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
134-
szDirAddText varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
135-
mFreeFormText varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
136-
szNonStdPh varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL
96+
DROP TABLE IF EXISTS `telecom_unidaslt_to_departments`;
97+
/*!40101 SET @saved_cs_client = @@character_set_client */;
98+
/*!40101 SET character_set_client = utf8 */;
99+
CREATE TABLE `telecom_unidaslt_to_departments` (
100+
`lMaster_id` bigint(20) unsigned NOT NULL,
101+
`department_id` bigint(20) unsigned NOT NULL,
102+
PRIMARY KEY (`lMaster_id`,`department_id`)
137103
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
104+
/*!40101 SET character_set_client = @saved_cs_client */;
105+
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
106+
107+
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
108+
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
109+
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
110+
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
111+
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
112+
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
113+
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
114+
115+
-- Dump completed on 2020-02-27 15:45:39

data/update_hr_tree.php

+87-9
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,20 @@
2626
*/
2727
function updateOfficialDepartment(UNL_Peoplefinder_Department $sap_dept, UNL_Officefinder_Department &$parent = null)
2828
{
29-
3029
if (!($dept = UNL_Officefinder_Department::getByorg_unit($sap_dept->org_unit))) {
3130
// Uhoh, new department!
3231
$dept = new UNL_Officefinder_Department();
3332

34-
// Now update all fields with the official data from SAP
35-
updateFields($dept, $sap_dept);
33+
// Now add department with the official data from SAP
34+
addDepartment($dept, $sap_dept);
3635

37-
echo 'New department found:'.$dept->name.' ('.$dept->org_unit.')'.PHP_EOL;
36+
echo 'New department found: '.$dept->name.' ('.$dept->org_unit.')'.PHP_EOL;
37+
} elseif (hasUpdate($dept, $sap_dept)) {
38+
39+
// Found changes in existing department so update it
40+
echo PHP_EOL . 'Existing department found with updateable changes: '.$dept->name.' ('.$dept->org_unit.')'.PHP_EOL;
41+
updateDepartment($dept, $sap_dept);
42+
echo "\tExisting department updated with changes: ".$dept->name.' ('.$dept->org_unit.')'.PHP_EOL;
3843
}
3944

4045
if ($parent) {
@@ -63,17 +68,90 @@ function updateOfficialDepartment(UNL_Peoplefinder_Department $sap_dept, UNL_Off
6368
* @param $old Object with the old data
6469
* @param $new Object with the new data
6570
*/
66-
function updateFields(UNL_Officefinder_Department $old, UNL_Peoplefinder_Department $new)
71+
function addDepartment(UNL_Officefinder_Department $old, UNL_Peoplefinder_Department $new)
6772
{
68-
foreach ($old as $key=>$val) {
69-
if (isset($new->$key)
70-
&& $key != 'options') {
71-
$old->$key = $new->$key;
73+
try {
74+
foreach ($old as $key => $val) {
75+
echo "\t" . $key . ": " . $val . PHP_EOL;
76+
if (isset($new->$key)
77+
&& $key != 'options') {
78+
$old->$key = $new->$key;
79+
}
7280
}
81+
82+
// Set updated by attributes
83+
$old->uidlastupdated = 'hr_tree';
84+
$old->dateupdated = date('Y-m-d H:m:s');
85+
7386
// Save it
7487
$old->save();
7588
if (!empty($new->org_abbr)) {
7689
$old->addAlias($new->org_abbr);
7790
}
91+
} catch (Exception $e) {
92+
echo 'Warning caught exception when adding ' . $old->name . ': ', $e->getMessage(), PHP_EOL;
7893
}
7994
}
95+
96+
/**
97+
* This method is used to update data within the UNL directory. It allows updates
98+
* only on certain fields.
99+
*
100+
* @param $old Object with the old data
101+
* @param $new Object with the new data
102+
*/
103+
function updateDepartment(UNL_Officefinder_Department $old, UNL_Peoplefinder_Department $new)
104+
{
105+
try {
106+
foreach ($old as $key => $val) {
107+
if (isUpdateableField($key) && isset($new->$key) && $key != 'options') {
108+
echo "\tUpdating field " . $key . ": from '" . $val . "' to '" . $new->$key . "'" . PHP_EOL;
109+
$old->$key = $new->$key;
110+
}
111+
}
112+
113+
// Set updated by attributes
114+
$old->uidlastupdated = 'hr_tree';
115+
$old->dateupdated = date('Y-m-d H:m:s');
116+
117+
// Save it
118+
$old->save();
119+
} catch (Exception $e) {
120+
echo 'Warning caught exception when updating ' . $old->name . ': ', $e->getMessage(), PHP_EOL;
121+
}
122+
}
123+
124+
/**
125+
* This method is used to compare the department objects for changes to help determin if it should be
126+
* updated.
127+
*
128+
* @param $old Object with the old data
129+
* @param $new Object with the new data
130+
*/
131+
function hasUpdate(UNL_Officefinder_Department $old, UNL_Peoplefinder_Department $new)
132+
{
133+
foreach ($old as $key => $val) {
134+
if (isUpdateableField($key) && isset($new->$key) && $key != 'options' && $old->$key != $new->$key) {
135+
return true;
136+
}
137+
}
138+
return false;
139+
}
140+
141+
/**
142+
* This method determines if field maybe updated on department from SAP value
143+
* Note: Most fields are not updateable since the officefinder version is maintained via directory and
144+
* we want to preserve those customized values.
145+
* @param $field
146+
* @return bool
147+
*/
148+
function isUpdateableField($field) {
149+
// Note org_unit will never be updateable since it's an unique identifier for a department
150+
// These fields should probably only be fields provided by SAP but not editable via directory admin
151+
$updateableFields = array(
152+
'bc_org_unit',
153+
'bc_name'
154+
);
155+
156+
return in_array($field, $updateableFields);
157+
}

0 commit comments

Comments
 (0)