DAViCal
vcard.php
1 <?php
6 require_once('AwlQuery.php');
7 require_once('vComponent.php');
8 
9 class VCard extends vComponent {
10 
26  function Write( $dav_id, $exists = true ) {
27  $qry = new AwlQuery();
28 
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();
32 
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';
36  }
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 )';
40  }
41 
42  $params = array( ':dav_id' => $dav_id );
43 
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;
51  }
52  foreach( $properties AS $k => $v ) {
53  $pname = ':' . strtolower($v->Name());
54  if ( $pname == ':n' ) $pname = ':name';
55  if ( !isset($params[$pname]) ) $params[$pname] = $v->Value();
56  }
57 
58  $qry->QDo( $sql, $params );
59 
60  $this->WriteAddresses($dav_id);
61  $this->WritePhones($dav_id);
62  $this->WriteEmails($dav_id);
63 
64  if ( ! $in_transaction ) $qry->Commit();
65  }
66 
67 
71  function WriteAddresses( $dav_id ) {
72  $addresses = $this->GetProperties('ADR');
73  $qry = new AwlQuery();
74 
75  // Only run a local transaction if we're not in one already.
76  $in_transaction = ($qry->TransactionState() == 1);
77  if ( ! $in_transaction ) $qry->Begin();
78 
79  $params = array( ':dav_id' => $dav_id );
80  $qry->QDo('DELETE FROM addressbook_address_adr WHERE dav_id = :dav_id', $params );
81 
82  $count = 0;
83  foreach( $addresses AS $adr ) {
84  $type = $adr->GetParameterValue('TYPE');
85  if ( is_array($type) ) $type = implode('~|~',$type);
86 
87  $params[':type'] = $type;
88  //explode on ; that is not preceeded by an \
89  $address = preg_split( '{(?<!\\\\);}', $adr->Value());
90  //$address = explode(';',$adr->Value());
91 
92  // We use @ to suppress the warnings here, because the NULL in the database suits us well.
93  @$params[':box_no'] = $address[0];
94  @$params[':unit_no'] = $address[1];
95  @$params[':street_address'] = $address[2];
96  @$params[':locality'] = $address[3];
97  @$params[':region'] = $address[4];
98  @$params[':postcode'] = $address[5];
99  @$params[':country'] = $address[6];
100  $params[':property'] = $adr->Render();
101  $params[':count'] = $count++;
102 
103  $qry->QDo( 'INSERT INTO addressbook_address_adr (dav_id, count, type, box_no, unit_no, street_address, locality, region, postcode, country, property)
104 VALUES ( :dav_id, :count, :type, :box_no, :unit_no, :street_address, :locality, :region, :postcode, :country, :property)', $params );
105  }
106  if ( ! $in_transaction ) $qry->Commit();
107  }
108 
109 
113  function WritePhones( $dav_id ) {
114  $telephones = $this->GetProperties('TEL');
115  $qry = new AwlQuery();
116 
117  // Only run a local transaction if we're not in one already.
118  $in_transaction = ($qry->TransactionState() == 1);
119  if ( ! $in_transaction ) $qry->Begin();
120 
121  $params = array( ':dav_id' => $dav_id );
122  $qry->QDo('DELETE FROM addressbook_address_tel WHERE dav_id = :dav_id', $params );
123 
124  $count = 0;
125  foreach( $telephones AS $tel ) {
126  $type = $tel->GetParameterValue('TYPE');
127  if ( is_array($type) ) $type = implode('~|~',$type);
128 
129  $params[':type'] = $type;
130  if ( ! isset($params[':type']) ) $params[':type'] = 'voice';
131 
132  $params[':tel'] = $tel->Value();
133  $params[':property'] = $tel->Render();
134  $params[':count'] = $count++;
135 
136  $qry->QDo( 'INSERT INTO addressbook_address_tel (dav_id, count, type, tel, property) VALUES ( :dav_id, :count, :type, :tel, :property)', $params );
137  }
138  if ( ! $in_transaction ) $qry->Commit();
139  }
140 
141 
150  function WriteEmails( $dav_id ) {
151  $emails = $this->GetProperties('EMAIL');
152  $qry = new AwlQuery();
153 
154  // Only run a local transaction if we're not in one already.
155  $in_transaction = ($qry->TransactionState() == 1);
156  if ( ! $in_transaction ) $qry->Begin();
157 
158  $params = array( ':dav_id' => $dav_id );
159  $qry->QDo('DELETE FROM addressbook_address_email WHERE dav_id = :dav_id', $params );
160 
161  $count = 0;
162  foreach( $emails AS $email ) {
163  $type = $email->GetParameterValue('TYPE');
164  if ( is_array($type) ) $type = implode('~|~',$type);
165 
166  $params[':type'] = $type;
167  $params[':email'] = $email->Value();
168  $params[':property'] = $email->Render();
169  $params[':count'] = $count++;
170 
171  $qry->QDo( 'INSERT INTO addressbook_address_email (dav_id, count, type, email, property) VALUES ( :dav_id, :count, :type, :email, :property)', $params );
172  }
173  if ( ! $in_transaction ) $qry->Commit();
174  }
175 
176 }
Write( $dav_id, $exists=true)
Definition: vcard.php:26
WriteAddresses( $dav_id)
Definition: vcard.php:71
WriteEmails( $dav_id)
Definition: vcard.php:150
Definition: vcard.php:9
WritePhones( $dav_id)
Definition: vcard.php:113