We should respond with sync-token if PROPFIND asks too.
[davical.git] / inc / vcard.php
blobbccf7874681be5135907593c9d28e0a14166dc3a
1 <?php
2 /**
3 * Extend the vComponent to specifically handle VCARD resources
4 */
6 require_once('AwlQuery.php');
7 require_once('vComponent.php');
9 class VCard extends vComponent {
11 /**
12 CREATE TABLE addressbook_resource (
13 dav_id INT8 NOT NULL REFERENCES caldav_data(dav_id) ON UPDATE CASCADE ON DELETE CASCADE PRIMARY KEY,
14 version TEXT,
15 uid TEXT,
16 nickname TEXT,
17 fn TEXT, -- fullname
18 n TEXT, -- Name Surname;First names
19 note TEXT,
20 org TEXT,
21 url TEXT,
22 fburl TEXT,
23 caluri TEXT
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();
33 if ( $exists ) {
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';
37 else {
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();
68 /**
69 CREATE TABLE addressbook_address_adr (
70 dav_id INT8 NOT NULL REFERENCES caldav_data(dav_id) ON UPDATE CASCADE ON DELETE CASCADE,
71 type TEXT,
72 box_no TEXT,
73 unit_no TEXT,
74 street_address TEXT,
75 locality TEXT,
76 region TEXT,
77 postcode TEXT,
78 country TEXT,
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 $type = $adr->GetParameterValue('TYPE');
94 if ( is_array($type) ) $type = implode('~|~',$type);
95 $params[':type'] = $type;
96 $address = explode(';',$adr->Value());
98 // We use @ to suppress the warnings here, because the NULL in the database suits us well.
99 @$params[':box_no'] = $address[0];
100 @$params[':unit_no'] = $address[1];
101 @$params[':street_address'] = $address[2];
102 @$params[':locality'] = $address[3];
103 @$params[':region'] = $address[4];
104 @$params[':postcode'] = $address[5];
105 @$params[':country'] = $address[6];
106 $params[':property'] = $adr->Render();
107 $qry->QDo( 'INSERT INTO addressbook_address_adr (dav_id, type, box_no, unit_no, street_address, locality, region, postcode, country, property)
108 VALUES( :dav_id, :type, :box_no, :unit_no, :street_address, :locality, :region, :postcode, :country, :property)', $params );
110 if ( ! $in_transaction ) $qry->Commit();
115 CREATE TABLE addressbook_address_tel (
116 dav_id INT8 NOT NULL REFERENCES caldav_data(dav_id) ON UPDATE CASCADE ON DELETE CASCADE,
117 type TEXT,
118 tel TEXT,
119 property TEXT -- The full text of the property
122 function WritePhones( $dav_id ) {
123 $telephones = $this->GetProperties('TEL');
124 $qry = new AwlQuery();
126 // Only run a local transaction if we're not in one already.
127 $in_transaction = ($qry->TransactionState() == 1);
128 if ( ! $in_transaction ) $qry->Begin();
130 $params = array( ':dav_id' => $dav_id );
131 $qry->QDo('DELETE FROM addressbook_address_tel WHERE dav_id = :dav_id', $params );
132 foreach( $telephones AS $tel ) {
133 $type = $tel->GetParameterValue('TYPE');
134 if ( is_array($type) ) $type = implode('~|~',$type);
135 $params[':type'] = $type;
136 if ( ! isset($params[':type']) ) $params[':type'] = 'voice';
137 $params[':tel'] = $tel->Value();
138 $params[':property'] = $tel->Render();
139 $qry->QDo( 'INSERT INTO addressbook_address_tel (dav_id, type, tel, property) VALUES( :dav_id, :type, :tel, :property)', $params );
141 if ( ! $in_transaction ) $qry->Commit();
146 CREATE TABLE addressbook_address_email (
147 dav_id INT8 NOT NULL REFERENCES caldav_data(dav_id) ON UPDATE CASCADE ON DELETE CASCADE,
148 type TEXT,
149 email TEXT,
150 property TEXT -- The full text of the property
153 function WriteEmails( $dav_id ) {
154 $emails = $this->GetProperties('EMAIL');
155 $qry = new AwlQuery();
157 // Only run a local transaction if we're not in one already.
158 $in_transaction = ($qry->TransactionState() == 1);
159 if ( ! $in_transaction ) $qry->Begin();
161 $params = array( ':dav_id' => $dav_id );
162 $qry->QDo('DELETE FROM addressbook_address_email WHERE dav_id = :dav_id', $params );
163 foreach( $emails AS $email ) {
164 $type = $email->GetParameterValue('TYPE');
165 if ( is_array($type) ) $type = implode('~|~',$type);
166 $params[':type'] = $type;
167 $params[':email'] = $email->Value();
168 $params[':property'] = $email->Render();
169 $qry->QDo( 'INSERT INTO addressbook_address_email (dav_id, type, email, property) VALUES( :dav_id, :type, :email, :property)', $params );
171 if ( ! $in_transaction ) $qry->Commit();