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

Revision 3334, 18.7 KB checked in by eduardoalex, 14 years ago (diff)

Ticket #1370 - Adicionada a opcao de escolha do grupo para os contatos importados

  • 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                function selectGroupsOwnerCanImportContacts($owner) {
110                        $query = "select id_group,title,short_name from phpgw_cc_groups where owner=$owner or owner in (select B.acl_location::bigint from phpgw_acl A, phpgw_acl B where
111                                                A.acl_location=B.acl_account and A.acl_account=B.acl_location
112                                                and A.acl_appname = 'contactcenter' and B.acl_appname = 'contactcenter'
113                                                and A.acl_rights & 4 <> 0 and B.acl_rights & 1 <> 0
114                                                and A.acl_location = $owner)"; //He can import contacts only to his groups, or shared groups that he gave read permission.
115                        $this->db->query($query);
116                       
117                        $return = array();
118                       
119                        while($this->db->next_record())
120                        {
121                                $return[] = $this->db->row();
122                        }
123                       
124                        return $return;
125                       
126                }
127               
128                //Owner = null means the owner setted on constructor.
129                function selectAllContacts( $field = false ,$shared_from=null)
130                {
131                        if ( $shared_from == NULL )
132                        {
133                                $query = 'select'
134                                                . ' phpgw_cc_connections.id_connection,'
135                                                . ' phpgw_cc_contact.id_contact,'
136                                                . ' phpgw_cc_contact.names_ordered,'
137                                                . ' phpgw_cc_contact.alias,'
138                                                . ' phpgw_cc_contact.birthdate,'
139                                                . ' phpgw_cc_contact.sex,'
140                                                . ' phpgw_cc_contact.pgp_key,'
141                                                . ' phpgw_cc_contact.notes,'
142                                                . ' phpgw_cc_contact.web_page,'
143                                                . ' phpgw_cc_contact.corporate_name,'
144                                                . ' phpgw_cc_contact.job_title,'
145                                                . ' phpgw_cc_contact.department,'
146                                                . ' phpgw_cc_connections.connection_name,'
147                                                . ' phpgw_cc_connections.connection_value,'
148                                                . ' phpgw_cc_contact_conns.id_typeof_contact_connection,'
149                                                . ' phpgw_cc_contact_addrs.id_typeof_contact_address,'
150                                                . ' phpgw_cc_addresses.address1,'
151                                                . ' phpgw_cc_addresses.address2,'
152                                                . ' phpgw_cc_addresses.complement,'
153                                                . ' phpgw_cc_addresses.postal_code,'
154                                                . ' phpgw_cc_city.city_name,'
155                                                . ' phpgw_cc_state.state_name,'
156                                                . ' phpgw_cc_addresses.id_country'
157                                                ;
158
159                                $query .= ' from'
160                                                . ' phpgw_cc_contact'
161                                                . ' inner join phpgw_cc_contact_conns on ( phpgw_cc_contact.id_contact = phpgw_cc_contact_conns.id_contact )'
162                                                . ' inner join phpgw_cc_connections on ( phpgw_cc_contact_conns.id_connection = phpgw_cc_connections.id_connection )'
163                                                . ' left join phpgw_cc_contact_addrs on ( phpgw_cc_contact.id_contact = phpgw_cc_contact_addrs.id_contact )'
164                                                . ' left join phpgw_cc_addresses on ( phpgw_cc_contact_addrs.id_address = phpgw_cc_addresses.id_address )'
165                                                . ' left join phpgw_cc_city on ( phpgw_cc_addresses.id_city = phpgw_cc_city.id_city )'
166                                                . ' left join phpgw_cc_state on ( phpgw_cc_addresses.id_state = phpgw_cc_state.id_state)'
167                                                ;
168
169                                $query .= ' where'
170                                                . " phpgw_cc_contact.id_owner = {$this->owner}"
171                                                //. ' and phpgw_cc_connections.connection_is_default = true'
172                                                ;
173
174                        }
175                        else {
176                                $sub_query = 'select A.id_related from phpgw_cc_contact_rels A,phpgw_acl B
177                                                          where B.acl_location!=\'run\' and A.id_contact = B.acl_location::bigint and A.id_related = B.acl_account and
178                                                          B.acl_appname = \'contactcenter\' and B.acl_rights & 1 <> 0
179                                                          and A.id_typeof_contact_relation=1 and A.id_contact = '.$shared_from.' and A.id_related='.$this->owner;
180
181                                $query = 'select C.id_connection, A.id_contact, A.names_ordered, C.connection_value , B.id_typeof_contact_connection from phpgw_cc_contact A,'.
182                                'phpgw_cc_contact_conns B, phpgw_cc_connections C where '.
183                                'A.id_contact = B.id_contact and B.id_connection = C.id_connection and '.
184                                'A.id_owner in ('.$shared_from.',('.$sub_query.'))'.
185                                ' and C.connection_is_default = true ';
186                        }
187
188                        if ( $field == 'only_email' )
189                                $query .= 'and phpgw_cc_contact_conns.id_typeof_contact_connection = 1 ';
190
191                        $query .= ' order by phpgw_cc_contact.names_ordered, phpgw_cc_connections.connection_value';
192
193                        if (!$this->db->query($query))
194                        {
195                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
196                        }
197
198                        $return = array();
199
200                        while($this->db->next_record())
201                        {
202                                $return[] = $this->db->row();
203                        }
204
205                        if( ! count( $return ) )
206                                return $return;
207
208                        foreach( $return as $i => $object )
209                        {
210                                if ( ! array_key_exists( $object[ 'id_contact' ], $all_contacts ) )
211                                        $all_contacts[ $object[ 'id_contact' ] ] = array(
212                                                'connection_value' => '',
213                                                'phone' => '',
214                                                'mobile' => '',
215                                                'names_ordered' => '',
216                                                'id_contact' => '',
217                                                'id_connection' => '',
218                                                'alias' => '',
219                                                'birthdate' => '',
220                                                'sex' => '',
221                                                'pgp_key' => '',
222                                                'notes' => '',
223                                                'web_page' => '',
224                                                'corporate_name' => '',
225                                                'job_title' => '',
226                                                'department' => '',                             
227                                                'main-mail' => '',
228                                                'aternative-mail' => '',
229                                                'business-phone' => '',
230                                                'business-address' => '',
231                                                'business-complement' => '',
232                                                'business-postal_code' => '',
233                                                'business-city_name' => '',
234                                                'business-state_name' => '',
235                                                'business-id_country' => '',
236                                                'business-fax' => '',
237                                                'business-pager' => '',
238                                                'business-mobile' => '',
239                                                'business-address-2' => '',
240                                                'home-phone' => '',
241                                                'home-address' => '',
242                                                'home-complement' => '',
243                                                'home-postal_code' => '',
244                                                'home-city_name' => '',
245                                                'home-state_name' => '',
246                                                'home-fax' => '',
247                                                'home-pager' => '',
248                                                'home-address-2' => ''
249                                               
250                                               
251                                        );
252
253                                switch( $object[ 'id_typeof_contact_connection' ] )
254                                {
255                                        case 1 :
256                                                $all_contacts[ $object[ 'id_contact' ] ][ 'connection_value' ] = $object[ 'connection_value' ];
257                                                switch ( strtolower( $object[ 'connection_name' ] ) )
258                                                {
259                                                        case 'alternativo' :
260                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'alternative-mail' ] = $object[ 'connection_value' ];
261                                                                break;
262                                                        case 'principal' :
263                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'main-mail' ] = $object[ 'connection_value' ];
264                                                                break;
265                                                }
266                                                break;
267                                        case 2 :
268                                                $all_contacts[ $object[ 'id_contact' ] ][ 'phone' ] = $object[ 'connection_value' ];
269                                                switch ( strtolower( $object[ 'connection_name' ] ) )
270                                                {
271                                                        case 'casa' :
272                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-phone' ] = $object[ 'connection_value' ];
273                                                                break;
274                                                        case 'celular' :
275                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'mobile' ] = $object[ 'connection_value' ];
276                                                                break;
277                                                        case 'trabalho' :
278                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-phone' ] = $object[ 'connection_value' ];
279                                                                break;                                                         
280                                                        case 'fax' :
281                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-fax' ] = $object[ 'connection_value' ];
282                                                                break;
283                                                        case 'pager' :
284                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-pager' ] = $object[ 'connection_value' ];
285                                                                break;
286                                                        case 'celular corporativo' :
287                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-mobile' ] = $object[ 'connection_value' ];
288                                                                break;                                                         
289                                                        case 'pager corporativo' :
290                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-pager' ] = $object[ 'connection_value' ];
291                                                                break;
292                                                        case 'fax corporativo' :
293                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-fax' ] = $object[ 'connection_value' ];
294                                                                break;
295                                                }
296                                                break;
297                                }
298
299                                $all_contacts[ $object[ 'id_contact' ] ][ 'names_ordered' ] = $object[ 'names_ordered' ];
300                                $all_contacts[ $object[ 'id_contact' ] ][ 'id_contact' ]    = $object[ 'id_contact' ];
301                                $all_contacts[ $object[ 'id_contact' ] ][ 'id_connection' ] = $object[ 'id_connection' ];
302                                $all_contacts[ $object[ 'id_contact' ] ][ 'alias' ]         = $object[ 'alias' ];                               
303                                $all_contacts[ $object[ 'id_contact' ] ][ 'birthdate' ]         = $object[ 'birthdate' ];
304                                $all_contacts[ $object[ 'id_contact' ] ][ 'sex' ]               = $object[ 'sex' ];
305                                $all_contacts[ $object[ 'id_contact' ] ][ 'pgp_key' ]           = $object[ 'pgp_key' ];
306                                $all_contacts[ $object[ 'id_contact' ] ][ 'notes' ]         = $object[ 'notes' ];
307                                $all_contacts[ $object[ 'id_contact' ] ][ 'web_page' ]          = $object[ 'web_page' ];
308                                $all_contacts[ $object[ 'id_contact' ] ][ 'corporate_name' ]= $object[ 'corporate_name' ];
309                                $all_contacts[ $object[ 'id_contact' ] ][ 'job_title' ]         = $object[ 'job_title' ];
310                                $all_contacts[ $object[ 'id_contact' ] ][ 'department' ]    = $object[ 'department' ];
311
312                                switch( $object[ 'id_typeof_contact_address' ] )
313                                {
314                                        case 1 :
315                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-address' ]     = $object[ 'address1' ];
316                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-address-2' ]   = $object[ 'address2' ];
317                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-complement' ]  = $object[ 'complement' ];
318                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-postal_code' ] = $object[ 'postal_code' ];
319                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-city_name' ]   = $object[ 'city_name' ];
320                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-state_name' ]  = $object[ 'state_name' ];
321                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-id_country' ]  = $object[ 'id_country' ];
322                                                break;
323                                        case 2 :
324                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-address' ]     = $object[ 'address1' ];
325                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-address-2' ]   = $object[ 'address2' ];
326                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-complement' ]  = $object[ 'complement' ];
327                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-postal_code' ] = $object[ 'postal_code' ];
328                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-city_name' ]   = $object[ 'city_name' ];
329                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-state_name' ]  = $object[ 'state_name' ];
330                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-id_country' ]  = $object[ 'id_country' ];
331                                                break;
332                                }
333                        }
334
335                        return array_values($all_contacts);
336                }
337
338                function verifyContact($email)
339                {
340                        $query = 'select A.names_ordered from phpgw_cc_contact A,'.
341                        'phpgw_cc_contact_conns B, phpgw_cc_connections C where '.
342                        'A.id_contact = B.id_contact and B.id_connection = C.id_connection '.
343                        'and B.id_typeof_contact_connection = 1 and '.
344                        'A.id_owner ='.$this->owner.' and C.connection_value = \''.$email.'\'';
345                                               
346                        if (!$this->db->query($query))
347                        {
348                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
349                        }
350                         
351                        $return = false;
352                       
353                        while($this->db->next_record())
354                        {
355                                $row = $this->db->row();
356                                $return[] =  $row['names_ordered'];
357                        }
358                       
359                        return $return;
360                }               
361               
362                function selectContactsByGroup($idGroup)
363                {
364                        $query = 'select C.id_connection, A.names_ordered, C.connection_value 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'.
369                        ' D.id_connection = C.id_connection and D.id_group = '.$idGroup.
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                        return $return;
386                }
387               
388                function selectContactsByGroupAlias($alias)
389                {
390                        $query = "select C.id_connection, A.names_ordered, C.connection_value from phpgw_cc_contact A, ".
391                        "phpgw_cc_contact_conns B, phpgw_cc_connections C,phpgw_cc_contact_grps D,phpgw_cc_groups E where ".
392                        "A.id_contact = B.id_contact and B.id_connection = C.id_connection ".
393                        "and B.id_typeof_contact_connection = 1 and ".
394                        "A.id_owner =".$this->owner." and ".                   
395                        "D.id_group = E.id_group and ".
396                        "D.id_connection = C.id_connection and E.short_name = '".$alias.
397                        "' order by A.names_ordered";
398                                               
399                        if (!$this->db->query($query))
400                        {
401                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
402                        }
403                         
404                        $return = false;
405                       
406                       
407                        while($this->db->next_record())
408                        {
409                                $return[] = $this->db->row();
410                        }
411                       
412                        return $return;
413                }
414               
415                function insertContactsByGroup($idGroup, $contacts)
416                {                                                                       
417                       
418                        foreach($contacts as $index => $idConnection)
419                        {                       
420                                $query = "INSERT INTO phpgw_cc_contact_grps(id_group,id_connection) ".
421                                                "VALUES(".$idGroup.",".$idConnection.")";                       
422
423                                if (!$this->db->query($query))
424                                        exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);                     
425                        }
426                                                                                               
427                        return True;           
428                }
429               
430                function updateContactsByGroup($id_group, $contacts)
431                {
432                                               
433                        $query = 'select C.id_connection from phpgw_cc_contact A,'.
434                        'phpgw_cc_contact_conns B, phpgw_cc_connections C,phpgw_cc_contact_grps D where '.
435                        'A.id_contact = B.id_contact and B.id_connection = C.id_connection '.
436                        'and B.id_typeof_contact_connection = 1 and '.
437                        //'A.id_owner ='.$this->owner.' and D.id_connection = C.id_connection and D.id_group = '.$id_group.
438                        ' D.id_connection = C.id_connection and D.id_group = '.$id_group. //If I have the group ID, why ask about owner?
439                        ' order by A.names_ordered';
440                                               
441                        if (!$this->db->query($query))
442                        {
443                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
444                        }
445                         
446                        $return = false;
447                       
448                       
449                        while($this->db->next_record())
450                        {
451                                $return[] = $this->db->row();
452                                 
453                        }
454                                               
455                        $array_connections = array();
456                       
457                        if($return) {
458                                foreach($return as $index => $connection){                             
459                                        array_push($array_connections,$connection['id_connection']);
460                                }
461                        }
462                       
463                        if(!is_array($contacts))
464                                $contacts = array();
465                       
466                        if(!is_array($connections))
467                                $connections = array();                         
468                                                                       
469                        $connections_to_add     = array_diff($contacts, $array_connections);
470                        $connections_to_remove  = array_diff($array_connections,$contacts);
471                       
472                        if($connections_to_add){
473                                $this -> insertContactsByGroup($id_group, $connections_to_add); 
474                        }
475                       
476                        if($connections_to_remove){
477                                $this -> deleteContactsByGroup($id_group, $connections_to_remove);
478                        }
479                       
480                        return True;
481                }               
482               
483                function deleteContactsByGroup($id_group, $contacts = null)
484                {
485                       
486                        $query = "DELETE FROM phpgw_cc_contact_grps ";
487                       
488                        if($contacts) {                                         
489                                $index = 0;
490                               
491                                foreach($contacts as $a => $id_connection) {
492                                         
493                                        if($index++)
494                                                $query .= " OR";
495                                        else
496                                                $query .= " WHERE (";
497                                         
498                                        $query .= " id_connection = ".$id_connection;                                                                           
499                                }
500                                $query .= ") AND ";
501                        }       
502                        else
503                                $query.= " WHERE ";
504                       
505                        $query.= "id_group = ".$id_group;
506                       
507                       
508                        if (!$this->db->query($query))                 
509                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);                             
510                                                                       
511                        return True;           
512                }
513               
514                function add_user_by_name($id_group){
515                        $query = 'select C.id_connection, A.id_contact, A.names_ordered, C.connection_value, B.id_typeof_contact_connection'.
516                                 ' from phpgw_cc_contact A, phpgw_cc_contact_conns B, phpgw_cc_connections C'.
517                                         ' where A.id_contact = B.id_contact and B.id_connection = C.id_connection'.
518                                                ' and A.last_update = (select max(up.last_update) from phpgw_cc_contact up where up.id_owner ='.$this->owner.")".
519                                                ' and A.id_owner ='.$this->owner.' and C.connection_is_default = true'.
520                                         ' order by A.names_ordered,C.connection_value';
521                       
522                                               
523                        if (!$this->db->query($query)){
524                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
525                        }
526                         
527                        $return = array();
528                       
529                        $array_connections = array();
530                        while($this->db->next_record()){
531                                $return = $this->db->row();
532                                array_push($array_connections, $return['id_connection']);                               
533                        }                       
534                        $this -> insertContactsByGroup($id_group, $array_connections);
535                                                                       
536                }
537               
538        }
539?>
Note: See TracBrowser for help on using the repository browser.