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

Revision 702, 10.0 KB checked in by niltonneto, 15 years ago (diff)

Resolve #450.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
RevLine 
[2]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 WHERE owner = '.$this->owner;
29                       
30                        if($id != '')
31                                $query .= ' AND id_group ='.$id;
32                       
33                        $query .= ' ORDER BY title';
34                       
35                        if (!$this->db->query($query))
36                        {
37                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
38                        }
39                         
40                        $return = false;
41                       
42                        while($this->db->next_record())
43                        {
44                                $return[] = $this->db->row();
45                        }
46       
47                       
48                        return $return;
49                }
50
51                function remove_accents($string) {
52                        return strtr($string,
53                        "?Ó??ó?Ý?úÁÀÃÂÄÇÉÈÊËÍÌ?ÎÏÑÕÔÓÒÖÚÙ?ÛÜ?áàãâäçéèêëíì?îïñóòõôöúù?ûüýÿ",
54                        "SOZsozYYuAAAAACEEEEIIIIINOOOOOUUUUUsaaaaaceeeeiiiiinooooouuuuuyy");
55                }
56               
57                function insert($data)
58                {
59                       
60                        $shortName = $this -> remove_accents(strtolower(str_replace(" ","", $data['title'])));                 
61                       
62                        $query = "INSERT INTO phpgw_cc_groups(title, owner,short_name) ".
63                                        "VALUES('".$data['title']."',".$this->owner.",'".$shortName."')";                       
64                                                                               
65                        if (!$this->db->query($query))
66                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);                             
67                                               
68                        $objSequence = $this->db->query("SELECT last_value FROM seq_phpgw_cc_groups");                 
69                        $id = $objSequence -> fields['last_value'];
70                        $this -> updateContactsByGroup($id, $data['contact_in_list']);
71
72                        return $id;             
73                }
74               
75                function update($data)
76                {
77                        $shortName = $this -> remove_accents(strtolower(str_replace(" ","", $data['title'])));
78                                               
79                        $query = "UPDATE phpgw_cc_groups SET title = '".$data['title']."',short_name = '".$shortName."' WHERE id_group = ".$data['id_group'];
80                       
81                        if (!$this->db->query($query))                 
82                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);                             
83                       
84                        $this -> updateContactsByGroup($data['id_group'], $data['contact_in_list']);           
85                                                                       
86                        return True;           
87                }
88               
89                function delete($data)
90                {
91                        $query = "DELETE FROM phpgw_cc_groups WHERE id_group = ".$data['id_group'];                     
92                                                                               
93                        if (!$this->db->query($query))                 
94                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);                             
95                                                                       
96                        $this -> deleteContactsByGroup($data['id_group'], $data['contact_in_list']);
97                                               
98                        return True;           
99                }               
100               
101                function deleteContactFromGroups($id)
102                {
103                        $query = "DELETE FROM phpgw_cc_contact_grps WHERE id_connection = ".$id;                       
104                                                                               
105                        if (!$this->db->query($query))                 
106                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);                             
107                                                                       
108                        return True;           
109                }
110               
111               
[702]112                function selectAllContacts( $field = false )
[2]113                {
[16]114                                               
115                        $query = 'select C.id_connection, A.id_contact, A.names_ordered, C.connection_value , B.id_typeof_contact_connection from phpgw_cc_contact A,'.
[2]116                        'phpgw_cc_contact_conns B, phpgw_cc_connections C where '.
[16]117                        'A.id_contact = B.id_contact and B.id_connection = C.id_connection and '.
[702]118                        'A.id_owner ='.$this->owner.' and C.connection_is_default = true ';
[16]119
[702]120                        if ( $field == 'only_email' )
121                                $query .= 'and B.id_typeof_contact_connection = 1 ';
122
123                        $query .= ' order by A.names_ordered,C.connection_value';
124
[16]125                                       
[2]126                        if (!$this->db->query($query))
127                        {
128                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
129                        }
130                         
[576]131                        $return = array();
[2]132                       
133                        while($this->db->next_record())
134                        {
135                                $return[] = $this->db->row();
136                        }
[576]137                       
138                        if(!count($return))
139                                return $return;                         
[293]140                        // Essa iteração transforma uma esturuta com contatos redundantes e separados por email ou tel.
141                        // em outra com apenas 1 elemento pra cada contato
142                        foreach($return as $i => $object){
143                                if ($object['id_typeof_contact_connection'] == 1){
144                                        $all_contacts[$object['id_contact']]['connection_value'] = $object['connection_value'];
145                    $all_contacts[$object['id_contact']]['names_ordered'] = $object['names_ordered'];
146                    $all_contacts[$object['id_contact']]['id_contact'] = $object['id_contact'];
147                    $all_contacts[$object['id_contact']]['id_connection'] = $object['id_connection'];
148                                }
149                                else
150                                        if ($object['id_typeof_contact_connection'] == 2){
151                                                $all_contacts[$object['id_contact']]['phone'] = $object['connection_value'];
152                                                $all_contacts[$object['id_contact']]['names_ordered'] = $object['names_ordered'];
153                                                $all_contacts[$object['id_contact']]['id_contact'] = $object['id_contact'];
154                                                $all_contacts[$object['id_contact']]['id_connection'] = $object['id_connection'];
155                                        }
156                        }
[2]157                       
[293]158                        return array_values($all_contacts);
[2]159                }               
160               
161                function verifyContact($email)
162                {
163                        $query = 'select A.names_ordered from phpgw_cc_contact A,'.
164                        'phpgw_cc_contact_conns B, phpgw_cc_connections C where '.
165                        'A.id_contact = B.id_contact and B.id_connection = C.id_connection '.
166                        'and B.id_typeof_contact_connection = 1 and '.
167                        'A.id_owner ='.$this->owner.' and C.connection_value = \''.$email.'\'';
168                                               
169                        if (!$this->db->query($query))
170                        {
171                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
172                        }
173                         
174                        $return = false;
175                       
176                        while($this->db->next_record())
177                        {
178                                $row = $this->db->row();
179                                $return[] =  $row['names_ordered'];
180                        }
181                       
182                        return $return;
183                }               
184               
185                function selectContactsByGroup($idGroup)
186                {
187                        $query = 'select C.id_connection, A.names_ordered, C.connection_value from phpgw_cc_contact A,'.
188                        'phpgw_cc_contact_conns B, phpgw_cc_connections C,phpgw_cc_contact_grps D where '.
189                        'A.id_contact = B.id_contact and B.id_connection = C.id_connection '.
190                        'and B.id_typeof_contact_connection = 1 and '.
191                        'A.id_owner ='.$this->owner.' and D.id_connection = C.id_connection and D.id_group = '.$idGroup.
192                        ' order by A.names_ordered';
193                                               
194                        if (!$this->db->query($query))
195                        {
196                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
197                        }
198                         
199                        $return = false;
200                       
201                       
202                        while($this->db->next_record())
203                        {
204                                $return[] = $this->db->row();
205                        }
206                       
207                        return $return;
208                }
209               
210                function selectContactsByGroupAlias($alias)
211                {
212                        $query = "select C.id_connection, A.names_ordered, C.connection_value from phpgw_cc_contact A, ".
213                        "phpgw_cc_contact_conns B, phpgw_cc_connections C,phpgw_cc_contact_grps D,phpgw_cc_groups E where ".
214                        "A.id_contact = B.id_contact and B.id_connection = C.id_connection ".
215                        "and B.id_typeof_contact_connection = 1 and ".
216                        "A.id_owner =".$this->owner." and ".                   
217                        "D.id_group = E.id_group and ".
218                        "D.id_connection = C.id_connection and E.short_name = '".$alias.
219                        "' order by A.names_ordered";
220                                               
221                        if (!$this->db->query($query))
222                        {
223                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
224                        }
225                         
226                        $return = false;
227                       
228                       
229                        while($this->db->next_record())
230                        {
231                                $return[] = $this->db->row();
232                        }
233                       
234                        return $return;
235                }
236               
237                function insertContactsByGroup($idGroup, $contacts)
238                {                                                                       
239                       
240                        foreach($contacts as $index => $idConnection)
241                        {                       
242                                $query = "INSERT INTO phpgw_cc_contact_grps(id_group,id_connection) ".
243                                                "VALUES(".$idGroup.",".$idConnection.")";                       
244
245                                if (!$this->db->query($query))
246                                        exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);                     
247                        }
248                                                                                               
249                        return True;           
250                }
251               
252                function updateContactsByGroup($id_group, $contacts)
253                {
254                                               
255                        $query = 'select C.id_connection from phpgw_cc_contact A,'.
256                        'phpgw_cc_contact_conns B, phpgw_cc_connections C,phpgw_cc_contact_grps D where '.
257                        'A.id_contact = B.id_contact and B.id_connection = C.id_connection '.
258                        'and B.id_typeof_contact_connection = 1 and '.
259                        'A.id_owner ='.$this->owner.' and D.id_connection = C.id_connection and D.id_group = '.$id_group.
260                        ' order by A.names_ordered';
261                                               
262                        if (!$this->db->query($query))
263                        {
264                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
265                        }
266                         
267                        $return = false;
268                       
269                       
270                        while($this->db->next_record())
271                        {
272                                $return[] = $this->db->row();
273                                 
274                        }
275                                               
276                        $array_connections = array();
277                       
278                        if($return) {
279                                foreach($return as $index => $connection){                             
280                                        array_push($array_connections,$connection['id_connection']);
281                                }
282                        }
283                       
284                        if(!is_array($contacts))
285                                $contacts = array();
286                       
287                        if(!is_array($connections))
288                                $connections = array();                         
289                                                                       
290                        $connections_to_add     = array_diff($contacts, $array_connections);
291                        $connections_to_remove  = array_diff($array_connections,$contacts);
292                       
293                        if($connections_to_add){
294                                $this -> insertContactsByGroup($id_group, $connections_to_add); 
295                        }
296                       
297                        if($connections_to_remove){
298                                $this -> deleteContactsByGroup($id_group, $connections_to_remove);
299                        }
300                       
301                        return True;
302                }               
303               
304                function deleteContactsByGroup($id_group, $contacts = null)
305                {
306                       
307                        $query = "DELETE FROM phpgw_cc_contact_grps ";
308                       
309                        if($contacts) {                                         
310                                $index = 0;
311                               
312                                foreach($contacts as $a => $id_connection) {
313                                         
314                                        if($index++)
315                                                $query .= " OR";
316                                        else
317                                                $query .= " WHERE (";
318                                         
319                                        $query .= " id_connection = ".$id_connection;                                                                           
320                                }
321                                $query .= ") AND ";
322                        }       
323                        else
324                                $query.= " WHERE ";
325                       
326                        $query.= "id_group = ".$id_group;
327                       
328                       
329                        if (!$this->db->query($query))                 
330                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);                             
331                                                                       
332                        return True;           
333                }
334        }
335?>
Note: See TracBrowser for help on using the repository browser.