1 | <?php |
---|
2 | require_once($_SESSION['phpgw_info']['workflow']['phpgw_api_inc'] . '/class.config.inc.php'); |
---|
3 | |
---|
4 | /** |
---|
5 | * @package Workflow |
---|
6 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
7 | * @author Sidnei Augusto Drovetto Jr. - drovetto@gmail.com |
---|
8 | */ |
---|
9 | class so_userinterface |
---|
10 | { |
---|
11 | /** |
---|
12 | * @var object database object |
---|
13 | * @access public |
---|
14 | */ |
---|
15 | var $db; |
---|
16 | |
---|
17 | /** |
---|
18 | * Constructor |
---|
19 | * @access public |
---|
20 | * @return object |
---|
21 | */ |
---|
22 | function so_userinterface() |
---|
23 | { |
---|
24 | $this->db =& Factory::getInstance('WorkflowObjects')->getDBWorkflow()->Link_ID; |
---|
25 | $this->db->SetFetchMode(ADODB_FETCH_ASSOC); |
---|
26 | } |
---|
27 | |
---|
28 | /** |
---|
29 | * Obtain the subnet of an IP Address based on the numer of bits of the subnet |
---|
30 | * @param string $ip The IP Address |
---|
31 | * @param int $bits Number of bits of the subnet |
---|
32 | * @return string The subnet of the IP |
---|
33 | * @access private |
---|
34 | */ |
---|
35 | private function getSubnet($ip, $bits) |
---|
36 | { |
---|
37 | $octets = explode('.', $ip); |
---|
38 | $output = array(); |
---|
39 | for ($i = 0; $i < 4; $i++) |
---|
40 | { |
---|
41 | if ($bits >= 8) |
---|
42 | { |
---|
43 | $output[] = $octets[$i]; |
---|
44 | $bits -= 8; |
---|
45 | continue; |
---|
46 | } |
---|
47 | $output[] = $octets[$i] & bindec(str_repeat('1', $bits) . str_repeat('0',8 - $bits)); |
---|
48 | $bits = 0; |
---|
49 | } |
---|
50 | return implode('.', $output); |
---|
51 | } |
---|
52 | |
---|
53 | /** |
---|
54 | * Get External Applications |
---|
55 | * @access public |
---|
56 | * @return array list of external applications |
---|
57 | */ |
---|
58 | function getExternalApplications() |
---|
59 | { |
---|
60 | /* load the intranet subnetworks */ |
---|
61 | $oldDB = $GLOBALS['phpgw']->db; |
---|
62 | $GLOBALS['phpgw']->db = $GLOBALS['ajax']->db; |
---|
63 | $config = new config('workflow'); |
---|
64 | $configValues = $config->read_repository(); |
---|
65 | $submasksString = $configValues['intranet_subnetworks']; |
---|
66 | $GLOBALS['phpgw']->db = $oldDB; |
---|
67 | |
---|
68 | $userIP = getenv('REMOTE_ADDR'); |
---|
69 | if (getenv('HTTP_X_FORWARDED_FOR')) |
---|
70 | { |
---|
71 | $tmpIP = explode(',', getenv('HTTP_X_FORWARDED_FOR')); |
---|
72 | $userIP = $tmpIP[0]; |
---|
73 | } |
---|
74 | |
---|
75 | /* check if the user has access to intranet applications, i.e., is in the intranet */ |
---|
76 | $showIntranetApplications = false; |
---|
77 | $submasks = explode(';', $submasksString); |
---|
78 | foreach ($submasks as $submask) |
---|
79 | { |
---|
80 | list($ip,$bits) = explode('/', trim($submask) . '/32', 2); |
---|
81 | if ($this->getSubnet($ip, $bits) == $this->getSubnet($userIP, $bits)) |
---|
82 | { |
---|
83 | $showIntranetApplications = true; |
---|
84 | break; |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | $preOutput = array(); |
---|
89 | $output = array(); |
---|
90 | |
---|
91 | /* select the sites that the user can access */ |
---|
92 | $externalApplicationsID = $GLOBALS['ajax']->acl->getUserGroupPermissions('APX', $_SESSION['phpgw_info']['workflow']['account_id']); |
---|
93 | if (!empty($externalApplicationsID)) |
---|
94 | { |
---|
95 | $result = Factory::getInstance('WorkflowObjects')->getDBGalaxia()->Link_ID->query("SELECT DISTINCT external_application_id, name, address, image, authentication, intranet_only FROM egw_wf_external_application WHERE (external_application_id IN (" . implode(', ', $externalApplicationsID) . ")) ORDER BY name"); |
---|
96 | $preOutput = $result->GetArray(-1); |
---|
97 | |
---|
98 | /* keep only associative elments and check if the user can access an intranet application */ |
---|
99 | for ($i = 0; $i < count($preOutput); $i++) |
---|
100 | { |
---|
101 | if (($preOutput[$i]['intranet_only'] == '1') && (!$showIntranetApplications)) |
---|
102 | continue; |
---|
103 | |
---|
104 | for ($j = 0; $j < $result->_numOfFields; $j++) |
---|
105 | unset($preOutput[$i][$j]); |
---|
106 | $output[] = $preOutput[$i]; |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | return $output; |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * Get User Organization ID |
---|
115 | * @param int $userID User identifier |
---|
116 | * @return mixed Informações sobre a organização ou false em caso de erro. |
---|
117 | * @access public |
---|
118 | */ |
---|
119 | function getUserOrganization($userID) |
---|
120 | { |
---|
121 | $query = "SELECT o.organizacao_id, o.nome, o.descricao, o.url_imagem, o.ativa FROM funcionario f, organizacao o WHERE (o.organizacao_id = f.organizacao_id) AND (f.funcionario_id = ?)"; |
---|
122 | $result = $this->db->query($query, array((int) $userID)); |
---|
123 | |
---|
124 | $output = $result->fetchRow(DB_FETCHMODE_ASSOC); |
---|
125 | if (!$output) |
---|
126 | return false; |
---|
127 | |
---|
128 | for ($i = 0; $i < $result->_numOfFields; $i++) |
---|
129 | unset($output[$i]); |
---|
130 | |
---|
131 | return $output; |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | * Get cost center list |
---|
136 | * @param int $organizationID The organization ID |
---|
137 | * @return array Lista de centros de custo |
---|
138 | * @access public |
---|
139 | */ |
---|
140 | function getCostCenters($organizationID) |
---|
141 | { |
---|
142 | $result = $this->db->query("SELECT nm_centro_custo, grupo, descricao FROM centro_custo WHERE (organizacao_id = ?) ORDER BY descricao", array($organizationID)); |
---|
143 | |
---|
144 | $output = $result->GetArray(-1); |
---|
145 | for ($i = 0; $i < count($output); $i++) |
---|
146 | for ($j = 0; $j < $result->_numOfFields; $j++) |
---|
147 | unset($output[$i][$j]); |
---|
148 | |
---|
149 | return $output; |
---|
150 | } |
---|
151 | |
---|
152 | /** |
---|
153 | * Get hierarchical Area |
---|
154 | * @return array |
---|
155 | * @access public |
---|
156 | */ |
---|
157 | function getHierarchicalArea($organizationID, $parent = null, $depth = 0) |
---|
158 | { |
---|
159 | if (is_null($parent)){ |
---|
160 | $query = "SELECT a.area_id, a.sigla, a.titular_funcionario_id FROM area a"; |
---|
161 | $query .=" INNER JOIN area_status a_s ON (a_s.area_status_id = a.area_status_id)"; |
---|
162 | $query .=" WHERE (a.superior_area_id IS NULL) AND (a.organizacao_id = ?) AND (a.ativa = 'S') ORDER BY a_s.nivel, a.sigla"; |
---|
163 | $result = $this->db->query($query, array($organizationID)); |
---|
164 | } else { |
---|
165 | $query = "SELECT a.area_id, a.sigla, a.titular_funcionario_id FROM area a"; |
---|
166 | $query .=" INNER JOIN area_status a_s ON (a_s.area_status_id = a.area_status_id)"; |
---|
167 | $query .=" WHERE (a.superior_area_id = ?) AND (a.ativa = 'S') ORDER BY a_s.nivel, a.sigla"; |
---|
168 | $result = $this->db->query($query, array($parent)); |
---|
169 | } |
---|
170 | |
---|
171 | $output = $result->GetArray(-1); |
---|
172 | for ($i = 0; $i < count($output); $i++) |
---|
173 | { |
---|
174 | for ($j = 0; $j < $result->_numOfFields; $j++) |
---|
175 | unset($output[$i][$j]); |
---|
176 | |
---|
177 | $output[$i]['children'] = $this->getHierarchicalArea($organizationID, $output[$i]['area_id'], $depth + 1); |
---|
178 | $output[$i]['depth'] = $depth; |
---|
179 | } |
---|
180 | |
---|
181 | return $output; |
---|
182 | } |
---|
183 | |
---|
184 | /** |
---|
185 | * Get organization area list |
---|
186 | * @return array |
---|
187 | * @access public |
---|
188 | */ |
---|
189 | function getAreaList($organizationID) |
---|
190 | { |
---|
191 | $result = $this->db->query("SELECT area_id, sigla FROM area WHERE (organizacao_id = ?) AND (ativa = 'S') ORDER BY sigla", array($organizationID)); |
---|
192 | |
---|
193 | $output = $result->GetArray(-1); |
---|
194 | for ($i = 0; $i < count($output); $i++) |
---|
195 | for ($j = 0; $j < $result->_numOfFields; $j++) |
---|
196 | unset($output[$i][$j]); |
---|
197 | |
---|
198 | return $output; |
---|
199 | } |
---|
200 | |
---|
201 | /** |
---|
202 | * Get organization categories list |
---|
203 | * @param int $organizationID The organization ID |
---|
204 | * @return array The categories list |
---|
205 | * @access public |
---|
206 | */ |
---|
207 | function getCategoriesList($organizationID) |
---|
208 | { |
---|
209 | $output = $this->db->query('SELECT funcionario_categoria_id, descricao FROM funcionario_categoria WHERE (organizacao_id = ?)', array($organizationID))->GetArray(); |
---|
210 | $numerOfEmployees = $this->db->GetAssoc('SELECT COALESCE(f.funcionario_categoria_id, 0) AS funcionario_categoria_id, COUNT(*) FROM funcionario f, funcionario_status fs WHERE (f.organizacao_id = ?) AND (f.funcionario_status_id = fs.funcionario_status_id) AND (fs.exibir = \'S\') GROUP BY funcionario_categoria_id', array($organizationID)); |
---|
211 | |
---|
212 | $output[] = array( |
---|
213 | 'funcionario_categoria_id' => 0, |
---|
214 | 'descricao' => 'Sem Vínculo' |
---|
215 | ); |
---|
216 | |
---|
217 | foreach ($output as &$row) |
---|
218 | $row['contagem'] .= (isset($numerOfEmployees[$row['funcionario_categoria_id']]) ? $numerOfEmployees[$row['funcionario_categoria_id']] : 0); |
---|
219 | |
---|
220 | return $output; |
---|
221 | } |
---|
222 | |
---|
223 | /** |
---|
224 | * Get Area Employees |
---|
225 | * @param int $areaID |
---|
226 | * @param int $organizationID |
---|
227 | * @return array |
---|
228 | * @access public |
---|
229 | */ |
---|
230 | function getAreaEmployees($areaID, $organizationID) |
---|
231 | { |
---|
232 | $organizationID = (int) $organizationID; |
---|
233 | $areaID = (int) $areaID; |
---|
234 | |
---|
235 | /* gather some info from the area */ |
---|
236 | $areaInfo = $this->db->query('SELECT COALESCE(titular_funcionario_id, -1) AS titular_funcionario_id, COALESCE(substituto_funcionario_id, -1) AS substituto_funcionario_id FROM area WHERE (organizacao_id = ?) AND (area_id = ?)', array($organizationID, $areaID))->GetArray(-1); |
---|
237 | if (empty($areaInfo)) |
---|
238 | return false; |
---|
239 | $areaInfo = $areaInfo[0]; |
---|
240 | $supervisors = '{' . implode(', ', $areaInfo) . '}'; |
---|
241 | |
---|
242 | /* load the employees from the area */ |
---|
243 | $query = "SELECT f.funcionario_id, f.organizacao_id, f.area_id, COALESCE(f.funcionario_categoria_id, 0) AS funcionario_categoria_id FROM funcionario f, funcionario_status s WHERE ((f.area_id = ?) OR (f.funcionario_id = ANY (?))) AND (f.funcionario_status_id = s.funcionario_status_id) AND (s.exibir = ?)"; |
---|
244 | $result = $this->db->query($query, array($areaID, $supervisors, 'S')); |
---|
245 | |
---|
246 | $employees = $result->GetArray(-1); |
---|
247 | $cachedLDAP = Factory::newInstance('CachedLDAP'); |
---|
248 | $cachedLDAP->setOperationMode($cachedLDAP->OPERATION_MODE_LDAP_DATABASE); |
---|
249 | $categoriesCount = array(); |
---|
250 | for ($i = 0; $i < count($employees); $i++) |
---|
251 | { |
---|
252 | /* remove numeric fields */ |
---|
253 | for ($j = 0; $j < $result->_numOfFields; $j++) |
---|
254 | unset($employees[$i][$j]); |
---|
255 | |
---|
256 | $employees[$i]['cn'] = ''; |
---|
257 | $employees[$i]['telephoneNumber'] = ''; |
---|
258 | if (in_array($employees[$i]['funcionario_id'], $areaInfo)) |
---|
259 | $employees[$i]['chief'] = ($employees[$i]['funcionario_id'] == $areaInfo['titular_funcionario_id']) ? 1 : 2; |
---|
260 | |
---|
261 | /* try to find the telephone number */ |
---|
262 | $entry = $cachedLDAP->getEntryByID($employees[$i]['funcionario_id']); |
---|
263 | if ($entry) |
---|
264 | { |
---|
265 | $employees[$i]['telephoneNumber'] = is_null($entry['telephonenumber']) ? '' : $entry['telephonenumber']; |
---|
266 | $employees[$i]['cn'] = is_null($entry['cn']) ? '' : $entry['cn']; |
---|
267 | $employees[$i]['removed'] = is_null($entry['last_update']); |
---|
268 | } |
---|
269 | |
---|
270 | /* count the number of employees in each category */ |
---|
271 | $categoryID = $employees[$i]['funcionario_categoria_id']; |
---|
272 | if (isset($categoriesCount[$categoryID])) |
---|
273 | $categoriesCount[$categoryID]++; |
---|
274 | else |
---|
275 | $categoriesCount[$categoryID] = 1; |
---|
276 | } |
---|
277 | $usedCategories = array_keys($categoriesCount); |
---|
278 | $availableCategories = $this->getCategoriesList($organizationID); |
---|
279 | $output = array(); |
---|
280 | $output['employees'] = $employees; |
---|
281 | $output['categories'] = array(); |
---|
282 | foreach ($availableCategories as $category) |
---|
283 | { |
---|
284 | if (!in_array($category['funcionario_categoria_id'], $usedCategories)) |
---|
285 | continue; |
---|
286 | |
---|
287 | $category['contagem'] = $categoriesCount[$category['funcionario_categoria_id']]; |
---|
288 | $output['categories'][] = $category; |
---|
289 | } |
---|
290 | |
---|
291 | usort($output['employees'], create_function('$a,$b', 'return strcasecmp($a[\'cn\'],$b[\'cn\']);')); |
---|
292 | |
---|
293 | return $output; |
---|
294 | } |
---|
295 | |
---|
296 | /** |
---|
297 | * Get employees from a specific category |
---|
298 | * @param int $categoryID The category ID |
---|
299 | * @param int $organizationID The organization ID |
---|
300 | * @return array The list o employees of that category |
---|
301 | * @access public |
---|
302 | */ |
---|
303 | function getCategoryEmployees($categoryID, $organizationID) |
---|
304 | { |
---|
305 | $organizationID = (int) $organizationID; |
---|
306 | |
---|
307 | /* load the employees from the area */ |
---|
308 | if ($categoryID == 0) |
---|
309 | { |
---|
310 | $query = "SELECT f.funcionario_id, f.organizacao_id, a.area_id, a.sigla AS area FROM funcionario f, funcionario_status s, area a WHERE (f.funcionario_status_id = s.funcionario_status_id) AND (f.area_id = a.area_id) AND (f.funcionario_categoria_id IS NULL) AND (s.exibir = ?) AND (f.organizacao_id = ?)"; |
---|
311 | $result = $this->db->query($query, array('S', $organizationID)); |
---|
312 | } |
---|
313 | else |
---|
314 | { |
---|
315 | $query = "SELECT f.funcionario_id, f.organizacao_id, a.area_id, a.sigla AS area FROM funcionario f, funcionario_status s, area a WHERE (f.funcionario_status_id = s.funcionario_status_id) AND (f.area_id = a.area_id) AND (f.funcionario_categoria_id = ?) AND (s.exibir = ?) AND (f.organizacao_id = ?)"; |
---|
316 | $result = $this->db->query($query, array((int)$categoryID, 'S', $organizationID)); |
---|
317 | } |
---|
318 | |
---|
319 | $employees = $result->GetArray(-1); |
---|
320 | $cachedLDAP = Factory::newInstance('CachedLDAP'); |
---|
321 | $cachedLDAP->setOperationMode($cachedLDAP->OPERATION_MODE_LDAP_DATABASE); |
---|
322 | for ($i = 0; $i < count($employees); $i++) |
---|
323 | { |
---|
324 | /* remove numeric fields */ |
---|
325 | for ($j = 0; $j < $result->_numOfFields; $j++) |
---|
326 | unset($employees[$i][$j]); |
---|
327 | |
---|
328 | $employees[$i]['cn'] = ''; |
---|
329 | $employees[$i]['telephoneNumber'] = ''; |
---|
330 | |
---|
331 | /* try to find the telephone number */ |
---|
332 | $entry = $cachedLDAP->getEntryByID($employees[$i]['funcionario_id']); |
---|
333 | if ($entry) |
---|
334 | { |
---|
335 | $employees[$i]['telephoneNumber'] = is_null($entry['telephonenumber']) ? '' : $entry['telephonenumber']; |
---|
336 | $employees[$i]['cn'] = is_null($entry['cn']) ? '' : $entry['cn'] . (is_null($entry['last_update']) ? ' <font color="red">(excluído)</font>' : ''); |
---|
337 | } |
---|
338 | } |
---|
339 | |
---|
340 | $output = array('employees' => $employees); |
---|
341 | return $output; |
---|
342 | } |
---|
343 | |
---|
344 | /** |
---|
345 | * Search Employee |
---|
346 | * @param int $searchTerm term to search |
---|
347 | * @param int $organizationID Id of organization |
---|
348 | * @return array employee data information |
---|
349 | * @access public |
---|
350 | */ |
---|
351 | function searchEmployee($searchTerm, $organizationID) |
---|
352 | { |
---|
353 | /* initialize some variables */ |
---|
354 | $output = array('employees' => array()); |
---|
355 | $ldapResult = array(); |
---|
356 | |
---|
357 | /* search for the $searchTerm in the LDAP */ |
---|
358 | $ldap =& Factory::getInstance('WorkflowObjects')->getLDAP(); |
---|
359 | $list = @ldap_search($ldap, Factory::getInstance('WorkflowLDAP')->getLDAPContext(), ('(&(cn=*' . $searchTerm . '*)(phpgwaccounttype=u))'), array('uidNumber', 'cn', 'telephoneNumber')); |
---|
360 | if ($list === false) |
---|
361 | return false; |
---|
362 | |
---|
363 | $entries = ldap_get_entries($ldap, $list); |
---|
364 | $employeesID = array(); |
---|
365 | for ($i = 0; $i < $entries['count']; $i++) |
---|
366 | $ldapResult[$entries[$i]['uidnumber'][0]] = array('cn' => $entries[$i]['cn'][0], 'telephoneNumber' => $entries[$i]['telephonenumber'][0]); |
---|
367 | |
---|
368 | /* check if any result was found */ |
---|
369 | if (count($ldapResult) < 1) |
---|
370 | return $output; |
---|
371 | |
---|
372 | /* load employee information */ |
---|
373 | $query = "SELECT f.funcionario_id AS funcionario_id, f.area_id AS area_id, a.sigla AS area FROM funcionario f, area a, funcionario_status s WHERE (f.area_id = a.area_id) AND (f.organizacao_id = $organizationID) AND (f.funcionario_id IN (" . implode(',', array_keys($ldapResult)) .")) AND (f.funcionario_status_id = s.funcionario_status_id) AND (s.exibir = 'S')"; |
---|
374 | $result = $this->db->query($query); |
---|
375 | |
---|
376 | $employees = $result->GetArray(-1); |
---|
377 | for ($i = 0; $i < count($employees); $i++) |
---|
378 | { |
---|
379 | for ($j = 0; $j < $result->_numOfFields; $j++) |
---|
380 | unset($employees[$i][$j]); |
---|
381 | $employees[$i]['cn'] = is_null($ldapResult[$employees[$i]['funcionario_id']]['cn']) ? '' : $ldapResult[$employees[$i]['funcionario_id']]['cn']; |
---|
382 | $employees[$i]['telephoneNumber'] = is_null($ldapResult[$employees[$i]['funcionario_id']]['telephoneNumber']) ? '' : $ldapResult[$employees[$i]['funcionario_id']]['telephoneNumber']; |
---|
383 | } |
---|
384 | |
---|
385 | $output['employees'] = $employees; |
---|
386 | return $output; |
---|
387 | } |
---|
388 | |
---|
389 | /** |
---|
390 | * Busca informações sobre um funcionário. |
---|
391 | * @param array $params Uma array contendo o ID do funcionário cujas informações serão extraídas e de sua organização (Ajax). |
---|
392 | * @param int $employeeID O ID do funcionário. |
---|
393 | * @param int $organizationID O ID da organização. |
---|
394 | * @return array Informações sobre o funcionário. |
---|
395 | * @access public |
---|
396 | */ |
---|
397 | function getEmployeeInfo($employeeID, $organizationID) |
---|
398 | { |
---|
399 | require_once dirname(__FILE__) . '/class.so_orgchart.inc.php'; |
---|
400 | |
---|
401 | $SOOrgchart = new so_orgchart(); |
---|
402 | $SOOrgchart->setExternalCalls(true); |
---|
403 | $output = $SOOrgchart->getEmployeeInfo($employeeID, $organizationID); |
---|
404 | $SOOrgchart->setExternalCalls(false); |
---|
405 | |
---|
406 | return $output; |
---|
407 | } |
---|
408 | |
---|
409 | /** |
---|
410 | * Busca informações sobre uma área. |
---|
411 | * @param array $params Uma array contendo o ID da área cujas informações serão extraídas e de sua organização (Ajax). |
---|
412 | * @param int $areaID O ID da área. |
---|
413 | * @param int $organizationID O ID da organização. |
---|
414 | * @return array Informações sobre a área. |
---|
415 | * @access public |
---|
416 | */ |
---|
417 | function getAreaInfo($areaID, $organizationID) |
---|
418 | { |
---|
419 | require_once dirname(__FILE__) . '/class.so_orgchart.inc.php'; |
---|
420 | |
---|
421 | $SOOrgchart = new so_orgchart(); |
---|
422 | $SOOrgchart->setExternalCalls(true); |
---|
423 | $output = $SOOrgchart->getAreaInfo($areaID, $organizationID); |
---|
424 | $SOOrgchart->setExternalCalls(false); |
---|
425 | |
---|
426 | return $output; |
---|
427 | } |
---|
428 | } |
---|
429 | ?> |
---|