source: trunk/expressoMail1_2/inc/class.db_functions.inc.php @ 502

Revision 502, 9.8 KB checked in by niltonneto, 16 years ago (diff)

Na função get_cc_contacts, adiciona os contatos compartilhados, caso exista algum.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2define('PHPGW_INCLUDE_ROOT','../');     
3define('PHPGW_API_INC','../phpgwapi/inc');     
4include_once(PHPGW_API_INC.'/class.db.inc.php');
5include_once('class.dynamic_contacts.inc.php');
6       
7class db_functions
8{       
9       
10        var $db;
11        var $user_id;
12       
13        function db_functions(){
14                $this->db = new db();           
15                $this->db->Halt_On_Error = 'no';
16                $this->db->connect(
17                                $_SESSION['phpgw_info']['expressomail']['server']['db_name'],
18                                $_SESSION['phpgw_info']['expressomail']['server']['db_host'],
19                                $_SESSION['phpgw_info']['expressomail']['server']['db_port'],
20                                $_SESSION['phpgw_info']['expressomail']['server']['db_user'],
21                                $_SESSION['phpgw_info']['expressomail']['server']['db_pass'],
22                                $_SESSION['phpgw_info']['expressomail']['server']['db_type']
23                );             
24                $this -> user_id = $_SESSION['phpgw_info']['expressomail']['user']['account_id'];       
25        }
26
27        // BEGIN of functions.
28        function get_cc_contacts()
29        {                               
30                $result = array();
31                $stringDropDownContacts = '';
32                // Pesquisa no CC os nomes e email dos contatos.
33                $query = 'select A.names_ordered, C.connection_value from phpgw_cc_contact A,
34                                        phpgw_cc_contact_conns B, phpgw_cc_connections C where
35                                        A.id_contact = B.id_contact and B.id_connection = C.id_connection
36                                        and B.id_typeof_contact_connection = 1 and
37                                        (A.id_owner ='.$this -> user_id.' or
38                                        A.id_owner in (
39                                        select id_related from phpgw_cc_contact_rels where id_contact='.$this -> user_id.' and id_typeof_contact_relation=1
40                                        )
41                                        )
42                                        group by A.names_ordered,C.connection_value
43                                        order by A.names_ordered';
44        if (!$this->db->query($query))
45                return null;
46                               
47               
48                while($this->db->next_record())
49                        $result[] = $this->db->row();
50
51                if (count($result) != 0)
52                {
53                        // Monta string                         
54                        foreach($result as $contact)
55                                $stringDropDownContacts = $stringDropDownContacts . $contact['names_ordered']. ';' . $contact['connection_value'] . ',';
56                        //Retira ultima virgula.
57                        $stringDropDownContacts = substr($stringDropDownContacts,0,(strlen($stringDropDownContacts) - 1));
58                }
59                else
60                        return null;
61
62                return $stringDropDownContacts;
63        }
64       
65        function get_cc_groups()
66        {
67                // Pesquisa no CC os Grupos Pessoais.
68                $stringDropDownContacts = '';                   
69                $result = array();
70                $query = 'select title, short_name from phpgw_cc_groups where owner ='.$this -> user_id.' order by title';
71                // Executa a query
72                if (!$this->db->query($query))
73                return null;
74                // Retorna cada resultado               
75                while($this->db->next_record())
76                        $result[] = $this->db->row();
77                // Se houver grupos ....
78                if (count($result) != 0)
79                {       
80                        foreach($result as $group)
81                                $stringDropDownContacts .=  $group['title']. ';' . $group['short_name'] . ',';
82                        //Retira ultima virgula.
83                        $stringDropDownContacts = substr($stringDropDownContacts,0,(strlen($stringDropDownContacts) - 1));
84                }
85                else
86                        return null;
87                       
88                return $stringDropDownContacts;
89        }
90       
91        function getContactsByGroupAlias($alias)
92        {
93               
94                $query = "select C.id_connection, A.names_ordered, C.connection_value from phpgw_cc_contact A, ".
95                "phpgw_cc_contact_conns B, phpgw_cc_connections C,phpgw_cc_contact_grps D,phpgw_cc_groups E where ".
96                "A.id_contact = B.id_contact and B.id_connection = C.id_connection ".
97                "and B.id_typeof_contact_connection = 1 and ".
98                "A.id_owner =".$this -> user_id." and ".                       
99                "D.id_group = E.id_group and ".
100                "D.id_connection = C.id_connection and E.short_name = '".$alias.
101                "' order by A.names_ordered";
102
103                if (!$this->db->query($query))
104                {
105                        exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
106                }
107
108                $return = false;
109
110                while($this->db->next_record())
111                {
112                        $return[] = $this->db->row();
113                }
114
115                return $return;
116        }
117
118        function getAddrs($array_addrs) {
119                $array_addrs_final = array();                           
120
121                for($i = 0; $i < count($array_addrs); $i++){
122                        $j = count($array_addrs_final);
123
124                        if(!strchr($array_addrs[$i],'@')
125                                        && strchr($array_addrs[$i],'<')
126                                         && strchr($array_addrs[$i],'>')) {             
127
128                                $alias = substr($array_addrs[$i], strpos($array_addrs[$i],'<'), strpos($array_addrs[$i],'>'));                         
129                                $alias = str_replace('<','', str_replace('>','',$alias));                                                                       
130                                $arrayContacts = $this -> getContactsByGroupAlias($alias);
131
132                                if($arrayContacts) {
133                                        foreach($arrayContacts as $index => $contact){
134                                                if($contact['names_ordered']) {
135                                                        $array_addrs_final[$j] = '"'.$contact['names_ordered'].'" <'.$contact['connection_value'].'>';
136                                                }
137                                                else
138                                                        $array_addrs_final[$j] = $contact['connection_value'];
139
140                                                $j++;
141                                        }
142                                }
143                        }
144                        else
145                                $array_addrs_final[$j++] = $array_addrs[$i];                                                   
146                }
147                return $array_addrs_final;
148        }
149
150        function get_dropdown_contacts(){
151               
152                $contacts = $this -> get_cc_contacts();
153                $groups = $this -> get_cc_groups();
154               
155                if(($contacts) && ($groups))
156                        $stringDropDownContacts = $contacts . ',' . $groups;
157                elseif ((!$contacts) && (!$groups))
158                        $stringDropDownContacts = '';
159                elseif (($contacts) && (!$groups))
160                        $stringDropDownContacts = $contacts;
161                elseif ((!$contacts) && ($groups))
162                        $stringDropDownContacts = $groups;
163                                       
164                if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['number_of_contacts'] &&
165                        $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['use_dynamic_contacts']) {
166                        $dynamic_contact = new dynamic_contacts();
167                        $dynamic = $dynamic_contact->dynamic_contact_toString();
168                        if ($dynamic)
169                                $stringDropDownContacts .= ($stringDropDownContacts ? ',' : '') . $dynamic;
170                }
171                return $stringDropDownContacts;
172        }
173        /*
174        function update_preferences($params){
175               
176                $aux = explode("##",$params['prefe_string']);
177                $new_prefe = array();
178               
179                foreach($aux as $key=>$tmp){
180                        if($key == 0){
181                                $new_prefe['max_email_per_page'] = $tmp;
182                        }
183                        if($key == 1){
184                                $new_prefe['save_deleted_msg'] = $tmp;
185                        }
186                        if($key == 2){
187                                $new_prefe['delete_trash_messages_after_n_days'] = $tmp;
188                        }
189                        if($key == 3){
190                                $new_prefe['delete_and_show_previous_message'] = $tmp;
191                        }
192                        if($key == 4){
193                                $new_prefe['alert_new_msg'] = $tmp;
194                        }
195                        if($key == 5){
196                                $new_prefe['mainscreen_showmail'] = $tmp;
197                        }
198                        if($key == 10){
199                                $new_prefe['signature'] = $tmp;
200                        }
201                        if($key == 7){
202                                $new_prefe['hide_folders'] = $tmp;
203                        }
204                        if($key == 6){
205                                $new_prefe['save_in_folder'] = $tmp;
206                        }
207                        if($key == 8){
208                                $new_prefe['line_height'] = $tmp;
209                        }
210                        if($key == 9){
211                                $new_prefe['font_size'] = $tmp;
212                        }
213                        if($key == 11){
214                                $new_prefe['use_shortcuts'] = $tmp;
215                        }
216                }
217               
218                $string_serial =  serialize($new_prefe);
219                $string_serial = get_magic_quotes_gpc() ? $string_serial : addslashes($string_serial);
220                $query = "update phpgw_preferences set preference_value = '".$string_serial."' where preference_app = 'expressoMail'".
221                                 " and preference_owner = '".$this->user_id."'";
222               
223                if (!$this->db->query($query))
224                return "Failed!";
225                else
226                        return "OK!";
227        }
228        */
229        function update_preferences($params){
230                $string_serial = urldecode($params['prefe_string']);                           
231                $string_serial = get_magic_quotes_gpc() ? $string_serial : addslashes($string_serial);
232                $query = "update phpgw_preferences set preference_value = '".$string_serial."' where preference_app = 'expressoMail'".
233                                 " and preference_owner = '".$this->user_id."'";
234               
235                if (!$this->db->query($query))
236                return $this->db->error;
237                else
238                        return array("success" => true);
239        }
240        function getUserByEmail($params){       
241                // Follow the referral
242                $email = $params['email'];
243                $query = 'select A.names_ordered, C.connection_name, C.connection_value, A.photo'.
244                                ' from phpgw_cc_contact A, phpgw_cc_contact_conns B, '.
245                                'phpgw_cc_connections C where A.id_contact = B.id_contact'.
246                                ' and B.id_connection = C.id_connection and A.id_contact ='.
247                                '(select A.id_contact from phpgw_cc_contact A, phpgw_cc_contact_conns B,'.
248                                'phpgw_cc_connections C where A.id_contact = B.id_contact'.
249                                ' and B.id_connection = C.id_connection and A.id_owner = '.$this -> user_id.
250                                ' and C.connection_value = \''.$email.'\') and '.
251                                'C.connection_is_default = true and B.id_typeof_contact_connection = 2';
252
253        if (!$this->db->query($query))
254                return null;
255
256
257                if($this->db->next_record()) {
258                        $result = $this->db->row();
259
260                        $obj =  array("cn" => $result['names_ordered'],
261                                          "email" => $email,
262                                          "type" => "personal",
263                                          "telefone" =>  $result['connection_value']);
264
265                        if($result['photo'])
266                                $_SESSION['phpgw_info']['expressomail']['contact_photo'] =  array($result['photo']);                           
267
268                        return $obj;
269                }
270                return $result;
271        }
272       
273        function get_dynamic_contacts()
274        {                               
275                // Pesquisa os emails e ultima inserção nos contatos dinamicos.
276                if(!$this->db->select('phpgw_expressomail_contacts','data',
277                                                  'id_owner ='.$this -> user_id,
278                                                  __LINE__,__FILE__))
279                {
280                return $this->db->Error;
281}
282                while($this->db->next_record())
283                {
284                        $result[] = $this->db->row();
285                }
286                if($result) foreach($result as $item)
287                {
288                        $contacts = unserialize($item['data']);
289                }
290                if (count($contacts) == 0)
291                {                       
292                        return null;
293                }       
294                //Sort by email
295                function cmp($a, $b) { return strcmp($a["email"], $b["email"]);}
296                usort($contacts,"cmp");
297                return $contacts;
298        }
299        function update_contacts($contacts=array())
300        {                       
301                // Atualiza um email nos contatos dinamicos.
302                if(!$this->db->update('phpgw_expressomail_contacts ','data=\''.serialize($contacts).'\'',
303                                                          'id_owner ='.$this -> user_id,
304                                                          __LINE__,__FILE__))
305                {
306                        return $this->db->Error;
307                }
308        return $contacts;
309        }       
310       
311        function insert_contact($contact)       
312        {
313                $contacts[] = array( 'timestamp'        => time(),
314                                                                'email'         => $contact );
315
316                // Insere um email nos contatos dinamicos.     
317                $query = 'INSERT INTO phpgw_expressomail_contacts (data, id_owner)  ' .
318                                        'values ( \''.serialize($contacts).'\', '.$this->user_id.')';
319               
320                if(!$this->db->query($query,__LINE__,__FILE__))
321                return $this->db->Error;
322        return $contacts;
323        }
324       
325        function remove_dynamic_contact($user_id,$line,$file)
326        {
327                $where = $user_id.' = id_owner';
328                $this->db->delete('phpgw_expressomail_contacts',$where,$line,$file);   
329        }
330}
331?>
Note: See TracBrowser for help on using the repository browser.