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

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

Resolve #453 e #454 (duplicado).

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