18Jul/090
CodeIgniter vCard Library to Export Vcard .VCF files with Zip Support
The Vcard.php Library Code (applications/library/Vcard.php) :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /* /* * --------------------------------------------------------------------------------------------------------------------------- * vCard library for Code Igniter applications extended from class_vcard from Troy Wolf [troy@troywolf.com] * --------------------------------------------------------------------------------------------------------------------------- */ /** * 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<br />"; $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<br />"; $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<br />"; /* 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 carlos.alcala@upandrunningsoftware.com//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<br />"; 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<br />"; if (!$this->card) { $this->build(); } if (!$this->filename) { $this->filename = trim($this->data['display_name']); } $this->filename = str_replace(" ", "_", $this->filename); /* IM NOT SURE OF THIS COMMENT header("Content-type: text/directory"); header("Content-Disposition: attachment; filename=".$this->filename.".vcf"); header("Pragma: public"); */ //echo $this->card; $datavcard = $this->getvcard(); $name = $this->filename.".vcf";//'mydata1.txt'; //$datavcard = 'A Data String!'; $this->ci->zip->add_data($name, $datavcard); // Write the zip file to a folder on your server. Name it "my_backup.zip" $this->ci->zip->archive(base_url().'vcards/'.$this->filename.'.zip'); // Download the file to your desktop. Name it "my_backup.zip" $this->ci->zip->download($this->filename.'.zip'); return true; } /** * Get Vcard for Download. * * @param * @return VCF file */ function getvcard() { $this->log .= "vcard download() called<br />"; 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<br />"; 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); } } // 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; } } ?> |
Then we can use it on a controller function like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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(); } |
The vcard function on the controller calls the library Vcard.php and then use the constructor, for that you have to arrange an array of COMMON VALUES for, we can use the Troy example for this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | function getvcard(){ /* 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'] = "troy@troywolf.com"; #$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'] = "Troy is an amazing guy!"; } |
You can customize the $datavcard array to suit your contact information and then passit to the constructor like the function before.
I hope this would be quite enough for this to get work on export vCards.
Thanks for your comments,
Carlos
PS. The Codeigniter Wiki for this library is here: vCard Library