2010年12月24日金曜日

CodeIgniterでGoogleContactsAPIのテスト(5)

さてちょっと時間が空いてしまいましたが今回はContactsの更新です。
これまで一覧取得、登録そして削除ときましたので更新が出来たら一通りデータをいじれるようになる訳です。更新も削除と同じく一覧取得で得ているidをキーに行ないます。

$obj->edit = (string)$entry->getEditLink()->href;

こんな感じで取得してましたね。
ZendFramework様々といったところでしょうか。

今回時間が空いてしまったのはjavascript等をいじっていたせいなんですがこれはまあ他のサイトを参考にしてアレンジしていただけなので省略してContactsデータの更新に焦点をあててご紹介します。

// perform login and set protocol version to 3.0
$client = Zend_Gdata_ClientLogin::getHttpClient($this->user, $this->pass, 'cp');
$client->setHeaders('If-Match: *');
$gdata = new Zend_Gdata($client);
$gdata->setMajorProtocolVersion(3);

$query = new Zend_Gdata_Query($this->input->post('edit'));
$org_entry = $gdata->getEntry($query);
$xml = simplexml_load_string($org_entry->getXML());

上記のような形でアクセスを行ないqueryのデータとしてeditキーを渡しています。
通常はheaderにETagの値を指定して更新を行うらしいですが自分しかデータをいじらないアカウントですのでアスタリスクを指定しETagはデータに含めていません。

このqueryで得られたデータを上書きしていってputすることでデータが更新されるのですがデータ削除を行なうことを考えて一旦メールアドレス等のデータを全削除してからデータ登録を行なったのですがエレメントの順番が大切なようでエラーが出てしまい今回は全てのデータを新たなDOMDocumentに追加してくという手法を取りました。

// create new entry
$doc = new DOMDocument();
$doc->formatOutput = true;
$entry = $doc->createElement('atom:entry');
$entry->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:atom', 'http://www.w3.org/2005/Atom');
$doc->appendChild($entry);

// add name element
$name = $doc->createElement('name');
$name->setAttribute('xmlns', 'http://schemas.google.com/g/2005');
$entry->appendChild($name);
//input->post()メソッドを利用してXSS対策
$fullName = $doc->createElement('fullName', $this->input->post('name', TRUE));
$name->appendChild($fullName);

// add org name element
$org = $doc->createElement('organization');
$org->setAttribute('xmlns', 'http://schemas.google.com/g/2005');
$org->setAttribute('rel' ,'http://schemas.google.com/g/2005#work');
$entry->appendChild($org);
$orgName = $doc->createElement('orgName', $this->input->post('org', TRUE));
$org->appendChild($orgName);

// add email elements
$emails = $this->input->post('email', TRUE);
$epleces = $this->input->post('email_place', TRUE);
foreach ($emails as $key => $e) {
 if ($e != '') {
  $email = $doc->createElement('email');
  $email->setAttribute('xmlns','http://schemas.google.com/g/2005');
  if ($epleces[$key] == 'custom') {
   $email->setAttribute('label', $this->input->post('email_custom', TRUE));
  } else {
   $email->setAttribute('rel', 'http://schemas.google.com/g/2005#' . $epleces[$key]);
  }
  $email->setAttribute('address', $e);
  $entry->appendChild($email);
 }
}

// add phone elements
$phones = $this->input->post('phone', TRUE);
$pplaces = $this->input->post('phone_place', TRUE);
foreach ($phones as $key => $p) {
 if ($p != '') {
  $phone = $doc->createElement('phoneNumber', $p);
  $phone->setAttribute('xmlns','http://schemas.google.com/g/2005');
  if ($pplaces[$key] == 'custom') {
   $phone->setAttribute('label', $this->input->post('phone_custom', TRUE));
  } else {
   $phone->setAttribute('rel', 'http://schemas.google.com/g/2005#' . $pplaces[$key]);
  }
  $entry->appendChild($phone);
 }
}

// add web elements
$webs =$this->input->post('web', TRUE);
$wplaces = $this->input->post('web_place', TRUE);
foreach ($webs as $key => $w) {
 if ($w != '') {
  $web = $doc->createElement('website');
  $web->setAttribute('xmlns','http://schemas.google.com/contact/2008');
  $web->setAttribute('href',$w);
  if ($wplaces[$key] == 'custom') {
   $web->setAttribute('label', $this->input->post('web_custom', TRUE));
  } else {
   $web->setAttribute('rel', $wplaces[$key]);
  }
  $entry->appendChild($web);
 }
}

foreach ($org_entry->category as $category) {
 $categorys = $doc->createElement('atom:category');
 $categorys->setAttribute('term', $category->getTerm());
 $categorys->setAttribute('scheme', $category->getScheme());
 $entry->appendChild($categorys);
}


$id = $doc->createElement('atom:id', $org_entry->id);
$entry->appendChild($id);

foreach ($org_entry->link as $link) {
 $links = $doc->createElement('atom:link');
 $links->setAttribute('href', $link->getHref());
 $links->setAttribute('rel', $link->getRel());
 $links->setAttribute('type', $link->getType());
 $entry->appendChild($links);
}

$title = $doc->createElement('atom:title',$this->input->post('name', TRUE));
$entry->appendChild($title);

$updated = $doc->createElement('atom:updated', $org_entry->updated);
$entry->appendChild($updated);

$edited = $doc->createElement('atom:edited', $org_entry->edited);
$entry->appendChild($edited);

更新データ以外のデータについては先程行なったqueryの結果をそのまま入れるようにしてあります。じっくりコードを見るとわかると思いますがGmailではメール等の付随データとして仕事や自宅等が選べるようになっていますがその機能にも対応しています。コード中にcustomの文字が見えるかと思います。カスタムデータの場合にはrel要素ではなくlabel要素としてデータをタグに含めて送ることでデータが登録されます。

$entryResult = $gdata->updateEntry($entry_data, $org_entry->getEditLink()>href);

最後にZendFrameworkGDataAPIのupdateEntryメソッドでデータを送って更新が完了です。一緒に送っているのは冒頭で述べたlinkタグに含まれるeditキーです。

これでContactsについては一通りデータのやりとりが出来るようになりました。次の予定ではこれら連絡先を管理するには大切なグループについて登録や更新、削除を出来るようにしてみたいと思います。この部分についてはIBMのサンプルがないので英語ドキュメントを読みながらの作業になります。さらに時間がかかってしまうかも?

0 件のコメント:

コメントを投稿