@@ -23,6 +23,13 @@ class UNL_Peoplefinder_Department implements Countable, Iterator
23
23
* @var number
24
24
*/
25
25
public $ org_unit ;
26
+
27
+ /**
28
+ * Organization abbreviation
29
+ *
30
+ * @var string
31
+ */
32
+ public $ org_abbr ;
26
33
27
34
/**
28
35
* Building the department main office is in.
@@ -63,29 +70,73 @@ class UNL_Peoplefinder_Department implements Countable, Iterator
63
70
64
71
protected $ _results ;
65
72
66
- protected $ _xml ;
73
+ /**
74
+ * SimpleXMLElement of the HR Tree file
75
+ *
76
+ * @var SimpleXMLElement
77
+ */
78
+ protected static $ _xml ;
79
+
80
+ public $ options = array ();
67
81
68
82
/**
69
83
* construct a department
70
84
*
71
85
* @param string $name Name of the department
72
86
*/
73
- function __construct ($ name )
87
+ function __construct ($ options = array () )
74
88
{
75
- $ this ->name = $ name ;
76
- $ this ->_xml = new SimpleXMLElement (file_get_contents (dirname (__FILE__ ).'/../../data/hr_tree.xml ' ));
77
- $ results = $ this ->_xml ->xpath ('//attribute[@name="org_unit"][@value="50000003"]/..//attribute[@name="name"][@value=" ' .$ this ->name .'"]/.. ' );
78
- if (isset ($ results [0 ])) {
79
- foreach ($ results [0 ] as $ attribute ) {
80
- if (isset ($ attribute ['name ' ])) {
81
- $ this ->{$ attribute ['name ' ]} = (string )$ attribute ['value ' ];
82
- }
89
+ if (!(
90
+ isset ($ options ['d ' ])
91
+ || isset ($ options ['org_unit ' ])
92
+ || isset ($ options ['xml ' ])
93
+ )
94
+ ) {
95
+ throw new Exception ('No department name or org_unit! Pass as the d or org_unit value. ' );
96
+ }
97
+ $ this ->options = $ options + $ this ->options ;
98
+
99
+ $ xml = self ::getXML ();
100
+
101
+ $ result = false ;
102
+
103
+ if (isset ($ options ['xml ' ])) {
104
+ $ result = $ options ['xml ' ];
105
+ } elseif (isset ($ options ['org_unit ' ])) {
106
+ $ result = self ::getXMLById ($ options ['org_unit ' ]);
107
+ } elseif (isset ($ options ['d ' ])) {
108
+ $ result = self ::getXMLByName ($ options ['d ' ]);
109
+ }
110
+
111
+ if (!$ result ) {
112
+ throw new Exception ('Invalid department ' , 404 );
113
+ }
114
+
115
+ $ this ->setFromSimpleXMLElement ($ result );
116
+ }
117
+
118
+ public function setFromSimpleXMLElement (SimpleXMLElement $ result )
119
+ {
120
+ foreach ($ result as $ attribute ) {
121
+ if (isset ($ attribute ['name ' ])) {
122
+ $ this ->{$ attribute ['name ' ]} = (string )$ attribute ['value ' ];
83
123
}
84
- } else {
85
- throw new Exception ('Invalid department name. ' );
86
124
}
87
125
}
88
126
127
+ /**
128
+ * Get the XML for the HR Tree
129
+ *
130
+ * @return SimpleXMLElement
131
+ */
132
+ protected static function getXML ()
133
+ {
134
+ if (!isset (self ::$ _xml )) {
135
+ self ::$ _xml = new SimpleXMLElement (file_get_contents (UNL_Peoplefinder::getDataDir ().'/hr_tree.xml ' ));
136
+ }
137
+ return self ::$ _xml ;
138
+ }
139
+
89
140
/**
90
141
* Retrieves people records from the LDAP directory
91
142
*
@@ -94,17 +145,9 @@ function __construct($name)
94
145
function getLDAPResults ()
95
146
{
96
147
if (!isset ($ this ->_results )) {
97
- $ options = array (
98
- 'bind_dn ' => UNL_Peoplefinder_Driver_LDAP::$ bindDN ,
99
- 'bind_password ' => UNL_Peoplefinder_Driver_LDAP::$ bindPW ,
100
- );
101
-
102
- $ this ->_ldap = UNL_LDAP ::getConnection ($ options );
103
- $ name = str_replace (array ('( ' ,') ' ,'* ' ,'\'' ,'" ' ), '' , $ this ->name );
104
- $ this ->_results = $ this ->_ldap ->search ('dc=unl,dc=edu ' ,
105
- '(unlHRPrimaryDepartment= ' .$ name .') ' );
106
- $ this ->_results ->sort ('cn ' );
107
- $ this ->_results ->sort ('sn ' );
148
+ UNL_Peoplefinder::$ resultLimit = 500 ;
149
+ $ pf = new UNL_Peoplefinder ($ this ->options );
150
+ $ this ->_results = $ pf ->getHROrgUnitNumberMatches ($ this ->org_unit );
108
151
}
109
152
return $ this ->_results ;
110
153
}
@@ -131,7 +174,7 @@ function rewind()
131
174
*/
132
175
function current ()
133
176
{
134
- return UNL_Peoplefinder_Record:: fromUNLLDAPEntry ( $ this ->getLDAPResults ()->current () );
177
+ return $ this ->getLDAPResults ()->current ();
135
178
}
136
179
137
180
function key ()
@@ -151,24 +194,61 @@ function valid()
151
194
152
195
function hasChildren ()
153
196
{
154
- $ results = $ this -> _xml -> xpath ('//attribute[@name="org_unit"][@value="50000003"]/..//attribute[@name="name "][@value=" ' .$ this ->name .'"]/../branch ' );
197
+ $ results = self :: getXML ()-> xpath ('//attribute[@name="org_unit"][@value="50000003"]/..//attribute[@name="org_unit "][@value=" ' .$ this ->org_unit .'"]/../branch ' );
155
198
return count ($ results )?true :false ;
156
199
}
157
200
158
201
function getChildren ()
159
202
{
160
203
$ children = array ();
161
- $ results = $ this -> _xml -> xpath ('//attribute[@name="org_unit"][@value="50000003"]/..//attribute[@name="name "][@value=" ' .$ this ->name .'"]/../branch ' );
204
+ $ results = self :: getXML ()-> xpath ('//attribute[@name="org_unit"][@value="50000003"]/..//attribute[@name="org_unit "][@value=" ' .$ this ->org_unit .'"]/../branch ' );
162
205
foreach ($ results as $ result ) {
163
206
foreach ($ result [0 ] as $ attribute ) {
164
207
if (isset ($ attribute ['name ' ])
165
- && $ attribute ['name ' ]=='name ' ) {
166
- $ children [] = ( string )$ attribute ['value ' ];
208
+ && $ attribute ['name ' ]=='org_unit ' ) {
209
+ $ children [] = self :: getById (( string )$ attribute ['value ' ]) ;
167
210
break ;
168
211
}
169
212
}
170
213
}
171
- asort ( $ children );
214
+
172
215
return $ children ;
173
216
}
217
+
218
+ /**
219
+ * Retrieve an official SAP Org entry by ID
220
+ *
221
+ * @param int $id ID, such as 5000XXXX
222
+ */
223
+ public static function getById ($ id , $ options = array ())
224
+ {
225
+ if ($ result = self ::getXMLById ($ id )) {
226
+ $ options ['xml ' ] = $ result ;
227
+ return new self ($ options );
228
+ }
229
+ return $ result ;
230
+ }
231
+
232
+ public static function getXMLByName ($ name )
233
+ {
234
+ $ xml = self ::getXML ();
235
+ $ quoted = preg_replace ('/([ \'\"\?])/ ' , '\\$1 ' , $ name );
236
+ $ xpath = '//attribute[@name="org_unit"][@value="50000003"]/..//attribute[@name="name"][@value=" ' .$ quoted .'"]/.. ' ;
237
+ $ results = $ xml ->xpath ($ xpath );
238
+ if (!$ results ) {
239
+ return false ;
240
+ }
241
+ return $ results [0 ];
242
+ }
243
+
244
+ public static function getXMLById ($ id )
245
+ {
246
+ $ xml = self ::getXML ();
247
+ $ xpath = '//attribute[@name="org_unit"][@value="50000003"]/..//attribute[@name="org_unit"][@value= ' .$ id .']/.. ' ;
248
+ $ results = $ xml ->xpath ($ xpath );
249
+ if (!$ results ) {
250
+ return false ;
251
+ }
252
+ return $ results [0 ];
253
+ }
174
254
}
0 commit comments