-
Notifications
You must be signed in to change notification settings - Fork 0
vCard Library
Category:Library::Export vCard Data
A simple href to the controller function:
[code]<?php foreach($contact as $key => $val){ anchor("controller/linkvcard/".$val['id'],'Export vCard'); } ?>[/code]
The controller function will be used to load, construct from a getvcard function contact data and then download the file(zipped or plain .vcf):
[code]function linkvcard($id = false)
{
$this->load->library('vcard');
$datavcard = $this->getvcard($id);
if (is_array($datavcard))
{
$this->vcard->vcard($datavcard);
}
else
{
$this->vcard->vcard();
}
$this->vcard->zipdownload();
}[/code]
The PHP vCard Class Library (applications/libraries/Vcard.php):
[code] <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /* /*
-
- vCard library for Code Igniter applications extended from class_vcard from Troy Wolf [[email protected]]
- Extended Class Library for Codeigniter from [email protected]
-
*/
/**
-
Codeigniter vCard library / class Vcard { /*
- vcard variables **/ protected $ci;
protected $log; protected $data; //array of this vcard's contact data private $filename; //filename for download file naming private $class; //PUBLIC, PRIVATE, CONFIDENTIAL private $revision_date; //vCard Date private $card; //vCard String
/**
- __construct **/ public function __construct() { $this->ci =& get_instance(); //$ci->load->library('session'); $this->ci->load->library('zip'); }
/** *The Vcards class constructor. You can set some defaults here if desired. * */
function Vcard($data = false) { $this->log = "New vcard() called
";$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 ); if(is_array($data)) { foreach($data as $item => $value) { $this->data[$item] = $value; } } return true; }
/** *The Reload method. This metod update the DATA array content. * */
function reload($data = false) { $this->log = "reload() called
";$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 ); if(is_array($data)) { foreach($data as $item => $value) { $this->data[$item] = $value; } } $this->build();
return $this->card; }
/**
- Build method checks all the values, builds appropriate defaults for
- missing values, generates the vcard data string
- @param
- @return VCF file
/
function build() { $this->log .= "vcard build() called
"; / For many of the values, if they are not passed in, we set defaults or build them based on other values. */ if (!$this->class) { $this->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->revision_date) { $this->revision_date = date('Y-m-d H:i:s'); }
$this->card = "BEGIN:VCARD\r\n";
$this->card .= "VERSION:3.0\r\n"; $this->card .= "CLASS:".$this->class."\r\n"; $this->card .= "PRODID:-//Vcard Extended Class from [email protected]//NONSGML Version 1//EN\r\n"; $this->card .= "REV:".$this->revision_date."\r\n"; $this->card .= "FN:".$this->data['display_name']."\r\n"; $this->card .= "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 .= "NICKNAME:".$this->data['nickname']."\r\n"; } if ($this->data['title']) { $this->card .= "TITLE:".$this->data['title']."\r\n"; } if ($this->data['company']) { $this->card .= "ORG:".$this->data['company']; } if ($this->data['department']) { $this->card .= ";".$this->data['department']; } $this->card .= "\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 .= "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 .= "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 .= "EMAIL;TYPE=internet,pref:".$this->data['email1']."\r\n"; } if ($this->data['email2']) { $this->card .= "EMAIL;TYPE=internet:".$this->data['email2']."\r\n"; } if ($this->data['office_tel']) { $this->card .= "TEL;TYPE=work,voice:".$this->data['office_tel']."\r\n"; } if ($this->data['home_tel']) { $this->card .= "TEL;TYPE=home,voice:".$this->data['home_tel']."\r\n"; } if ($this->data['cell_tel']) { $this->card .= "TEL;TYPE=cell,voice:".$this->data['cell_tel']."\r\n"; } if ($this->data['fax_tel']) { $this->card .= "TEL;TYPE=work,fax:".$this->data['fax_tel']."\r\n"; } if ($this->data['pager_tel']) { $this->card .= "TEL;TYPE=work,pager:".$this->data['pager_tel']."\r\n"; } if ($this->data['url']) { $this->card .= "URL;TYPE=work:".$this->data['url']."\r\n"; } if ($this->data['birthday']) { $this->card .= "BDAY:".$this->data['birthday']."\r\n"; } if ($this->data['role']) { $this->card .= "ROLE:".$this->data['role']."\r\n"; } if ($this->data['note']) { $this->card .= "NOTE:".$this->data['note']."\r\n"; } $this->card .= "TZ:".$this->data['timezone']."\r\n"; $this->card .= "END:VCARD\r\n"; }
/**
- Download method streams the vcard to the browser client.
- @param
- @return VCF file
/
function download() { /$this->log .= "vcard download() called
"; if (!$this->card) { $this->build(); } if (!$this->filename) { $this->filename = trim($this->data['display_name']); } $this->filename = str_replace(" ", "_", $this->filename); header("Content-type: text/directory"); header("Content-Disposition: attachment; filename=".$this->filename.".vcf"); header("Pragma: public"); echo $this->card; */ echo $this->getvcard(); return true; }
/**
-
Zipdownload method. Streams the vcard zipped to the browser client.
-
@param
-
@return VCF file ZIPPED */
function zipdownload() {$this->log .= "vcard download() called
"; if (!$this->card) { $this->build(); } if (!$this->filename) { $this->filename = trim($this->data['display_name']); } $this->filename = str_replace(" ", "_", $this->filename); //echo $this->card;
$datavcard = $this->getvcard();$name = $this->filename.".vcf"; //$datavcard = 'A Data String!';
$this->ci->zip->add_data($name, $datavcard);
// Write the zip file to a folder on your server. $this->ci->zip->archive(base_url().'vcards/'.$this->filename.'.zip');
// Download the file to your desktop. $this->ci->zip->download($this->filename.'.zip'); return true; } /**
-
Get Vcard for Download.
-
@param
-
@return VCF file /
function getvcard() { $this->log .= "vcard download() called
"; if (!$this->card) { $this->build(); } if (!$this->filename) { $this->filename = trim($this->data['display_name']); } $this->filename = str_replace(" ", "_", $this->filename); header("Content-type: text/directory"); header("Content-Disposition: attachment; filename=".$this->filename.".vcf"); header("Pragma: public"); return $this->card; //return true; } /* -
Get Vcard for Download.
-
@param
-
@return VCF file */
function returnvcard() { $this->log .= "vcard download() called
"; if (!$this->card) { $this->build(); } if (!$this->filename) { $this->filename = trim($this->data['display_name']); } $this->filename = str_replace(" ", "_", $this->filename); //header("Content-type: text/directory"); //header("Content-Disposition: attachment; filename=".$this->filename.".vcf"); //header("Pragma: public"); return $this->card; //return true; }
/**
-
Zip Download method. Streams the vcard ARRAY zipped to the browser client.
-
@param
-
@return VCF file ZIPPED */
function zipdownloads($filename = false, $vcards = false) {foreach($vcards as $item => $value) { foreach($value as $key => $val) { $this->ci->zip->add_data($key, $val); }
}
//$this->ci->zip->add_data($vcardsdata); // Write the zip file to a folder on your server. $this->ci->zip->archive(base_url().'vcards/'.$filename.'.zip'); // Download the file to your desktop. $this->ci->zip->download($filename.'.zip'); return true; }
} ?> [/code]
Finally we need the getvcard function to populate the vcard constructor, here is an example from Troy Wolf: [code]function getvcard($id = false){ $datavcardrevision = array(); /* filename is the name of the .vcf file that will be sent to the user if you call the download() method. If you leave this blank, the class will automatically build a filename using the contact's data. */ #$datavcardfilename = "";
/* If you leave this blank, the current timestamp will be used. */ #$datavcardrevision_date = "";
/* Possible values are PUBLIC, PRIVATE, and CONFIDENTIAL. If you leave class blank, it will default to PUBLIC. */ #$datavcardclass = "PUBLIC";
/* Contact's name data. If you leave display_name blank, it will be built using the first and last name. */ #$datavcarddata['display_name'] = ""; $datavcarddata['first_name'] = "Troy"; $datavcarddata['last_name'] = "Wolf"; #$datavcarddata['additional_name'] = ""; //Middle name #$datavcarddata['name_prefix'] = ""; //Mr. Mrs. Dr. #$datavcarddata['name_suffix'] = ""; //DDS, MD, III, other designations. $datavcarddata['nickname'] = "TJ";
/* Contact's company, department, title, profession */ $datavcarddata['company'] = "TroyWolf.com"; #$datavcarddata['department'] = ""; $datavcarddata['title'] = "Web Developer"; #$datavcarddata['role'] = "";
/* Contact's work address */ #$datavcarddata['work_po_box'] = ""; #$datavcarddata['work_extended_address'] = ""; $datavcarddata['work_address'] = "7027 N. Hickory"; $datavcarddata['work_city'] = "Kansas City"; $datavcarddata['work_state'] = "MO"; $datavcarddata['work_postal_code'] = "64118"; #$datavcarddata['work_country'] = "United States of America";
/* Contact's home address */ #$datavcarddata['home_po_box'] = ""; #$datavcarddata['home_extended_address'] = ""; $datavcarddata['home_address'] = "7027 N. Hickory"; $datavcarddata['home_city'] = "Kansas City"; $datavcarddata['home_state'] = "MO"; $datavcarddata['home_postal_code'] = "64118"; #$datavcarddata['home_country'] = "United States of America";
/* Contact's telephone numbers. */ $datavcarddata['office_tel'] = ""; #$datavcarddata['home_tel'] = ""; $datavcarddata['cell_tel'] = "(816) 739-9653"; $datavcarddata['fax_tel'] = ""; #$datavcarddata['pager_tel'] = "";
/* Contact's email addresses */ $datavcarddata['email1'] = "[email protected]"; #$datavcarddata['email2'] = "";
/* Contact's website */ $datavcarddata['url'] = "http://www.troywolf.com";
/* Some other contact data. */ #$datavcarddata['photo'] = ""; //Enter a URL. $datavcarddata['birthday'] = "1971-08-13"; $datavcarddata['timezone'] = "-06:00";
/* If you leave this blank, the class will default to using last_name or company. */ #$datavcarddata['sort_string'] = "";
/* Notes about this contact. */ $datavcarddata['note'] = "Carlos and Troy are amazing devs!";
return $datavcardrevision; }[/code]
I hope this would be usefull for everyone.
Thanks for your comments,
Carlos