source: trunk/workflow/inc/class.bo_participants.inc.php @ 795

Revision 795, 7.2 KB checked in by viani, 15 years ago (diff)

Ticket #488 - Inclusão do módulo workflow no ramo trunk do repositório Expresso.

  • Property svn:executable set to *
Line 
1<?php
2/**************************************************************************\
3* eGroupWare                                                               *
4* http://www.egroupware.org                                                *
5* --------------------------------------------                             *
6*  This program is free software; you can redistribute it and/or modify it *
7*  under the terms of the GNU General Public License as published by the   *
8*  Free Software Foundation; either version 2 of the License, or (at your  *
9*  option) any later version.                                              *
10\**************************************************************************/
11
12require_once 'common.inc.php';
13require_once 'class.bo_ajaxinterface.inc.php';
14
15/**
16 * @package Workflow
17 * @license http://www.gnu.org/copyleft/gpl.html GPL
18 * @author Sidnei Augusto Drovetto Jr. - drovetto@gmail.com
19 * @author Rodrigo Daniel C de Lira - rodrigo.lira@gmail.com
20 */
21class bo_participants extends bo_ajaxinterface
22{
23        /**
24         * @var resource $ldap Conexão com o LDAP
25         * @access public
26         */
27        var $ldap;
28
29        /**
30         * Construtor da classe bo_participants
31         * @return object
32         * @access public
33         */
34        function bo_participants()
35        {
36                $this->ldap = &$GLOBALS['workflow']['factory']->getInstance('WorkflowLDAP');
37        }
38
39        /**
40         * Busca as organizações do LDAP
41         * @return array A lista de organizações
42         * @access public
43         */
44        function getOrganizations()
45        {
46                return $this->ldap->getOrganizations();
47        }
48
49        /**
50         * Busca as entidades (usuários, grupos ou listas públicas) de uma organização
51         * @param array $params Array que contém parâmetros necessários para fazer a busca (podem ser advindos de Ajax)
52         * @param bool $raw Se false, indica que os dados devem ser retornados em código HTML (via Ajax), se true indica que os dados devem ser retornados em forma de array
53         * @return mixed A lista de entidades em formato de array ou em formato de string HTML para construção de combo box
54         * @access public
55         */
56        function getEntities($params, $raw = false)
57        {
58                if (preg_match('/^[a-z0-9_\- =,]+$/i', $params['context']) < 1)
59                {
60                        if ($raw)
61                                return array();
62                        else
63                                return '';
64                }
65
66                $id = ($params['id'] === 'mail') ? 'mail' : 'id';
67                $usePreffix = ($params['usePreffix'] == true) ? true : false;
68
69                $output = array();
70                $entities = $params['entities'];
71
72                /* se requisitado, carrega os usuários */
73                if (strpos($entities, 'u') !== false)
74                {
75                        $preffix = ($usePreffix) ? 'u' : '';
76                        $ents = $this->ldap->getUsers($params['context']);
77                        foreach ($ents as $ent)
78                                $output[$preffix . $ent[$id]] = $ent['name'];
79                }
80
81                /* se requisitado, carrega os grupos */
82                if (strpos($entities, 'g') !== false)
83                {
84                        $preffix = ($usePreffix) ? 'g' : '';
85                        $ents = $this->ldap->getGroups($params['context']);
86                        foreach ($ents as $ent)
87                                $output[$preffix . $ent[$id]] = $ent['name'];
88                }
89
90                /* se requisitado, carrega as listas públicas */
91                if (strpos($entities, 'l') !== false)
92                {
93                        $preffix = ($usePreffix) ? 'l' : '';
94                        $ents = $this->ldap->getPublicLists($params['context']);
95                        foreach ($ents as $ent)
96                                $output[$preffix . $ent[$id]] = $ent['name'];
97                }
98
99                if (!$raw)
100                        $output = implode("\n", array_map(create_function('$a,$b', 'return \'<option value="\' . $a . \'">\' . $b . \'</option>\';'), array_keys($output), array_values($output)));
101
102                return $output;
103        }
104
105        /**
106         * Busca os setores de um dado contexto
107         * @param array $params Array que contém parâmetros necessários para fazer a busca (podem ser advindos de Ajax)
108         * @param bool $raw Se false, indica que os dados devem ser retornados em código HTML (via Ajax), se true indica que os dados devem ser retornados em forma de array
109         * @return mixed A lista de setores em formato de array ou em formato de string HTML para construção de combo box
110         * @access public
111         */
112        function getSectors($params, $raw = false)
113        {
114                if (preg_match('/^[a-z0-9_\- ]+$/i', $params['organization']) < 1)
115                {
116                        if ($raw)
117                                return array();
118                        else
119                                return array('sectors' => '', 'participants' => '');
120                }
121
122                $output = array('ou=' . $params['organization'] . ',' . $this->ldap->getLDAPContext() => $params['organization']);
123                $sectorList = $this->ldap->getSectors($params['organization'], true);
124                foreach ($sectorList as $sector)
125                        $output[$sector['dn']] = str_repeat('&nbsp;', 3 * $sector['level']) . $sector['ou'];
126
127                if (!$raw)
128                {
129                        $newOutput['sectors'] = implode("\n", array_map(create_function('$a,$b', 'return \'<option value="\' . $a . \'">\' . $b . \'</option>\';'), array_keys($output), array_values($output)));
130                        reset($output);
131                        $params['context'] = key($output);
132                        $newOutput['participants'] = $this->getEntities($params, false);
133                        $output = $newOutput;
134                }
135
136                return $output;
137        }
138
139        /**
140         * Efetua uma busca no catálogo LDAP (completo)
141         * @param array $params Array que contém parâmetros necessários para fazer a busca (podem ser advindos de Ajax)
142         * @param bool $raw Se false, indica que os dados devem ser retornados em código HTML (via Ajax), se true indica que os dados devem ser retornados em forma de array
143         * @return mixed Os registros encontrados, em formato de array ou em formato de string HTML para construção de combo box
144         * @access public
145         */
146        function globalSearch($params, $raw = false)
147        {
148                $entities = $params['entities'];
149
150                $searchTerm = $params['searchTerm'];
151
152                if (strlen(str_replace(' ', '', $searchTerm)) < 3)
153                        return array('warnings' => array('Utilize ao menos três caracteres em sua busca.'));
154
155                if (preg_match('/^[a-z0-9_\- =,]+$/i', $searchTerm) < 1)
156                        return array('warnings' => array('O parâmetro de busca é inválido.'));
157
158                $searchTerm = '*' . str_replace(' ', '*', trim($searchTerm)) . '*';
159
160                $searchUsers = (strpos($entities, 'u') !== false);
161                $searchGroups = (strpos($entities, 'g') !== false);
162                $searchLists = (strpos($entities, 'l') !== false);
163
164                /* faz a busca */
165                $output = array();
166                $output['participants'] = $this->ldap->search($searchTerm, $searchUsers, $searchGroups, $searchLists);
167
168                /* limita os resultados e define uma mensagem que será exibida */
169                $participantsCount = count($output['participants']);
170                if ($participantsCount > 200)
171                {
172                        $participantsCount = 200;
173                        $output['participants'] = array_slice($output['participants'], 0, 200);
174                        $output['warnings'][] = 'Sua busca foi limitada a 200 registros.';
175                }
176                else
177                {
178                        if ($participantsCount === 0)
179                                $output['warnings'][] = 'Nenhum registro encontrado.';
180                        else
181                                if ($participantsCount > 1)
182                                        $output['warnings'][] = $participantsCount . ' registros encontrados';
183
184                }
185
186                /* se necessário, gera a saída em formato HTML */
187                if (!$raw)
188                {
189                        $id = ($params['id'] === 'mail') ? 'mail' : 'id';
190                        $usePreffix = ($params['usePreffix'] == true) ? true : false;
191
192                        $newOutput = array();
193                        foreach ($output['participants'] as $row)
194                        {
195                                $organization = str_replace('ou=', '', implode('/', array_reverse(array_filter(explode(',', $row['dn']), create_function('$a', 'return (substr($a, 0, 2) == "ou");')))));
196                                $key = ($usePreffix ? $row['type'] : '') . $row[$id];
197                                $value = "{$row['name']} ({$organization})";
198                                $newOutput[$key] = $value;
199                        }
200
201                        $newOutput = implode("\n", array_map(create_function('$a,$b', 'return \'<option value="\' . $a . \'">\' . $b . \'</option>\';'), array_keys($newOutput), array_values($newOutput)));
202                        $output['participants'] = $newOutput;
203                }
204
205                return $output;
206        }
207}
Note: See TracBrowser for help on using the repository browser.