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

Revision 3216, 13.6 KB checked in by rodsouza, 14 years ago (diff)

Ticket #884 - Distinguindo endereço comer./resid. na export. de contato no formato ExpressoLivre?

  • 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_connections.connection_value,'
120                                                . ' phpgw_cc_contact_conns.id_typeof_contact_connection,'
121                                                . ' phpgw_cc_contact_addrs.id_typeof_contact_address,'
122                                                . ' phpgw_cc_addresses.address1,'
123                                                . ' phpgw_cc_addresses.complement,'
124                                                . ' phpgw_cc_addresses.postal_code,'
125                                                . ' phpgw_cc_city.city_name,'
126                                                . ' phpgw_cc_state.state_name,'
127                                                . ' phpgw_cc_addresses.id_country';
128
129                                $query .= ' from'
130                                                . ' phpgw_cc_contact'
131                                                . ' inner join phpgw_cc_contact_conns on ( phpgw_cc_contact.id_contact = phpgw_cc_contact_conns.id_contact )'
132                                                . ' inner join phpgw_cc_connections on ( phpgw_cc_contact_conns.id_connection = phpgw_cc_connections.id_connection )'
133                                                . ' left join phpgw_cc_contact_addrs on ( phpgw_cc_contact.id_contact = phpgw_cc_contact_addrs.id_contact )'
134                                                . ' left join phpgw_cc_addresses on ( phpgw_cc_contact_addrs.id_address = phpgw_cc_addresses.id_address )'
135                                                . ' left join phpgw_cc_city on ( phpgw_cc_addresses.id_city = phpgw_cc_city.id_city )'
136                                                . ' left join phpgw_cc_state on ( phpgw_cc_addresses.id_state = phpgw_cc_state.id_state)';
137
138                                $query .= ' where'
139                                                . " phpgw_cc_contact.id_owner = {$this->owner}"
140                                                . ' and phpgw_cc_connections.connection_is_default = true';
141
142                        }
143                        else {
144                                $sub_query = 'select A.id_related from phpgw_cc_contact_rels A,phpgw_acl B
145                                                          where B.acl_location!=\'run\' and A.id_contact = B.acl_location::bigint and A.id_related = B.acl_account and
146                                                          B.acl_appname = \'contactcenter\' and B.acl_rights & 1 <> 0
147                                                          and A.id_typeof_contact_relation=1 and A.id_contact = '.$shared_from.' and A.id_related='.$this->owner;
148
149                                $query = 'select C.id_connection, A.id_contact, A.names_ordered, C.connection_value , B.id_typeof_contact_connection from phpgw_cc_contact A,'.
150                                'phpgw_cc_contact_conns B, phpgw_cc_connections C where '.
151                                'A.id_contact = B.id_contact and B.id_connection = C.id_connection and '.
152                                'A.id_owner in ('.$shared_from.',('.$sub_query.'))'.
153                                ' and C.connection_is_default = true ';
154                        }
155
156                        if ( $field == 'only_email' )
157                                $query .= 'and phpgw_cc_contact_conns.id_typeof_contact_connection = 1 ';
158
159                        $query .= ' order by phpgw_cc_contact.names_ordered, phpgw_cc_connections.connection_value';
160
161                        if (!$this->db->query($query))
162                        {
163                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
164                        }
165
166                        $return = array();
167
168                        while($this->db->next_record())
169                        {
170                                $return[] = $this->db->row();
171                        }
172
173                        if( ! count( $return ) )
174                                return $return;
175
176                        foreach( $return as $i => $object )
177                        {
178                                if ( ! array_key_exists( $object[ 'id_contact' ], $all_contacts ) )
179                                        $all_contacts[ $object[ 'id_contact' ] ] = array(
180                                                'connection_value' => '',
181                                                'phone' => '',
182                                                'names_ordered' => '',
183                                                'id_contact' => '',
184                                                'id_connection' => '',
185                                                'alias' => '',
186                                                'business-address' => '',
187                                                'complement' => '',
188                                                'postal_code' => '',
189                                                'city_name' => '',
190                                                'state_name' => '',
191                                                'id_country' => '',
192                                                'home-address' => '',
193                                                'complement' => '',
194                                                'postal_code' => '',
195                                                'city_name' => '',
196                                                'state_name' => '',
197                                                'id_country' => ''
198                                        );
199
200                                switch( $object[ 'id_typeof_contact_connection' ] )
201                                {
202                                        case 1 :
203                                                $all_contacts[ $object[ 'id_contact' ] ][ 'connection_value' ] = $object[ 'connection_value' ];
204                                                break;
205                                        case 2 :
206                                                $all_contacts[ $object[ 'id_contact' ] ][ 'phone' ] = $object[ 'connection_value' ];
207                                                break;
208                                }
209
210                                $all_contacts[ $object[ 'id_contact' ] ][ 'names_ordered' ] = $object[ 'names_ordered' ];
211                                $all_contacts[ $object[ 'id_contact' ] ][ 'id_contact' ]    = $object[ 'id_contact' ];
212                                $all_contacts[ $object[ 'id_contact' ] ][ 'id_connection' ] = $object[ 'id_connection' ];
213                                $all_contacts[ $object[ 'id_contact' ] ][ 'alias' ]         = $object[ 'alias' ];
214
215                                switch( $object[ 'id_typeof_contact_address' ] )
216                                {
217                                        case 1 :
218                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-address' ]     = $object[ 'address1' ];
219                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-complement' ]  = $object[ 'complement' ];
220                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-postal_code' ] = $object[ 'postal_code' ];
221                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-city_name' ]   = $object[ 'city_name' ];
222                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-state_name' ]  = $object[ 'state_name' ];
223                                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-id_country' ]  = $object[ 'id_country' ];
224                                                break;
225                                        case 2 :
226                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-address' ]     = $object[ 'address1' ];
227                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-complement' ]  = $object[ 'complement' ];
228                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-postal_code' ] = $object[ 'postal_code' ];
229                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-city_name' ]   = $object[ 'city_name' ];
230                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-state_name' ]  = $object[ 'state_name' ];
231                                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-id_country' ]  = $object[ 'id_country' ];
232                                                break;
233                                }
234                        }
235
236                        return array_values($all_contacts);
237                }
238
239                function verifyContact($email)
240                {
241                        $query = 'select A.names_ordered from phpgw_cc_contact A,'.
242                        'phpgw_cc_contact_conns B, phpgw_cc_connections C where '.
243                        'A.id_contact = B.id_contact and B.id_connection = C.id_connection '.
244                        'and B.id_typeof_contact_connection = 1 and '.
245                        'A.id_owner ='.$this->owner.' and C.connection_value = \''.$email.'\'';
246                                               
247                        if (!$this->db->query($query))
248                        {
249                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
250                        }
251                         
252                        $return = false;
253                       
254                        while($this->db->next_record())
255                        {
256                                $row = $this->db->row();
257                                $return[] =  $row['names_ordered'];
258                        }
259                       
260                        return $return;
261                }               
262               
263                function selectContactsByGroup($idGroup)
264                {
265                        $query = 'select C.id_connection, A.names_ordered, C.connection_value from phpgw_cc_contact A,'.
266                        'phpgw_cc_contact_conns B, phpgw_cc_connections C,phpgw_cc_contact_grps D where '.
267                        'A.id_contact = B.id_contact and B.id_connection = C.id_connection '.
268                        'and B.id_typeof_contact_connection = 1 and '.
269                        //'A.id_owner ='.$this->owner.' and'.
270                        ' D.id_connection = C.id_connection and D.id_group = '.$idGroup.
271                        ' order by A.names_ordered';
272                                               
273                        if (!$this->db->query($query))
274                        {
275                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
276                        }
277                         
278                        $return = false;
279                       
280                       
281                        while($this->db->next_record())
282                        {
283                                $return[] = $this->db->row();
284                        }
285                       
286                        return $return;
287                }
288               
289                function selectContactsByGroupAlias($alias)
290                {
291                        $query = "select C.id_connection, A.names_ordered, C.connection_value from phpgw_cc_contact A, ".
292                        "phpgw_cc_contact_conns B, phpgw_cc_connections C,phpgw_cc_contact_grps D,phpgw_cc_groups E where ".
293                        "A.id_contact = B.id_contact and B.id_connection = C.id_connection ".
294                        "and B.id_typeof_contact_connection = 1 and ".
295                        "A.id_owner =".$this->owner." and ".                   
296                        "D.id_group = E.id_group and ".
297                        "D.id_connection = C.id_connection and E.short_name = '".$alias.
298                        "' order by A.names_ordered";
299                                               
300                        if (!$this->db->query($query))
301                        {
302                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
303                        }
304                         
305                        $return = false;
306                       
307                       
308                        while($this->db->next_record())
309                        {
310                                $return[] = $this->db->row();
311                        }
312                       
313                        return $return;
314                }
315               
316                function insertContactsByGroup($idGroup, $contacts)
317                {                                                                       
318                       
319                        foreach($contacts as $index => $idConnection)
320                        {                       
321                                $query = "INSERT INTO phpgw_cc_contact_grps(id_group,id_connection) ".
322                                                "VALUES(".$idGroup.",".$idConnection.")";                       
323
324                                if (!$this->db->query($query))
325                                        exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);                     
326                        }
327                                                                                               
328                        return True;           
329                }
330               
331                function updateContactsByGroup($id_group, $contacts)
332                {
333                                               
334                        $query = 'select C.id_connection from phpgw_cc_contact A,'.
335                        'phpgw_cc_contact_conns B, phpgw_cc_connections C,phpgw_cc_contact_grps D where '.
336                        'A.id_contact = B.id_contact and B.id_connection = C.id_connection '.
337                        'and B.id_typeof_contact_connection = 1 and '.
338                        //'A.id_owner ='.$this->owner.' and D.id_connection = C.id_connection and D.id_group = '.$id_group.
339                        ' D.id_connection = C.id_connection and D.id_group = '.$id_group. //If I have the group ID, why ask about owner?
340                        ' order by A.names_ordered';
341                                               
342                        if (!$this->db->query($query))
343                        {
344                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
345                        }
346                         
347                        $return = false;
348                       
349                       
350                        while($this->db->next_record())
351                        {
352                                $return[] = $this->db->row();
353                                 
354                        }
355                                               
356                        $array_connections = array();
357                       
358                        if($return) {
359                                foreach($return as $index => $connection){                             
360                                        array_push($array_connections,$connection['id_connection']);
361                                }
362                        }
363                       
364                        if(!is_array($contacts))
365                                $contacts = array();
366                       
367                        if(!is_array($connections))
368                                $connections = array();                         
369                                                                       
370                        $connections_to_add     = array_diff($contacts, $array_connections);
371                        $connections_to_remove  = array_diff($array_connections,$contacts);
372                       
373                        if($connections_to_add){
374                                $this -> insertContactsByGroup($id_group, $connections_to_add); 
375                        }
376                       
377                        if($connections_to_remove){
378                                $this -> deleteContactsByGroup($id_group, $connections_to_remove);
379                        }
380                       
381                        return True;
382                }               
383               
384                function deleteContactsByGroup($id_group, $contacts = null)
385                {
386                       
387                        $query = "DELETE FROM phpgw_cc_contact_grps ";
388                       
389                        if($contacts) {                                         
390                                $index = 0;
391                               
392                                foreach($contacts as $a => $id_connection) {
393                                         
394                                        if($index++)
395                                                $query .= " OR";
396                                        else
397                                                $query .= " WHERE (";
398                                         
399                                        $query .= " id_connection = ".$id_connection;                                                                           
400                                }
401                                $query .= ") AND ";
402                        }       
403                        else
404                                $query.= " WHERE ";
405                       
406                        $query.= "id_group = ".$id_group;
407                       
408                       
409                        if (!$this->db->query($query))                 
410                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);                             
411                                                                       
412                        return True;           
413                }
414        }
415?>
Note: See TracBrowser for help on using the repository browser.