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

Revision 5131, 29.0 KB checked in by wmerlotto, 12 years ago (diff)

Ticket #2305 - Enviando alteracoes, desenvolvidas internamente na Prognus, do modulo contactcenter.

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