You are here

vcardfield.module in VCard Field 7

File

vcardfield.module
View source
<?php

// $Id$

/**
 * Implementation of hook_field_info().
 */
function vcardfield_field_info() {
  return array(
    'vcardfield' => array(
      'label' => t('VCard'),
      'description' => t('Custom VCard field.'),
      'default_widget' => 'vcardfield_vcardfield_form',
      'default_formatter' => 'vcardfield_default',
    ),
  );
}

/*
* Implementation of hook_menu()
*/
function vcardfield_menu() {
  $items = array();
  $items['getvcard/%/%/%'] = array(
    'title' => '',
    'page callback' => 'vcardfield_create_vcard',
    'access callback' => 'user_access',
    'page arguments' => array(
      1,
      2,
      3,
    ),
    'access arguments' => array(
      'access content',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implements hook_field_widget_info().
 */
function vcardfield_field_widget_info() {
  return array(
    'vcardfield_vcardfield_form' => array(
      'label' => t('VCard Field Form'),
      'field types' => array(
        'vcardfield',
      ),
      'behaviors' => array(
        'multiple values' => FIELD_BEHAVIOR_DEFAULT,
        //Use FIELD_BEHAVIOR_NONE for no default value.
        'default value' => FIELD_BEHAVIOR_DEFAULT,
      ),
    ),
  );
}
function vcardfield_field_widget_info_alter(&$info) {

  // nothing yet
}

/**
 * Implements hook_field_formatter_info().
 */
function vcardfield_field_formatter_info() {
  return array(
    'vcardfield_default' => array(
      'label' => t('Default'),
      'field types' => array(
        'vcardfield',
      ),
    ),
  );
}

/**
 * Implements hook_field_formatter_view().
 */
function vcardfield_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  switch ($display['type']) {
    case 'vcardfield_default':
      foreach ($items as $delta => $item) {
        if ($entity_type == 'node') {
          $item['nid'] = $entity->nid;
        }
        if ($entity_type == 'user') {
          $item['nid'] = $entity->uid;
        }
        $item['type'] = $entity_type;
        $item['delta'] = $delta;
        $element[$delta]['#markup'] = theme('vcardfield', $item);
      }
      break;
  }
  return $element;
}

/**
 * Implementation of hook_field_is_empty().
 */
function vcardfield_field_is_empty($item, $field) {
  if ($field['type'] == 'vcardfield') {
    if (empty($item['first_name']) && empty($item['first_name']) && empty($item['email'])) {
      return TRUE;
    }
  }
  return FALSE;
}

/**
 * Implementation of hook_field_settings_form().
 */
function vcardfield_field_settings_form($field, $instance, $has_data) {
  if ($field['type'] == 'vcardfield') {
    $settings = $field['settings'];
    $form = array();
    return $form;
  }
}

/**
 * Implementation of hook_field_validate().
 */
function vcardfield_field_validate($obj_type, $object, $field, $instance, $langcode, &$items, &$errors) {
  if ($field['type'] == 'vcardfield') {
  }
}

/**
 * Implementation of hook_field_widget_form().
 */
function vcardfield_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $base = $element;
  if ($instance['widget']['type'] == 'vcardfield_vcardfield_form') {
    $widget = $instance['widget'];
    $settings = $widget['settings'];
    $element['vcard_title'] = array(
      '#markup' => variable_get('vcard_title', t('<div><h3>Vcard form</h3><div>')),
    );
    $element['prefix'] = array(
      '#type' => 'textfield',
      '#title' => t('Prefix'),
      '#maxlength' => 10,
      '#default_value' => isset($items[$delta]['prefix']) ? $items[$delta]['prefix'] : NULL,
    );
    $element['first_name'] = array(
      '#type' => 'textfield',
      '#title' => t('First Name'),
      '#maxlength' => 100,
      '#default_value' => isset($items[$delta]['first_name']) ? $items[$delta]['first_name'] : NULL,
    );
    $element['middle_name'] = array(
      '#type' => 'textfield',
      '#title' => t('Middle Name'),
      '#maxlength' => 100,
      '#default_value' => isset($items[$delta]['middle_name']) ? $items[$delta]['middle_name'] : NULL,
    );
    $element['last_name'] = array(
      '#type' => 'textfield',
      '#title' => t('Last Name'),
      '#maxlength' => 100,
      '#default_value' => isset($items[$delta]['last_name']) ? $items[$delta]['last_name'] : NULL,
    );
    $element['suffix'] = array(
      '#type' => 'textfield',
      '#title' => t('Suffix'),
      '#maxlength' => 25,
      '#default_value' => isset($items[$delta]['suffix']) ? $items[$delta]['suffix'] : NULL,
    );
    $element['full_name'] = array(
      '#type' => 'textfield',
      '#title' => t('Full Name'),
      '#maxlength' => 255,
      '#default_value' => isset($items[$delta]['full_name']) ? $items[$delta]['full_name'] : NULL,
    );
    $element['photo'] = array(
      '#type' => 'textfield',
      '#title' => t('Photo'),
      '#maxlength' => 255,
      '#default_value' => isset($items[$delta]['photo']) ? $items[$delta]['photo'] : NULL,
    );
    $element['title'] = array(
      '#type' => 'textfield',
      '#title' => t('Title'),
      '#maxlength' => 100,
      '#default_value' => isset($items[$delta]['title']) ? $items[$delta]['title'] : NULL,
    );
    $element['organization'] = array(
      '#type' => 'textfield',
      '#title' => t('Organization'),
      '#maxlength' => 100,
      '#default_value' => isset($items[$delta]['organization']) ? $items[$delta]['organization'] : NULL,
    );
    $element['address'] = array(
      '#type' => 'textfield',
      '#title' => t('Address'),
      '#maxlength' => 255,
      '#default_value' => isset($items[$delta]['address']) ? $items[$delta]['address'] : NULL,
    );
    $element['city'] = array(
      '#type' => 'textfield',
      '#title' => t('City'),
      '#maxlength' => 100,
      '#default_value' => isset($items[$delta]['city']) ? $items[$delta]['city'] : NULL,
    );
    $element['address_type'] = array(
      '#type' => 'select',
      '#title' => t('Address Type'),
      '#options' => array(
        'work' => t('Work'),
        'home' => t('Home'),
      ),
      '#default_value' => isset($items[$delta]['address_type']) ? $items[$delta]['address_type'] : NULL,
    );
    $element['region'] = array(
      '#type' => 'textfield',
      '#title' => t('Region'),
      '#maxlength' => 100,
      '#default_value' => isset($items[$delta]['region']) ? $items[$delta]['region'] : NULL,
    );
    $element['postalcode'] = array(
      '#type' => 'textfield',
      '#title' => t('Postal Code'),
      '#maxlength' => 15,
      '#default_value' => isset($items[$delta]['postalcode']) ? $items[$delta]['postalcode'] : NULL,
    );
    $element['country'] = array(
      '#type' => 'textfield',
      '#title' => t('Country'),
      '#maxlength' => 100,
      '#default_value' => isset($items[$delta]['country']) ? $items[$delta]['country'] : NULL,
    );
    $element['phone_default'] = array(
      '#type' => 'textfield',
      '#title' => t('Phone Default'),
      '#maxlength' => 25,
      '#default_value' => isset($items[$delta]['phone_default']) ? $items[$delta]['phone_default'] : NULL,
    );
    $element['phone_cell'] = array(
      '#type' => 'textfield',
      '#title' => t('Phone Mobile'),
      '#maxlength' => 25,
      '#default_value' => isset($items[$delta]['phone_cell']) ? $items[$delta]['phone_cell'] : NULL,
      '#group' => 'additional_settingsg',
    );
    $element['phone_fax'] = array(
      '#type' => 'textfield',
      '#title' => t('Phone Fax'),
      '#maxlength' => 25,
      '#default_value' => isset($items[$delta]['phone_fax']) ? $items[$delta]['phone_fax'] : NULL,
      '#group' => 'additional_settingsg',
    );
    $element['phone_home'] = array(
      '#type' => 'textfield',
      '#title' => t('Phone Home'),
      '#maxlength' => 25,
      '#default_value' => isset($items[$delta]['phone_home']) ? $items[$delta]['phone_home'] : NULL,
    );
    $element['email'] = array(
      '#type' => 'textfield',
      '#title' => t('Email'),
      '#maxlength' => 255,
      '#default_value' => isset($items[$delta]['email']) ? $items[$delta]['email'] : NULL,
    );
    $element['link'] = array(
      '#type' => 'textfield',
      '#title' => t('Link'),
      '#maxlength' => 255,
      '#default_value' => isset($items[$delta]['link']) ? $items[$delta]['link'] : NULL,
      '#element_validate' => array(
        '_vcardfield_link_validate',
      ),
    );
    $element['label'] = array(
      '#type' => 'textfield',
      '#title' => t('Link Label'),
      '#maxlength' => 255,
      '#default_value' => isset($items[$delta]['label']) ? $items[$delta]['label'] : 'Get Vcard',
    );
    $element['instance'] = array(
      '#type' => 'hidden',
      '#maxlength' => 255,
      '#default_value' => $element['#field_name'],
    );
  }
  return $element;
}

/**
 * Validation callback for a vcardfield link element.
 */
function _vcardfield_link_validate($element, &$form_state, $form) {
  $value = $element['#value'];
  if (!empty($value) && !valid_url($value, TRUE)) {
    form_error($element, t('Invalid URL.'));
  }
}
function vcardfield_theme($existing, $type, $theme, $path) {
  return array(
    'vcardfield' => array(
      'variables' => array(
        'item' => NULL,
      ),
      'template' => 'vcardfield',
    ),
  );
}
function template_preprocess_vcardfield(&$variables) {
  $variables['vcard_url'] = 'getvcard/' . str_replace('field_', '', $variables['instance']) . '/' . $variables['nid'] . '/' . $variables['delta'] . '/' . $variables['type'];
  $variables['vcard_link'] = l($variables['label'], $variables['vcard_url'], array(
    'attributes' => array(
      'rel' => 'nofollow',
    ),
  ));
  $variables['prefix'] = check_plain($variables['prefix']);
  $variables['first_name'] = check_plain($variables['first_name']);
  $variables['middle_name'] = array_key_exists('middle_name', $variables) ? check_plain($variables['middle_name']) : '';
  $variables['last_name'] = check_plain($variables['last_name']);
  $variables['suffix'] = check_plain($variables['suffix']);
  $variables['full_name'] = check_plain($variables['full_name']);
  $variables['photo'] = array_key_exists('photo', $variables) ? check_plain($variables['photo']) : '';
  $variables['title'] = check_plain($variables['title']);
  $variables['organization'] = check_plain($variables['organization']);
  $variables['address_type'] = check_plain($variables['address_type']);
  $variables['address'] = check_plain($variables['address']);
  $variables['city'] = check_plain($variables['city']);
  $variables['region'] = check_plain($variables['region']);
  $variables['country'] = check_plain($variables['country']);
  $variables['phone_default'] = check_plain($variables['phone_default']);
  $variables['phone_cell'] = check_plain($variables['phone_cell']);
  $variables['phone_fax'] = check_plain($variables['phone_fax']);
  $variables['phone_home'] = check_plain($variables['phone_home']);
  $variables['email'] = check_plain($variables['email']);
  $variables['link'] = check_plain($variables['link']);
}

/*
 * Function to print Vcard
 */
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>";
}