source: trunk/contactcenter/inc/class.so_group.inc.php @ 4754

Revision 4754, 23.7 KB checked in by roberto.santosjunior, 13 years ago (diff)

Ticket #1820 - Incluir Contato Compartilhado duplicado. r4599

  • 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                        $this->db->query("select * from phpgw_cc_groups where
58                                        upper(title) like '".strtoupper($data['title'])."' and owner=".$this->owner);
59                        if($this->db->next_record())
60                                return false;//Não posso criar grupos com nomes iguais
61                        $shortName = $this -> remove_accents(strtolower(str_replace(" ","", $data['title'])));                 
62                       
63                        $query = "INSERT INTO phpgw_cc_groups(title, owner,short_name) ".
64                                        "VALUES('".$data['title']."',".$this->owner.",'".$shortName."')";                       
65                                                                               
66                        if (!$this->db->query($query))
67                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);                             
68                                               
69                        $objSequence = $this->db->query("SELECT last_value FROM seq_phpgw_cc_groups");                 
70                        $id = $objSequence -> fields['last_value'];
71                        $this -> updateContactsByGroup($id, $data['contact_in_list']);
72
73                        return $id;             
74                }
75               
76                function update($data)
77                {
78                        $this->db->query("select * from phpgw_cc_groups where
79                                        id_group!='".$data['id_group']."' and
80                                        upper(title) like '".strtoupper($data['title'])."' and owner=".$this->owner);
81                        if($this->db->next_record())
82                                return false;//Não posso criar grupos com nomes iguais
83                        $shortName = $this -> remove_accents(strtolower(str_replace(" ","", $data['title'])));
84                                               
85                        $query = "UPDATE phpgw_cc_groups SET title = '".$data['title']."',short_name = '".$shortName."' WHERE id_group = ".$data['id_group'];
86                       
87                        if (!$this->db->query($query))                 
88                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);                             
89                       
90                        $this -> updateContactsByGroup($data['id_group'], $data['contact_in_list']);           
91                                                                       
92                        return True;           
93                }
94               
95                function delete($data)
96                {
97                        $query = "DELETE FROM phpgw_cc_groups WHERE id_group = ".$data['id_group'];                     
98                                                                               
99                        if (!$this->db->query($query))                 
100                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);                             
101                                                                       
102                        $this -> deleteContactsByGroup($data['id_group'], $data['contact_in_list']);
103                                               
104                        return True;           
105                }               
106               
107                /*!
108                 * @function select_owner_groups
109                 * @abstract Busca todos os grupos do usuário atual.
110                 * @author Luiz Carlos Viana Melo - Prognus
111                 * @param (integer) $id_owner O ID do usuário.
112                 * @return Retorna uma estrutura contendo:
113                 * array(
114                 *              array(
115                 *                      id_group => O ID do grupo;
116                 *                      title => O nome do grupo;
117                 *                      short_name => O nome abreviado do grupo.
118                 *              )
119                 * )
120                 */
121                function select_owner_groups($id_owner = false)
122                {
123                        $query = 'SELECT id_group, title, short_name FROM phpgw_cc_groups ';
124                        if ($id_owner)
125                                $query .= 'WHERE owner = ' . $id_owner. ' ORDER BY title;';
126                        else
127                                $query .= 'WHERE owner = ' . $this->owner . ' ORDER BY title;';
128                               
129                        if (!$this->db->query($query))
130                        {
131                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
132                        }
133                       
134                        $return = false;
135                       
136                        while($this->db->next_record())
137                        {
138                                $return[] = $this->db->row();
139                        }
140                       
141                        return $return;
142                }
143               
144                /*!
145                 * @function select_contact_groups
146                 * @abstract Seleciona os grupos do contato.
147                 * @author Luiz Carlos Viana Melo - Prognus
148                 * @param (integer) $id_contact O ID do contato.
149                 * @return Retorna uma estrutura contendo:
150                 * array(
151                 *              array(
152                 *                      id_contact => O ID do contato;
153                 *                      id_group => O ID do grupo;
154                 *                      title => O nome do grupo;
155                 *                      short_name => O nome abreviado do grupo.
156                 *              )
157                 * )
158                 */
159                function select_contact_groups($id_contact)
160                {
161                        $query = 'SELECT Contato.id_contact, ContatoGprs.id_connection, Grupos.id_group, Grupos.title, ' .
162                                'Grupos.short_name FROM phpgw_cc_contact_conns Contato, ' .
163                                'phpgw_cc_connections Conexao, phpgw_cc_contact_grps ContatoGprs, ' .
164                                'phpgw_cc_groups Grupos WHERE Contato.id_contact = ' . $id_contact .
165                                ' AND Contato.id_connection = Conexao.id_connection AND ' .
166                                'Conexao.id_connection = ContatoGprs.id_connection AND ' .
167                                'ContatoGprs.id_group = Grupos.id_group ORDER BY title';
168                       
169                        if (!$this->db->query($query))
170                        {
171                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
172                        }
173                       
174                        $return = false;
175                       
176                        while($this->db->next_record())
177                        {
178                                $return[] = $this->db->row();
179                        }
180                       
181                        return $return;
182                }
183               
184                function deleteContactFromGroups($id)
185                {
186                        $query = "DELETE FROM phpgw_cc_contact_grps WHERE id_connection = ".$id;                       
187                                                                               
188                        if (!$this->db->query($query))                 
189                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);                             
190                                                                       
191                        return True;           
192                }
193               
194                function selectGroupsOwnerCanImportContacts($owner) {
195                        $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
196                       A.acl_location=B.acl_account::text and A.acl_account::text=B.acl_location
197                       and A.acl_appname = 'contactcenter' and B.acl_appname = 'contactcenter'
198                       and A.acl_rights & 4 <> 0 and B.acl_rights & 1 <> 0
199                       and A.acl_location = '".$owner."')"; //He can import contacts only to his groups, or shared groups that he gave read permission.
200                       
201                        $this->db->query($query);
202                       
203                        $return = array();
204                       
205                        while($this->db->next_record())
206                        {
207                                $return[] = $this->db->row();
208                        }
209                       
210                        return $return;
211                       
212                }
213               
214                //Owner = null means the owner setted on constructor.
215                function selectAllContacts( $field = false ,$shared_from=null)
216                {
217                        if ( $shared_from == NULL )
218                        {
219                                $query = 'select'
220                                                . ' C.id_connection,'
221                                                . ' A.id_contact,'
222                                                . ' A.names_ordered,'
223                                                . ' A.alias,'
224                                                . ' A.birthdate,'
225                                                . ' A.sex,'
226                                                . ' A.pgp_key,'
227                                                . ' A.notes,'
228                                                . ' A.web_page,'
229                                                . ' A.corporate_name,'
230                                                . ' A.job_title,'
231                                                . ' A.department,'
232                                                . ' C.connection_name,'
233                                                . ' C.connection_value,'
234                                                . ' B.id_typeof_contact_connection,'
235                                                . ' phpgw_cc_contact_addrs.id_typeof_contact_address,'
236                                                . ' phpgw_cc_addresses.address1,'
237                                                . ' phpgw_cc_addresses.address2,'
238                                                . ' phpgw_cc_addresses.complement,'
239                                                . ' phpgw_cc_addresses.postal_code,'
240                                                . ' phpgw_cc_city.city_name,'
241                                                . ' phpgw_cc_state.state_name,'
242                                                . ' phpgw_cc_addresses.id_country'
243                                                ;
244
245                                $query .= ' from'
246                        . ' phpgw_cc_contact A'
247                        . ' inner join phpgw_cc_contact_conns B on ( A.id_contact = B.id_contact )'
248                        . ' inner join phpgw_cc_connections C on ( B.id_connection = C.id_connection )'
249                        . ' left join phpgw_cc_contact_addrs on ( A.id_contact = phpgw_cc_contact_addrs.id_contact )'
250                                                . ' left join phpgw_cc_addresses on ( phpgw_cc_contact_addrs.id_address = phpgw_cc_addresses.id_address )'
251                                                . ' left join phpgw_cc_city on ( phpgw_cc_addresses.id_city = phpgw_cc_city.id_city )'
252                                                . ' left join phpgw_cc_state on ( phpgw_cc_addresses.id_state = phpgw_cc_state.id_state)'
253                                                ;
254
255                                $query .= ' where'
256                                                . " A.id_owner = {$this->owner}"
257                                                //. ' and phpgw_cc_connections.connection_is_default = true'
258                                                ;
259
260                        }
261                        else {
262                                $sub_query = 'select A.id_related from phpgw_cc_contact_rels A,phpgw_acl B
263                                                          where B.acl_location!=\'run\' and A.id_contact = B.acl_location::bigint and A.id_related = B.acl_account and
264                                                          B.acl_appname = \'contactcenter\' and B.acl_rights & 1 <> 0
265                                                          and A.id_typeof_contact_relation=1 and A.id_contact = '.$shared_from.' and A.id_related='.$this->owner;
266
267                                $query = 'select C.id_connection, A.id_contact, A.names_ordered, C.connection_value , B.id_typeof_contact_connection from phpgw_cc_contact A,'.
268                                'phpgw_cc_contact_conns B, phpgw_cc_connections C where '.
269                                'A.id_contact = B.id_contact and B.id_connection = C.id_connection and '.
270                                'A.id_owner in ('.$shared_from.',('.$sub_query.'))'.
271                                ' and C.connection_is_default = true ';
272                        }
273
274                        if ( $field == 'only_email' )
275                                $query .= 'and B.id_typeof_contact_connection = 1 ';
276
277                        $query .= ' order by A.names_ordered, C.connection_value';
278
279                        if (!$this->db->query($query))
280                        {
281                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
282                        }
283
284                        $return = array();
285
286                        while($this->db->next_record())
287                        {
288                                $return[] = $this->db->row();
289                        }
290
291                        if( ! count( $return ) )
292                                return $return;
293
294                        $all_contacts = array();
295                        foreach( $return as $i => $object )
296                        {
297                                if ( ! array_key_exists( $object[ 'id_contact' ], $all_contacts ) )
298                                        $all_contacts[ $object[ 'id_contact' ] ] = array(
299                                                'connection_value' => '',
300                                                'phone' => '',
301                                                'mobile' => '',
302                                                'names_ordered' => '',
303                                                'id_contact' => '',
304                                                'id_connection' => '',
305                                                'alias' => '',
306                                                'birthdate' => '',
307                                                'sex' => '',
308                                                'pgp_key' => '',
309                                                'notes' => '',
310                                                'web_page' => '',
311                                                'corporate_name' => '',
312                                                'job_title' => '',
313                                                'department' => '',                             
314                                                'main-mail' => '',
315                                                'aternative-mail' => '',
316                                                'business-phone' => '',
317                                                'business-address' => '',
318                                                'business-complement' => '',
319                                                'business-postal_code' => '',
320                                                'business-city_name' => '',
321                                                'business-state_name' => '',
322                                                'business-id_country' => '',
323                                                'business-fax' => '',
324                                                'business-pager' => '',
325                                                'business-mobile' => '',
326                                                'business-address-2' => '',
327                                                'home-phone' => '',
328                                                'home-address' => '',
329                                                'home-complement' => '',
330                                                'home-postal_code' => '',
331                                                'home-city_name' => '',
332                                                'home-state_name' => '',
333                                                'home-fax' => '',
334                                                'home-pager' => '',
335                                                'home-address-2' => ''
336                                               
337                                               
338                                        );
339
340                                switch( $object[ 'id_typeof_contact_connection' ] )
341                                {
342                                        case 1 :
343                                                $all_contacts[ $object[ 'id_contact' ] ][ 'connection_value' ] = $object[ 'connection_value' ];
344                                                switch ( strtolower( $object[ 'connection_name' ] ) )
345                                                {
346                                                        case 'alternativo' :
347                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'alternative-mail' ] = $object[ 'connection_value' ];
348                                                                break;
349                                                        case 'principal' :
350                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'main-mail' ] = $object[ 'connection_value' ];
351                                                                break;
352                                                }
353                                                break;
354                                        case 2 :
355                                                $all_contacts[ $object[ 'id_contact' ] ][ 'phone' ] = $object[ 'connection_value' ];
356                                                switch ( strtolower( $object[ 'connection_name' ] ) )
357                                                {
358                                                        case 'casa' :
359                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-phone' ] = $object[ 'connection_value' ];
360                                                                break;
361                                                        case 'celular' :
362                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'mobile' ] = $object[ 'connection_value' ];
363                                                                break;
364                                                        case 'trabalho' :
365                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-phone' ] = $object[ 'connection_value' ];
366                                                                break;                                                         
367                                                        case 'fax' :
368                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-fax' ] = $object[ 'connection_value' ];
369                                                                break;
370                                                        case 'pager' :
371                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-pager' ] = $object[ 'connection_value' ];
372                                                                break;
373                                                        case 'celular corporativo' :
374                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-mobile' ] = $object[ 'connection_value' ];
375                                                                break;                                                         
376                                                        case 'pager corporativo' :
377                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-pager' ] = $object[ 'connection_value' ];
378                                                                break;
379                                                        case 'fax corporativo' :
380                                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-fax' ] = $object[ 'connection_value' ];
381                                                                break;
382                                                }
383                                                break;
384                                }
385
386                                $all_contacts[ $object[ 'id_contact' ] ][ 'names_ordered' ] = $object[ 'names_ordered' ];
387                                $all_contacts[ $object[ 'id_contact' ] ][ 'id_contact' ]    = $object[ 'id_contact' ];
388                                $all_contacts[ $object[ 'id_contact' ] ][ 'id_connection' ] = $object[ 'id_connection' ];
389                                $all_contacts[ $object[ 'id_contact' ] ][ 'alias' ]         = $object[ 'alias' ];                               
390                                $all_contacts[ $object[ 'id_contact' ] ][ 'birthdate' ]         = $object[ 'birthdate' ];
391                                $all_contacts[ $object[ 'id_contact' ] ][ 'sex' ]               = $object[ 'sex' ];
392                                $all_contacts[ $object[ 'id_contact' ] ][ 'pgp_key' ]           = $object[ 'pgp_key' ];
393                                $all_contacts[ $object[ 'id_contact' ] ][ 'notes' ]         = $object[ 'notes' ];
394                                $all_contacts[ $object[ 'id_contact' ] ][ 'web_page' ]          = $object[ 'web_page' ];
395                                $all_contacts[ $object[ 'id_contact' ] ][ 'corporate_name' ]= $object[ 'corporate_name' ];
396                                $all_contacts[ $object[ 'id_contact' ] ][ 'job_title' ]         = $object[ 'job_title' ];
397                                $all_contacts[ $object[ 'id_contact' ] ][ 'department' ]    = $object[ 'department' ];
398
399                                switch( $object[ 'id_typeof_contact_address' ] )
400                                {
401                                        case 1 :
402                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-address' ]     = $object[ 'address1' ];
403                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-address-2' ]   = $object[ 'address2' ];
404                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-complement' ]  = $object[ 'complement' ];
405                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-postal_code' ] = $object[ 'postal_code' ];
406                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-city_name' ]   = $object[ 'city_name' ];
407                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-state_name' ]  = $object[ 'state_name' ];
408                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-id_country' ]  = $object[ 'id_country' ];
409                                                break;
410                                        case 2 :
411                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-address' ]     = $object[ 'address1' ];
412                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-address-2' ]   = $object[ 'address2' ];
413                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-complement' ]  = $object[ 'complement' ];
414                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-postal_code' ] = $object[ 'postal_code' ];
415                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-city_name' ]   = $object[ 'city_name' ];
416                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-state_name' ]  = $object[ 'state_name' ];
417                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-id_country' ]  = $object[ 'id_country' ];
418                                                break;
419                                }
420                        }
421
422                        return array_values($all_contacts);
423                }
424
425                function verifyContact($email, $name, $phone)
426                {
427                        if ($email) {
428                       
429                                $query = 'select A.names_ordered, C.id_connection from phpgw_cc_contact A,'.
430                                'phpgw_cc_contact_conns B, phpgw_cc_connections C where '.
431                                'A.id_contact = B.id_contact and B.id_connection = C.id_connection '.
432                                'and B.id_typeof_contact_connection = 1 and '.
433                                "A.id_owner ='" .$this->owner."' and C.connection_value = '".$email. "'";
434
435                                if (!$this->db->query($query))
436                                {       
437                                        exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
438                                }
439                         
440                                $return = false;
441                       
442                                while($this->db->next_record())
443                                {
444                                        $row = $this->db->row();
445                                        $return[] =  $row['names_ordered'];
446                                }
447                        }
448                       
449                        if (!$return && $phone) {
450
451                                $query = 'select A.names_ordered, C.id_connection from phpgw_cc_contact A,'.
452                                'phpgw_cc_contact_conns B, phpgw_cc_connections C where '.
453                                'A.id_contact = B.id_contact and B.id_connection = C.id_connection '.
454                                'and B.id_typeof_contact_connection = 2 and '.
455                                "A.id_owner ='" .$this->owner."' and C.connection_value = '".$phone. "'";
456
457                                if (!$this->db->query($query))
458                                {       
459                                        exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
460                                }
461                         
462                                $return = false;
463                       
464                                while($this->db->next_record())
465                                {
466                                        $row = $this->db->row();
467                                        $return[] =  $row['names_ordered'];
468                                }
469
470                        }
471                       
472                        if (!$return && $name) {
473
474                                $query = "select names_ordered, C.id_connection from phpgw_cc_contact where id_owner = '" . $this->owner . "' and RTRIM(names_ordered) = '". $name. "'";
475
476                                if (!$this->db->query($query))
477                                {       
478                                        exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
479                                }
480                         
481                                $return = false;
482                       
483                                while($this->db->next_record())
484                                {
485                                        $row = $this->db->row();
486                                        $return[] =  $row['names_ordered'];
487                                }
488
489                        }
490                    $return[] =  $row['id_connection'];
491
492                        return $return;
493                }               
494
495                function verifySharedContact($owner,$email)
496                {
497                        $query = 'select A.names_ordered, C.id_connection from phpgw_cc_contact A,'.
498                        'phpgw_cc_contact_conns B, phpgw_cc_connections C where '.
499                        'A.id_contact = B.id_contact and B.id_connection = C.id_connection '.
500                        'and B.id_typeof_contact_connection = 1 and '.
501                        'A.id_owner ='.$owner.' and C.connection_value = \''.$email.'\'';
502
503                        if (!$this->db->query($query))
504                        {
505                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
506                        }
507
508                        $return = false;
509
510                        while($this->db->next_record())
511                        {
512                                $row = $this->db->row();
513                                $return[] =  $row['names_ordered'];
514                                $return[] =  $row['id_connection'];
515                        }
516
517                        return $return;
518                }
519
520                function selectContactsByGroup($idGroup)
521                {
522                        $query = 'select C.id_connection, A.names_ordered, C.connection_value, A.id_contact from phpgw_cc_contact A,'.
523                        'phpgw_cc_contact_conns B, phpgw_cc_connections C,phpgw_cc_contact_grps D where '.
524                        'A.id_contact = B.id_contact and B.id_connection = C.id_connection '.
525                        'and '.
526                        //'A.id_owner ='.$this->owner.' and'.
527                        ' D.id_connection = C.id_connection and D.id_group = '.$idGroup.
528                        ' order by A.names_ordered';
529                                               
530                        if (!$this->db->query($query))
531                        {
532                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
533                        }
534                         
535                        $return = false;
536                       
537                       
538                        while($this->db->next_record())
539                        {
540                                $return[] = $this->db->row();
541                        }
542                       
543                        return $return;
544                }
545               
546                function selectContactsByGroupAlias($alias)
547                {
548                        $query = "select C.id_connection, A.names_ordered, C.connection_value from phpgw_cc_contact A, ".
549                        "phpgw_cc_contact_conns B, phpgw_cc_connections C,phpgw_cc_contact_grps D,phpgw_cc_groups E where ".
550                        "A.id_contact = B.id_contact and B.id_connection = C.id_connection ".
551                        " and ".
552                        "A.id_owner =".$this->owner." and ".                   
553                        "D.id_group = E.id_group and ".
554                        "D.id_connection = C.id_connection and E.short_name = '".$alias.
555                        "' order by A.names_ordered";
556                                               
557                        if (!$this->db->query($query))
558                        {
559                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
560                        }
561                         
562                        $return = false;
563                       
564                       
565                        while($this->db->next_record())
566                        {
567                                $return[] = $this->db->row();
568                        }
569                       
570                        return $return;
571                }
572               
573                function insertContactsByGroup($idGroup, $contacts)
574                {                                                                       
575                       
576                        foreach($contacts as $index => $idConnection)
577                        {                       
578                                $query = "INSERT INTO phpgw_cc_contact_grps(id_group,id_connection) ".
579                                                "VALUES(".$idGroup.",".$idConnection.")";                       
580
581                                if (!$this->db->query($query))
582                                        exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);                     
583                        }
584                                                                                               
585                        return True;           
586                }
587               
588                function updateContactsByGroup($id_group, $contacts)
589                {
590                                               
591                        $query = 'select C.id_connection from phpgw_cc_contact A,'.
592                        'phpgw_cc_contact_conns B, phpgw_cc_connections C,phpgw_cc_contact_grps D where '.
593                        'A.id_contact = B.id_contact and B.id_connection = C.id_connection '.
594                        ' and '.
595                        //'A.id_owner ='.$this->owner.' and D.id_connection = C.id_connection and D.id_group = '.$id_group.
596                        ' D.id_connection = C.id_connection and D.id_group = '.$id_group. //If I have the group ID, why ask about owner?
597                        ' order by A.names_ordered';
598                                               
599                        if (!$this->db->query($query))
600                        {
601                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
602                        }
603                         
604                        $return = false;
605                       
606                       
607                        while($this->db->next_record())
608                        {
609                                $return[] = $this->db->row();
610                                 
611                        }
612                                               
613                        $array_connections = array();
614                       
615                        if($return) {
616                                foreach($return as $index => $connection){                             
617                                        array_push($array_connections,$connection['id_connection']);
618                                }
619                        }
620                       
621                        if(!is_array($contacts))
622                                $contacts = array();
623                       
624                        if(!is_array($connections))
625                                $connections = array();                         
626                                                                       
627                        $connections_to_add     = array_diff($contacts, $array_connections);
628                        $connections_to_remove  = array_diff($array_connections,$contacts);
629                       
630                        if($connections_to_add){
631                                $this -> insertContactsByGroup($id_group, $connections_to_add); 
632                        }
633                       
634                        if($connections_to_remove){
635                                $this -> deleteContactsByGroup($id_group, $connections_to_remove);
636                        }
637                       
638                        return True;
639                }               
640               
641                function updateContactGroups($id_defaultconnection, $connections, $groups)
642                {
643                        if (is_array($groups) && count($groups) > 0)
644                        {
645                                $query = "UPDATE phpgw_cc_contact_grps SET id_connection = " . $id_defaultconnection . " WHERE (";
646                                $more = false;
647                                foreach ($connections as $connection)
648                                {
649                                        if ($more)
650                                        $query .= " OR ";
651                                        $query .= "id_connection = " . $connection['id_connection'];
652                                        $more = true;
653                                }
654                                $query .= ") AND (";
655                                $more = false;
656                                foreach ($groups as $group)
657                                {
658                                        if ($more)
659                                        $query .= " OR ";
660                                        $query .= "id_group = " . $group['id_group'];
661                                        $more = true;
662                                }
663                                $query .= ");";
664                                if (!$this->db->query($query))                 
665                                        exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);                             
666                                                                       
667                                return true;
668                        }
669                }
670               
671                function deleteContactsByGroup($id_group, $contacts = null)
672                {
673                       
674                        $query = "DELETE FROM phpgw_cc_contact_grps ";
675                       
676                        if($contacts) {                                         
677                                $index = 0;
678                               
679                                foreach($contacts as $a => $id_connection) {
680                                         
681                                        if($index++)
682                                                $query .= " OR";
683                                        else
684                                                $query .= " WHERE (";
685                                         
686                                        $query .= " id_connection = ".$id_connection;                                                                           
687                                }
688                                $query .= ") AND ";
689                        }       
690                        else
691                                $query.= " WHERE ";
692                       
693                        $query.= "id_group = ".$id_group;
694                       
695                       
696                        if (!$this->db->query($query))                 
697                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);                             
698                                                                       
699                        return True;           
700                }
701               
702                function add_user_by_name($id_group){
703                        $query = 'select C.id_connection, A.id_contact, A.names_ordered, C.connection_value, B.id_typeof_contact_connection'.
704                                 ' from phpgw_cc_contact A, phpgw_cc_contact_conns B, phpgw_cc_connections C'.
705                                         ' where A.id_contact = B.id_contact and B.id_connection = C.id_connection'.
706                                                ' and A.last_update = (select max(up.last_update) from phpgw_cc_contact up where up.id_owner ='.$this->owner.")".
707                                                ' and A.id_owner ='.$this->owner.' and C.connection_is_default = true'.
708                                         ' order by A.names_ordered,C.connection_value';
709                       
710                                               
711                        if (!$this->db->query($query)){
712                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
713                        }
714                         
715                        $return = array();
716                       
717                        $array_connections = array();
718                        while($this->db->next_record()){
719                                $return = $this->db->row();
720                                array_push($array_connections, $return['id_connection']);                               
721                        }                       
722                        $this -> insertContactsByGroup($id_group, $array_connections);
723                                                                       
724                }
725               
726        }
727?>
Note: See TracBrowser for help on using the repository browser.