PHP Classes

File: test_vcard.php

Recommend this page to a friend!
  Classes of CPK Smithies   IMC Objects 2   test_vcard.php   Download  
File: test_vcard.php
Role: Example script
Content type: text/plain
Description: Example code
Class: IMC Objects 2
Parse and generate vCard and iCalendar files
Author: By
Last change:
Date: 13 years ago
Size: 2,542 bytes
 

Contents

Class file image Download
<?php
/**
 * Test functionality of vCard classes
 * @author cpks
 * @license Public Domain
 * @version 1.0
 */
require 'imc_vCard.php';
define('SKIP_FAX', TRUE);
if (
SKIP_FAX) require 'imc_ptm.php';

// Create a stand-alone vCard object from code
// A vCard object can write (but not read!) a file.
$vcard = new imc\vCard;
// add a Name property
$prop = new imc\PropertyN;
$prop->suffix = 'Mr'; // was $vcard->setNameSuffix('Mr.'); etc.
$prop->forename = 'Andreas';
$prop->surname = 'Haberstroh';
$vcard->addProp($prop);
// add an Address property
$prop = new imc\PropertyAdr; // or, new imc\PropertyAdr(array('TYPE' => 'DOM,HOME'));
$prop->setTypeParam(array('DOM', 'HOME'));
// set the address fields
$prop->street = '341 N. Bitterbush St.';
$prop->state = 'CA';
$prop->locality = 'Orange';
$prop->zip = '92868';
$prop->country = 'USA';
$vcard->addProp($prop);
// add a FN ("file under") property
$vcard->setFN('Andreas Haberstroh');
// add a TELephone number
$tel = new imc\customProperty('TEL', '714-532-9493', array('TYPE' => 'HOME'));
$vcard->addProp($tel);
$vcard->setVersion('3.0'); // required by the standard
// write the vCard to file
$vcard->writeFile('test_vcard.vcf');

/*
echo "<h1>vCard Object Usage</h1>\n<pre>\n";
print_r($vcard);
echo "</pre>\n";
*/

// Demonstrate reading a vcard file. This might contain multiple vCard
// objects. So we don't use a vCard, we use a ComponentSet. This will
// construct and contain all the vCard objects we need.
$vcard = new imc\ComponentSet;
$vcard->readFile('sample.vcf');
// Component Sets (and components) are Countable
echo count($vcard), " vCards in sample.vcf:\n";
// and Traversable
foreach ($vcard as $vc) {
 
// get all WORK contact details
 
$iter = $vc->getPropertiesOfType('*', 'WORK');
  if (
SKIP_FAX) {// if we want to drop fax values
   
$inner = $iter;
   
$iter = new imc\propertyTypeMask($inner, 'FAX');
  }
 
// get the 'FN' value. This method can be used even for non-unique
  // values in fact.
 
$person_name = $vc->getUniqueProperty('FN')->getValue();
 
$colwidth = strlen($person_name) + 1;
  echo
$person_name, ' ';
 
$iter->rewind(); // standard to do this before first use
 
if (!$iter->valid())
    echo
"(no contact details)\n";
  else {
    echo
$iter->current()->getValue() . PHP_EOL; // 1st contact detail
    // iterate over subsequent ones
   
for ($iter->next(); $iter->valid(); $iter->next())
      echo
str_repeat(' ', $colwidth), $iter->current()->getValue(), PHP_EOL;
  }
}