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

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