source: trunk/services/class.db.php @ 5072

Revision 5072, 11.4 KB checked in by airton, 13 years ago (diff)

Ticket #2267 - Exibir mais detalhes dos contatos retornados na busca rapida de destinatarios

  • Property svn:executable set to *
Line 
1<?php
2/**
3*
4* Copyright (C) 2011 Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
5*
6* This program is free software; you can redistribute it and/or modify it under
7* the terms of the GNU Affero General Public License version 3 as published by
8* the Free Software Foundation with the addition of the following permission
9* added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
10* WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE
11* WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
12*
13* This program is distributed in the hope that it will be useful, but WITHOUT
14* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15* FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16* details.
17*
18* You should have received a copy of the GNU Affero General Public License
19* along with this program; if not, see www.gnu.org/licenses or write to
20* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21* MA 02110-1301 USA.
22*
23* This code is based on the OpenXchange Connector and on the Prognus pSync
24* Connector both developed by the community and licensed under the GPL
25* version 2 or above as published by the Free Software Foundation.
26*
27* You can contact Prognus Software Livre headquarters at Av. Tancredo Neves,
28* 6731, PTI, Bl. 05, Esp. 02, Sl. 10, Foz do Iguaçu - PR - Brasil or at
29* e-mail address prognus@prognus.com.br.
30*
31* Serviço de banco de dados
32*
33* Serviço que faz a busca no banco de dados para retornar os usuários, de acordo com o parâmetro de busca.
34*
35* @package    <services>
36* @license    http://www.gnu.org/copyleft/gpl.html GPL
37* @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
38* @version    1.0
39* @sponsor    Caixa Econômica Federal
40*/
41
42    if(!isset($_SESSION['phpgw_info']['expressomail']['server']['db_name'])) {
43        include_once('../header.inc.php');
44        $_SESSION['phpgw_info']['expressomail']['server']['db_name'] = $GLOBALS['phpgw_info']['server']['db_name']; 
45        $_SESSION['phpgw_info']['expressomail']['server']['db_host'] = $GLOBALS['phpgw_info']['server']['db_host'];
46        $_SESSION['phpgw_info']['expressomail']['server']['db_port'] = $GLOBALS['phpgw_info']['server']['db_port'];
47        $_SESSION['phpgw_info']['expressomail']['server']['db_user'] = $GLOBALS['phpgw_info']['server']['db_user'];
48        $_SESSION['phpgw_info']['expressomail']['server']['db_pass'] = $GLOBALS['phpgw_info']['server']['db_pass'];
49        $_SESSION['phpgw_info']['expressomail']['server']['db_type'] = $GLOBALS['phpgw_info']['server']['db_type'];
50    }
51    else{
52        define('PHPGW_INCLUDE_ROOT','../');     
53        define('PHPGW_API_INC','../phpgwapi/inc');       
54        include_once(PHPGW_API_INC.'/class.db.inc.php');
55    }
56
57
58class DBService
59{   
60    var $connection;
61    var $db;
62    var $user_id;
63
64    function DBService()
65    {
66        $this->db = new db();           
67        $this->db->Halt_On_Error = 'no';
68        $this->db->connect(
69            $_SESSION['phpgw_info']['expressomail']['server']['db_name'],
70            $_SESSION['phpgw_info']['expressomail']['server']['db_host'],
71            $_SESSION['phpgw_info']['expressomail']['server']['db_port'],
72            $_SESSION['phpgw_info']['expressomail']['server']['db_user'],
73            $_SESSION['phpgw_info']['expressomail']['server']['db_pass'],
74            $_SESSION['phpgw_info']['expressomail']['server']['db_type']
75        );             
76        $this -> user_id = $_SESSION['phpgw_info']['expressomail']['user']['account_id'];
77    }
78
79    function search_contacts($search_for)
80    {
81        $result = array();
82        $query = 'select'
83       
84                                                . ' C.id_connection,'
85                                                . ' A.id_contact,'
86                                                . ' A.names_ordered,'
87                                                . ' A.alias,'
88                                                . ' A.birthdate,'
89                                                . ' A.sex,'
90                                                . ' A.pgp_key,'
91                                                . ' A.notes,'
92                                                . ' A.web_page,'
93                                                . ' A.corporate_name,'
94                                                . ' A.job_title,'
95                                                . ' A.department,'
96                                                . ' C.connection_name,'
97                                                . ' C.connection_value,'
98                                                . ' B.id_typeof_contact_connection,'
99                                                . ' phpgw_cc_contact_addrs.id_typeof_contact_address,'
100                                                . ' phpgw_cc_addresses.address1,'
101                                                . ' phpgw_cc_addresses.address2,'
102                                                . ' phpgw_cc_addresses.complement,'
103                                                . ' phpgw_cc_addresses.postal_code,'
104                                                . ' phpgw_cc_city.city_name,'
105                                                . ' phpgw_cc_state.state_name,'
106                                                . ' phpgw_cc_addresses.id_country'
107                                                ;
108
109                $query .= ' from'
110                        . ' phpgw_cc_contact A'
111                        . ' inner join phpgw_cc_contact_conns B on ( A.id_contact = B.id_contact )'
112                        . ' inner join phpgw_cc_connections C on ( B.id_connection = C.id_connection )'
113                        . ' left join phpgw_cc_contact_addrs on ( A.id_contact = phpgw_cc_contact_addrs.id_contact )'
114                                                . ' left join phpgw_cc_addresses on ( phpgw_cc_contact_addrs.id_address = phpgw_cc_addresses.id_address )'
115                                                . ' left join phpgw_cc_city on ( phpgw_cc_addresses.id_city = phpgw_cc_city.id_city )'
116                                                . ' left join phpgw_cc_state on ( phpgw_cc_addresses.id_state = phpgw_cc_state.id_state)'
117                                                ;
118
119                                $query .= ' where '
120                                        . 'A.id_owner=' . $_SESSION['phpgw_info']['expressomail']['user']['account_id']
121                                        . ' and names_ordered LIKE \'%' . $search_for . '%\'';                         
122                               
123        if (!$this->db->query($query))
124             return null;
125         
126         while($this->db->next_record())
127            $result[] = $this->db->row();
128
129         
130         
131         $all_contacts = array();
132                        foreach( $result as $i => $object )
133                        {
134                                if ( ! array_key_exists( $object[ 'id_contact' ], $all_contacts ) )
135                                        $all_contacts[ $object[ 'id_contact' ] ] = array(
136                                                'connection_value' => '',
137                                                'telephonenumber' => '',
138                                                'mobile' => '',
139                                                'cn' => '',
140                                                'id_contact' => '',
141                                                'id_connection' => '',
142                                                'alias' => '',
143                                                'birthdate' => '',
144                                                'sex' => '',
145                                                'pgp_key' => '',
146                                                'notes' => '',
147                                                'web_page' => '',
148                                                'corporate_name' => '',
149                                                'job_title' => '',
150                                                'department' => '',                             
151                                                'mail' => '',
152                                                'aternative-mail' => '',
153                                                'business-phone' => '',
154                                                'business-address' => '',
155                                                'business-complement' => '',
156                                                'business-postal_code' => '',
157                                                'business-city_name' => '',
158                                                'business-state_name' => '',
159                                                'business-id_country' => '',
160                                                'business-fax' => '',
161                                                'business-pager' => '',
162                                                'business-mobile' => '',
163                                                'business-address-2' => '',
164                                                'home-phone' => '',
165                                                'home-address' => '',
166                                                'home-complement' => '',
167                                                'home-postal_code' => '',
168                                                'home-city_name' => '',
169                                                'home-state_name' => '',
170                                                'home-fax' => '',
171                                                'home-pager' => '',
172                                                'home-address-2' => ''
173                                               
174                                               
175                                        );
176
177                                switch( $object[ 'id_typeof_contact_connection' ] )
178                                {
179                                        case 1 :
180                                                $all_contacts[ $object[ 'id_contact' ] ][ 'connection_value' ] = $object[ 'connection_value' ];
181                                                switch ( strtolower( $object[ 'connection_name' ] ) )
182                                                {
183                                                        case 'alternativo' :
184                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'alternative-mail' ] = $object[ 'connection_value' ];
185                                                                break;
186                                                        case 'principal' :
187                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'mail' ] = $object[ 'connection_value' ];
188                                                                break;
189                                                }
190                                                break;
191                                        case 2 :
192                                                $all_contacts[ $object[ 'id_contact' ] ][ 'telephonenumber' ] = $object[ 'connection_value' ];
193                                                switch ( strtolower( $object[ 'connection_name' ] ) )
194                                                {
195                                                        case 'casa' :
196                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-phone' ] = $object[ 'connection_value' ];
197                                                                break;
198                                                        case 'celular' :
199                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'mobile' ] = $object[ 'connection_value' ];
200                                                                break;
201                                                        case 'trabalho' :
202                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-phone' ] = $object[ 'connection_value' ];
203                                                                break;                                                         
204                                                        case 'fax' :
205                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-fax' ] = $object[ 'connection_value' ];
206                                                                break;
207                                                        case 'pager' :
208                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-pager' ] = $object[ 'connection_value' ];
209                                                                break;
210                                                        case 'celular corporativo' :
211                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-mobile' ] = $object[ 'connection_value' ];
212                                                                break;                                                         
213                                                        case 'pager corporativo' :
214                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-pager' ] = $object[ 'connection_value' ];
215                                                                break;
216                                                        case 'fax corporativo' :
217                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-fax' ] = $object[ 'connection_value' ];
218                                                                break;
219                                                }
220                                                break;
221                                }
222
223                                $all_contacts[ $object[ 'id_contact' ] ][ 'cn' ] = $object[ 'names_ordered' ];
224                                $all_contacts[ $object[ 'id_contact' ] ][ 'id_contact' ]    = $object[ 'id_contact' ];
225                                $all_contacts[ $object[ 'id_contact' ] ][ 'id_connection' ] = $object[ 'id_connection' ];
226                                $all_contacts[ $object[ 'id_contact' ] ][ 'alias' ]         = $object[ 'alias' ];                               
227                                $all_contacts[ $object[ 'id_contact' ] ][ 'birthdate' ]         = $object[ 'birthdate' ];
228                                $all_contacts[ $object[ 'id_contact' ] ][ 'sex' ]               = $object[ 'sex' ];
229                                $all_contacts[ $object[ 'id_contact' ] ][ 'pgp_key' ]           = $object[ 'pgp_key' ];
230                                $all_contacts[ $object[ 'id_contact' ] ][ 'notes' ]         = $object[ 'notes' ];
231                                $all_contacts[ $object[ 'id_contact' ] ][ 'web_page' ]          = $object[ 'web_page' ];
232                                $all_contacts[ $object[ 'id_contact' ] ][ 'corporate_name' ]= $object[ 'corporate_name' ];
233                                $all_contacts[ $object[ 'id_contact' ] ][ 'job_title' ]         = $object[ 'job_title' ];
234                                $all_contacts[ $object[ 'id_contact' ] ][ 'department' ]    = $object[ 'department' ];
235
236                                switch( $object[ 'id_typeof_contact_address' ] )
237                                {
238                                        case 1 :
239                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-address' ]     = $object[ 'address1' ];
240                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-address-2' ]   = $object[ 'address2' ];
241                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-complement' ]  = $object[ 'complement' ];
242                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-postal_code' ] = $object[ 'postal_code' ];
243                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-city_name' ]   = $object[ 'city_name' ];
244                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-state_name' ]  = $object[ 'state_name' ];
245                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-id_country' ]  = $object[ 'id_country' ];
246                                                break;
247                                        case 2 :
248                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-address' ]     = $object[ 'address1' ];
249                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-address-2' ]   = $object[ 'address2' ];
250                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-complement' ]  = $object[ 'complement' ];
251                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-postal_code' ] = $object[ 'postal_code' ];
252                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-city_name' ]   = $object[ 'city_name' ];
253                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-state_name' ]  = $object[ 'state_name' ];
254                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-id_country' ]  = $object[ 'id_country' ];
255                                                break;
256                                }
257                        }
258
259                        return array_values($all_contacts);
260        }
261
262}
263
264ServiceLocator::register( 'db', new DBService() );
265
266?>
Note: See TracBrowser for help on using the repository browser.