source: branches/2.2/workflow/inc/class.bo_participants.inc.php @ 3704

Revision 3704, 7.5 KB checked in by asaikawa, 13 years ago (diff)

Ticket #1506 - Adicionado parametro get_email no componente, que indica se deve ser recuperado o endereco de email

  • 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 = &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                if($params['id'] === 'mail')
67                {
68                        $id = 'mail';
69                        $usePreffix = false;
70                }
71                else
72                {
73                        $id = 'id';
74                        $usePreffix = ($params['usePreffix'] == true) ? true : false;
75                }
76
77                $output = array();
78                $entities = $params['entities'];
79
80                /* se requisitado, carrega os usuários */
81                if (strpos($entities, 'u') !== false)
82                {
83                        $preffix = ($usePreffix) ? 'u' : '';
84                        $ents = $this->ldap->getUsers($params['context'], $params['onlyVisibleAccounts']);
85                        foreach ($ents as $ent)
86                                $output[$preffix . $ent[$id]] = $ent['name'];
87                }
88
89                /* se requisitado, carrega os grupos */
90                if (strpos($entities, 'g') !== false)
91                {
92                        $preffix = ($usePreffix) ? 'g' : '';
93                        $ents = $this->ldap->getGroups($params['context']);
94                        foreach ($ents as $ent)
95                                $output[$preffix . $ent[$id]] = $ent['name'];
96                }
97
98                /* se requisitado, carrega as listas públicas */
99                if (strpos($entities, 'l') !== false)
100                {
101                        $preffix = ($usePreffix) ? 'l' : '';
102                        $ents = $this->ldap->getPublicLists($params['context']);
103                        foreach ($ents as $ent)
104                                $output[$preffix . $ent[$id]] = $ent['name'];
105                }
106
107                if (!$raw)
108                        $output = implode("\n", array_map(create_function('$a,$b', 'return \'<option value="\' . $a . \'">\' . $b . \'</option>\';'), array_keys($output), array_values($output)));
109
110                return $output;
111        }
112
113        /**
114         * Busca os setores de um dado contexto
115         * @param array $params Array que contém parâmetros necessários para fazer a busca (podem ser advindos de Ajax)
116         * @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
117         * @return mixed A lista de setores em formato de array ou em formato de string HTML para construção de combo box
118         * @access public
119         */
120        function getSectors($params, $raw = false)
121        {
122                if (preg_match('/^[a-z0-9_\- ]+$/i', $params['organization']) < 1)
123                {
124                        if ($raw)
125                                return array();
126                        else
127                                return array('sectors' => '', 'participants' => '');
128                }
129
130                $output = array('ou=' . $params['organization'] . ',' . $this->ldap->getLDAPContext() => $params['organization']);
131                $sectorList = $this->ldap->getSectors($params['organization'], true);
132                foreach ($sectorList as $sector)
133                        $output[$sector['dn']] = str_repeat('&nbsp;', 3 * $sector['level']) . $sector['ou'];
134
135                if (!$raw)
136                {
137                        $newOutput['sectors'] = implode("\n", array_map(create_function('$a,$b', 'return \'<option value="\' . $a . \'">\' . $b . \'</option>\';'), array_keys($output), array_values($output)));
138                        reset($output);
139                        $params['context'] = key($output);
140                        $newOutput['participants'] = $this->getEntities($params, false);
141                        $output = $newOutput;
142                }
143
144                return $output;
145        }
146
147        /**
148         * Efetua uma busca no catálogo LDAP (completo)
149         * @param array $params Array que contém parâmetros necessários para fazer a busca (podem ser advindos de Ajax)
150         * @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
151         * @return mixed Os registros encontrados, em formato de array ou em formato de string HTML para construção de combo box
152         * @access public
153         */
154        function globalSearch($params, $raw = false)
155        {
156                $entities = $params['entities'];
157
158                $searchTerm = $params['searchTerm'];
159
160                if (strlen(str_replace(' ', '', $searchTerm)) < 3)
161                        return array('warnings' => array('Utilize ao menos três caracteres em sua busca.'));
162
163                if (preg_match('/^[a-z0-9_\- =,]+$/i', $searchTerm) < 1)
164                        return array('warnings' => array('O parâmetro de busca é inválido.'));
165
166                $searchTerm = '*' . str_replace(' ', '*', trim($searchTerm)) . '*';
167
168                $searchUsers = (strpos($entities, 'u') !== false);
169                $searchGroups = (strpos($entities, 'g') !== false);
170                $searchLists = (strpos($entities, 'l') !== false);
171
172                $onlyVisibleAccounts = $params['onlyVisibleAccounts'];
173
174                /* faz a busca */
175                $output = array();
176                $output['participants'] = $this->ldap->search($searchTerm, $searchUsers, $searchGroups, $searchLists, null, $onlyVisibleAccounts);
177
178                /* limita os resultados e define uma mensagem que será exibida */
179                $participantsCount = count($output['participants']);
180                if ($participantsCount > 200)
181                {
182                        $participantsCount = 200;
183                        $output['participants'] = array_slice($output['participants'], 0, 200);
184                        $output['warnings'][] = 'Sua busca foi limitada a 200 registros.';
185                }
186                else
187                {
188                        if ($participantsCount === 0)
189                                $output['warnings'][] = 'Nenhum registro encontrado.';
190                        else
191                                if ($participantsCount > 1)
192                                        $output['warnings'][] = $participantsCount . ' registros encontrados';
193
194                }
195
196                /* se necessário, gera a saída em formato HTML */
197                if (!$raw)
198                {
199                        if($params['id'] === 'mail')
200                        {
201                                $id = 'mail';
202                                $usePreffix = false;
203                        }
204                        else
205                        {
206                                $id = 'id';
207                                $usePreffix = ($params['usePreffix'] == true) ? true : false;
208                        }
209
210                        $newOutput = array();
211                        foreach ($output['participants'] as $row)
212                        {
213                                $organization = str_replace('ou=', '', implode('/', array_reverse(array_filter(explode(',', $row['dn']), create_function('$a', 'return (substr($a, 0, 2) == "ou");')))));
214                                $key = ($usePreffix ? $row['type'] : '') . $row[$id];
215                                $value = "{$row['name']} ({$organization})";
216                                $newOutput[$key] = $value;
217                        }
218
219                        $newOutput = implode("\n", array_map(create_function('$a,$b', 'return \'<option value="\' . $a . \'">\' . $b . \'</option>\';'), array_keys($newOutput), array_values($newOutput)));
220                        $output['participants'] = $newOutput;
221                }
222
223                return $output;
224        }
225}
Note: See TracBrowser for help on using the repository browser.