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

Revision 1280, 11.0 KB checked in by eduardoalex, 15 years ago (diff)

Ticket #453 - Edição de grupos compartilhados.

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