source: contrib/z-push/backend/BackendContactsExpresso.php @ 3637

Revision 3637, 56.1 KB checked in by emersonfaria, 13 years ago (diff)

Ticket #1476 - Commit inicial dos arquivos do Projeto Z-Push customizados para o Expresso

  • Property svn:executable set to *
Line 
1<?php
2/***********************************************
3 * File      :   BackendContactsExpresso.php
4 * Project   :   Z-Push
5 * Descr     :   This Contact Backend is for Expresso Groupware.
6 *
7 * Created   :   29.09.2010 - emerson-faria.nobre@serpro.gov.br
8 *
9 * ï¿œ Zarafa Deutschland GmbH, www.zarafaserver.de
10 * This file is distributed under GPL v2.
11 * Consult LICENSE file for details
12 ************************************************/
13include_once('diffbackend.php');
14include_once('include/z_RTF.php');
15include_once "include/dbconnect.php";
16
17class BackendContactsExpresso extends BackendDiff {
18        var $_user;
19        var $_devid;
20        var $_protocolversion;
21        var $db;
22
23        function __construct()
24        {
25                $this->db = dbconnect();
26
27                if (!$this->db) {
28                        echo "A DB connect error occured.\n";
29                        exit;
30                }
31        }
32
33        function Setup($user, $devid, $protocolversion) {
34                $this->_user = $user;
35                $this->_devid = $devid;
36                $this->_protocolversion = $protocolversion;
37                //TODO: Autenticar Usuario no LDAP
38
39                return true;
40        }
41
42        function SendMail($rfc822, $forward = false, $reply = false, $parent = false) {
43                return false;
44        }
45
46        function GetWasteBasket() {
47                return false;
48        }
49
50        function GetMessageList($folderid, $cutoffdate) {
51                $id_owner = $this->parseOwner($this->_user);
52                if ($id_owner == false) return false;
53
54                debugLog('ContactsExpresso::GetMessageList('.$id_owner.')');
55                $messages = array();
56                $ids = array();
57                try {
58                        $result = pg_query($this->db,"BEGIN;");
59                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
60                        $result = pg_query($this->db, "select given_names, family_names, last_update, id_contact from phpgw_cc_contact where id_owner = " . $id_owner . ";");
61                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
62                        while ($row = pg_fetch_row($result)) {
63                                $message = array();
64                                $message["id"] = $row[3];
65                                $message["mod"] = substr($row[2], 0, strlen($row[2])-3);
66                                $message["flags"] = 1; // always 'read'
67                                $messages[] = $message;
68                        }
69                        $result = pg_query($this->db,"COMMIT;");
70                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
71                } catch (Exception $e) {
72                        pg_query($this->db,"ROLLBACK;");
73                        debugLog("exception -> " . $e->getMessage() . " - ARQUIVO: " . $e->getFile() . " - LINHA: " . $e->getLine());
74                }
75                return $messages;
76        }
77
78        function GetFolderList() {
79                debugLog('ContactsExpresso::GetFolderList()');
80                $contacts = array();
81                $folder = $this->StatFolder("root");
82                $contacts[] = $folder;
83                return $folder;
84                //return $contacts;
85        }
86
87        function GetFolder($id) {
88                debugLog('ContactsExpresso::GetFolder('.$id.')');
89                if($id == "root") {
90                        $folder = new SyncFolder();
91                        $folder->serverid = $id;
92                        $folder->parentid = "0";
93                        $folder->displayname = "Contatos";
94                        $folder->type = SYNC_FOLDER_TYPE_CONTACT;
95
96                        return $folder;
97                } else return false;
98        }
99
100        function StatFolder($id) {
101                debugLog('ContactsExpresso::StatFolder('.$id.')');
102                $folder = $this->GetFolder($id);
103
104                $stat = array();
105                $stat["id"] = $id;
106                $stat["parent"] = $folder->parentid;
107                $stat["mod"] = $folder->displayname;
108
109                return $stat;
110        }
111
112        function GetAttachmentData($attname) {
113                return false;
114        }
115
116        function StatMessage($folderid, $id) {
117                debugLog('ContactsExpresso::StatMessage('.$folderid.', '.$id.')');
118                if($folderid != "root") return false;
119
120                $id_owner = $this->parseOwner($this->_user);
121                if ($id_owner == false) return false;
122
123                try {
124                        $result = pg_query($this->db,"BEGIN;");
125                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
126                        $result_contact = pg_query($this->db, "select last_update from phpgw_cc_contact where id_contact = " . $id . ";");
127                        if ($result_contact == FALSE) throw new Exception(pg_last_error($this->db));
128                        while ($row_contact = pg_fetch_row($result_contact)) {
129                                if(isset($row_contact[0])) {
130                                        $message = array();
131                                        $message["mod"] = substr($row_contact[0], 0, strlen($row_contact[0])-3);
132                                        $message["id"] = $id;
133                                        $message["flags"] = 1;
134                                        return $message;
135                                }
136                        }
137                        $result = pg_query($this->db,"COMMIT;");
138                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
139                } catch (Exception $e) {
140                        pg_query($this->db,"ROLLBACK;");
141                        debugLog("exception -> " . $e->getMessage() . " - ARQUIVO: " . $e->getFile() . " - LINHA: " . $e->getLine());
142                }
143                return false;
144        }
145
146        function GetMessage($folderid, $id, $truncsize, $mimesupport = 0) {
147                debugLog('ContactsExpresso::GetMessage('.$folderid.', '.$id.', ..)');
148                if($folderid != "root") return;
149
150                // Parse the database into object
151                $id_owner = $this->parseOwner($this->_user);
152                if ($id_owner == false) return;
153
154                $message = new SyncContact();
155                try {
156                        $result = pg_query($this->db,"BEGIN;");
157                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
158                        $result_contact = pg_query($this->db, "select id_contact, id_owner, id_status, photo, alias, id_prefix, given_names, family_names, names_ordered, id_suffix, birthdate, sex, pgp_key, notes, is_global, last_status, last_update, category, web_page, corporate_name, job_title, department from phpgw_cc_contact where id_contact = " . $id . ";");
159                        if ($result_contact == FALSE) throw new Exception(pg_last_error($this->db));
160                        while ($row_contact = pg_fetch_row($result_contact)) {
161                                //if(isset($row_contact[3])) {
162                                //      $message->picture = base64_encode($row_contact[3]);
163                                //}
164                                if(isset($row_contact[4])) {
165                                        $message->nickname = utf8_encode($row_contact[4]);
166                                }
167                                if(isset($row_contact[7])) {
168                                        $arrayFamilyName = explode(' ',trim($row_contact[7]));
169                                        if (sizeof($arrayFamilyName) > 1) {
170                                                $message->lastname = utf8_encode($arrayFamilyName[sizeof($arrayFamilyName) - 1]);
171                                                for ($i = 0; $i < (sizeof($arrayFamilyName) - 1); $i++) {
172                                                        $message->middlename .= " " . utf8_encode($arrayFamilyName[$i]);
173                                                }
174                                                $message->middlename = trim($message->middlename);
175                                        } else {
176                                                $message->lastname = utf8_encode(trim($row_contact[7]));
177                                        }
178                                        $message->fileas = $message->lastname;
179                                }
180                                if(isset($row_contact[6])) {
181                                        $message->firstname = utf8_encode(trim($row_contact[6]));
182                                        if(isset($row_contact[7])) $message->fileas .= ', ' . $message->firstname;
183                                        else $message->fileas = $message->firstname;
184                                }
185                                if(isset($row_contact[10])) {
186                                        $dataParte = explode('-',$row_contact[10]);
187                                        $data = $dataParte[2] . '-' . $dataParte[1] . '-' . $dataParte[0];
188                                        $tz = date_default_timezone_get();
189                                        date_default_timezone_set('UTC');
190                                        $message->birthday = strtotime($data);
191                                        $message->birthday += 3600 * 10; // Soma 10 horas para nao alterar a data quando mudar o Timezone
192                                        date_default_timezone_set($tz);
193                                }
194                                // TODO:Incluir campo de Aniversario na sincronizacao. O BD do Expresso ainda nao tem esse campo :-(
195                                if(isset($row_contact[13])) {
196                                        $message->body = utf8_encode(str_replace('\\n', chr(13) . chr(10), $this->escape($row_contact[13])));
197                                        $message->bodysize = strlen($message->body);
198                                        $message->bodytruncated = 0;
199                                }
200                                //TODO:Tratar o conteudo do campo de categorias
201                                //if(isset($row_contact[17])) {
202                                //      $message->categories = utf8_encode($row_contact[17]);
203                                //}
204                                if(isset($row_contact[18])) {
205                                        $message->webpage = utf8_encode($row_contact[18]);
206                                }
207                                if(isset($row_contact[19])) {
208                                        $message->companyname = utf8_encode($row_contact[19]);
209                                        if (!isset($row_contact[6]) and !isset($row_contact[7])) $message->fileas = $message->companyname;
210                                }
211                                if(isset($row_contact[20])) {
212                                        $message->jobtitle = utf8_encode($row_contact[20]);
213                                }
214                                if(isset($row_contact[21])) {
215                                        $message->department = utf8_encode($row_contact[21]);
216                                }
217
218                                // Endereço Comercial
219                                $result_addresses_comercial = pg_query($this->db,"select co.id_address, co.id_city, city_name, co.id_state, state_symbol, co.id_country, address1, address2, complement, address_other, postal_code, po_box, address_is_default from phpgw_cc_addresses co join phpgw_cc_contact_addrs ca on (co.id_address = ca.id_address) join phpgw_cc_typeof_ct_addrs tca on (ca.id_typeof_contact_address = tca.id_typeof_contact_address) left outer join phpgw_cc_city ci on (ci.id_city = co.id_city) left outer join phpgw_cc_state cs on (cs.id_state = co.id_state) where tca.contact_address_type_name = 'Comercial' and ca.id_contact = " . $row_contact[0] . ";");
220                                if ($result_addresses_comercial == FALSE) throw new Exception(pg_last_error($this->db));
221                                while ($row_addresses_comercial = pg_fetch_row($result_addresses_comercial)) {
222                                        if (isset($row_addresses_comercial[2])) {
223                                                $message->businesscity = utf8_encode($row_addresses_comercial[2]);
224                                        }
225                                        if (isset($row_addresses_comercial[5])) {
226                                                $message->businesscountry = utf8_encode($row_addresses_comercial[5]);
227                                        }
228                                        if (isset($row_addresses_comercial[10])) {
229                                                $message->businesspostalcode = utf8_encode($row_addresses_comercial[10]);
230                                        }
231                                        if (isset($row_addresses_comercial[4])) {
232                                                $message->businessstate = utf8_encode($row_addresses_comercial[4]);
233                                        }
234                                        if (isset($row_addresses_comercial[6])) {
235                                                $message->businessstreet = utf8_encode($row_addresses_comercial[6]);
236                                        }
237                                        if (isset($row_addresses_comercial[8])) {
238                                                if (isset($message->businessstreet)) {
239                                                        $message->businessstreet .= ":";
240                                                }
241                                                $message->businessstreet .= utf8_encode($row_addresses_comercial[8]);
242                                        }
243                                        if (isset($row_addresses_comercial[7])) {
244                                                if (isset($message->businessstreet)) {
245                                                        $message->businessstreet .= ":";
246                                                }
247                                                $message->businessstreet .= utf8_encode($row_addresses_comercial[7]);
248                                        }
249                                        if (isset($row_addresses_comercial[9])) {
250                                                if (isset($message->businessstreet)) {
251                                                        $message->businessstreet .= ":";
252                                                }
253                                                $message->businessstreet .= utf8_encode($row_addresses_comercial[9]);
254                                        }
255                                        if (isset($row_addresses_comercial[11])) {
256                                                if (isset($message->businessstreet)) {
257                                                        $message->businessstreet .= ":";
258                                                }
259                                                $message->businessstreet .= utf8_encode($row_addresses_comercial[11]);
260                                        }
261                                }
262                                // Endereço Residencial
263                                $result_addresses_residencial = pg_query($this->db,"select co.id_address, co.id_city, city_name, co.id_state, state_name, co.id_country, address1, address2, complement, address_other, postal_code, po_box, address_is_default from phpgw_cc_addresses co join phpgw_cc_contact_addrs ca on (co.id_address = ca.id_address) join phpgw_cc_typeof_ct_addrs tca on (ca.id_typeof_contact_address = tca.id_typeof_contact_address) left outer join phpgw_cc_city ci on (ci.id_city = co.id_city) left outer join phpgw_cc_state cs on (cs.id_state = co.id_state) where tca.contact_address_type_name = 'Residencial' and ca.id_contact = " . $row_contact[0] . ";");
264                                if ($result_addresses_residencial == FALSE) throw new Exception(pg_last_error($this->db));
265                                while ($row_addresses_residencial = pg_fetch_row($result_addresses_residencial)) {
266                                        if (isset($row_addresses_residencial[2])) {
267                                                $message->homecity = utf8_encode($row_addresses_residencial[2]);
268                                        }
269                                        if (isset($row_addresses_residencial[5])) {
270                                                $message->homecountry = utf8_encode($row_addresses_residencial[5]);
271                                        }
272                                        if (isset($row_addresses_residencial[10])) {
273                                                $message->homepostalcode = utf8_encode($row_addresses_residencial[10]);
274                                        }
275                                        if (isset($row_addresses_residencial[4])) {
276                                                $message->homestate = utf8_encode($row_addresses_residencial[4]);
277                                        }
278                                        if (isset($row_addresses_residencial[6])) {
279                                                $message->homestreet = utf8_encode($row_addresses_residencial[6]);
280                                        }
281                                        if (isset($row_addresses_residencial[8])) {
282                                                if (isset($message->homestreet)) {
283                                                        $message->homestreet .= ":";
284                                                }
285                                                $message->homestreet .= utf8_encode($row_addresses_residencial[8]);
286                                        }
287                                        if (isset($row_addresses_residencial[7])) {
288                                                if (isset($message->homestreet)) {
289                                                        $message->homestreet .= ":";
290                                                }
291                                                $message->homestreet .= utf8_encode($row_addresses_residencial[7]);
292                                        }
293                                        if (isset($row_addresses_residencial[9])) {
294                                                if (isset($message->homestreet)) {
295                                                        $message->homestreet .= ":";
296                                                }
297                                                $message->homestreet .= utf8_encode($row_addresses_residencial[9]);
298                                        }
299                                        if (isset($row_addresses_residencial[11])) {
300                                                if (isset($message->homestreet)) {
301                                                        $message->homestreet .= ":";
302                                                }
303                                                $message->homestreet .= utf8_encode($row_addresses_residencial[11]);
304                                        }
305                                }
306                                // Emails
307                                $result_emails = pg_query($this->db,"select cn.id_connection, connection_name, connection_value, connection_is_default from phpgw_cc_connections cn join phpgw_cc_contact_conns cc on (cn.id_connection = cc.id_connection) join phpgw_cc_typeof_ct_conns ct on (ct.id_typeof_contact_connection = cc.id_typeof_contact_connection) where ct.contact_connection_type_name = 'Email' and cc.id_contact = "  . $row_contact[0] . ";");
308                                if ($result_emails == FALSE) throw new Exception(pg_last_error($this->db));
309                                while ($row_emails = pg_fetch_row($result_emails)) {
310                                        if ($row_emails[1] == "Principal") {
311                                                $message->email1address = utf8_encode($row_emails[2]);
312                                        }
313                                        if ($row_emails[1] == "Alternativo") {
314                                                $message->email2address = utf8_encode($row_emails[2]);
315                                        }
316                                        //TODO : Atribuir o email3address. O Expresso ainda não tem campo para um terceiro email :(
317                                }
318                                // Telefones
319                                $result_tel = pg_query($this->db,"select cn.id_connection, connection_name, connection_value, connection_is_default from phpgw_cc_connections cn join phpgw_cc_contact_conns cc on (cn.id_connection = cc.id_connection) join phpgw_cc_typeof_ct_conns ct on (ct.id_typeof_contact_connection = cc.id_typeof_contact_connection) where ct.contact_connection_type_name = 'Telefone' and cc.id_contact = "  . $row_contact[0] . ";");
320                                if ($result_tel == FALSE) throw new Exception(pg_last_error($this->db));
321                                while ($row_tel = pg_fetch_row($result_tel)) {
322                                        if ($row_tel[1] == "Trabalho") {
323                                                $message->businessphonenumber = utf8_encode($row_tel[2]);
324                                        }
325                                        if ($row_tel[1] == "Casa") {
326                                                $message->homephonenumber = utf8_encode($row_tel[2]);
327                                        }
328                                        if ($row_tel[1] == "Celular") {
329                                                $message->mobilephonenumber = utf8_encode($row_tel[2]);
330                                        }
331                                        if ($row_tel[1] == "Fax") {
332                                                $message->businessfaxnumber = utf8_encode($row_tel[2]);
333                                        }
334                                        if ($row_tel[1] == "Principal") {
335                                                $message->business2phonenumber = utf8_encode($row_tel[2]);
336                                        }
337                                        if ($row_tel[1] == "Alternativo") {
338                                                $message->home2phonenumber = utf8_encode($row_tel[2]);
339                                        }
340                                        //TODO : Permitir mais de um número de telefone para Trabalho, Casa e Celular. O Expresso ainda não suporta isso :(
341                                }
342                        }
343                        $result = pg_query($this->db,"COMMIT;");
344                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
345                } catch (Exception $e) {
346                        pg_query($this->db,"ROLLBACK;");
347                        debugLog("exception -> " . $e->getMessage() . " - ARQUIVO: " . $e->getFile() . " - LINHA: " . $e->getLine());
348                        return;
349                }
350                return $message;
351        }
352
353        function DeleteMessage($folderid, $id) {
354                debugLog('ContactsExpresso::DeleteMessage('.$folderid.', '.$id.', ..)');
355
356                $id_owner = $this->parseOwner($this->_user);
357                if ($id_owner == false) return false;
358
359                $result = pg_query($this->db,"BEGIN;");
360                try {
361                        $result_contact = pg_query($this->db, "select id_contact from phpgw_cc_contact where id_contact = " . $id . ";");
362                        if ($result_contact == FALSE) throw new Exception(pg_last_error($this->db));
363                        while ($row_contact = pg_fetch_row($result_contact)) {
364                                if(isset($row_contact[0])) {
365                                        $id_contact = $row_contact[0];
366                                }
367                        }
368                        if(!isset($id_contact)){
369                                return true;
370                        }
371
372                        $result_contact_addrs = pg_query($this->db, "select id_address from phpgw_cc_contact_addrs where id_contact = " . $id_contact . ";");
373                        if ($result_contact_addrs == FALSE) throw new Exception(pg_last_error($this->db));
374                        while ($row_contact_addrs = pg_fetch_row($result_contact_addrs)) {
375                                if(isset($row_contact_addrs[0])) {
376                                        $id_address = $row_contact_addrs[0];
377                                        $result_delete_address = pg_delete($this->db, "phpgw_cc_addresses", array('id_address' => $id_address));
378                                        if ($result_delete_address == FALSE) throw new Exception(pg_last_error($this->db));
379                                }
380                        }
381
382                        $result_delete_contact_addrs = pg_delete($this->db, "phpgw_cc_contact_addrs", array('id_contact' => $id_contact));
383                        if ($result_delete_contact_addrs == FALSE) throw new Exception(pg_last_error($this->db));
384
385                        $result_contact_conns = pg_query($this->db, "select id_connection from phpgw_cc_contact_conns where id_contact = " . $id_contact . ";");
386                        if ($result_contact_conns == FALSE) throw new Exception(pg_last_error($this->db));
387                        while ($row_contact_conns = pg_fetch_row($result_contact_conns)) {
388                                if(isset($row_contact_conns[0])) {
389                                        $id_connection = $row_contact_conns[0];
390                                        $result_delete_connections = pg_delete($this->db, "phpgw_cc_connections", array('id_connection' => $id_connection));
391                                        if ($result_delete_connections == FALSE) throw new Exception(pg_last_error($this->db));
392                                        $result_delete_contact_grps = pg_delete($this->db,"phpgw_cc_contact_grps", array('id_connection' => $id_connection));
393                                        if ($result_delete_contact_grps == FALSE) throw new Exception(pg_last_error($this->db));
394                                }
395                        }
396
397                        $result_delete_contact_conns = pg_delete($this->db, "phpgw_cc_contact_conns", array('id_contact' => $id_contact));
398                        if ($result_delete_contact_conns == FALSE) throw new Exception(pg_last_error($this->db));
399                        $result_delete_contact = pg_delete($this->db, "phpgw_cc_contact", array('id_contact' => $id_contact));
400                        if ($result_delete_contact == FALSE) throw new Exception(pg_last_error($this->db));
401                        $result = pg_query($this->db,"COMMIT;");
402                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
403                } catch (Exception $e) {
404                        pg_query($this->db,"ROLLBACK;");
405                        debugLog("exception -> " . $e->getMessage() . " - ARQUIVO: " . $e->getFile() . " - LINHA: " . $e->getLine());
406                        return false;
407                }
408                return true;
409        }
410
411        function SetReadFlag($folderid, $id, $flags) {
412                return false;
413        }
414
415        function ChangeMessage($folderid, $id, $message) {
416                debugLog('ContactsExpresso::ChangeMessage('.$folderid.', '.$id.', ..)');
417
418                $id_owner = $this->parseOwner($this->_user);
419                if ($id_owner == false) return false;
420                try {
421                        $result = pg_query($this->db,"BEGIN;");
422                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
423
424                        // Obtem o id_contact
425                        $found_id_contact = false;
426                        if (trim($id) !== "") {
427                                $result_contact = pg_query($this->db, "select id_contact from phpgw_cc_contact where id_contact = " . $id . ";");
428                                if ($result_contact == FALSE) throw new Exception(pg_last_error($this->db));
429                                // tenta localizar id_contact para fazer Update
430                                while ($row_contact = pg_fetch_row($result_contact)) {
431                                        if(isset($row_contact[0])) {
432                                                $id_contact = $row_contact[0];
433                                                $found_id_contact = true;
434                                        }
435                                }
436                        }
437                        // se não encontrou id_contact para fazer Update, define o próximo id_contact para fazer Insert
438                        if (!isset($id_contact)) {
439                                $result = pg_query($this->db,"LOCK TABLE phpgw_cc_contact IN ACCESS EXCLUSIVE MODE;");
440                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
441                                $result_contact_max_id = pg_query($this->db, "select max(id_contact) from phpgw_cc_contact;");
442                                if ($result_contact_max_id == FALSE) throw new Exception(pg_last_error($this->db));
443                                $row_contact_max_id = pg_fetch_row($result_contact_max_id);
444                                if (isset($row_contact_max_id[0])) {
445                                        $id_contact = $row_contact_max_id[0] + 1;
446                                } else $id_contact = 1;
447                        }
448
449                        // Procura o id_address comercial e residencial
450                        $result_address = pg_query($this->db, "select co.id_address, tca.contact_address_type_name from phpgw_cc_addresses co join phpgw_cc_contact_addrs ca on (co.id_address = ca.id_address) join phpgw_cc_typeof_ct_addrs tca on (ca.id_typeof_contact_address = tca.id_typeof_contact_address) where ca.id_contact = " . $id_contact . ";");
451                        if ($result_address == FALSE) throw new Exception(pg_last_error($this->db));
452                        $found_id_address_comercial = false;
453                        $found_id_address_residencial = false;
454                        while ($row_address = pg_fetch_row($result_address)) {
455                                if(isset($row_address[0])) {
456                                        if ($row_address[1] == "Comercial") {
457                                                $id_address_comercial = $row_address[0];
458                                                $found_id_address_comercial = true;
459                                        }
460                                        if ($row_address[1] == "Residencial") {
461                                                $id_address_residencial = $row_address[0];
462                                                $found_id_address_residencial = true;
463                                        }
464                                }
465                        }
466                        // Verifica se os campos de endereco estao preenchidos na mensagem recebida do dispositivo movel
467                        $isset_business_address_fields = false;
468                        if(isset($message->businessstate) or isset($message->businesspostalcode) or isset($message->businessstreet)) {
469                                $isset_business_address_fields = true;
470                        }
471                        $isset_home_address_fields = false;
472                        if(isset($message->homestate) or isset($message->homepostalcode) or isset($message->homestreet)) {
473                                $isset_home_address_fields = true;
474                        }
475                        // Obtem o ultimo id_address
476                        if (!($found_id_address_comercial and $found_id_address_residencial) and ($isset_business_address_fields or $isset_home_address_fields)) {
477                                $result = pg_query($this->db,"LOCK TABLE phpgw_cc_addresses IN ACCESS EXCLUSIVE MODE;");
478                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
479                                $result_address_max_id = pg_query($this->db, "select max(id_address) from phpgw_cc_addresses;");
480                                if ($result_address_max_id == FALSE) throw new Exception(pg_last_error($this->db));
481                                $array_row_address_max_id = pg_fetch_row($result_address_max_id);
482                                if (isset($array_row_address_max_id)) $row_address_max_id = $array_row_address_max_id[0];
483                                if (!isset($row_address_max_id)) $row_address_max_id = 0;
484                        }
485                        $next_offset_address_to_insert = 0;
486                        // se não encontrou id_address_comercial para fazer Update, define o próximo id_address_comercial para fazer Insert
487                        if (!$found_id_address_comercial and $isset_business_address_fields) {
488                                $next_offset_address_to_insert += 1;
489                                $id_address_comercial = $row_address_max_id + $next_offset_address_to_insert;
490                        }
491                        // se não encontrou id_address_residencial para fazer Update, define o próximo id_address_residencial para fazer Insert
492                        if (!$found_id_address_residencial and $isset_home_address_fields) {
493                                $next_offset_address_to_insert += 1;
494                                $id_address_residencial = $row_address_max_id + $next_offset_address_to_insert;
495                        }
496
497                        // Procura o id_connection de Emails e Telefones
498                        $result_connection = pg_query($this->db, "select cn.id_connection, connection_name, connection_is_default, cc.id_typeof_contact_connection from phpgw_cc_connections cn join phpgw_cc_contact_conns cc on (cn.id_connection = cc.id_connection) join phpgw_cc_typeof_ct_conns ct on (ct.id_typeof_contact_connection = cc.id_typeof_contact_connection) where cc.id_contact = " . $id_contact . ";");
499                        if ($result_connection == FALSE) throw new Exception(pg_last_error($this->db));
500                        $found_id_connection_email_principal = false;
501                        $found_id_connection_email_alternativo = false;
502                        $found_id_connection_tel_trabalho = false;
503                        $found_id_connection_tel_casa = false;
504                        $found_id_connection_tel_celular = false;
505                        $found_id_connection_tel_fax = false;
506                        $found_id_connection_tel_principal = false;
507                        $found_id_connection_tel_alternativo = false;
508                        $tel_default = "";
509                        $email_default = "";
510                        while ($row_connection = pg_fetch_row($result_connection)) {
511                                if(isset($row_connection[0])) {
512                                        if ($row_connection[1] == "Principal" and $row_connection[3] == 1) {
513                                                $id_connection_email_principal = $row_connection[0];
514                                                $found_id_connection_email_principal = true;
515                                                if ($row_connection[2] == 't') $email_default = "Principal";
516                                        }
517                                        if ($row_connection[1] == "Alternativo" and $row_connection[3] == 1) {
518                                                $id_connection_email_alternativo = $row_connection[0];
519                                                $found_id_connection_email_alternativo = true;
520                                                if ($row_connection[2] == 't') $email_default = "Alternativo";
521                                        }
522                                        if ($row_connection[1] == "Trabalho") {
523                                                $id_connection_tel_trabalho = $row_connection[0];
524                                                $found_id_connection_tel_trabalho = true;
525                                                if ($row_connection[2] == 't') $tel_default = "Trabalho";
526                                        }
527                                        if ($row_connection[1] == "Casa") {
528                                                $id_connection_tel_casa = $row_connection[0];
529                                                $found_id_connection_tel_casa = true;
530                                                if ($row_connection[2] == 't') $tel_default = "Casa";
531                                        }
532                                        if ($row_connection[1] == "Celular") {
533                                                $id_connection_tel_celular = $row_connection[0];
534                                                $found_id_connection_tel_celular = true;
535                                                if ($row_connection[2] == 't') $tel_default = "Celular";
536                                        }
537                                        if ($row_connection[1] == "Fax") {
538                                                $id_connection_tel_fax = $row_connection[0];
539                                                $found_id_connection_tel_fax = true;
540                                                if ($row_connection[2] == 't') $tel_default = "Fax";
541                                        }
542                                        if ($row_connection[1] == "Principal" and $row_connection[3] == 2) {
543                                                $id_connection_tel_principal = $row_connection[0];
544                                                $found_id_connection_tel_principal = true;
545                                                if ($row_connection[2] == 't') $tel_default = "Principal";
546                                        }
547                                        if ($row_connection[1] == "Alternativo" and $row_connection[3] == 2) {
548                                                $id_connection_tel_alternativo = $row_connection[0];
549                                                $found_id_connection_tel_alternativo = true;
550                                                if ($row_connection[2] == 't') $tel_default = "Alternativo";
551                                        }
552                                }
553                        }
554                        // Obtem o ultimo id_connection
555                        if (!($found_id_connection_email_principal and $found_id_connection_email_alternativo and $found_id_connection_tel_trabalho and $found_id_connection_tel_celular and $found_id_connection_tel_casa and $found_id_connection_tel_fax and $found_id_connection_tel_principal and $found_id_connection_tel_alternativo) and (isset($message->email1address) or isset($message->email2address) or isset($message->businessphonenumber) or isset($message->homephonenumber) or isset($message->mobilephonenumber) or isset($message->businessfaxnumber) or isset($message->business2phonenumber) or isset($message->home2phonenumber))){
556                                $result = pg_query($this->db,"LOCK TABLE phpgw_cc_connections IN ACCESS EXCLUSIVE MODE;");
557                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
558                                $result_connection_max_id = pg_query($this->db, "select max(id_connection) from phpgw_cc_connections;");
559                                if ($result_connection_max_id == FALSE) throw new Exception(pg_last_error($this->db));
560                                $array_row_connection_max_id = pg_fetch_row($result_connection_max_id);
561                                if (isset($array_row_connection_max_id)) $row_connection_max_id = $array_row_connection_max_id[0];
562                                if (!isset($row_connection_max_id)) $row_connection_max_id = 0;
563                        }
564                        $next_offset_connection_to_insert = 0;
565                        // se não encontrou id_connection_email_principal para fazer Update, define o próximo id_connection_email_principal para fazer Insert
566                        if (!$found_id_connection_email_principal and isset($message->email1address)) {
567                                $next_offset_connection_to_insert += 1;
568                                $id_connection_email_principal = $row_connection_max_id + $next_offset_connection_to_insert;
569                        }
570                        // se não encontrou id_connection_email_alternativo para fazer Update, define o próximo id_connection_email_alternativo para fazer Insert
571                        if (!$found_id_connection_email_alternativo and isset($message->email2address)) {
572                                $next_offset_connection_to_insert += 1;
573                                $id_connection_email_alternativo = $row_connection_max_id + $next_offset_connection_to_insert;
574                        }
575                        // se não encontrou $id_connection_tel_trabalho para fazer Update, define o próximo $id_connection_tel_trabalho para fazer Insert
576                        if (!$found_id_connection_tel_trabalho and isset($message->businessphonenumber)) {
577                                $next_offset_connection_to_insert += 1;
578                                $id_connection_tel_trabalho = $row_connection_max_id + $next_offset_connection_to_insert;
579                        }
580                        // se não encontrou $id_connection_tel_casa para fazer Update, define o próximo $id_connection_tel_casa para fazer Insert
581                        if (!$found_id_connection_tel_casa and isset($message->homephonenumber)) {
582                                $next_offset_connection_to_insert += 1;
583                                $id_connection_tel_casa = $row_connection_max_id + $next_offset_connection_to_insert;
584                        }
585                        // se não encontrou $id_connection_tel_celular para fazer Update, define o próximo $id_connection_tel_celular para fazer Insert
586                        if (!$found_id_connection_tel_celular and isset($message->mobilephonenumber)) {
587                                $next_offset_connection_to_insert += 1;
588                                $id_connection_tel_celular = $row_connection_max_id + $next_offset_connection_to_insert;
589                        }
590                        // se não encontrou $id_connection_tel_fax para fazer Update, define o próximo $id_connection_tel_fax para fazer Insert
591                        if (!$found_id_connection_tel_fax and isset($message->businessfaxnumber)) {
592                                $next_offset_connection_to_insert += 1;
593                                $id_connection_tel_fax = $row_connection_max_id + $next_offset_connection_to_insert;
594                        }
595                        // se não encontrou $id_connection_tel_principal para fazer Update, define o próximo $id_connection_tel_principal para fazer Insert
596                        if (!$found_id_connection_tel_principal and isset($message->business2phonenumber)) {
597                                $next_offset_connection_to_insert += 1;
598                                $id_connection_tel_principal = $row_connection_max_id + $next_offset_connection_to_insert;
599                        }
600                        // se não encontrou $id_connection_tel_alternativo para fazer Update, define o próximo $id_connection_tel_alternativo para fazer Insert
601                        if (!$found_id_connection_tel_alternativo and isset($message->home2phonenumber)) {
602                                $next_offset_connection_to_insert += 1;
603                                $id_connection_tel_alternativo = $row_connection_max_id + $next_offset_connection_to_insert;
604                        }
605
606                        // Incluir/Alterar registro na tabela phpgw_cc_contact no Banco de Dados
607                        //if(isset($message->picture)) {
608                        //      $arrayContact["photo"] = base64_decode($message->picture);
609                        //}
610                        if(isset($message->nickname)) {
611                                $arrayContact["alias"] = utf8_decode($message->nickname);
612                        }
613                        if(isset($message->firstname)) {
614                                $arrayContact["given_names"] = utf8_decode($message->firstname);
615                                $arrayContact["names_ordered"] = utf8_decode($message->firstname);
616                        }
617                        if (isset($message->middlename)) {
618                                $arrayContact["family_names"] = utf8_decode($message->middlename);
619                                if(isset($message->firstname)) {
620                                        $arrayContact["names_ordered"] .= " ";
621                                }
622                                $arrayContact["names_ordered"] .= utf8_decode($message->middlename);
623                        }
624                        if(isset($message->lastname)) {
625                                if(isset($message->middlename)) {
626                                        $arrayContact["family_names"] .= " ";
627                                }
628                                if (isset($arrayContact["family_names"])) $arrayContact["family_names"] .= utf8_decode($message->lastname);
629                                else $arrayContact["family_names"] = utf8_decode($message->lastname);
630                                if(isset($message->firstname) or isset($message->middlename)) {
631                                        $arrayContact["names_ordered"] .= " ";
632                                }
633                                $arrayContact["names_ordered"] .= utf8_decode($message->lastname);
634                        }
635                        if (isset($message->birthday)) {
636                                $tz = date_default_timezone_get();
637                                date_default_timezone_set('UTC');
638                                $arrayContact["birthdate"] = date("Y-m-d",$message->birthday);
639                                date_default_timezone_set($tz);
640                        }
641                        //TODO: Incluir o campo de Aniversario na Sincronizacao. O DB do Expresso nao tem esse campo :-(
642
643                        if(isset($message->rtf)) {
644                                $rtf_to_ascii = new rtf();
645                                $rtf_to_ascii->output("ascii");
646                                $result_loadrtf = $rtf_to_ascii->loadrtf(base64_decode($message->rtf));
647                                if ($result_loadrtf == true) $rtf_to_ascii->parse();
648                                $arrayContact["notes"] = $rtf_to_ascii->out;
649                        }
650                        //TODO: Tratar o conteudo do campo de categorias
651                        //if(isset($message->categories)) {
652                        //      $arrayContact["category"] = $this->truncateString(utf8_decode($message->categories),20);
653                        //}
654                        if(isset($message->webpage)) {
655                                $arrayContact["web_page"] = $this->truncateString(utf8_decode($message->webpage),100);
656                        }
657                        if(isset($message->companyname)) {
658                                $arrayContact["corporate_name"] = $this->truncateString(utf8_decode($message->companyname),100);
659                        }
660                        if(isset($message->jobtitle)) {
661                                $arrayContact["job_title"] = $this->truncateString(utf8_decode($message->jobtitle),40);
662                        }
663                        if(isset($message->department)) {
664                                $arrayContact["department"] = $this->truncateString(utf8_decode($message->department),30);
665                        }
666                        if (!$found_id_contact){
667                                $arrayContact["id_contact"] = $id_contact;
668                                $arrayContact["id_owner"] = $id_owner;
669                                $result = pg_insert($this->db, 'phpgw_cc_contact', $arrayContact);
670                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
671                        } else {
672                                $result = pg_update($this->db, 'phpgw_cc_contact', $arrayContact, array('id_contact' => $id_contact));
673                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
674                        }
675
676                        // Incluir/Alterar Endereco Comercial na tabela phpgw_cc_addresses no Banco de Dados
677                        if(isset($message->businessstate)) {
678                                $result = pg_query($this->db, "select id_state, id_country from phpgw_cc_state where LOWER(to_ASCII(state_name)) = '" . trim(strtolower($this->removeAccents(utf8_decode($message->businessstate)))) . "' or LOWER(to_ASCII(state_symbol)) = '" . trim(strtolower($this->removeAccents(utf8_decode($message->businessstate)))) . "';");
679                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
680                                $row = pg_fetch_row($result);
681                                if(isset($row[0])) {
682                                        $arrayAddressComercial["id_state"] = $row[0];
683                                }
684                                if(isset($row[1])) {
685                                        $arrayAddressComercial["id_country"] = $row[1];
686                                }
687                                if(isset($message->businesscity) and isset($arrayAddressComercial["id_state"])) {
688                                        $result = pg_query($this->db, "select id_city from phpgw_cc_city where id_state = " . $arrayAddressComercial["id_state"] . " and LOWER(to_ASCII(city_name)) = '" . trim(strtolower($this->removeAccents(utf8_decode($message->businesscity)))) . "';");
689                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
690                                        $row = pg_fetch_row($result);
691                                        if(isset($row[0])) {
692                                                $arrayAddressComercial["id_city"] = $row[0];
693                                        }
694                                }
695                        }
696                        if(isset($message->businesspostalcode)) {
697                                $arrayAddressComercial["postal_code"] = $this->truncateString(utf8_decode($message->businesspostalcode),15);
698                        }
699                        if(isset($message->businessstreet)) {
700                                $arrayAddressComercial["address1"] = $this->truncateString(utf8_decode($message->businessstreet),60);
701                        }
702                        if($isset_business_address_fields) {
703                                if (!$found_id_address_comercial) {
704                                        $arrayAddressComercial["id_address"] = $id_address_comercial;
705                                        $result = pg_insert($this->db, 'phpgw_cc_addresses', $arrayAddressComercial);
706                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
707                                        $arrayContactAddressComercial["id_contact"] = $id_contact;
708                                        $arrayContactAddressComercial["id_address"] = $id_address_comercial;
709                                        $arrayContactAddressComercial["id_typeof_contact_address"] = 1; //comercial
710                                        $result = pg_insert($this->db, 'phpgw_cc_contact_addrs', $arrayContactAddressComercial);
711                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
712                                } else {
713                                        $result = pg_update($this->db, 'phpgw_cc_addresses', $arrayAddressComercial, array('id_address' => $id_address_comercial));
714                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
715                                }
716                        } elseif ($found_id_address_comercial) {
717                                $result = pg_delete($this->db, "phpgw_cc_contact_addrs", array('id_contact' => $id_contact, 'id_address' => $id_address_comercial));
718                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
719                                $result = pg_delete($this->db, "phpgw_cc_addresses", array('id_address' => $id_address_comercial));
720                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
721                        }
722
723                        // Incluir/Alterar Endereco Residencial na tabela phpgw_cc_addresses no Banco de Dados
724                        if(isset($message->homestate)) {
725                                $result = pg_query($this->db, "select id_state, id_country from phpgw_cc_state where LOWER(to_ASCII(state_name)) = '" . trim(strtolower($this->removeAccents(utf8_decode($message->homestate)))) . "' or LOWER(to_ASCII(state_symbol)) = '" . trim(strtolower($this->removeAccents(utf8_decode($message->homestate)))) . "';");
726                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
727                                $row = pg_fetch_row($result);
728                                if(isset($row[0])) {
729                                        $arrayAddressResidencial["id_state"] = $row[0];
730                                }
731                                if(isset($row[1])) {
732                                        $arrayAddressResidencial["id_country"] = $row[1];
733                                }
734                                if(isset($message->homecity) and isset($arrayAddressResidencial["id_state"])) {
735                                        $result = pg_query($this->db, "select id_city from phpgw_cc_city where id_state = " . $arrayAddressResidencial["id_state"] . " and LOWER(to_ASCII(city_name)) = '" . trim(strtolower($this->removeAccents(utf8_decode($message->homecity)))) . "';");
736                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
737                                        $row = pg_fetch_row($result);
738                                        if(isset($row[0])) {
739                                                $arrayAddressResidencial["id_city"] = $row[0];
740                                        }
741                                }
742                        }
743                        if(isset($message->homepostalcode)) {
744                                $arrayAddressResidencial["postal_code"] = $this->truncateString(utf8_decode($message->homepostalcode),15);
745                        }
746                        if(isset($message->homestreet)) {
747                                $arrayAddressResidencial["address1"] = $this->truncateString(utf8_decode($message->homestreet),60);
748                        }
749                        if($isset_home_address_fields) {
750                                if (!$found_id_address_residencial) {
751                                        $arrayAddressResidencial["id_address"] = $id_address_residencial;
752                                        $result = pg_insert($this->db, 'phpgw_cc_addresses', $arrayAddressResidencial);
753                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
754                                        $arrayContactAddressResidencial["id_contact"] = $id_contact;
755                                        $arrayContactAddressResidencial["id_address"] = $id_address_residencial;
756                                        $arrayContactAddressResidencial["id_typeof_contact_address"] = 2; //residencial
757                                        $result = pg_insert($this->db, 'phpgw_cc_contact_addrs', $arrayContactAddressResidencial);
758                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
759                                } else {
760                                        $result = pg_update($this->db, 'phpgw_cc_addresses', $arrayAddressResidencial, array('id_address' => $id_address_residencial));
761                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
762                                }
763                        } elseif ($found_id_address_residencial) {
764                                $result = pg_delete($this->db, "phpgw_cc_contact_addrs", array('id_contact' => $id_contact, 'id_address' => $id_address_residencial));
765                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
766                                $result = pg_delete($this->db, "phpgw_cc_addresses", array('id_address' => $id_address_residencial));
767                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
768                        }
769
770                        // Email Principal
771                        if(isset($message->email1address)) {
772                                $arrayConnectionEmailPrincipal["connection_value"] = $this->truncateString(utf8_decode($message->email1address),100);
773                                if (!$found_id_connection_email_principal){
774                                        $arrayConnectionEmailPrincipal["id_connection"] = $id_connection_email_principal;
775                                        $arrayConnectionEmailPrincipal["connection_name"] = "Principal";
776                                        if ($email_default != "Alternativo"){
777                                                $arrayConnectionEmailPrincipal["connection_is_default"] = 't';
778                                                $email_default = "Principal";
779                                        } else {
780                                                $arrayConnectionEmailPrincipal["connection_is_default"] = 'f';
781                                        }
782                                        $result = pg_insert($this->db, 'phpgw_cc_connections', $arrayConnectionEmailPrincipal);
783                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
784                                        $arrayContactConnection["id_contact"] = $id_contact;
785                                        $arrayContactConnection["id_connection"] = $id_connection_email_principal;
786                                        $arrayContactConnection["id_typeof_contact_connection"] = 1;
787                                        $result = pg_insert($this->db, 'phpgw_cc_contact_conns', $arrayContactConnection);
788                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
789                                } else {
790                                        $result = pg_update($this->db, 'phpgw_cc_connections', $arrayConnectionEmailPrincipal, array('id_connection' => $id_connection_email_principal));
791                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
792                                }
793
794                        } elseif ($found_id_connection_email_principal) {
795                                $result = pg_delete($this->db, "phpgw_cc_contact_conns", array('id_contact' => $id_contact, 'id_connection' => $id_connection_email_principal));
796                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
797                                $result = pg_delete($this->db, "phpgw_cc_contact_grps", array('id_connection' => $id_connection_email_principal));
798                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
799                                $result = pg_delete($this->db, "phpgw_cc_connections", array('id_connection' => $id_connection_email_principal));
800                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
801                        }
802
803                        // Email Alternativo
804                        if(isset($message->email2address)) {
805                                $arrayConnectionEmailAlternativo["connection_value"] = $this->truncateString(utf8_decode($message->email2address),100);
806                                if (!$found_id_connection_email_alternativo){
807                                        $arrayConnectionEmailAlternativo["id_connection"] = $id_connection_email_alternativo;
808                                        $arrayConnectionEmailAlternativo["connection_name"] = "Alternativo";
809                                        if ($email_default != "Principal"){
810                                                $arrayConnectionEmailAlternativo["connection_is_default"] = 't';
811                                                $email_default = "Alternativo";
812                                        } else {
813                                                $arrayConnectionEmailAlternativo["connection_is_default"] = 'f';
814                                        }
815                                        $result = pg_insert($this->db, 'phpgw_cc_connections', $arrayConnectionEmailAlternativo);
816                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
817                                        $arrayContactConnection["id_contact"] = $id_contact;
818                                        $arrayContactConnection["id_connection"] = $id_connection_email_alternativo;
819                                        $arrayContactConnection["id_typeof_contact_connection"] = 1;
820                                        $result = pg_insert($this->db, 'phpgw_cc_contact_conns', $arrayContactConnection);
821                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
822                                } else {
823                                        $result = pg_update($this->db, 'phpgw_cc_connections', $arrayConnectionEmailAlternativo, array('id_connection' => $id_connection_email_alternativo));
824                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
825                                }
826
827                        } elseif ($found_id_connection_email_alternativo) {
828                                $result = pg_delete($this->db, "phpgw_cc_contact_conns", array('id_contact' => $id_contact, 'id_connection' => $id_connection_email_alternativo));
829                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
830                                $result = pg_delete($this->db, "phpgw_cc_contact_grps", array('id_connection' => $id_connection_email_alternativo));
831                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
832                                $result = pg_delete($this->db, "phpgw_cc_connections", array('id_connection' => $id_connection_email_alternativo));
833                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
834                        }
835                        //TODO : Atribuir o email3address. O Expresso ainda não tem campo para um terceiro email :(
836
837                        // Telefone Trabalho
838                        if(isset($message->businessphonenumber)) {
839                                $arrayConnectionTelTrabalho["connection_value"] = $this->truncateString(utf8_decode($message->businessphonenumber),100);
840                                if (!$found_id_connection_tel_trabalho){
841                                        $arrayConnectionTelTrabalho["id_connection"] = $id_connection_tel_trabalho;
842                                        $arrayConnectionTelTrabalho["connection_name"] = "Trabalho";
843                                        if ($tel_default != "Celular" and $tel_default != "Casa" and $tel_default != "Fax"  and $tel_default != "Principal"  and $tel_default != "Alternativo"){
844                                                $arrayConnectionTelTrabalho["connection_is_default"] = 't';
845                                                $tel_default = "Trabalho";
846                                        } else {
847                                                $arrayConnectionTelTrabalho["connection_is_default"] = 'f';
848                                        }
849                                        $result = pg_insert($this->db, 'phpgw_cc_connections', $arrayConnectionTelTrabalho);
850                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
851                                        $arrayContactConnection["id_contact"] = $id_contact;
852                                        $arrayContactConnection["id_connection"] = $id_connection_tel_trabalho;
853                                        $arrayContactConnection["id_typeof_contact_connection"] = 2;
854                                        $result = pg_insert($this->db, 'phpgw_cc_contact_conns', $arrayContactConnection);
855                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
856                                } else {
857                                        $result = pg_update($this->db, 'phpgw_cc_connections', $arrayConnectionTelTrabalho, array('id_connection' => $id_connection_tel_trabalho));
858                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
859                                }
860
861                        } elseif ($found_id_connection_tel_trabalho) {
862                                $result = pg_delete($this->db, "phpgw_cc_contact_conns", array('id_contact' => $id_contact, 'id_connection' => $id_connection_tel_trabalho));
863                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
864                                $result = pg_delete($this->db, "phpgw_cc_contact_grps", array('id_connection' => $id_connection_tel_trabalho));
865                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
866                                $result = pg_delete($this->db, "phpgw_cc_connections", array('id_connection' => $id_connection_tel_trabalho));
867                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
868                        }
869
870                        // Telefone Celular
871                        if(isset($message->mobilephonenumber)) {
872                                $arrayConnectionTelCelular["connection_value"] = $this->truncateString(utf8_decode($message->mobilephonenumber),100);
873                                if (!$found_id_connection_tel_celular){
874                                        $arrayConnectionTelCelular["id_connection"] = $id_connection_tel_celular;
875                                        $arrayConnectionTelCelular["connection_name"] = "Celular";
876                                        if ($tel_default != "Trabalho" and $tel_default != "Casa" and $tel_default != "Fax"   and $tel_default != "Principal"  and $tel_default != "Alternativo"){
877                                                $arrayConnectionTelCelular["connection_is_default"] = 't';
878                                                $tel_default = "Celular";
879                                        } else {
880                                                $arrayConnectionTelCelular["connection_is_default"] = 'f';
881                                        }
882                                        $result = pg_insert($this->db, 'phpgw_cc_connections', $arrayConnectionTelCelular);
883                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
884                                        $arrayContactConnection["id_contact"] = $id_contact;
885                                        $arrayContactConnection["id_connection"] = $id_connection_tel_celular;
886                                        $arrayContactConnection["id_typeof_contact_connection"] = 2;
887                                        $result = pg_insert($this->db, 'phpgw_cc_contact_conns', $arrayContactConnection);
888                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
889                                } else {
890                                        $result = pg_update($this->db, 'phpgw_cc_connections', $arrayConnectionTelCelular, array('id_connection' => $id_connection_tel_celular));
891                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
892                                }
893
894                        } elseif ($found_id_connection_tel_celular) {
895                                $result = pg_delete($this->db, "phpgw_cc_contact_conns", array('id_contact' => $id_contact, 'id_connection' => $id_connection_tel_celular));
896                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
897                                $result = pg_delete($this->db, "phpgw_cc_contact_grps", array('id_connection' => $id_connection_tel_celular));
898                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
899                                $result = pg_delete($this->db, "phpgw_cc_connections", array('id_connection' => $id_connection_tel_celular));
900                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
901                        }
902
903                        // Telefone Casa
904                        if(isset($message->homephonenumber)) {
905                                $arrayConnectionTelCasa["connection_value"] = $this->truncateString(utf8_decode($message->homephonenumber),100);
906                                if (!$found_id_connection_tel_casa){
907                                        $arrayConnectionTelCasa["id_connection"] = $id_connection_tel_casa;
908                                        $arrayConnectionTelCasa["connection_name"] = "Casa";
909                                        if ($tel_default != "Trabalho" and $tel_default != "Celular" and $tel_default != "Fax" and $tel_default != "Principal"  and $tel_default != "Alternativo"){
910                                                $arrayConnectionTelCasa["connection_is_default"] = 't';
911                                                $tel_default = "Casa";
912                                        } else {
913                                                $arrayConnectionTelCasa["connection_is_default"] = 'f';
914                                        }
915                                        $result = pg_insert($this->db, 'phpgw_cc_connections', $arrayConnectionTelCasa);
916                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
917                                        $arrayContactConnection["id_contact"] = $id_contact;
918                                        $arrayContactConnection["id_connection"] = $id_connection_tel_casa;
919                                        $arrayContactConnection["id_typeof_contact_connection"] = 2;
920                                        $result = pg_insert($this->db, 'phpgw_cc_contact_conns', $arrayContactConnection);
921                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
922                                } else {
923                                        $result = pg_update($this->db, 'phpgw_cc_connections', $arrayConnectionTelCasa, array('id_connection' => $id_connection_tel_casa));
924                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
925                                }
926
927                        } elseif ($found_id_connection_tel_casa) {
928                                $result = pg_delete($this->db, "phpgw_cc_contact_conns", array('id_contact' => $id_contact, 'id_connection' => $id_connection_tel_casa));
929                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
930                                $result = pg_delete($this->db, "phpgw_cc_contact_grps", array('id_connection' => $id_connection_tel_casa));
931                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
932                                $result = pg_delete($this->db, "phpgw_cc_connections", array('id_connection' => $id_connection_tel_casa));
933                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
934                        }
935
936                        // Fax
937                        if(isset($message->businessfaxnumber)) {
938                                $arrayConnectionFax["connection_value"] = $this->truncateString(utf8_decode($message->businessfaxnumber),100);
939                                if (!$found_id_connection_tel_fax){
940                                        $arrayConnectionFax["id_connection"] = $id_connection_tel_fax;
941                                        $arrayConnectionFax["connection_name"] = "Fax";
942                                        if ($tel_default != "Trabalho" and $tel_default != "Celular" and $tel_default != "Casa" and $tel_default != "Principal"  and $tel_default != "Alternativo"){
943                                                $arrayConnectionFax["connection_is_default"] = 't';
944                                                $tel_default = "Fax";
945                                        } else {
946                                                $arrayConnectionFax["connection_is_default"] = 'f';
947                                        }
948                                        $result = pg_insert($this->db, 'phpgw_cc_connections', $arrayConnectionFax);
949                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
950                                        $arrayContactConnection["id_contact"] = $id_contact;
951                                        $arrayContactConnection["id_connection"] = $id_connection_tel_fax;
952                                        $arrayContactConnection["id_typeof_contact_connection"] = 2;
953                                        $result = pg_insert($this->db, 'phpgw_cc_contact_conns', $arrayContactConnection);
954                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
955                                } else {
956                                        $result = pg_update($this->db, 'phpgw_cc_connections', $arrayConnectionFax, array('id_connection' => $id_connection_tel_fax));
957                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
958                                }
959
960                        } elseif ($found_id_connection_tel_fax) {
961                                $result = pg_delete($this->db, "phpgw_cc_contact_conns", array('id_contact' => $id_contact, 'id_connection' => $id_connection_tel_fax));
962                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
963                                $result = pg_delete($this->db, "phpgw_cc_contact_grps", array('id_connection' => $id_connection_tel_fax));
964                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
965                                $result = pg_delete($this->db, "phpgw_cc_connections", array('id_connection' => $id_connection_tel_fax));
966                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
967                        }
968
969                        // Telefone Principal
970                        if(isset($message->business2phonenumber)) {
971                                $arrayConnectionTelPrincipal["connection_value"] = $this->truncateString(utf8_decode($message->business2phonenumber),100);
972                                if (!$found_id_connection_tel_principal){
973                                        $arrayConnectionTelPrincipal["id_connection"] = $id_connection_tel_principal;
974                                        $arrayConnectionTelPrincipal["connection_name"] = "Principal";
975                                        if ($tel_default != "Celular" and $tel_default != "Casa" and $tel_default != "Fax"  and $tel_default != "Trabalho"  and $tel_default != "Alternativo"){
976                                                $arrayConnectionTelPrincipal["connection_is_default"] = 't';
977                                                $tel_default = "Principal";
978                                        } else {
979                                                $arrayConnectionTelPrincipal["connection_is_default"] = 'f';
980                                        }
981                                        $result = pg_insert($this->db, 'phpgw_cc_connections', $arrayConnectionTelPrincipal);
982                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
983                                        $arrayContactConnection["id_contact"] = $id_contact;
984                                        $arrayContactConnection["id_connection"] = $id_connection_tel_principal;
985                                        $arrayContactConnection["id_typeof_contact_connection"] = 2;
986                                        $result = pg_insert($this->db, 'phpgw_cc_contact_conns', $arrayContactConnection);
987                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
988                                } else {
989                                        $result = pg_update($this->db, 'phpgw_cc_connections', $arrayConnectionTelPrincipal, array('id_connection' => $id_connection_tel_principal));
990                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
991                                }
992
993                        } elseif ($found_id_connection_tel_principal) {
994                                $result = pg_delete($this->db, "phpgw_cc_contact_conns", array('id_contact' => $id_contact, 'id_connection' => $id_connection_tel_principal));
995                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
996                                $result = pg_delete($this->db, "phpgw_cc_contact_grps", array('id_connection' => $id_connection_tel_principal));
997                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
998                                $result = pg_delete($this->db, "phpgw_cc_connections", array('id_connection' => $id_connection_tel_principal));
999                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
1000                        }
1001
1002                        // Telefone Alternativo
1003                        if(isset($message->home2phonenumber)) {
1004                                $arrayConnectionTelAlternativo["connection_value"] = $this->truncateString(utf8_decode($message->home2phonenumber),100);
1005                                if (!$found_id_connection_tel_alternativo){
1006                                        $arrayConnectionTelAlternativo["id_connection"] = $id_connection_tel_alternativo;
1007                                        $arrayConnectionTelAlternativo["connection_name"] = "Alternativo";
1008                                        if ($tel_default != "Trabalho" and $tel_default != "Celular" and $tel_default != "Fax" and $tel_default != "Principal"  and $tel_default != "Casa"){
1009                                                $arrayConnectionTelAlternativo["connection_is_default"] = 't';
1010                                                $tel_default = "Alternativo";
1011                                        } else {
1012                                                $arrayConnectionTelAlternativo["connection_is_default"] = 'f';
1013                                        }
1014                                        $result = pg_insert($this->db, 'phpgw_cc_connections', $arrayConnectionTelAlternativo);
1015                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
1016                                        $arrayContactConnection["id_contact"] = $id_contact;
1017                                        $arrayContactConnection["id_connection"] = $id_connection_tel_alternativo;
1018                                        $arrayContactConnection["id_typeof_contact_connection"] = 2;
1019                                        $result = pg_insert($this->db, 'phpgw_cc_contact_conns', $arrayContactConnection);
1020                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
1021                                } else {
1022                                        $result = pg_update($this->db, 'phpgw_cc_connections', $arrayConnectionTelAlternativo, array('id_connection' => $id_connection_tel_alternativo));
1023                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
1024                                }
1025
1026                        } elseif ($found_id_connection_tel_alternativo) {
1027                                $result = pg_delete($this->db, "phpgw_cc_contact_conns", array('id_contact' => $id_contact, 'id_connection' => $id_connection_tel_alternativo));
1028                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
1029                                $result = pg_delete($this->db, "phpgw_cc_contact_grps", array('id_connection' => $id_connection_tel_alternativo));
1030                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
1031                                $result = pg_delete($this->db, "phpgw_cc_connections", array('id_connection' => $id_connection_tel_alternativo));
1032                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
1033                        }
1034                       
1035                        //TODO : Permitir mais de um número de telefone para Trabalho, Celular e Casa. O Expresso ainda não suporta isso :(
1036
1037                        $result = pg_query($this->db,"COMMIT;");
1038                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
1039                        if (!$id) {
1040                                $id = $id_contact;
1041                        }
1042                } catch (Exception $e) {
1043                        debugLog("exception -> " . $e->getMessage() . " - ARQUIVO: " . $e->getFile() . " - LINHA: " . $e->getLine());
1044                        pg_query($this->db,"ROLLBACK;");
1045                        return false;
1046                }
1047                return $this->StatMessage($folderid, $id);
1048        }
1049
1050        function MoveMessage($folderid, $id, $newfolderid) {
1051                return false;
1052        }
1053
1054        // -----------------------------------
1055
1056        function getPath() {
1057                return str_replace('%u', $this->_user, VCARDDIR_DIR);
1058        }
1059
1060        function escape($data){
1061                if (is_array($data)) {
1062                        foreach ($data as $key => $val) {
1063                                $data[$key] = $this->escape($val);
1064                        }
1065                        return $data;
1066                }
1067                $data = str_replace("\r\n", "\n", $data);
1068                $data = str_replace("\r", "\n", $data);
1069                $data = str_replace(array('\\', ';', ',', "\n"), array('\\\\', '\\;', '\\,', '\\n'), $data);
1070                //return u2wi($data);
1071                return $data;
1072        }
1073
1074        function unescape($data){
1075                $data = str_replace(array('\\\\', '\\;', '\\,', '\\n','\\N'),array('\\', ';', ',', "\n", "\n"),$data);
1076                return $data;
1077        }
1078
1079        function  removeAccents($data){
1080                $data = strtr($data,"ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÜÚÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùüúþÿ","aaaaaaaceeeeiiii noooooxouuutbaaaaaaaceeeeiiii nooooo/ouuuty");
1081                return $data;
1082        }
1083
1084        function truncateString($string, $size)
1085        {
1086                if(strlen($string) <= $size) return $string;
1087                else return substr($string, 0, $size - 1);
1088        }
1089
1090        function parseOwner($id_owner)
1091        {
1092                $pos = strripos($id_owner, '\\');
1093                if ($pos === false) {
1094                        if (strlen($id_owner) <= 2) return false;
1095                        else return substr($id_owner, 0, strlen($id_owner)-2);
1096                } else {
1097                        if (strlen($id_owner) <= $pos + 3) {
1098                                return false;
1099                        } else {
1100                                return substr(substr($id_owner, $pos + 1), 0, strlen(substr($id_owner, $pos + 1))-2);
1101                        }
1102                }
1103        }
1104};
1105?>
Note: See TracBrowser for help on using the repository browser.