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 
82  function WriteAddresses( $dav_id ) {
83  $addresses = $this->GetProperties('ADR');
84  $qry = new AwlQuery();
85 
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();
89 
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  //explode on ; that is not preceeded by an \
97  $address = preg_split( '{(?<!\\\\);}', $adr->Value());
98  //$address = explode(';',$adr->Value());
99 
100  // We use @ to suppress the warnings here, because the NULL in the database suits us well.
101  @$params[':box_no'] = $address[0];
102  @$params[':unit_no'] = $address[1];
103  @$params[':street_address'] = $address[2];
104  @$params[':locality'] = $address[3];
105  @$params[':region'] = $address[4];
106  @$params[':postcode'] = $address[5];
107  @$params[':country'] = $address[6];
108  $params[':property'] = $adr->Render();
109  $qry->QDo( 'INSERT INTO addressbook_address_adr (dav_id, type, box_no, unit_no, street_address, locality, region, postcode, country, property)
110 VALUES( :dav_id, :type, :box_no, :unit_no, :street_address, :locality, :region, :postcode, :country, :property)', $params );
111  }
112  if ( ! $in_transaction ) $qry->Commit();
113  }
114 
115 
124  function WritePhones( $dav_id ) {
125  $telephones = $this->GetProperties('TEL');
126  $qry = new AwlQuery();
127 
128  // Only run a local transaction if we're not in one already.
129  $in_transaction = ($qry->TransactionState() == 1);
130  if ( ! $in_transaction ) $qry->Begin();
131 
132  $params = array( ':dav_id' => $dav_id );
133  $qry->QDo('DELETE FROM addressbook_address_tel WHERE dav_id = :dav_id', $params );
134  foreach( $telephones AS $tel ) {
135  $type = $tel->GetParameterValue('TYPE');
136  if ( is_array($type) ) $type = implode('~|~',$type);
137  $params[':type'] = $type;
138  if ( ! isset($params[':type']) ) $params[':type'] = 'voice';
139  $params[':tel'] = $tel->Value();
140  $params[':property'] = $tel->Render();
141  $qry->QDo( 'INSERT INTO addressbook_address_tel (dav_id, type, tel, property) VALUES( :dav_id, :type, :tel, :property)', $params );
142  }
143  if ( ! $in_transaction ) $qry->Commit();
144  }
145 
146 
155  function WriteEmails( $dav_id ) {
156  $emails = $this->GetProperties('EMAIL');
157  $qry = new AwlQuery();
158 
159  // Only run a local transaction if we're not in one already.
160  $in_transaction = ($qry->TransactionState() == 1);
161  if ( ! $in_transaction ) $qry->Begin();
162 
163  $params = array( ':dav_id' => $dav_id );
164  $qry->QDo('DELETE FROM addressbook_address_email WHERE dav_id = :dav_id', $params );
165  foreach( $emails AS $email ) {
166  $type = $email->GetParameterValue('TYPE');
167  if ( is_array($type) ) $type = implode('~|~',$type);
168  $params[':type'] = $type;
169  $params[':email'] = $email->Value();
170  $params[':property'] = $email->Render();
171  $qry->QDo( 'INSERT INTO addressbook_address_email (dav_id, type, email, property) VALUES( :dav_id, :type, :email, :property)', $params );
172  }
173  if ( ! $in_transaction ) $qry->Commit();
174  }
175 
176 }
VCard\Write
Write( $dav_id, $exists=true)
Definition: vcard.php:26
VCard
Definition: vcard.php:9
VCard\WriteAddresses
WriteAddresses( $dav_id)
Definition: vcard.php:82
VCard\WriteEmails
WriteEmails( $dav_id)
Definition: vcard.php:155
VCard\WritePhones
WritePhones( $dav_id)
Definition: vcard.php:124