TransactionState() == 1); if ( ! $in_transaction ) $qry->Begin(); if ( $exists ) { $sql = 'UPDATE addressbook_resource SET version=:version, uid=:uid, nickname=:nickname, fn=:fn, n=:name, note=:note, org=:org, url=:url, fburl=:fburl, caladruri=:caladruri, caluri=:caluri WHERE dav_id=:dav_id'; } else { $sql = 'INSERT INTO addressbook_resource ( dav_id, version, uid, nickname, fn, n, note, org, url, fburl, caladruri, caluri ) VALUES( :dav_id, :version, :uid, :nickname, :fn, :name, :note, :org, :url, :fburl, :caladruri, :caluri )'; } $params = array( ':dav_id' => $dav_id ); $wanted = array('VERSION' => true, 'UID' => true, 'NICKNAME' => true, 'FN' => true, 'N' => true, 'NOTE'=> true, 'ORG' => true, 'URL' => true, 'FBURL' => true, 'CALADRURI' => true, 'CALURI' => true); $properties = $this->GetProperties( $wanted ); foreach( $wanted AS $k => $v ) { $pname = ':' . strtolower($k); if ( $pname == ':n' ) $pname = ':name'; $params[$pname] = null; } foreach( $properties AS $k => $v ) { $pname = ':' . strtolower($v->Name()); if ( $pname == ':n' ) $pname = ':name'; if ( !isset($params[$pname]) /** @TODO: or this is one is in the user's language */ ) $params[$pname] = $v->Value(); } $qry->QDo( $sql, $params ); $this->WriteAddresses($dav_id); $this->WritePhones($dav_id); $this->WriteEmails($dav_id); if ( ! $in_transaction ) $qry->Commit(); } /** CREATE TABLE addressbook_address_adr ( dav_id INT8 NOT NULL REFERENCES caldav_data(dav_id) ON UPDATE CASCADE ON DELETE CASCADE, type TEXT, box_no TEXT, unit_no TEXT, street_address TEXT, locality TEXT, region TEXT, postcode TEXT, country TEXT, property TEXT -- The full text of the property ); */ function WriteAddresses( $dav_id ) { $addresses = $this->GetProperties('ADR'); $qry = new AwlQuery(); // Only run a local transaction if we're not in one already. $in_transaction = ($qry->TransactionState() == 1); if ( ! $in_transaction ) $qry->Begin(); $params = array( ':dav_id' => $dav_id ); $qry->QDo('DELETE FROM addressbook_address_adr WHERE dav_id = :dav_id', $params ); foreach( $addresses AS $adr ) { $params[':type'] = $adr->GetParameterValue('TYPE'); $address = explode(';',$adr->Value()); // We use @ to suppress the warnings here, because the NULL in the database suits us well. @$params[':box_no'] = $address[0]; @$params[':unit_no'] = $address[1]; @$params[':street_address'] = $address[2]; @$params[':locality'] = $address[3]; @$params[':region'] = $address[4]; @$params[':postcode'] = $address[5]; @$params[':country'] = $address[6]; $params[':property'] = $adr->Render(); $qry->QDo( 'INSERT INTO addressbook_address_adr (dav_id, type, box_no, unit_no, street_address, locality, region, postcode, country, property) VALUES( :dav_id, :type, :box_no, :unit_no, :street_address, :locality, :region, :postcode, :country, :property)', $params ); } if ( ! $in_transaction ) $qry->Commit(); } /** CREATE TABLE addressbook_address_tel ( dav_id INT8 NOT NULL REFERENCES caldav_data(dav_id) ON UPDATE CASCADE ON DELETE CASCADE, type TEXT, tel TEXT, property TEXT -- The full text of the property ); */ function WritePhones( $dav_id ) { $telephones = $this->GetProperties('TEL'); $qry = new AwlQuery(); // Only run a local transaction if we're not in one already. $in_transaction = ($qry->TransactionState() == 1); if ( ! $in_transaction ) $qry->Begin(); $params = array( ':dav_id' => $dav_id ); $qry->QDo('DELETE FROM addressbook_address_tel WHERE dav_id = :dav_id', $params ); foreach( $telephones AS $tel ) { $params[':type'] = $tel->GetParameterValue('TYPE'); if ( ! isset($params[':type']) ) $params[':type'] = 'voice'; $params[':tel'] = $tel->Value(); $params[':property'] = $tel->Render(); $qry->QDo( 'INSERT INTO addressbook_address_tel (dav_id, type, tel, property) VALUES( :dav_id, :type, :tel, :property)', $params ); } if ( ! $in_transaction ) $qry->Commit(); } /** CREATE TABLE addressbook_address_email ( dav_id INT8 NOT NULL REFERENCES caldav_data(dav_id) ON UPDATE CASCADE ON DELETE CASCADE, type TEXT, email TEXT, property TEXT -- The full text of the property ); */ function WriteEmails( $dav_id ) { $emails = $this->GetProperties('EMAIL'); $qry = new AwlQuery(); // Only run a local transaction if we're not in one already. $in_transaction = ($qry->TransactionState() == 1); if ( ! $in_transaction ) $qry->Begin(); $params = array( ':dav_id' => $dav_id ); $qry->QDo('DELETE FROM addressbook_address_email WHERE dav_id = :dav_id', $params ); foreach( $emails AS $email ) { $params[':type'] = $email->GetParameterValue('TYPE'); $params[':email'] = $email->Value(); $params[':property'] = $email->Render(); $qry->QDo( 'INSERT INTO addressbook_address_email (dav_id, type, email, property) VALUES( :dav_id, :type, :email, :property)', $params ); } if ( ! $in_transaction ) $qry->Commit(); } /**************** Incluido para funcionar com expresso ***************** ************************************************************************ ***********************************************************************/ function Write_expresso( $uidnumber, $exists = true ) { // Only run a local transaction if we're not in one already. $uid_value = $this->GetProperties('UID'); foreach($uid_value AS $G => $H) { $uid = preg_split('/@/', $H->Value()); } $qry = new AwlQuery( "select id_contact from phpgw_cc_contact where id_contact= :id AND id_owner = :owner ", array(':id' => $uid[0],':owner' => $uidnumber)); if( $qry->Exec() && $qry->rows() > 0 ) { $response_code = 200; $params = array(':id_contact' => $uid[0]); $sql = 'UPDATE phpgw_cc_contact SET photo=:photo, alias=:nickname, family_names =:ft, given_names=:name, names_ordered =:fn, birthdate =:bday, notes=:note WHERE id_contact=:id_contact'; } else { $response_code = 204; $qry = new AwlQuery( "select max(id_contact) from phpgw_cc_contact"); $qry->Exec(); $cont=$qry->fetch(); $params = array(':id_contact' => $cont->max + 1); $sql = 'INSERT INTO phpgw_cc_contact ( id_contact, id_owner, photo, alias, given_names, family_names, names_ordered, birthdate, notes ) VALUES ( :id_contact, :id_owner, :photo, :nickname, :name , :ft, :fn , :bday, :note)'; } $params[':id_owner']=$uidnumber; $wanted = array('PHOTO' => true,'NICKNAME' => true, 'FN' => true, 'N' => true, 'NOTE'=> true, 'BDAY' => true); $properties = $this->GetProperties( $wanted ); foreach( $wanted AS $k => $v ) { $pname = ':' . strtolower($k); if ( $pname == ':n' ) $pname = ':name'; $params[$pname] = null; } foreach( $properties AS $k => $v ) { $pname = ':' . strtolower($v->Name()); if ( $pname == ':n' ) $pname = ':name'; if ( $pname == ':name' ){ $nome=preg_split('/;/', $v->Value()); $params[':name']=$nome[1]; $params[':ft']=$nome[0]; continue; } if ( !isset($params[$pname]) /** @TODO: or this is one is in the user's language */ ) $params[$pname] = $v->Value(); } $qry = new AwlQuery( $sql, $params ); $qry->Exec(); if ( $response_code == 200) $id_contactt = $uid[0]; else $id_contactt = $cont->max + 1; $emails = $this->GetProperties('EMAIL'); $tels = $this->GetProperties('TEL'); if ( isset($emails) || isset($tels)) { $qry = new AwlQuery("select id_connection from phpgw_cc_contact_conns where id_contact= :id", array(':id' => $id_contactt )); if ($qry->Exec() && $qry->rows() > 0) { while( $idconns = $qry->Fetch() ) { $qryy = new AwlQuery("DELETE from phpgw_cc_contact_conns where id_contact= :id AND id_connection = :id_connection", array(':id' => $id_contactt ,':id_connection' => $idconns->id_connection)); $qryy->Exec(); $qryy = new AwlQuery("DELETE from phpgw_cc_connections where id_connection = :id_connection", array(':id_connection' => $idconns->id_connection)); $qryy->Exec(); } if (isset($emails)) { foreach( $emails AS $email ) { $params[':type'] = $email->GetParameterValue('TYPE'); $params[':email'] = $email->Value(); $qry = new AwlQuery( "select max(id_connection) from phpgw_cc_connections"); $qry->Exec(); $cont=$qry->fetch(); if ( strtoupper($params[':type']) == 'WORK' ) {$params[':type'] = "Principal"; $params[':default'] = true; } else if ( $params[':type'] == 'pref' ) {$params[':type'] = "Principal"; $params[':default'] = true; } else { $params[':default'] = false; $params[':type'] = "Alternativo"; } $params[':id_conns'] = $cont->max + 1; dbg_log_array( "PUTTTTTT", 'EMAILSSSS', $params, true ); $qry = new AwlQuery( 'INSERT INTO phpgw_cc_connections (id_connection, connection_name, connection_value, connection_is_default) VALUES( :id_conns, :type, :email, :default )', $params ); $qry->Exec(); $qry = new AwlQuery( 'INSERT INTO phpgw_cc_contact_conns (id_contact, id_connection, id_typeof_contact_connection) VALUES( :id_contact, :id_conns, 1)', array(':id_contact' => $id_contactt , ':id_conns' => $cont->max + 1)); $qry->Exec(); } } if (isset($tels)) { foreach( $tels AS $tel ) { $params[':type'] = $tel->GetParameterValue('TYPE'); if ( ! isset($params[':type']) ) $params[':type'] = 'voice'; $params[':tel'] = $tel->Value(); $params[':property'] = $tel->Render(); $qry = new AwlQuery( "select max(id_connection) from phpgw_cc_connections"); $qry->Exec(); $cont=$qry->fetch(); if ( strtoupper($params[':type']) == 'WORK') {$params[':type'] = "Trabalho"; $params[':default'] = true; } else if ( $params[':type'] == 'pref') {$params[':type'] = "Celular"; $params[':default'] = true; } else if ( strtoupper($params[':type']) == 'HOME') { $params[':default'] = false; $params[':type'] = "Casa"; } else if ( strtoupper($params[':type']) == 'CELL') { $params[':default'] = false; $params[':type'] = "Celular"; } else if ( strtoupper($params[':type']) == 'FAX') { $params[':default'] = false; $params[':type'] = "Fax"; } $params[':id_conns'] = $cont->max + 1; dbg_log_array( "PUTTTTTT", 'TELSSSSSSSSS', $params, true ); $qry = new AwlQuery( 'INSERT INTO phpgw_cc_connections (id_connection, connection_name, connection_value, connection_is_default) VALUES( :id_conns, :type, :tel, :default )', $params ); $qry->Exec(); $qry = new AwlQuery( 'INSERT INTO phpgw_cc_contact_conns (id_contact, id_connection, id_typeof_contact_connection) VALUES( :id_contact, :id_conns, 2)', array(':id_contact' => $id_contactt , ':id_conns' => $cont->max + 1)); $qry->Exec(); } } } else { if (isset($emails)) { foreach( $emails AS $email ) { $params[':type'] = $email->GetParameterValue('TYPE'); $params[':email'] = $email->Value(); $qry = new AwlQuery( "select max(id_connection) from phpgw_cc_connections"); $qry->Exec(); $cont=$qry->fetch(); if ( strtoupper($params[':type']) == 'WORK') {$params[':type'] = "Principal"; $params[':default'] = true; } else if ( $params[':type'] == 'pref') {$params[':type'] = "Principal"; $params[':default'] = true; } else { $params[':default'] = false; $params[':type'] = "Alternativo"; } $params[':id_conns'] = $cont->max + 1; dbg_log_array( "PUTTTTTT", 'EMAILSSSS INSERT', $params, true ); $qry = new AwlQuery( 'INSERT INTO phpgw_cc_connections (id_connection, connection_name, connection_value, connection_is_default) VALUES( :id_conns, :type, :email, :default )', $params ); $qry->Exec(); $qry = new AwlQuery( 'INSERT INTO phpgw_cc_contact_conns (id_contact, id_connection, id_typeof_contact_connection) VALUES( :id_contact, :id_conns, 1)', array(':id_contact' => $id_contactt , ':id_conns' => $cont->max + 1)); $qry->Exec(); } } if (isset($tels)) { foreach( $tels AS $tel ) { $params[':type'] = $tel->GetParameterValue('TYPE'); if ( ! isset($params[':type']) ) $params[':type'] = 'voice'; $params[':tel'] = $tel->Value(); $params[':property'] = $tel->Render(); $qry = new AwlQuery( "select max(id_connection) from phpgw_cc_connections"); $qry->Exec(); $cont=$qry->fetch(); if ( strtoupper($params[':type']) == 'WORK') {$params[':type'] = "Trabalho"; $params[':default'] = true; } else if ( $params[':type'] == 'pref') {$params[':type'] = "Celular"; $params[':default'] = true; } else if ( strtoupper($params[':type']) == 'HOME') { $params[':default'] = false; $params[':type'] = "Casa"; } else if ( strtoupper($params[':type']) == 'CELL') { $params[':default'] = false; $params[':type'] = "Celular"; } else if ( strtoupper($params[':type']) == 'FAX') { $params[':default'] = false; $params[':type'] = "Fax"; } $params[':id_conns'] = $cont->max + 1; dbg_log_array( "PUTTTTTT", 'TELSSSSS INSERT', $params, true ); $qry = new AwlQuery( 'INSERT INTO phpgw_cc_connections (id_connection, connection_name, connection_value, connection_is_default) VALUES( :id_conns, :type, :tel, :default )', $params ); $qry->Exec(); $qry = new AwlQuery( 'INSERT INTO phpgw_cc_contact_conns (id_contact, id_connection, id_typeof_contact_connection) VALUES( :id_contact, :id_conns, 2)', array(':id_contact' => $id_contactt , ':id_conns' => $cont->max + 1)); $qry->Exec(); } } } } $addresses = $this->GetProperties('ADR'); if ( isset($addresses)) { $qry = new AwlQuery("select id_address from phpgw_cc_contact_addrs where id_contact= :id", array(':id' => $id_contactt)); if ($qry->Exec() && $qry->rows() > 0) { while( $idconns = $qry->Fetch() ) { $qryy = new AwlQuery("DELETE from phpgw_cc_contact_addrs where id_contact= :id AND id_address = :id_connection", array(':id' => $id_contactt ,':id_connection' => $idconns->id_address)); $qryy->Exec(); $qryy = new AwlQuery("DELETE from phpgw_cc_addresses where id_address = :id_connection", array(':id_connection' => $idconns->id_address)); $qryy->Exec(); } foreach( $addresses AS $adr ) { $params[':type'] = $adr->GetParameterValue('TYPE'); $address = explode(';',$adr->Value()); // We use @ to suppress the warnings here, because the NULL in the database suits us well. @$params[':box_no'] = $address[0]; @$params[':unit_no'] = $address[1]; @$params[':street_address'] = $address[2]; @$params[':locality'] = $address[3]; @$params[':region'] = $address[4]; @$params[':postcode'] = $address[5]; @$params[':country'] = substr($address[6],0,2); $qry = new AwlQuery( "select max(id_address) from phpgw_cc_addresses"); $qry->Exec(); $cont=$qry->fetch(); if ( strtoupper($params[':type']) == 'WORK') {$params[':type'] = 2; $params[':default'] = true; } else { $params[':default'] = false; $params[':type'] = 1; } $params[':id_conns'] = $cont->max + 1; $qry = new AwlQuery( "select id_city from phpgw_cc_city where city_name = :idcity", array(':idcity' => $address[3])); if ( @$qry->Exec() && $qry->rows() > 0 ) { $city=$qry->fetch(); $params[':locality'] = $city->id_city; } else $params[':locality'] = ""; $qry = new AwlQuery( "select id_state from phpgw_cc_state where state_name = :idstate OR state_symbol = :idstate ", array(':idstate' => $address[4])); if ( @$qry->Exec() && $qry->rows() > 0 ) { $state=$qry->fetch(); $params[':region'] = $state->id_state; } else $params[':region'] = ""; dbg_log_array( "PUTTTTTT", 'ADDRESSSSSSS', $params, true ); $qry = new AwlQuery( 'INSERT INTO phpgw_cc_addresses ( id_address,id_city,id_state,id_country,address1,postal_code,address_is_default) VALUES( :id_conns, :locality, :region, :country, :street_address, :postcode, :default )', $params ); $qry->Exec(); $qry = new AwlQuery( 'INSERT INTO phpgw_cc_contact_addrs (id_contact, id_address, id_typeof_contact_address ) VALUES( :id_contact, :id_conns, :type)', array(':id_contact' => $id_contactt , ':id_conns' => $cont->max + 1,':type' => $params[':type'])); $qry->Exec(); } } else { foreach( $addresses AS $adr ) { $params[':type'] = $adr->GetParameterValue('TYPE'); $address = explode(';',$adr->Value()); // We use @ to suppress the warnings here, because the NULL in the database suits us well. @$params[':box_no'] = $address[0]; @$params[':unit_no'] = $address[1]; @$params[':street_address'] = $address[2]; @$params[':locality'] = $address[3]; @$params[':region'] = $address[4]; @$params[':postcode'] = $address[5]; @$params[':country'] = substr($address[6],0,2); $qry = new AwlQuery( "select max(id_address) from phpgw_cc_addresses"); $qry->Exec(); $cont=$qry->fetch(); if ( strtoupper($params[':type']) == 'WORK') {$params[':type'] = 2; $params[':default'] = true; } else { $params[':default'] = false; $params[':type'] = 1; } $params[':id_conns'] = $cont->max + 1; $qry = new AwlQuery( "select id_city from phpgw_cc_city where city_name = :idcity", array(':idcity' => $address[3])); if ( @$qry->Exec() && $qry->rows() > 0 ) { $city=$qry->fetch(); $params[':locality'] = $city->id_city; } else $params[':locality'] = ""; $qry = new AwlQuery( "select id_state from phpgw_cc_state where state_name = :idstate OR state_symbol = :idstate ", array(':idstate' => $address[4])); if ( @$qry->Exec() && $qry->rows() > 0 ) { $state=$qry->fetch(); $params[':region'] = $state->id_state; } else $params[':region'] = ""; dbg_log_array( "PUTTTTTT", 'ADDRESSSSSSSSSS INSERT', $params, true ); $qry = new AwlQuery( 'INSERT INTO phpgw_cc_addresses ( id_address,id_city,id_state,id_country,address1,postal_code,address_is_default) VALUES( :id_conns, :locality, :region, :country, :street_address, :postcode, :default )', $params ); $qry->Exec(); $qry = new AwlQuery( 'INSERT INTO phpgw_cc_contact_addrs (id_contact, id_address, id_typeof_contact_address ) VALUES( :id_contact, :id_conns, :type)', array(':id_contact' => $id_contactt , ':id_conns' => $cont->max + 1,':type' => $params[':type'])); $qry->Exec(); } } } return $response_code; } }