source: trunk/workflow/inc/class.so_userinterface.inc.php @ 1470

Revision 1470, 24.3 KB checked in by rodsouza, 15 years ago (diff)

Ticket #638 - Permitir numeros e hifen como parâmetro da busca e ordenar o resultado (por grupo).

  • Property svn:executable set to *
Line 
1<?php
2require_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 */
9class 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 = &$GLOBALS['workflow']['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 = $GLOBALS['workflow']['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 = $GLOBALS['workflow']['factory']->newInstance('CachedLDAP');
248                $cachedLDAP->setOperationMode($cachedLDAP->OPERATION_MODE_NORMAL);
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 = $GLOBALS['workflow']['factory']->newInstance('CachedLDAP');
321                $cachedLDAP->setOperationMode($cachedLDAP->OPERATION_MODE_NORMAL);
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(), 'bygroup' => array( ) );
355                $ldapResult = array();
356
357                /* search for the $searchTerm in the LDAP */
358                $ldap = &$GLOBALS['workflow']['workflowObjects']->getLDAP();
359
360                $searchTermExploded = explode(" ", $searchTerm);
361                $fullSearch = false;
362
363                if(count($searchTermExploded) > 0){
364                        for($i=1; $i<count($searchTermExploded); $i++){
365                                if(strlen($searchTermExploded[$i]) > 2){
366                                        $fullSearch = true;
367                                }
368                        }
369
370                        if($fullSearch){
371                                $searchTerm = implode("*", $searchTermExploded);
372                        }
373                }
374
375                $list = @ldap_search($ldap, $GLOBALS['workflow']['factory']->getInstance('WorkflowLDAP')->getLDAPContext(), ('(&(cn=*' . $searchTerm . '*)(phpgwaccounttype=u))'), array('uidNumber', 'cn', 'telephoneNumber'));
376                if ($list === false)
377                        return false;
378
379                $entries = ldap_get_entries($ldap, $list);
380                $employeesID = array();
381                for ($i = 0; $i < $entries['count']; $i++)
382                        $ldapResult[$entries[$i]['uidnumber'][0]] = array('cn' => $entries[$i]['cn'][0], 'telephoneNumber' => $entries[$i]['telephonenumber'][0]);
383
384                $uids = implode( ',', array_keys( $ldapResult ) );
385
386                /* load employee information */
387                $query = 'SELECT f.funcionario_id AS funcionario_id, f.area_id AS area_id, a.sigla AS area';
388                $query .= ' FROM funcionario f, area a, funcionario_status s';
389                $query .= " WHERE (f.area_id = a.area_id) AND (f.organizacao_id = {$organizationID})";
390                $query .= " AND (" . ( ( ! empty( $uids ) ) ? "f.funcionario_id IN ({$uids}) OR " : '' );
391                $query .= " a.sigla LIKE UPPER(?))";
392                $query .= " AND (f.funcionario_status_id = s.funcionario_status_id) AND (s.exibir = 'S')";
393                $query .= " ORDER BY a.sigla asc";
394
395                $result = $this->db->query($query, array( "%{$searchTerm}%" ) );
396
397                if ( ! $result )
398                        return array( );
399
400                $uids = array( );
401
402                $employees = $result->GetArray(-1);
403                for ($i = 0; $i < count($employees); $i++)
404                {
405                        for ($j = 0; $j < $result->_numOfFields; $j++)
406                                unset($employees[$i][$j]);
407
408                        if ( array_key_exists( $employees[$i]['funcionario_id'], $ldapResult ) )
409                        {
410                                $employees[$i]['cn'] = is_null($ldapResult[$employees[$i]['funcionario_id']]['cn']) ? '' : $ldapResult[$employees[$i]['funcionario_id']]['cn'];
411                                $employees[$i]['telephoneNumber'] = is_null($ldapResult[$employees[$i]['funcionario_id']]['telephoneNumber']) ? '' : $ldapResult[$employees[$i]['funcionario_id']]['telephoneNumber'];
412                        }
413                        else
414                        {
415                                $employees[$i]['cn'] = $employees[$i]['funcionario_id'];
416                                $employees[$i]['telephoneNumber'] = '';
417
418                                $output['bygroup'][ $employees[$i]['funcionario_id'] ] = $employees[ $i ];
419                                array_splice( $employees, $i--, 1 );
420                        }
421                }
422
423                if ( count( $output['bygroup'] ) )
424                {
425                        $list = ldap_search($ldap, $GLOBALS['workflow']['factory']->getInstance('WorkflowLDAP')->getLDAPContext(), ('(&(|(uidNumber=' . implode( ')(uidNumber=', array_keys( $output['bygroup'] ) ) . '))(phpgwaccounttype=u))'), array('uidNumber', 'cn', 'telephoneNumber'));
426
427                        if ( $list !== false )
428                        {
429                                $entries = ldap_get_entries($ldap, $list);
430
431                                for ( $i = 0; $i < $entries[ 'count' ]; $i++ )
432                                {
433                                        $output[ 'bygroup' ][ $entries[$i]['uidnumber'][0] ][ 'cn' ] = $entries[$i]['cn'][0];
434                                        $output[ 'bygroup' ][ $entries[$i]['uidnumber'][0] ][ 'telephoneNumber' ] = $entries[$i]['telephonenumber'][0];
435                                }
436                        }
437                }
438
439                $output[ 'bygroup' ] = array_values( $output[ 'bygroup' ] );
440                $output[ 'employees' ] = $employees;
441
442                return $output;
443        }
444
445        /**
446         * Busca informações sobre um funcionário.
447         * @param array $params Uma array contendo o ID do funcionário cujas informações serão extraídas e de sua organização (Ajax).
448         * @param int $employeeID O ID do funcionário.
449         * @param int $organizationID O ID da organização.
450         * @return array Informações sobre o funcionário.
451         * @access public
452         */
453        function getEmployeeInfo($employeeID, $organizationID)
454        {
455                require_once dirname(__FILE__) . '/class.so_orgchart.inc.php';
456
457                $SOOrgchart = new so_orgchart();
458                $SOOrgchart->setExternalCalls(true);
459                $output = $SOOrgchart->getEmployeeInfo($employeeID, $organizationID);
460                $SOOrgchart->setExternalCalls(false);
461
462                return $output;
463        }
464
465        /**
466         * Busca informações sobre uma área.
467         * @param array $params Uma array contendo o ID da área cujas informações serão extraídas e de sua organização (Ajax).
468         * @param int $areaID O ID da área.
469         * @param int $organizationID O ID da organização.
470         * @return array Informações sobre a área.
471         * @access public
472         */
473        function getAreaInfo($areaID, $organizationID)
474        {
475                require_once dirname(__FILE__) . '/class.so_orgchart.inc.php';
476
477                $SOOrgchart = new so_orgchart();
478                $SOOrgchart->setExternalCalls(true);
479                $output = $SOOrgchart->getAreaInfo($areaID, $organizationID);
480                $SOOrgchart->setExternalCalls(false);
481
482                return $output;
483        }
484
485        /**
486         * Get useful phones list
487         * @param int $organizationID The organization ID
488         * @return array Useful phones list
489         * @access public
490         */
491        function getUsefulPhones( $organizationID )
492        {
493                $result = $this -> db -> query( "SELECT descricao, numero FROM telefone WHERE (organizacao_id = ?) ORDER BY descricao", array( $organizationID ) );
494
495                $output = $result->GetArray(-1);
496                for ($i = 0; $i < count($output); $i++)
497                        for ($j = 0; $j < $result->_numOfFields; $j++)
498                                unset($output[$i][$j]);
499
500                return $output;
501        }
502
503        /**
504         * Get areas with substitute boss
505         * @param int $organizationID The organization ID
506         * @return array areas with substitute boss
507         * @access public
508         */
509        function getAreaWithSubtituteBoss( $organizationID )
510        {
511                $result = $this -> db -> query( "SELECT sigla as area, titular_funcionario_id as titular, substituto_funcionario_id as substituto FROM area WHERE (organizacao_id = ?) and substituto_funcionario_id is not null ORDER BY area", array( $organizationID ) );
512
513                $cachedLDAP = $GLOBALS[ 'workflow' ][ 'factory' ] -> newInstance( 'CachedLDAP' );
514                $cachedLDAP -> setOperationMode( $cachedLDAP -> OPERATION_MODE_LDAP_DATABASE );
515
516                $output = $result->GetArray(-1);
517                for ( $i = 0; $i < count($output); $i++ )
518                {
519                        for ($j = 0; $j < $result->_numOfFields; $j++)
520                                unset($output[$i][$j]);
521
522                        $entry = $cachedLDAP -> getEntryByID( $output[ $i ][ 'titular' ] );
523                        if ( $entry && ( ! is_null( $entry[ 'cn' ] ) ) )
524                                $output[ $i ][ 'titular' ] = $entry[ 'cn' ];
525
526                        $entry = $cachedLDAP -> getEntryByID( $output[ $i ][ 'substituto' ] );
527                        if ( $entry && ( ! is_null( $entry[ 'cn' ] ) ) )
528                                $output[ $i ][ 'substituto' ] = $entry[ 'cn' ];
529                }
530
531                return $output;
532        }
533
534        /**
535         * Get manning list
536         * @param int $organizationID The organization ID
537         * @return array The manning list
538         * @access public
539         */
540        function getManning( $organizationID )
541        {
542                $result = $this -> db -> query( 'SELECT localidade_id, descricao FROM localidade WHERE (organizacao_id = ?) ORDER BY descricao', array( $organizationID ) );
543
544                $output = $result->GetArray( -1 );
545                for ( $i = 0; $i < count($output); $i++ )
546                        for ($j = 0; $j < $result->_numOfFields; $j++)
547                                unset($output[$i][$j]);
548
549                return $output;
550        }
551
552        /**
553         * Get employees from a specific location
554         * @param int $categoryID The category ID
555         * @param int $organizationID The organization ID
556         * @return array The list o employees of that location
557         * @access public
558         */
559        function getManningEmployees( $locationID, $organizationID )
560        {
561                $organizationID = ( int ) $organizationID;
562                $locationID = ( int ) $locationID;
563
564                // load the employees from the location
565                $query = "SELECT f.funcionario_id, f.organizacao_id, f.area_id, a.sigla AS area,"
566                        . " COALESCE(f.funcionario_categoria_id, 0) AS funcionario_categoria_id"
567                        . " FROM funcionario f, funcionario_status s, area a"
568                        . " WHERE (f.area_id = a.area_id)"
569                        . " AND (f.funcionario_status_id = s.funcionario_status_id)"
570                        . " AND (f.organizacao_id = ?)"
571                        . " AND (f.localidade_id = ?)"
572                        . " AND (s.exibir = ?)";
573
574                $result = $this -> db -> query( $query, array( $organizationID, $locationID, 'S' ) );
575
576                $employees = $result -> GetArray( -1 );
577                $cachedLDAP = $GLOBALS[ 'workflow' ][ 'factory' ] -> newInstance( 'CachedLDAP' );
578                $cachedLDAP -> setOperationMode( $cachedLDAP -> OPERATION_MODE_NORMAL );
579
580                $categoriesCount = array( );
581                for ( $i = 0; $i < count( $employees ); $i++ )
582                {
583                        // remove numeric fields
584                        for ( $j = 0; $j < $result -> _numOfFields; $j++ )
585                                unset( $employees[ $i ][ $j ] );
586
587                        $employees[ $i ][ 'cn' ] = '';
588                        $employees[ $i ][ 'telephoneNumber' ] = '';
589
590                        // try to find the telephone number
591                        $entry = $cachedLDAP -> getEntryByID( $employees[ $i ][ 'funcionario_id' ] );
592                        if ( $entry )
593                        {
594                                $employees[ $i ][ 'telephoneNumber' ] = is_null( $entry[ 'telephonenumber' ] ) ? '' : $entry[ 'telephonenumber' ];
595                                $employees[ $i ][ 'cn' ] = is_null( $entry[ 'cn' ] ) ? '' : $entry[ 'cn' ];
596                                $employees[ $i ][ 'removed' ] = is_null( $entry[ 'last_update' ] );
597                        }
598
599                        // count the number of employees in each category
600                        $categoryID = $employees[ $i ][ 'funcionario_categoria_id' ];
601                        if ( isset( $categoriesCount[ $categoryID ] ) )
602                                $categoriesCount[ $categoryID ]++;
603                        else
604                                $categoriesCount[ $categoryID ] = 1;
605                }
606
607                $usedCategories = array_keys( $categoriesCount );
608                $availableCategories = $this -> getCategoriesList( $organizationID );
609                $output = array( );
610                $output[ 'employees' ] = $employees;
611                $output[ 'categories' ] = array( );
612                foreach ( $availableCategories as $category )
613                {
614                        if ( ! in_array( $category[ 'funcionario_categoria_id' ], $usedCategories ) )
615                                continue;
616
617                        $category[ 'contagem' ] = $categoriesCount[ $category[ 'funcionario_categoria_id' ] ];
618                        $output[ 'categories' ][ ] = $category;
619                }
620
621                usort( $output[ 'employees' ], create_function( '$a,$b', 'return strcasecmp($a[\'cn\'],$b[\'cn\']);' ) );
622
623                return $output;
624        }
625
626        /**
627         * Return the list of employees in alphabetical order
628         * @param int $organizationID The organization ID
629         * @return array The list o employees
630         * @access public
631         */
632        function getAlphabeticalEmployees( $organizationID )
633        {
634                $organizationID = ( int ) $organizationID;
635
636                // load the employees from the location
637                $query = "SELECT f.funcionario_id, f.organizacao_id, f.area_id, a.sigla AS area,"
638                        . " COALESCE(f.funcionario_categoria_id, 0) AS funcionario_categoria_id"
639                        . " FROM funcionario f, funcionario_status s, area a"
640                        . " WHERE (f.area_id = a.area_id)"
641                        . " AND (f.funcionario_status_id = s.funcionario_status_id)"
642                        . " AND (f.organizacao_id = ?)"
643                        . " AND (s.exibir = ?)";
644
645                $result = $this -> db -> query( $query, array( $organizationID, 'S' ) );
646
647                $employees = $result -> GetArray( -1 );
648
649                $cachedLDAP = $GLOBALS[ 'workflow' ][ 'factory' ] -> newInstance( 'CachedLDAP' );
650                $cachedLDAP -> setOperationMode( $cachedLDAP -> OPERATION_MODE_NORMAL );
651
652                $categoriesCount = array( );
653                for ( $i = 0; $i < count( $employees ); $i++ )
654                {
655                        // remove numeric fields
656                        for ( $j = 0; $j < $result -> _numOfFields; $j++ )
657                                unset( $employees[ $i ][ $j ] );
658
659                        $employees[ $i ][ 'cn' ] = '';
660                        $employees[ $i ][ 'telephoneNumber' ] = '';
661
662                        // try to find the telephone number
663                        $entry = $cachedLDAP -> getEntryByID( $employees[ $i ][ 'funcionario_id' ] );
664                        if ( $entry )
665                        {
666                                $employees[ $i ][ 'telephoneNumber' ] = is_null( $entry[ 'telephonenumber' ] ) ? '' : $entry[ 'telephonenumber' ];
667                                $employees[ $i ][ 'cn' ] = is_null( $entry[ 'cn' ] ) ? '' : $entry[ 'cn' ];
668                                $employees[ $i ][ 'removed' ] = is_null( $entry[ 'last_update' ] );
669                        }
670
671                }
672
673                usort( $employees, create_function( '$a,$b', 'return strcasecmp($a[\'cn\'],$b[\'cn\']);' ) );
674
675                $paging = $GLOBALS['workflow']['factory']->newInstance('Paging', 50, $_POST);
676                $employees = $paging->restrictItems( $employees );
677
678                // count the number of employees in each category
679                for ( $i = 0; $i < count( $employees ); $i++ )
680                {
681                        $categoryID = $employees[ $i ][ 'funcionario_categoria_id' ];
682                        if ( isset( $categoriesCount[ $categoryID ] ) )
683                                $categoriesCount[ $categoryID ]++;
684                        else
685                                $categoriesCount[ $categoryID ] = 1;
686                }
687
688                $usedCategories = array_keys( $categoriesCount );
689                $availableCategories = $this -> getCategoriesList( $organizationID );
690                $output = array( );
691
692                $output['employees'] = $employees;
693                $output['categories'] = array( );
694                $output['paging_links'] = $paging -> commonLinks();
695
696                foreach ( $availableCategories as $category )
697                {
698                        if ( ! in_array( $category[ 'funcionario_categoria_id' ], $usedCategories ) )
699                                continue;
700
701                        $category[ 'contagem' ] = $categoriesCount[ $category[ 'funcionario_categoria_id' ] ];
702                        $output[ 'categories' ][ ] = $category;
703                }
704
705                return $output;
706        }
707}
708?>
Note: See TracBrowser for help on using the repository browser.