Part of the EllisLab Network
x
 
Create New Page
 View Previous Changes    ( Last updated by jeremygimbel )

vCard Library revised

I’ve done a revision to a previously posted vCard library.

The vcard.zip file includes a sample controller with usage examples as well as the library itself.
File:vcard.zip


Here is the library:

<?php  if( ! defined('BASEPATH')) exit('No direct script access allowed'); 
/**
* CodeIgniter vCard  library
* Extended by Jeremy Gimbel [jeremy@jgimbel.com]
* Based upon vCard library for Codeigniter by Carlos Alcala [carlos.alcala@upandrunningsoftware.com]
* and class_vcard from Troy Wolf [troy@troywolf.com]
*
* March 3, 2010
*
* Usage within Codeigniter:
*
* Place the Vcard.php file in your system/application/libraries directory.
* Load the library using $this->load->library('vcard');
* Create an associative array for the card data using keys for each field that match those below
* Generate a vCard file using one of the generate methods (generate_string, generate_file, 
* generate_download)
*
* See sample app.php controller file for an example.
*
* Information at: http://dreadfullyposh.com/
*/

class Vcard
{
    
// private CI instance
    
private $ci;
    
    
// private array for data from caller
    
private $data;
    
    
// private string for storing the text of the finished card
    
private $card_string;
    
    
    public function 
__construct()
    
{
        $this
->ci =& get_instance();

    
}
    
    
    
/** 
     * Vcard class constructor 
     * 
     * Initializes the data array, either to blank values to values
     * provided in the parameters. An optional filename or directory
     * can be  specified to generate the vCard in one step
     * 
     * @access public 
     * @param array $data
     * @param string $filename
     *  
     */ 
    
public function Vcard($data false$filename false)
    
{
        
// initialize the array
        
$this->data = array(
            
'display_name' => null,
            
'first_name' => null,
            
'last_name' => null,
            
'additional_name' => null,
            
'name_prefix' => null,
            
'name_suffix' => null,
            
'nickname' => null,
            
'title' => null,
            
'role' => null,
            
'department' => null,
            
'company' => null,
            
'work_po_box' => null,
            
'work_extended_address' => null,
            
'work_address' => null,
            
'work_city' => null,
            
'work_state' => null,
            
'work_postal_code' => null,
            
'work_country' => null,
            
'home_po_box' => null,
            
'home_extended_address' => null,
            
'home_address' => null,
            
'home_city' => null,
            
'home_state' => null,
            
'home_postal_code' => null,
            
'home_country' => null,
            
'office_tel' => null,
            
'home_tel' => null,
            
'cell_tel' => null,
            
'fax_tel' => null,
            
'pager_tel' => null,
            
'email1' => null,
            
'email2' => null,
            
'url' => null,
            
'photo' => null,
            
'birthday' => null,
            
'timezone' => null,
            
'sort_string' => null,
            
'note' => null,
            
'revision_date' => null,
            
'class' => null
        
);
        
        
// check if an array of data was provided
        // if so, add values from the array to the 
        // class data array
        
if(is_array($data))
        
{
            
foreach($data as $item => $value)
            
{
                $this
->data[$item] $value;
            
}
        }
    
        
// check if a filename was provided
        // if so, load the generate_file() method
        
if(is_string($filename))
        
{
            $this
->generate_file($filename);
    
        
}
    
    }
    
    
    
/** 
     * Load 
     * 
     * Initializes the data array, to values provided in 
     * the parameters. 
     * 
     * @access public 
     * @param array $data
     *  
     */ 
    
public function load($data)
    
{
        
// initialize the array
        
$this->data = array(
            
'display_name' => null,
            
'first_name' => null,
            
'last_name' => null,
            
'additional_name' => null,
            
'name_prefix' => null,
            
'name_suffix' => null,
            
'nickname' => null,
            
'title' => null,
            
'role' => null,
            
'department' => null,
            
'company' => null,
            
'work_po_box' => null,
            
'work_extended_address' => null,
            
'work_address' => null,
            
'work_city' => null,
            
'work_state' => null,
            
'work_postal_code' => null,
            
'work_country' => null,
            
'home_po_box' => null,
            
'home_extended_address' => null,
            
'home_address' => null,
            
'home_city' => null,
            
'home_state' => null,
            
'home_postal_code' => null,
            
'home_country' => null,
            
'office_tel' => null,
            
'home_tel' => null,
            
'cell_tel' => null,
            
'fax_tel' => null,
            
'pager_tel' => null,
            
'email1' => null,
            
'email2' => null,
            
'url' => null,
            
'photo' => null,
            
'birthday' => null,
            
'timezone' => null,
            
'sort_string' => null,
            
'note' => null,
            
'revision_date' => null,
            
'class' => null
        
);
        
        
// make sure data array was provided
        // if so, load the values into the class
        // data array
        
if(is_array($data))
        
{
            
foreach($data as $item => $value)
            
{
                $this
->data[$item] $value;
            
}
        }
    
    }
    
    
    
/** 
     * generate_file 
     * 
     * Generates a vcf file on the server. Accepts either 
     * a filename or directory. If a filename is provided
     * the method generates the vcf file with that name. If
     * a directory is provided, the filename is built from
     * the display name and the file is placed in the specified
     * directory.
     * 
     * @access public 
     * @param string $filename
     * @return string path and filename 
     */ 
    
public function generate_file($filename)
    {
        $this
->_build();
        
        if(
is_dir($filename))
            
$filename .= $this->_build_filename();
        
        
$fh fopen($filename'w');
        
        if(!
$fh)
            return 
false;
        
        
fwrite($fh$this->card_string);
        
fclose($fh);

        return 
$filename;
    
}
    
    
    
/** 
     * generate_string 
     * 
     * Generates a vcf formatted string. 
     * 
     * @access public 
     * @return string vcf formatted data 
     */ 
    
public function generate_string()
    
{
    
        $this
->_build();
        return 
$this->card_string;
    
    
}
    
    
    
/** 
     * generate_download 
     * 
     * Generates a vcf file and forces a download to the
     * browser. Accepts a filename If a filename is not 
     * provided, the filename is built from the display 
     * name.
     * 
     * @access public 
     * @param string $filename
     */ 
    
public function generate_download($filename null)
    
{
        $this
->_build();
        
        if(
$filename == null)
        
{
            $filename 
$this->_build_filename();
        
        
}
        $this
->ci->load->helper('download');
        
        
force_download($filename$this->card_string);     
    
    
    
}
    
    
    
/** 
     * _build 
     * 
     * Generates a vcf formatted string from the data array 
     * and stores it in the private class variable
     * 
     * @access private 
     */ 
    
private function _build()
    
{
        
/*
        For many of the values, if they are not passed in, we set defaults or
        build them based on other values.
        */

        
if(!$this->data['class']{ $this->data['class'"PUBLIC"}
        
if(!$this->data['display_name']
        
{
              $this
->data['display_name'trim($this->data['first_name']." ".$this->data['last_name']);
        
}
        
if(!$this->data['sort_string']{ $this->data['sort_string'$this->data['last_name']}
        
if(!$this->data['sort_string']{ $this->data['sort_string'$this->data['company']}
        
if(!$this->data['timezone']{ $this->data['timezone'date("O"); }
        
if(!$this->data['revision_date']{ $this->data['revision_date'date('Y-m-d H:i:s'); }

          $this
->card_string "BEGIN:VCARD\r\n";
        
$this->card_string .= "VERSION:3.0\r\n";
        
$this->card_string .= "CLASS:".$this->data['class']."\r\n";
        
$this->card_string .= "PRODID:-//Vcard Extended Class from carlos.alcala@upandrunningsoftware.com//NONSGML Version 1//EN\r\n";
        
$this->card_string .= "REV:".$this->data['revision_date']."\r\n";
          
$this->card_string .= "FN:".$this->data['display_name']."\r\n";
        
$this->card_string .= "N:"
          
.$this->data['last_name'].";"
          
.$this->data['first_name'].";"
          
.$this->data['additional_name'].";"
          
.$this->data['name_prefix'].";"
          
.$this->data['name_suffix']."\r\n";

        if(
$this->data['nickname']{ $this->card_string .= "NICKNAME:".$this->data['nickname']."\r\n"}
          
if($this->data['title']{ $this->card_string .= "TITLE:".$this->data['title']."\r\n"}
          
if($this->data['company']{ $this->card_string .= "ORG:".$this->data['company']}
          
if($this->data['department']{ $this->card_string .= ";".$this->data['department']}
          $this
->card_string .= "\r\n";
      
          if(
$this->data['work_po_box']
        
|| $this->data['work_extended_address']
        
|| $this->data['work_address']
        
|| $this->data['work_city']
        
|| $this->data['work_state']
        
|| $this->data['work_postal_code']
        
|| $this->data['work_country']
        
{
              $this
->card_string .= "ADR;TYPE=work:"
            
.$this->data['work_po_box'].";"
            
.$this->data['work_extended_address'].";"
            
.$this->data['work_address'].";"
            
.$this->data['work_city'].";"
            
.$this->data['work_state'].";"
            
.$this->data['work_postal_code'].";"
            
.$this->data['work_country']."\r\n";
        
}
      
          
if($this->data['home_po_box']
        
|| $this->data['home_extended_address']
        
|| $this->data['home_address']
        
|| $this->data['home_city']
        
|| $this->data['home_state']
        
|| $this->data['home_postal_code']
        
|| $this->data['home_country']
        
{
              $this
->card_string .= "ADR;TYPE=home:"
            
.$this->data['home_po_box'].";"
            
.$this->data['home_extended_address'].";"
            
.$this->data['home_address'].";"
            
.$this->data['home_city'].";"
            
.$this->data['home_state'].";"
            
.$this->data['home_postal_code'].";"
            
.$this->data['home_country']."\r\n";
        
}
    
        
if($this->data['email1']{ $this->card_string .= "EMAIL;TYPE=internet,pref:".$this->data['email1']."\r\n"}
        
if($this->data['email2']{ $this->card_string .= "EMAIL;TYPE=internet:".$this->data['email2']."\r\n"}
        
if($this->data['office_tel']{ $this->card_string .= "TEL;TYPE=work,voice:".$this->data['office_tel']."\r\n"}
        
if($this->data['home_tel']{ $this->card_string .= "TEL;TYPE=home,voice:".$this->data['home_tel']."\r\n"}
        
if($this->data['cell_tel']{ $this->card_string .= "TEL;TYPE=cell,voice:".$this->data['cell_tel']."\r\n"}
        
if($this->data['fax_tel']{ $this->card_string .= "TEL;TYPE=work,fax:".$this->data['fax_tel']."\r\n"}
        
if($this->data['pager_tel']{ $this->card_string .= "TEL;TYPE=work,pager:".$this->data['pager_tel']."\r\n"}
        
if($this->data['url']{ $this->card_string .= "URL;TYPE=work:".$this->data['url']."\r\n"}
          
if($this->data['birthday']{ $this->card_string .= "BDAY:".$this->data['birthday']."\r\n"}
          
if($this->data['role']{ $this->card_string .= "ROLE:".$this->data['role']."\r\n"}
          
if($this->data['note']{ $this->card_string .= "NOTE:".$this->data['note']."\r\n"}
          $this
->card_string .= "TZ:".$this->data['timezone']."\r\n";
        
$this->card_string .= "END:VCARD\r\n";
    
    
}
    
    
/** 
     * _build_filename 
     * 
     * Generates a filename from the display name
     * in the card data
     * 
     * @access private 
     * @return string filename 
     */ 
    
private function _build_filename()
    
{
        $filename 
trim($this->data['display_name']);
        
$filename str_replace(" ""_"$filename);
        
$filename .= '.vcf';
        
        return 
$filename;
    
    
}

And here is the sample controller:

<?php
/**
* CodeIgniter vCard  library
* Extended by Jeremy Gimbel [jeremy@jgimbel.com]
* Based upon vCard library for Codeigniter by Carlos Alcala [carlos.alcala@upandrunningsoftware.com]
* and class_vcard from Troy Wolf [troy@troywolf.com]
*
* March 3, 2010
*
* This is a very basic sample controller for using the vCard library
*
* Information at: http://dreadfullyposh.com/

*/
class App extends Controller {

    
function App()
    
{
        parent
::Controller();    
    
}
    
    
function index()
    
{
        
/*
        Initialize an array to store the various vCard data
        */
        
$card_data = array();

        
/*
        If you leave this blank, the current timestamp will be used.
        */
        //$card_data['revision_date'] = "";

        /*
        Possible values are PUBLIC, PRIVATE, and CONFIDENTIAL. If you leave class
        blank, it will default to PUBLIC.
        */
        //$card_data['class'] = "PUBLIC";

        /*
        Contact's name data.
        If you leave display_name blank, it will be built using the first and last name.
        */
        //$card_data['display_name'] = "";
        
$card_data['first_name'"Test";
        
$card_data['last_name'"Person";
        
$card_data['additional_name'"A"//Middle name
        
$card_data['name_prefix'"Mr.";  //Mr. Mrs. Dr.
        
$card_data['name_suffix'""//DDS, MD, III, other designations.
        
$card_data['nickname'"TP";

        
/*
        Contact's company, department, title, profession
        */
        
$card_data['company'"Test Company";
        
//$card_data['department'] = "";
        
$card_data['title'"Senior Web Developer";
        
$card_data['role'"Developer";

        
/*
        Contact's work address
        */
        //$card_data['work_po_box'] = "";
        //$card_data['work_extended_address'] = "";
        
$card_data['work_address'"123 Main Street";
        
$card_data['work_city'"New York";
        
$card_data['work_state'"NY";
        
$card_data['work_postal_code'"10023";
        
//$card_data['work_country'] = "United States of America";

        /*
        Contact's home address
        */
        //$card_data['home_po_box'] = "";
        //$card_data['home_extended_address'] = "";
        
$card_data['home_address'"456 Broadway";
        
$card_data['home_city'"New York";
        
$card_data['home_state'"NY";
        
$card_data['home_postal_code'"10001";
        
//$card_data['home_country'] = "United States of America";

        /*
        Contact's telephone numbers.
        */
        
$card_data['office_tel'"";
        
//$card_data['home_tel'] = "";
        
$card_data['cell_tel'"(123) 456-7890";
        
$card_data['fax_tel'"";
        
//$card_data['pager_tel'] = "";

        /*
        Contact's email addresses
        */
        
$card_data['email1'"email@domain.com";
        
//$card_data['email2'] = "";

        /*
        Contact's website
        */
        
$card_data['url'"http://www.yoursite.com";

        
/*
        Some other contact data.
        */
        //$card_data['photo'] = "";  //Enter a URL.
        
$card_data['birthday'"1979-01-02";
        
$card_data['timezone'"-05:00";

        
/*
        If you leave this blank, the class will default to using last_name or company.
        */
        //$card_data['sort_string'] = "";

        /*
        Notes about this contact.
        */
        
$card_data['note'"Some notes go here.";
        
        
        
/*
        Now we load up the library.
        */
        
$this->load->library('vcard');
        
        
/*
        Load the $card_data array into the library
        */
        
$this->vcard->load($card_data);
        
        
/*
        Now we can generate a vCard in a variety of ways.
        */
        
        // Generate a file on a server, providing a path and filename.
        // Path and filename are returned
        
$path_and_filename $this->vcard->generate_file('/path/to/filename.vcf');
        
        
// Generate a file on a server, providing only a path. Filename is generated.
        // Path and filename are returned
        
$path_and_filename $this->vcard->generate_file('/path/to/');
        
        
// Generate a vCard data string (to write to file yourself, etc.)
        
$string $this->vcard->generate_string();
        
        
// Generate a vCard and force download to the browser, providing a filename
        
$this->vcard->generate_download('filename.vcf');
        
        
// Generate a vCard and force download to the browser, generate a filename automatically
        
$this->vcard->generate_download();
        


    

    
}

/* End of file app.php */
/* Location: ./system/application/controllers/app.php */ 

I hope someone will find these changes to the original work useful.