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

Revision 3215, 12.3 KB checked in by rodsouza, 14 years ago (diff)

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