3 * Extend the vComponent to specifically handle VCARD resources
6 require_once('AwlQuery.php');
7 require_once('vComponent.php');
9 class VCard
extends vComponent
{
12 CREATE TABLE addressbook_resource (
13 dav_id INT8 NOT NULL REFERENCES caldav_data(dav_id) ON UPDATE CASCADE ON DELETE CASCADE PRIMARY KEY,
18 n TEXT, -- Name Surname;First names
26 function Write( $dav_id, $exists = true ) {
27 $qry = new AwlQuery();
29 // Only run a local transaction if we're not in one already.
30 $in_transaction = ($qry->TransactionState() == 1);
31 if ( ! $in_transaction ) $qry->Begin();
34 $sql = 'UPDATE addressbook_resource SET version=:version, uid=:uid, nickname=:nickname, fn=:fn, n=:name,
35 note=:note, org=:org, url=:url, fburl=:fburl, caladruri=:caladruri, caluri=:caluri WHERE dav_id=:dav_id';
38 $sql = 'INSERT INTO addressbook_resource ( dav_id, version, uid, nickname, fn, n, note, org, url, fburl, caladruri, caluri )
39 VALUES( :dav_id, :version, :uid, :nickname, :fn, :name, :note, :org, :url, :fburl, :caladruri, :caluri )';
42 $params = array( ':dav_id' => $dav_id );
44 $wanted = array('VERSION' => true, 'UID' => true, 'NICKNAME' => true, 'FN' => true, 'N' => true,
45 'NOTE'=> true, 'ORG' => true, 'URL' => true, 'FBURL' => true, 'CALADRURI' => true, 'CALURI' => true);
46 $properties = $this->GetProperties( $wanted );
47 foreach( $wanted AS $k => $v ) {
48 $pname = ':' . strtolower($k);
49 if ( $pname == ':n' ) $pname = ':name';
50 $params[$pname] = null;
52 foreach( $properties AS $k => $v ) {
53 $pname = ':' . strtolower($v->Name());
54 if ( $pname == ':n' ) $pname = ':name';
55 if ( !isset($params[$pname]) /** @todo or this is one is in the user's language */ ) $params[$pname] = $v->Value();
58 $qry->QDo( $sql, $params );
60 $this->WriteAddresses($dav_id);
61 $this->WritePhones($dav_id);
62 $this->WriteEmails($dav_id);
64 if ( ! $in_transaction ) $qry->Commit();
69 CREATE TABLE addressbook_address_adr (
70 dav_id INT8 NOT NULL REFERENCES caldav_data(dav_id) ON UPDATE CASCADE ON DELETE CASCADE,
79 property TEXT -- The full text of the property
82 function WriteAddresses( $dav_id ) {
83 $addresses = $this->GetProperties('ADR');
84 $qry = new AwlQuery();
86 // Only run a local transaction if we're not in one already.
87 $in_transaction = ($qry->TransactionState() == 1);
88 if ( ! $in_transaction ) $qry->Begin();
90 $params = array( ':dav_id' => $dav_id );
91 $qry->QDo('DELETE FROM addressbook_address_adr WHERE dav_id = :dav_id', $params );
92 foreach( $addresses AS $adr ) {
93 $params[':type'] = $adr->GetParameterValue('TYPE');
94 $address = explode(';',$adr->Value());
96 // We use @ to suppress the warnings here, because the NULL in the database suits us well.
97 @$params[':box_no'] = $address[0];
98 @$params[':unit_no'] = $address[1];
99 @$params[':street_address'] = $address[2];
100 @$params[':locality'] = $address[3];
101 @$params[':region'] = $address[4];
102 @$params[':postcode'] = $address[5];
103 @$params[':country'] = $address[6];
104 $params[':property'] = $adr->Render();
105 $qry->QDo( 'INSERT INTO addressbook_address_adr (dav_id, type, box_no, unit_no, street_address, locality, region, postcode, country, property)
106 VALUES( :dav_id, :type, :box_no, :unit_no, :street_address, :locality, :region, :postcode, :country, :property)', $params );
108 if ( ! $in_transaction ) $qry->Commit();
113 CREATE TABLE addressbook_address_tel (
114 dav_id INT8 NOT NULL REFERENCES caldav_data(dav_id) ON UPDATE CASCADE ON DELETE CASCADE,
117 property TEXT -- The full text of the property
120 function WritePhones( $dav_id ) {
121 $telephones = $this->GetProperties('TEL');
122 $qry = new AwlQuery();
124 // Only run a local transaction if we're not in one already.
125 $in_transaction = ($qry->TransactionState() == 1);
126 if ( ! $in_transaction ) $qry->Begin();
128 $params = array( ':dav_id' => $dav_id );
129 $qry->QDo('DELETE FROM addressbook_address_tel WHERE dav_id = :dav_id', $params );
130 foreach( $telephones AS $tel ) {
131 $params[':type'] = $tel->GetParameterValue('TYPE');
132 if ( ! isset($params[':type']) ) $params[':type'] = 'voice';
133 $params[':tel'] = $tel->Value();
134 $params[':property'] = $tel->Render();
135 $qry->QDo( 'INSERT INTO addressbook_address_tel (dav_id, type, tel, property) VALUES( :dav_id, :type, :tel, :property)', $params );
137 if ( ! $in_transaction ) $qry->Commit();
142 CREATE TABLE addressbook_address_email (
143 dav_id INT8 NOT NULL REFERENCES caldav_data(dav_id) ON UPDATE CASCADE ON DELETE CASCADE,
146 property TEXT -- The full text of the property
149 function WriteEmails( $dav_id ) {
150 $emails = $this->GetProperties('EMAIL');
151 $qry = new AwlQuery();
153 // Only run a local transaction if we're not in one already.
154 $in_transaction = ($qry->TransactionState() == 1);
155 if ( ! $in_transaction ) $qry->Begin();
157 $params = array( ':dav_id' => $dav_id );
158 $qry->QDo('DELETE FROM addressbook_address_email WHERE dav_id = :dav_id', $params );
159 foreach( $emails AS $email ) {
160 $params[':type'] = $email->GetParameterValue('TYPE');
161 $params[':email'] = $email->Value();
162 $params[':property'] = $email->Render();
163 $qry->QDo( 'INSERT INTO addressbook_address_email (dav_id, type, email, property) VALUES( :dav_id, :type, :email, :property)', $params );
165 if ( ! $in_transaction ) $qry->Commit();