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

Revision 576, 9.9 KB checked in by niltonneto, 15 years ago (diff)

Resolve #387

  • 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 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               
112                function selectAllContacts()
113                {
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,'.
116                        'phpgw_cc_contact_conns B, phpgw_cc_connections C where '.
117                        'A.id_contact = B.id_contact and B.id_connection = C.id_connection and '.
118                        'A.id_owner ='.$this->owner.' and C.connection_is_default = true '.
119                        ' order by A.names_ordered,C.connection_value';
120
121                                       
122                        if (!$this->db->query($query))
123                        {
124                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
125                        }
126                         
127                        $return = array();
128                       
129                        while($this->db->next_record())
130                        {
131                                $return[] = $this->db->row();
132                        }
133                       
134                        if(!count($return))
135                                return $return;                         
136                        // Essa iteração transforma uma esturuta com contatos redundantes e separados por email ou tel.
137                        // em outra com apenas 1 elemento pra cada contato
138                        foreach($return as $i => $object){
139                                if ($object['id_typeof_contact_connection'] == 1){
140                                        $all_contacts[$object['id_contact']]['connection_value'] = $object['connection_value'];
141                    $all_contacts[$object['id_contact']]['names_ordered'] = $object['names_ordered'];
142                    $all_contacts[$object['id_contact']]['id_contact'] = $object['id_contact'];
143                    $all_contacts[$object['id_contact']]['id_connection'] = $object['id_connection'];
144                                }
145                                else
146                                        if ($object['id_typeof_contact_connection'] == 2){
147                                                $all_contacts[$object['id_contact']]['phone'] = $object['connection_value'];
148                                                $all_contacts[$object['id_contact']]['names_ordered'] = $object['names_ordered'];
149                                                $all_contacts[$object['id_contact']]['id_contact'] = $object['id_contact'];
150                                                $all_contacts[$object['id_contact']]['id_connection'] = $object['id_connection'];
151                                        }
152                        }
153                       
154                        return array_values($all_contacts);
155                }               
156               
157                function verifyContact($email)
158                {
159                        $query = 'select A.names_ordered from phpgw_cc_contact A,'.
160                        'phpgw_cc_contact_conns B, phpgw_cc_connections C where '.
161                        'A.id_contact = B.id_contact and B.id_connection = C.id_connection '.
162                        'and B.id_typeof_contact_connection = 1 and '.
163                        'A.id_owner ='.$this->owner.' and C.connection_value = \''.$email.'\'';
164                                               
165                        if (!$this->db->query($query))
166                        {
167                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
168                        }
169                         
170                        $return = false;
171                       
172                        while($this->db->next_record())
173                        {
174                                $row = $this->db->row();
175                                $return[] =  $row['names_ordered'];
176                        }
177                       
178                        return $return;
179                }               
180               
181                function selectContactsByGroup($idGroup)
182                {
183                        $query = 'select C.id_connection, A.names_ordered, C.connection_value from phpgw_cc_contact A,'.
184                        'phpgw_cc_contact_conns B, phpgw_cc_connections C,phpgw_cc_contact_grps D where '.
185                        'A.id_contact = B.id_contact and B.id_connection = C.id_connection '.
186                        'and B.id_typeof_contact_connection = 1 and '.
187                        'A.id_owner ='.$this->owner.' and D.id_connection = C.id_connection and D.id_group = '.$idGroup.
188                        ' order by A.names_ordered';
189                                               
190                        if (!$this->db->query($query))
191                        {
192                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
193                        }
194                         
195                        $return = false;
196                       
197                       
198                        while($this->db->next_record())
199                        {
200                                $return[] = $this->db->row();
201                        }
202                       
203                        return $return;
204                }
205               
206                function selectContactsByGroupAlias($alias)
207                {
208                        $query = "select C.id_connection, A.names_ordered, C.connection_value from phpgw_cc_contact A, ".
209                        "phpgw_cc_contact_conns B, phpgw_cc_connections C,phpgw_cc_contact_grps D,phpgw_cc_groups E where ".
210                        "A.id_contact = B.id_contact and B.id_connection = C.id_connection ".
211                        "and B.id_typeof_contact_connection = 1 and ".
212                        "A.id_owner =".$this->owner." and ".                   
213                        "D.id_group = E.id_group and ".
214                        "D.id_connection = C.id_connection and E.short_name = '".$alias.
215                        "' order by A.names_ordered";
216                                               
217                        if (!$this->db->query($query))
218                        {
219                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
220                        }
221                         
222                        $return = false;
223                       
224                       
225                        while($this->db->next_record())
226                        {
227                                $return[] = $this->db->row();
228                        }
229                       
230                        return $return;
231                }
232               
233                function insertContactsByGroup($idGroup, $contacts)
234                {                                                                       
235                       
236                        foreach($contacts as $index => $idConnection)
237                        {                       
238                                $query = "INSERT INTO phpgw_cc_contact_grps(id_group,id_connection) ".
239                                                "VALUES(".$idGroup.",".$idConnection.")";                       
240
241                                if (!$this->db->query($query))
242                                        exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);                     
243                        }
244                                                                                               
245                        return True;           
246                }
247               
248                function updateContactsByGroup($id_group, $contacts)
249                {
250                                               
251                        $query = 'select C.id_connection from phpgw_cc_contact A,'.
252                        'phpgw_cc_contact_conns B, phpgw_cc_connections C,phpgw_cc_contact_grps D where '.
253                        'A.id_contact = B.id_contact and B.id_connection = C.id_connection '.
254                        'and B.id_typeof_contact_connection = 1 and '.
255                        'A.id_owner ='.$this->owner.' and D.id_connection = C.id_connection and D.id_group = '.$id_group.
256                        ' order by A.names_ordered';
257                                               
258                        if (!$this->db->query($query))
259                        {
260                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
261                        }
262                         
263                        $return = false;
264                       
265                       
266                        while($this->db->next_record())
267                        {
268                                $return[] = $this->db->row();
269                                 
270                        }
271                                               
272                        $array_connections = array();
273                       
274                        if($return) {
275                                foreach($return as $index => $connection){                             
276                                        array_push($array_connections,$connection['id_connection']);
277                                }
278                        }
279                       
280                        if(!is_array($contacts))
281                                $contacts = array();
282                       
283                        if(!is_array($connections))
284                                $connections = array();                         
285                                                                       
286                        $connections_to_add     = array_diff($contacts, $array_connections);
287                        $connections_to_remove  = array_diff($array_connections,$contacts);
288                       
289                        if($connections_to_add){
290                                $this -> insertContactsByGroup($id_group, $connections_to_add); 
291                        }
292                       
293                        if($connections_to_remove){
294                                $this -> deleteContactsByGroup($id_group, $connections_to_remove);
295                        }
296                       
297                        return True;
298                }               
299               
300                function deleteContactsByGroup($id_group, $contacts = null)
301                {
302                       
303                        $query = "DELETE FROM phpgw_cc_contact_grps ";
304                       
305                        if($contacts) {                                         
306                                $index = 0;
307                               
308                                foreach($contacts as $a => $id_connection) {
309                                         
310                                        if($index++)
311                                                $query .= " OR";
312                                        else
313                                                $query .= " WHERE (";
314                                         
315                                        $query .= " id_connection = ".$id_connection;                                                                           
316                                }
317                                $query .= ") AND ";
318                        }       
319                        else
320                                $query.= " WHERE ";
321                       
322                        $query.= "id_group = ".$id_group;
323                       
324                       
325                        if (!$this->db->query($query))                 
326                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);                             
327                                                                       
328                        return True;           
329                }
330        }
331?>
Note: See TracBrowser for help on using the repository browser.