source: branches/1.2/workflow/inc/class.so_userinterface.inc.php @ 1349

Revision 1349, 14.8 KB checked in by niltonneto, 15 years ago (diff)

Ticket #561 - Inclusão do módulo Workflow faltante nessa versão.

  • 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                        $result = $this->db->query("SELECT area_id, sigla, titular_funcionario_id FROM area WHERE (superior_area_id IS NULL) AND (organizacao_id = ?) AND (ativa = 'S') ORDER BY sigla", array($organizationID));
161                else
162                        $result = $this->db->query("SELECT area_id, sigla, titular_funcionario_id FROM area WHERE (superior_area_id = ?) AND (ativa = 'S') ORDER BY sigla", array($parent));
163
164                $output = $result->GetArray(-1);
165                for ($i = 0; $i < count($output); $i++)
166                {
167                        for ($j = 0; $j < $result->_numOfFields; $j++)
168                                unset($output[$i][$j]);
169
170                        $output[$i]['children'] = $this->getHierarchicalArea($organizationID, $output[$i]['area_id'], $depth + 1);
171                        $output[$i]['depth'] = $depth;
172                }
173
174                return $output;
175        }
176
177        /**
178         * Get organization area list
179         * @return array
180         * @access public
181         */
182        function getAreaList($organizationID)
183        {
184                $result = $this->db->query("SELECT area_id, sigla FROM area WHERE (organizacao_id = ?) AND (ativa = 'S') ORDER BY sigla", array($organizationID));
185
186                $output = $result->GetArray(-1);
187                for ($i = 0; $i < count($output); $i++)
188                        for ($j = 0; $j < $result->_numOfFields; $j++)
189                                unset($output[$i][$j]);
190
191                return $output;
192        }
193
194        /**
195         * Get organization categories list
196         * @param int $organizationID The organization ID
197         * @return array The categories list
198         * @access public
199         */
200        function getCategoriesList($organizationID)
201        {
202                $output = $this->db->query('SELECT funcionario_categoria_id, descricao FROM funcionario_categoria WHERE (organizacao_id = ?)', array($organizationID))->GetArray();
203                $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));
204
205                $output[] = array(
206                        'funcionario_categoria_id' => 0,
207                        'descricao' => 'Sem Vínculo'
208                );
209
210                foreach ($output as &$row)
211                        $row['contagem'] .= (isset($numerOfEmployees[$row['funcionario_categoria_id']]) ? $numerOfEmployees[$row['funcionario_categoria_id']] : 0);
212
213                return $output;
214        }
215
216        /**
217         * Get Area Employees
218         * @param int $areaID
219         * @param int $organizationID
220         * @return array
221         * @access public
222         */
223        function getAreaEmployees($areaID, $organizationID)
224        {
225                $organizationID = (int) $organizationID;
226                $areaID = (int) $areaID;
227
228                /* gather some info from the area */
229                $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);
230                if (empty($areaInfo))
231                        return false;
232                $areaInfo = $areaInfo[0];
233                $supervisors = '{' . implode(', ', $areaInfo) . '}';
234
235                /* load the employees from the area */
236                $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 = ?)";
237                $result = $this->db->query($query, array($areaID, $supervisors, 'S'));
238
239                $employees = $result->GetArray(-1);
240                $cachedLDAP = $GLOBALS['workflow']['factory']->newInstance('CachedLDAP');
241                $cachedLDAP->setOperationMode($cachedLDAP->OPERATION_MODE_LDAP_DATABASE);
242                $categoriesCount = array();
243                for ($i = 0; $i < count($employees); $i++)
244                {
245                        /* remove numeric fields */
246                        for ($j = 0; $j < $result->_numOfFields; $j++)
247                                unset($employees[$i][$j]);
248
249                        $employees[$i]['cn'] = '';
250                        $employees[$i]['telephoneNumber'] = '';
251                        if (in_array($employees[$i]['funcionario_id'], $areaInfo))
252                                $employees[$i]['chief'] = ($employees[$i]['funcionario_id'] == $areaInfo['titular_funcionario_id']) ? 1 : 2;
253
254                        /* try to find the telephone number */
255                        $entry = $cachedLDAP->getEntryByID($employees[$i]['funcionario_id']);
256                        if ($entry)
257                        {
258                                $employees[$i]['telephoneNumber'] = is_null($entry['telephonenumber']) ? '' : $entry['telephonenumber'];
259                                $employees[$i]['cn'] = is_null($entry['cn']) ? '' : $entry['cn'];
260                                $employees[$i]['removed'] = is_null($entry['last_update']);
261                        }
262
263                        /* count the number of employees in each category */
264                        $categoryID = $employees[$i]['funcionario_categoria_id'];
265                        if (isset($categoriesCount[$categoryID]))
266                                $categoriesCount[$categoryID]++;
267                        else
268                                $categoriesCount[$categoryID] = 1;
269                }
270                $usedCategories = array_keys($categoriesCount);
271                $availableCategories = $this->getCategoriesList($organizationID);
272                $output = array();
273                $output['employees'] = $employees;
274                $output['categories'] = array();
275                foreach ($availableCategories as $category)
276                {
277                        if (!in_array($category['funcionario_categoria_id'], $usedCategories))
278                                continue;
279
280                        $category['contagem'] = $categoriesCount[$category['funcionario_categoria_id']];
281                        $output['categories'][] = $category;
282                }
283
284                usort($output['employees'], create_function('$a,$b', 'return strcasecmp($a[\'cn\'],$b[\'cn\']);'));
285
286                return $output;
287        }
288
289        /**
290         * Get employees from a specific category
291         * @param int $categoryID The category ID
292         * @param int $organizationID The organization ID
293         * @return array The list o employees of that category
294         * @access public
295         */
296        function getCategoryEmployees($categoryID, $organizationID)
297        {
298                $organizationID = (int) $organizationID;
299
300                /* load the employees from the area */
301                if ($categoryID == 0)
302                {
303                        $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 = ?)";
304                        $result = $this->db->query($query, array('S', $organizationID));
305                }
306                else
307                {
308                        $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 = ?)";
309                        $result = $this->db->query($query, array((int)$categoryID, 'S', $organizationID));
310                }
311
312                $employees = $result->GetArray(-1);
313                $cachedLDAP = $GLOBALS['workflow']['factory']->newInstance('CachedLDAP');
314                $cachedLDAP->setOperationMode($cachedLDAP->OPERATION_MODE_LDAP_DATABASE);
315                for ($i = 0; $i < count($employees); $i++)
316                {
317                        /* remove numeric fields */
318                        for ($j = 0; $j < $result->_numOfFields; $j++)
319                                unset($employees[$i][$j]);
320
321                        $employees[$i]['cn'] = '';
322                        $employees[$i]['telephoneNumber'] = '';
323
324                        /* try to find the telephone number */
325                        $entry = $cachedLDAP->getEntryByID($employees[$i]['funcionario_id']);
326                        if ($entry)
327                        {
328                                $employees[$i]['telephoneNumber'] = is_null($entry['telephonenumber']) ? '' : $entry['telephonenumber'];
329                                $employees[$i]['cn'] = is_null($entry['cn']) ? '' : $entry['cn'] . (is_null($entry['last_update']) ? ' <font color="red">(excluído)</font>' : '');
330                        }
331                }
332
333                $output = array('employees' => $employees);
334                return $output;
335        }
336
337        /**
338         * Search Employee
339         * @param int $searchTerm term to search
340         * @param int $organizationID  Id of organization
341         * @return array employee data information
342         * @access public
343         */
344        function searchEmployee($searchTerm, $organizationID)
345        {
346                /* initialize some variables */
347                $output = array('employees' => array());
348                $ldapResult = array();
349
350                /* search for the $searchTerm in the LDAP */
351                $ldap = &$GLOBALS['workflow']['workflowObjects']->getLDAP();
352                $list = @ldap_search($ldap, $GLOBALS['workflow']['factory']->getInstance('WorkflowLDAP')->getLDAPContext(), ('(&(cn=*' . $searchTerm . '*)(phpgwaccounttype=u))'), array('uidNumber', 'cn', 'telephoneNumber'));
353                if ($list === false)
354                        return false;
355
356                $entries = ldap_get_entries($ldap, $list);
357                $employeesID = array();
358                for ($i = 0; $i < $entries['count']; $i++)
359                        $ldapResult[$entries[$i]['uidnumber'][0]] = array('cn' => $entries[$i]['cn'][0], 'telephoneNumber' => $entries[$i]['telephonenumber'][0]);
360
361                /* check if any result was found */
362                if (count($ldapResult) < 1)
363                        return $output;
364
365                /* load employee information */
366                $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')";
367                $result = $this->db->query($query);
368
369                $employees = $result->GetArray(-1);
370                for ($i = 0; $i < count($employees); $i++)
371                {
372                        for ($j = 0; $j < $result->_numOfFields; $j++)
373                                unset($employees[$i][$j]);
374                        $employees[$i]['cn'] = is_null($ldapResult[$employees[$i]['funcionario_id']]['cn']) ? '' : $ldapResult[$employees[$i]['funcionario_id']]['cn'];
375                        $employees[$i]['telephoneNumber'] = is_null($ldapResult[$employees[$i]['funcionario_id']]['telephoneNumber']) ? '' : $ldapResult[$employees[$i]['funcionario_id']]['telephoneNumber'];
376                }
377
378                $output['employees'] = $employees;
379                return $output;
380        }
381
382        /**
383         * Busca informações sobre um funcionário.
384         * @param array $params Uma array contendo o ID do funcionário cujas informações serão extraídas e de sua organização (Ajax).
385         * @param int $employeeID O ID do funcionário.
386         * @param int $organizationID O ID da organização.
387         * @return array Informações sobre o funcionário.
388         * @access public
389         */
390        function getEmployeeInfo($employeeID, $organizationID)
391        {
392                require_once dirname(__FILE__) . '/class.so_orgchart.inc.php';
393
394                $SOOrgchart = new so_orgchart();
395                $SOOrgchart->setExternalCalls(true);
396                $output = $SOOrgchart->getEmployeeInfo($employeeID, $organizationID);
397                $SOOrgchart->setExternalCalls(false);
398
399                return $output;
400        }
401
402        /**
403         * Busca informações sobre uma área.
404         * @param array $params Uma array contendo o ID da área cujas informações serão extraídas e de sua organização (Ajax).
405         * @param int $areaID O ID da área.
406         * @param int $organizationID O ID da organização.
407         * @return array Informações sobre a área.
408         * @access public
409         */
410        function getAreaInfo($areaID, $organizationID)
411        {
412                require_once dirname(__FILE__) . '/class.so_orgchart.inc.php';
413
414                $SOOrgchart = new so_orgchart();
415                $SOOrgchart->setExternalCalls(true);
416                $output = $SOOrgchart->getAreaInfo($areaID, $organizationID);
417                $SOOrgchart->setExternalCalls(false);
418
419                return $output;
420        }
421}
422?>
Note: See TracBrowser for help on using the repository browser.