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

Revision 3322, 17.0 KB checked in by eduardoalex, 14 years ago (diff)

Ticket #1259 - adicionado campos para exportacao de todos os padroes

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