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