source: branches/2.2/contactcenter/inc/class.so_group.inc.php @ 3217

Revision 3217, 14.7 KB checked in by rodsouza, 14 years ago (diff)

Ticket #884 - Distinguindo telefone e e-mail na exportação de contato no formato ExpressoLivre?

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2  /***************************************************************************\
3  * eGroupWare - Contacts Center                                              *
4  * http://www.egroupware.org                                                 *
5  * Storage Object Classes                                                    *
6  * Written by:                                                               *
7  *  - Nilton Emilio Buhrer Neto <niltonneto@celepar.pr.gov.br>                       *
8  *  sponsored by Celepar - http://celepar.pr.gov.br                          *
9  * ------------------------------------------------------------------------- *
10  *  This program is free software; you can redistribute it and/or modify it  *
11  *  under the terms of the GNU General Public License as published by the    *
12  *  Free Software Foundation; either version 2 of the License, or (at your   *
13  *  option) any later version.                                               *
14  \***************************************************************************/
15        class so_group {
16               
17                var $db;
18                var $owner;
19
20                function so_group ()
21                {
22                        $this->db    = $GLOBALS['phpgw']->db;
23                        $this->owner = $GLOBALS['phpgw_info']['user']['account_id'];
24                }
25               
26                function select($id = '')
27                {
28                        $query = 'SELECT id_group,title,short_name FROM phpgw_cc_groups ';
29                        if($id != '')
30                                $query .= 'WHERE id_group ='.$id;
31                       
32                        $query .= ' ORDER BY title';
33                       
34                        if (!$this->db->query($query))
35                        {
36                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
37                        }
38                         
39                        $return = false;
40                       
41                        while($this->db->next_record())
42                        {
43                                $return[] = $this->db->row();
44                        }
45                       
46                        return $return;
47                }
48
49                function remove_accents($string) {
50                        return strtr($string,
51                        "?Ó??ó?Ý?úÁÀÃÂÄÇÉÈÊËÍÌ?ÎÏÑÕÔÓÒÖÚÙ?ÛÜ?áàãâäçéèêëíì?îïñóòõôöúù?ûüýÿ",
52                        "SOZsozYYuAAAAACEEEEIIIIINOOOOOUUUUUsaaaaaceeeeiiiiinooooouuuuuyy");
53                }
54               
55                function insert($data)
56                {
57                       
58                        $shortName = $this -> remove_accents(strtolower(str_replace(" ","", $data['title'])));                 
59                       
60                        $query = "INSERT INTO phpgw_cc_groups(title, owner,short_name) ".
61                                        "VALUES('".$data['title']."',".$this->owner.",'".$shortName."')";                       
62                                                                               
63                        if (!$this->db->query($query))
64                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);                             
65                                               
66                        $objSequence = $this->db->query("SELECT last_value FROM seq_phpgw_cc_groups");                 
67                        $id = $objSequence -> fields['last_value'];
68                        $this -> updateContactsByGroup($id, $data['contact_in_list']);
69
70                        return $id;             
71                }
72               
73                function update($data)
74                {
75                        $shortName = $this -> remove_accents(strtolower(str_replace(" ","", $data['title'])));
76                                               
77                        $query = "UPDATE phpgw_cc_groups SET title = '".$data['title']."',short_name = '".$shortName."' WHERE id_group = ".$data['id_group'];
78                       
79                        if (!$this->db->query($query))                 
80                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);                             
81                       
82                        $this -> updateContactsByGroup($data['id_group'], $data['contact_in_list']);           
83                                                                       
84                        return True;           
85                }
86               
87                function delete($data)
88                {
89                        $query = "DELETE FROM phpgw_cc_groups WHERE id_group = ".$data['id_group'];                     
90                                                                               
91                        if (!$this->db->query($query))                 
92                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);                             
93                                                                       
94                        $this -> deleteContactsByGroup($data['id_group'], $data['contact_in_list']);
95                                               
96                        return True;           
97                }               
98               
99                function deleteContactFromGroups($id)
100                {
101                        $query = "DELETE FROM phpgw_cc_contact_grps WHERE id_connection = ".$id;                       
102                                                                               
103                        if (!$this->db->query($query))                 
104                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);                             
105                                                                       
106                        return True;           
107                }
108               
109                //Owner = null means the owner setted on constructor.
110                function selectAllContacts( $field = false ,$shared_from=null)
111                {
112                        if ( $shared_from == NULL )
113                        {
114                                $query = 'select'
115                                                . ' phpgw_cc_connections.id_connection,'
116                                                . ' phpgw_cc_contact.id_contact,'
117                                                . ' phpgw_cc_contact.names_ordered,'
118                                                . ' phpgw_cc_contact.alias,'
119                                                . ' phpgw_cc_connections.connection_name,'
120                                                . ' phpgw_cc_connections.connection_value,'
121                                                . ' phpgw_cc_contact_conns.id_typeof_contact_connection,'
122                                                . ' phpgw_cc_contact_addrs.id_typeof_contact_address,'
123                                                . ' phpgw_cc_addresses.address1,'
124                                                . ' phpgw_cc_addresses.complement,'
125                                                . ' phpgw_cc_addresses.postal_code,'
126                                                . ' phpgw_cc_city.city_name,'
127                                                . ' phpgw_cc_state.state_name,'
128                                                . ' phpgw_cc_addresses.id_country'
129                                                ;
130
131                                $query .= ' from'
132                                                . ' phpgw_cc_contact'
133                                                . ' inner join phpgw_cc_contact_conns on ( phpgw_cc_contact.id_contact = phpgw_cc_contact_conns.id_contact )'
134                                                . ' inner join phpgw_cc_connections on ( phpgw_cc_contact_conns.id_connection = phpgw_cc_connections.id_connection )'
135                                                . ' left join phpgw_cc_contact_addrs on ( phpgw_cc_contact.id_contact = phpgw_cc_contact_addrs.id_contact )'
136                                                . ' left join phpgw_cc_addresses on ( phpgw_cc_contact_addrs.id_address = phpgw_cc_addresses.id_address )'
137                                                . ' left join phpgw_cc_city on ( phpgw_cc_addresses.id_city = phpgw_cc_city.id_city )'
138                                                . ' left join phpgw_cc_state on ( phpgw_cc_addresses.id_state = phpgw_cc_state.id_state)'
139                                                ;
140
141                                $query .= ' where'
142                                                . " phpgw_cc_contact.id_owner = {$this->owner}"
143                                                //. ' and phpgw_cc_connections.connection_is_default = true'
144                                                ;
145
146                        }
147                        else {
148                                $sub_query = 'select A.id_related from phpgw_cc_contact_rels A,phpgw_acl B
149                                                          where B.acl_location!=\'run\' and A.id_contact = B.acl_location::bigint and A.id_related = B.acl_account and
150                                                          B.acl_appname = \'contactcenter\' and B.acl_rights & 1 <> 0
151                                                          and A.id_typeof_contact_relation=1 and A.id_contact = '.$shared_from.' and A.id_related='.$this->owner;
152
153                                $query = 'select C.id_connection, A.id_contact, A.names_ordered, C.connection_value , B.id_typeof_contact_connection from phpgw_cc_contact A,'.
154                                'phpgw_cc_contact_conns B, phpgw_cc_connections C where '.
155                                'A.id_contact = B.id_contact and B.id_connection = C.id_connection and '.
156                                'A.id_owner in ('.$shared_from.',('.$sub_query.'))'.
157                                ' and C.connection_is_default = true ';
158                        }
159
160                        if ( $field == 'only_email' )
161                                $query .= 'and phpgw_cc_contact_conns.id_typeof_contact_connection = 1 ';
162
163                        $query .= ' order by phpgw_cc_contact.names_ordered, phpgw_cc_connections.connection_value';
164
165                        if (!$this->db->query($query))
166                        {
167                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
168                        }
169
170                        $return = array();
171
172                        while($this->db->next_record())
173                        {
174                                $return[] = $this->db->row();
175                        }
176
177                        if( ! count( $return ) )
178                                return $return;
179
180                        foreach( $return as $i => $object )
181                        {
182                                if ( ! array_key_exists( $object[ 'id_contact' ], $all_contacts ) )
183                                        $all_contacts[ $object[ 'id_contact' ] ] = array(
184                                                'connection_value' => '',
185                                                'phone' => '',
186                                                'mobile' => '',
187                                                'names_ordered' => '',
188                                                'id_contact' => '',
189                                                'id_connection' => '',
190                                                'alias' => '',
191                                                'main-mail' => '',
192                                                'aternative-mail' => '',
193                                                'business-phone' => '',
194                                                'business-address' => '',
195                                                'business-complement' => '',
196                                                'business-postal_code' => '',
197                                                'business-city_name' => '',
198                                                'business-state_name' => '',
199                                                'business-id_country' => '',
200                                                'home-phone' => '',
201                                                'home-address' => '',
202                                                'home-complement' => '',
203                                                'home-postal_code' => '',
204                                                'home-city_name' => '',
205                                                'home-state_name' => '',
206                                                'home-id_country' => ''
207                                        );
208
209                                switch( $object[ 'id_typeof_contact_connection' ] )
210                                {
211                                        case 1 :
212                                                $all_contacts[ $object[ 'id_contact' ] ][ 'connection_value' ] = $object[ 'connection_value' ];
213                                                switch ( strtolower( $object[ 'connection_name' ] ) )
214                                                {
215                                                        case 'alternativo' :
216                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'alternative-mail' ] = $object[ 'connection_value' ];
217                                                                break;
218                                                        case 'principal' :
219                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'main-mail' ] = $object[ 'connection_value' ];
220                                                                break;
221                                                }
222                                                break;
223                                        case 2 :
224                                                $all_contacts[ $object[ 'id_contact' ] ][ 'phone' ] = $object[ 'connection_value' ];
225                                                switch ( strtolower( $object[ 'connection_name' ] ) )
226                                                {
227                                                        case 'casa' :
228                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-phone' ] = $object[ 'connection_value' ];
229                                                                break;
230                                                        case 'celular' :
231                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'mobile' ] = $object[ 'connection_value' ];
232                                                                break;
233                                                        case 'trabalho' :
234                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-phone' ] = $object[ 'connection_value' ];
235                                                                break;
236                                                }
237                                                break;
238                                }
239
240                                $all_contacts[ $object[ 'id_contact' ] ][ 'names_ordered' ] = $object[ 'names_ordered' ];
241                                $all_contacts[ $object[ 'id_contact' ] ][ 'id_contact' ]    = $object[ 'id_contact' ];
242                                $all_contacts[ $object[ 'id_contact' ] ][ 'id_connection' ] = $object[ 'id_connection' ];
243                                $all_contacts[ $object[ 'id_contact' ] ][ 'alias' ]         = $object[ 'alias' ];
244
245                                switch( $object[ 'id_typeof_contact_address' ] )
246                                {
247                                        case 1 :
248                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-address' ]     = $object[ 'address1' ];
249                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-complement' ]  = $object[ 'complement' ];
250                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-postal_code' ] = $object[ 'postal_code' ];
251                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-city_name' ]   = $object[ 'city_name' ];
252                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-state_name' ]  = $object[ 'state_name' ];
253                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-id_country' ]  = $object[ 'id_country' ];
254                                                break;
255                                        case 2 :
256                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-address' ]     = $object[ 'address1' ];
257                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-complement' ]  = $object[ 'complement' ];
258                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-postal_code' ] = $object[ 'postal_code' ];
259                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-city_name' ]   = $object[ 'city_name' ];
260                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-state_name' ]  = $object[ 'state_name' ];
261                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-id_country' ]  = $object[ 'id_country' ];
262                                                break;
263                                }
264                        }
265
266                        return array_values($all_contacts);
267                }
268
269                function verifyContact($email)
270                {
271                        $query = 'select A.names_ordered from phpgw_cc_contact A,'.
272                        'phpgw_cc_contact_conns B, phpgw_cc_connections C where '.
273                        'A.id_contact = B.id_contact and B.id_connection = C.id_connection '.
274                        'and B.id_typeof_contact_connection = 1 and '.
275                        'A.id_owner ='.$this->owner.' and C.connection_value = \''.$email.'\'';
276                                               
277                        if (!$this->db->query($query))
278                        {
279                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
280                        }
281                         
282                        $return = false;
283                       
284                        while($this->db->next_record())
285                        {
286                                $row = $this->db->row();
287                                $return[] =  $row['names_ordered'];
288                        }
289                       
290                        return $return;
291                }               
292               
293                function selectContactsByGroup($idGroup)
294                {
295                        $query = 'select C.id_connection, A.names_ordered, C.connection_value from phpgw_cc_contact A,'.
296                        'phpgw_cc_contact_conns B, phpgw_cc_connections C,phpgw_cc_contact_grps D where '.
297                        'A.id_contact = B.id_contact and B.id_connection = C.id_connection '.
298                        'and B.id_typeof_contact_connection = 1 and '.
299                        //'A.id_owner ='.$this->owner.' and'.
300                        ' D.id_connection = C.id_connection and D.id_group = '.$idGroup.
301                        ' order by A.names_ordered';
302                                               
303                        if (!$this->db->query($query))
304                        {
305                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
306                        }
307                         
308                        $return = false;
309                       
310                       
311                        while($this->db->next_record())
312                        {
313                                $return[] = $this->db->row();
314                        }
315                       
316                        return $return;
317                }
318               
319                function selectContactsByGroupAlias($alias)
320                {
321                        $query = "select C.id_connection, A.names_ordered, C.connection_value from phpgw_cc_contact A, ".
322                        "phpgw_cc_contact_conns B, phpgw_cc_connections C,phpgw_cc_contact_grps D,phpgw_cc_groups E where ".
323                        "A.id_contact = B.id_contact and B.id_connection = C.id_connection ".
324                        "and B.id_typeof_contact_connection = 1 and ".
325                        "A.id_owner =".$this->owner." and ".                   
326                        "D.id_group = E.id_group and ".
327                        "D.id_connection = C.id_connection and E.short_name = '".$alias.
328                        "' order by A.names_ordered";
329                                               
330                        if (!$this->db->query($query))
331                        {
332                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
333                        }
334                         
335                        $return = false;
336                       
337                       
338                        while($this->db->next_record())
339                        {
340                                $return[] = $this->db->row();
341                        }
342                       
343                        return $return;
344                }
345               
346                function insertContactsByGroup($idGroup, $contacts)
347                {                                                                       
348                       
349                        foreach($contacts as $index => $idConnection)
350                        {                       
351                                $query = "INSERT INTO phpgw_cc_contact_grps(id_group,id_connection) ".
352                                                "VALUES(".$idGroup.",".$idConnection.")";                       
353
354                                if (!$this->db->query($query))
355                                        exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);                     
356                        }
357                                                                                               
358                        return True;           
359                }
360               
361                function updateContactsByGroup($id_group, $contacts)
362                {
363                                               
364                        $query = 'select C.id_connection from phpgw_cc_contact A,'.
365                        'phpgw_cc_contact_conns B, phpgw_cc_connections C,phpgw_cc_contact_grps D where '.
366                        'A.id_contact = B.id_contact and B.id_connection = C.id_connection '.
367                        'and B.id_typeof_contact_connection = 1 and '.
368                        //'A.id_owner ='.$this->owner.' and D.id_connection = C.id_connection and D.id_group = '.$id_group.
369                        ' D.id_connection = C.id_connection and D.id_group = '.$id_group. //If I have the group ID, why ask about owner?
370                        ' order by A.names_ordered';
371                                               
372                        if (!$this->db->query($query))
373                        {
374                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
375                        }
376                         
377                        $return = false;
378                       
379                       
380                        while($this->db->next_record())
381                        {
382                                $return[] = $this->db->row();
383                                 
384                        }
385                                               
386                        $array_connections = array();
387                       
388                        if($return) {
389                                foreach($return as $index => $connection){                             
390                                        array_push($array_connections,$connection['id_connection']);
391                                }
392                        }
393                       
394                        if(!is_array($contacts))
395                                $contacts = array();
396                       
397                        if(!is_array($connections))
398                                $connections = array();                         
399                                                                       
400                        $connections_to_add     = array_diff($contacts, $array_connections);
401                        $connections_to_remove  = array_diff($array_connections,$contacts);
402                       
403                        if($connections_to_add){
404                                $this -> insertContactsByGroup($id_group, $connections_to_add); 
405                        }
406                       
407                        if($connections_to_remove){
408                                $this -> deleteContactsByGroup($id_group, $connections_to_remove);
409                        }
410                       
411                        return True;
412                }               
413               
414                function deleteContactsByGroup($id_group, $contacts = null)
415                {
416                       
417                        $query = "DELETE FROM phpgw_cc_contact_grps ";
418                       
419                        if($contacts) {                                         
420                                $index = 0;
421                               
422                                foreach($contacts as $a => $id_connection) {
423                                         
424                                        if($index++)
425                                                $query .= " OR";
426                                        else
427                                                $query .= " WHERE (";
428                                         
429                                        $query .= " id_connection = ".$id_connection;                                                                           
430                                }
431                                $query .= ") AND ";
432                        }       
433                        else
434                                $query.= " WHERE ";
435                       
436                        $query.= "id_group = ".$id_group;
437                       
438                       
439                        if (!$this->db->query($query))                 
440                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);                             
441                                                                       
442                        return True;           
443                }
444        }
445?>
Note: See TracBrowser for help on using the repository browser.