* * sponsored by Celepar - http://celepar.pr.gov.br * * ------------------------------------------------------------------------- * * This program is free software; you can redistribute it and/or modify it * * under the terms of the GNU General Public License as published by the * * Free Software Foundation; either version 2 of the License, or (at your * * option) any later version. * \***************************************************************************/ class so_group { var $db; var $owner; function so_group () { $this->db = $GLOBALS['phpgw']->db; $this->owner = $GLOBALS['phpgw_info']['user']['account_id']; } function select($id = '') { $query = 'SELECT id_group,title,short_name FROM phpgw_cc_groups WHERE owner = '.$this->owner; if($id != '') $query .= ' AND id_group ='.$id; $query .= ' ORDER BY title'; if (!$this->db->query($query)) { exit ('Query failed! File: '.__FILE__.' on line'.__LINE__); } $return = false; while($this->db->next_record()) { $return[] = $this->db->row(); } return $return; } function remove_accents($string) { return strtr($string, "?Ó??ó?Ý?úÁÀÃÂÄÇÉÈÊËÍÌ?ÎÏÑÕÔÓÒÖÚÙ?ÛÜ?áàãâäçéèêëíì?îïñóòõôöúù?ûüýÿ", "SOZsozYYuAAAAACEEEEIIIIINOOOOOUUUUUsaaaaaceeeeiiiiinooooouuuuuyy"); } function insert($data) { $shortName = $this -> remove_accents(strtolower(str_replace(" ","", $data['title']))); $query = "INSERT INTO phpgw_cc_groups(title, owner,short_name) ". "VALUES('".$data['title']."',".$this->owner.",'".$shortName."')"; if (!$this->db->query($query)) exit ('Query failed! File: '.__FILE__.' on line'.__LINE__); $objSequence = $this->db->query("SELECT last_value FROM seq_phpgw_cc_groups"); $id = $objSequence -> fields['last_value']; $this -> updateContactsByGroup($id, $data['contact_in_list']); return $id; } function update($data) { $shortName = $this -> remove_accents(strtolower(str_replace(" ","", $data['title']))); $query = "UPDATE phpgw_cc_groups SET title = '".$data['title']."',short_name = '".$shortName."' WHERE id_group = ".$data['id_group']; if (!$this->db->query($query)) exit ('Query failed! File: '.__FILE__.' on line'.__LINE__); $this -> updateContactsByGroup($data['id_group'], $data['contact_in_list']); return True; } function delete($data) { $query = "DELETE FROM phpgw_cc_groups WHERE id_group = ".$data['id_group']; if (!$this->db->query($query)) exit ('Query failed! File: '.__FILE__.' on line'.__LINE__); $this -> deleteContactsByGroup($data['id_group'], $data['contact_in_list']); return True; } function deleteContactFromGroups($id) { $query = "DELETE FROM phpgw_cc_contact_grps WHERE id_connection = ".$id; if (!$this->db->query($query)) exit ('Query failed! File: '.__FILE__.' on line'.__LINE__); return True; } function selectAllContacts( $field = false ) { $query = 'select C.id_connection, A.id_contact, A.names_ordered, C.connection_value , B.id_typeof_contact_connection from phpgw_cc_contact A,'. 'phpgw_cc_contact_conns B, phpgw_cc_connections C where '. 'A.id_contact = B.id_contact and B.id_connection = C.id_connection and '. 'A.id_owner ='.$this->owner.' and C.connection_is_default = true '; if ( $field == 'only_email' ) $query .= 'and B.id_typeof_contact_connection = 1 '; $query .= ' order by A.names_ordered,C.connection_value'; if (!$this->db->query($query)) { exit ('Query failed! File: '.__FILE__.' on line'.__LINE__); } $return = array(); while($this->db->next_record()) { $return[] = $this->db->row(); } if(!count($return)) return $return; // Essa iteração transforma uma esturuta com contatos redundantes e separados por email ou tel. // em outra com apenas 1 elemento pra cada contato foreach($return as $i => $object){ if ($object['id_typeof_contact_connection'] == 1){ $all_contacts[$object['id_contact']]['connection_value'] = $object['connection_value']; $all_contacts[$object['id_contact']]['names_ordered'] = $object['names_ordered']; $all_contacts[$object['id_contact']]['id_contact'] = $object['id_contact']; $all_contacts[$object['id_contact']]['id_connection'] = $object['id_connection']; } else if ($object['id_typeof_contact_connection'] == 2){ $all_contacts[$object['id_contact']]['phone'] = $object['connection_value']; $all_contacts[$object['id_contact']]['names_ordered'] = $object['names_ordered']; $all_contacts[$object['id_contact']]['id_contact'] = $object['id_contact']; $all_contacts[$object['id_contact']]['id_connection'] = $object['id_connection']; } } return array_values($all_contacts); } function verifyContact($email) { $query = 'select A.names_ordered from phpgw_cc_contact A,'. 'phpgw_cc_contact_conns B, phpgw_cc_connections C where '. 'A.id_contact = B.id_contact and B.id_connection = C.id_connection '. 'and B.id_typeof_contact_connection = 1 and '. 'A.id_owner ='.$this->owner.' and C.connection_value = \''.$email.'\''; if (!$this->db->query($query)) { exit ('Query failed! File: '.__FILE__.' on line'.__LINE__); } $return = false; while($this->db->next_record()) { $row = $this->db->row(); $return[] = $row['names_ordered']; } return $return; } function selectContactsByGroup($idGroup) { $query = 'select C.id_connection, A.names_ordered, C.connection_value from phpgw_cc_contact A,'. 'phpgw_cc_contact_conns B, phpgw_cc_connections C,phpgw_cc_contact_grps D where '. 'A.id_contact = B.id_contact and B.id_connection = C.id_connection '. 'and B.id_typeof_contact_connection = 1 and '. 'A.id_owner ='.$this->owner.' and D.id_connection = C.id_connection and D.id_group = '.$idGroup. ' order by A.names_ordered'; if (!$this->db->query($query)) { exit ('Query failed! File: '.__FILE__.' on line'.__LINE__); } $return = false; while($this->db->next_record()) { $return[] = $this->db->row(); } return $return; } function selectContactsByGroupAlias($alias) { $query = "select C.id_connection, A.names_ordered, C.connection_value from phpgw_cc_contact A, ". "phpgw_cc_contact_conns B, phpgw_cc_connections C,phpgw_cc_contact_grps D,phpgw_cc_groups E where ". "A.id_contact = B.id_contact and B.id_connection = C.id_connection ". "and B.id_typeof_contact_connection = 1 and ". "A.id_owner =".$this->owner." and ". "D.id_group = E.id_group and ". "D.id_connection = C.id_connection and E.short_name = '".$alias. "' order by A.names_ordered"; if (!$this->db->query($query)) { exit ('Query failed! File: '.__FILE__.' on line'.__LINE__); } $return = false; while($this->db->next_record()) { $return[] = $this->db->row(); } return $return; } function insertContactsByGroup($idGroup, $contacts) { foreach($contacts as $index => $idConnection) { $query = "INSERT INTO phpgw_cc_contact_grps(id_group,id_connection) ". "VALUES(".$idGroup.",".$idConnection.")"; if (!$this->db->query($query)) exit ('Query failed! File: '.__FILE__.' on line'.__LINE__); } return True; } function updateContactsByGroup($id_group, $contacts) { $query = 'select C.id_connection from phpgw_cc_contact A,'. 'phpgw_cc_contact_conns B, phpgw_cc_connections C,phpgw_cc_contact_grps D where '. 'A.id_contact = B.id_contact and B.id_connection = C.id_connection '. 'and B.id_typeof_contact_connection = 1 and '. 'A.id_owner ='.$this->owner.' and D.id_connection = C.id_connection and D.id_group = '.$id_group. ' order by A.names_ordered'; if (!$this->db->query($query)) { exit ('Query failed! File: '.__FILE__.' on line'.__LINE__); } $return = false; while($this->db->next_record()) { $return[] = $this->db->row(); } $array_connections = array(); if($return) { foreach($return as $index => $connection){ array_push($array_connections,$connection['id_connection']); } } if(!is_array($contacts)) $contacts = array(); if(!is_array($connections)) $connections = array(); $connections_to_add = array_diff($contacts, $array_connections); $connections_to_remove = array_diff($array_connections,$contacts); if($connections_to_add){ $this -> insertContactsByGroup($id_group, $connections_to_add); } if($connections_to_remove){ $this -> deleteContactsByGroup($id_group, $connections_to_remove); } return True; } function deleteContactsByGroup($id_group, $contacts = null) { $query = "DELETE FROM phpgw_cc_contact_grps "; if($contacts) { $index = 0; foreach($contacts as $a => $id_connection) { if($index++) $query .= " OR"; else $query .= " WHERE ("; $query .= " id_connection = ".$id_connection; } $query .= ") AND "; } else $query.= " WHERE "; $query.= "id_group = ".$id_group; if (!$this->db->query($query)) exit ('Query failed! File: '.__FILE__.' on line'.__LINE__); return True; } } ?>