You are here

function vcardfield_create_vcard in VCard Field 7

1 string reference to 'vcardfield_create_vcard'
vcardfield_menu in ./vcardfield.module

File

./vcardfield.module, line 333

Code

function vcardfield_create_vcard($field = '', $nid = 0, $delta = 0, $type = 'node') {
  header('Content-Description: File Transfer');
  header('Content-Type: text/x-vcard; charset=utf-8');
  header('Content-Disposition: attachment; filename="vcard.vcf"');

  // BEGIN:VCARD
  // VERSION:3.0
  $field_type = field_info_field_types('vcardfield');
  if ($type == 'node') {
    $entity = entity_load($type, array(
      $nid,
    ));
    if (!isset($entity[$nid])) {
      drupal_not_found();
      drupal_exit();
    }
    $ent = field_info_instances($type, $entity[$nid]->type);
  }
  if ($type == 'user') {
    $entity = entity_load($type, array(
      $nid,
    ));
    if (!isset($entity[$nid])) {
      drupal_not_found();
      drupal_exit();
    }
    $ent = field_info_instances($type, $type);
  }
  $field_name = 'field_' . $field;
  $field_info = field_info_field($field_name);
  $lang = $entity[$nid]->language;
  if ($lang == '') {
    $lang = 'und';
  }
  $entity_field = $entity[$nid]->{$field_name};
  if (!empty($entity_field[$lang][$delta])) {
    $vcard_field = $entity_field[$lang][$delta];
  }
  else {
    $vcard_field = $entity_field['und'][$delta];
  }
  if ($vcard_field['prefix'] != '') {
    $prefix = check_plain($vcard_field['prefix']);
  }
  else {
    $prefix = '';
  }
  if ($vcard_field['first_name'] != '') {
    $fname = check_plain($vcard_field['first_name']);
  }
  else {
    $fname = '';
  }
  if ($vcard_field['middle_name'] != '') {
    $middle_name = check_plain($vcard_field['middle_name']);
  }
  else {
    $middle_name = '';
  }
  if ($vcard_field['last_name'] != '') {
    $lname = check_plain($vcard_field['last_name']);
  }
  else {
    $lname = '';
  }
  if ($vcard_field['suffix'] != '') {
    $suffix = check_plain($vcard_field['suffix']);
  }
  else {
    $suffix = '';
  }
  if ($vcard_field['full_name'] != '') {
    $full_name = check_plain($vcard_field['full_name']);
  }
  else {
    $full_name = '';
  }
  if ($vcard_field['photo'] != '') {
    $photo = check_plain($vcard_field['photo']);
  }
  else {
    $photo = '';
  }
  if ($vcard_field['email'] != '') {
    $email = check_plain($vcard_field['email']);
  }
  else {
    $email = '';
  }
  if ($vcard_field['title'] != '') {
    $title = check_plain($vcard_field['title']);
  }
  else {
    $title = '';
  }
  if ($vcard_field['address_type'] != '') {
    $address_type = check_plain($vcard_field['address_type']);
  }
  else {
    $address_type = '';
  }
  if ($vcard_field['address'] != '') {
    $address = check_plain($vcard_field['address']);
  }
  else {
    $address = '';
  }
  if ($vcard_field['organization'] != '') {
    $org = check_plain($vcard_field['organization']);
  }
  else {
    $org = '';
  }
  if ($vcard_field['city'] != '') {
    $city = check_plain($vcard_field['city']);
  }
  else {
    $city = '';
  }
  if ($vcard_field['region'] != '') {
    $region = check_plain($vcard_field['region']);
  }
  else {
    $region = '';
  }
  if ($vcard_field['postalcode'] != '') {
    $zip = check_plain($vcard_field['postalcode']);
  }
  else {
    $zip = '';
  }
  if ($vcard_field['country'] != '') {
    $country = check_plain($vcard_field['country']);
  }
  else {
    $country = '';
  }
  if ($vcard_field['phone_default'] != '') {
    $workphone = check_plain($vcard_field['phone_default']);
  }
  else {
    $workphone = '';
  }
  if ($vcard_field['phone_home'] != '') {
    $homephone = check_plain($vcard_field['phone_home']);
  }
  else {
    $homephone = '';
  }
  if ($vcard_field['phone_fax'] != '') {
    $fax = check_plain($vcard_field['phone_fax']);
  }
  else {
    $fax = '';
  }
  if ($vcard_field['phone_cell'] != '') {
    $cell = check_plain($vcard_field['phone_cell']);
  }
  else {
    $cell = '';
  }
  if ($vcard_field['link'] != '') {
    $url = check_plain($vcard_field['link']);
  }
  else {
    $url = '';
  }
  $output = "N;CHARSET=UTF-8:" . $lname . ";" . $fname . ";" . $middle_name . ";" . $prefix . ";" . $suffix . "\n";
  if ($org != "") {
    $output .= "ORG;CHARSET=utf-8:" . $org;
    $output .= "\n";
  }
  if ($full_name != "") {
    $output .= "FN;CHARSET=utf-8:" . $full_name;
    $output .= "\n";
  }
  if ($email != "") {
    $output .= "EMAIL;TYPE=PREF,INTERNET:" . $email;
    $output .= "\n";
  }
  if ($title != "") {
    $output .= "TITLE;CHARSET=utf-8:" . $title;
    $output .= "\n";
  }
  $output .= "ADR;CHARSET=utf-8;TYPE=" . $address_type . ":;;" . $address . ";" . $city . ";" . $region . ";" . $zip . ";" . $country;
  $output .= "\n";
  if ($workphone != "") {
    $output .= "TEL;TYPE=WORK,VOICE:" . $workphone;
    $output .= "\n";
  }
  if ($homephone != "") {
    $output .= "TEL;TYPE=HOME,VOICE:" . $homephone;
    $output .= "\n";
  }
  if ($fax != "") {
    $output .= "TEL;type=FAX:" . $fax;
    $output .= "\n";
  }
  if ($cell != "") {
    $output .= "TEL;TYPE=CELL,VOICE:" . $cell;
    $output .= "\n";
  }
  if ($url != "") {
    $output .= "URL:" . $url;
  }
  if ($photo != "") {

    // Encode data to base64
    $photolink = file_get_contents($photo);
    $photodata = base64_encode($photolink);
    $output .= "PHOTO;ENCODING=BASE64;TYPE=JPEG:" . $photodata;
    $output .= "\n";
  }
  echo "BEGIN:VCARD";
  echo "\n";
  echo $output;
  echo "\n";
  echo "END:VCARD";

  //END:VCARD

  //echo "</pre>";
}