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

Revision 3344, 18.8 KB checked in by eduardoalex, 14 years ago (diff)

Ticket #1370 - Adicionada correcao no sql da funcao selectGroupsOwnerCanImportContacts

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