You are here

contactinfo.module in Contact Info 7

Collects contact information and displays it in an hCard block.

File

contactinfo.module
View source
<?php

/**
 * @file
 * Collects contact information and displays it in an hCard block.
 */

/**
 * Implements hook_help().
 */
function contactinfo_help($path, $arg) {
  switch ($path) {
    case 'admin/help#contactinfo':
      $output = '<p>' . t('Contact information that you provide will be displayed on the site in the <a href="http://microformats.org/wiki/hcard">hCard microformat</a>. An hCard is a small bundle of code that you want to put on your web site so that Google Maps (and other mapping services) can more easily index the site&rsquo;s location information.') . '</p>';
      return $output;
    case 'admin/config/system/contactinfo':
      return '<p>' . t("Enter your site’s contact information into the appropriate fields. All fields are optional.") . '</p>';
  }
}

/**
 * Implements hook_permission().
 */
function contactinfo_permission() {
  return array(
    'administer contactinfo' => array(
      'title' => t('Administer contact information'),
      'description' => t('Edit contact information for this site.'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function contactinfo_menu() {
  $items['admin/config/system/contactinfo'] = array(
    'title' => 'Contact information',
    'description' => 'Configure contact information that is publicly displayed on this site.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'contactinfo_admin_settings',
    ),
    'access arguments' => array(
      'administer contactinfo',
    ),
    'file' => 'contactinfo.form.inc',
    'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
  );
  return $items;
}

/**
 * Implements hook_theme().
 */
function contactinfo_theme($existing, $type, $theme, $path) {
  return array(
    'contactinfo_admin_settings' => array(
      'render element' => 'form',
    ),
    'contactinfo' => array(
      'variables' => array(
        'contactinfo' => NULL,
        'type' => 'personal',
        'given_name' => NULL,
        'family_name' => NULL,
        'org' => NULL,
        'tagline' => NULL,
        'street_address' => NULL,
        'extended_address' => NULL,
        'locality' => NULL,
        'region' => NULL,
        'postal_code' => NULL,
        'country' => NULL,
        'longitude' => NULL,
        'latitude' => NULL,
        'phones' => array(),
        'faxes' => array(),
        'email' => NULL,
      ),
      'template' => 'contactinfo',
    ),
  );
}

/**
 * Theme preprocess function for the contact information block.
 *
 * @param array $variables
 *   Template variables as defined by contactinfo_theme().
 *   $variable['contactinfo'] is equivalent to contactinfo_get_contactinfo().
 *
 * @see contactinfo_theme()
 * @see contactinfo_get_contactinfo()
 */
function template_preprocess_contactinfo(array &$variables) {
  drupal_add_css(drupal_get_path('module', 'contactinfo') . '/css/contactinfo.css');

  // Build $variables from scratch.
  $contactinfo = $variables['contactinfo'];
  $variables['type'] = $contactinfo['type'];
  $variables['given_name'] = check_plain($contactinfo['fn_n']['given-name']);
  $variables['family_name'] = check_plain($contactinfo['fn_n']['family-name']);
  $variables['org'] = $contactinfo['use_site_name'] ? check_plain(variable_get('site_name', '')) : check_plain($contactinfo['org']);
  $variables['street_address'] = check_plain($contactinfo['adr']['street-address']);
  $variables['extended_address'] = check_plain($contactinfo['adr']['extended-address']);
  $variables['locality'] = check_plain($contactinfo['adr']['locality']);
  $variables['region'] = check_plain($contactinfo['adr']['region']);
  $variables['postal_code'] = check_plain($contactinfo['adr']['postal-code']);
  $variables['country'] = check_plain($contactinfo['adr']['country-name']);
  $variables['longitude'] = check_plain($contactinfo['location']['longitude']);
  $variables['latitude'] = check_plain($contactinfo['location']['latitude']);
  $variables['tagline'] = $contactinfo['use_site_slogan'] ? check_plain(variable_get('site_slogan', '')) : check_plain($contactinfo['tagline']);

  // Generate formatted longitude and latitude.
  $variables['longitude_formatted'] = contactinfo_coord_convert($variables['longitude'], 'longitude');
  $variables['latitude_formatted'] = contactinfo_coord_convert($variables['latitude'], 'latitude');

  // Generates the output for the 'phones' variable.
  if ($contactinfo['phone']['voice']) {
    $phone_text = check_plain($contactinfo['phone']['voice']);
    $phones = explode(',', $phone_text);
    $variables['phones'] = array_map('trim', $phones);
  }

  // Generates the output for the 'faxes' variable.
  if ($contactinfo['phone']['fax']) {
    $fax_text = check_plain($contactinfo['phone']['fax']);
    $faxes = explode(',', $fax_text);
    $variables['faxes'] = array_map('trim', $faxes);
  }

  // Generate the output for the 'email' variable.
  if ($contactinfo['email']) {
    $email = check_plain($contactinfo['email']);

    // Use obfuscation provided by invisimail module.
    if (function_exists('invisimail_encode_html')) {
      $variables['email'] = invisimail_encode_html($email);
      $variables['email_url'] = INVISIMAIL_MAILTO_ASCII . $variables['email'];
    }
    else {
      $variables['email'] = $email;
      $variables['email_url'] = 'mailto:' . $email;
    }
  }

  // Generate ID.
  $id = 'contactinfo';
  if ($contactinfo['type'] == 'personal') {
    $id .= !empty($contactinfo['fn_n']['given-name']) ? '-' . check_plain($contactinfo['fn_n']['given-name']) : '';
    $id .= !empty($contactinfo['fn_n']['family-name']) ? '-' . check_plain($contactinfo['fn_n']['family-name']) : '';
  }
  else {
    $id .= !empty($contactinfo['org']) ? '-' . check_plain($contactinfo['org']) : '';
  }
  $variables['id'] = drupal_html_id($id);
}

/**
 * Validate an email address.
 */
function contactinfo_validate_email($element, &$form_state) {
  if (!empty($element['#value']) && !valid_email_address($element['#value'])) {
    form_error($element, t('You must enter a valid e-mail address.'));
  }
}

/**
 * Implements hook_block_info().
 */
function contactinfo_block_info() {
  $blocks['hcard'] = array(
    'info' => 'Contact information',
    'weight' => 10,
    'status' => 1,
    'region' => 'footer',
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function contactinfo_block_view($delta) {
  $block = array();
  switch ($delta) {
    case 'hcard':
      $contactinfo = contactinfo_get_contactinfo();
      if ($contactinfo) {
        $block = array(
          'subject' => '',
          'content' => theme('contactinfo', array(
            'contactinfo' => $contactinfo,
          )),
        );
      }
      return $block;
  }
}

/**
 * Theme function for the Contact Info settings form.
 *
 * It's just a wrapper so that themers can do what they want with this form.
 */
function theme_contactinfo_admin_settings($variables) {
  $form = $variables['form'];
  return drupal_render_children($form);
}

/**
 * Implements hook_contextual_links_view_alter().
 */
function contactinfo_contextual_links_view_alter(array &$element, array $items) {
  $block = isset($element['#element']['#block']) ? $element['#element']['#block'] : NULL;
  if (is_object($block) && $block->module == 'contactinfo' && user_access('administer contactinfo')) {
    $element['#links']['contactinfo-edit'] = array(
      'title' => t('Edit information'),
      'href' => 'admin/config/system/contactinfo',
      'query' => drupal_get_destination(),
      'attributes' => array(
        'title' => t('Edit your contact information'),
      ),
    );
  }
}

/**
 * Helper function to convert longitude or latitude points.
 *
 * Convert a decimal-degree longitude or latitude point into degrees and
 * decimal minutes.
 *
 * @param float $decimal
 *   Decimal value for a longitude or latitude point.
 * @param string $direction
 *   Strings 'longitude' or 'latitude' are the only acceptable inputs.
 *
 * @return string
 *   String containing a single character for N, S, E, or W, the degrees as
 *   whole number, and minutes as a decimal value.
 */
function contactinfo_coord_convert($decimal, $direction) {
  $decimal = floatval($decimal);
  if (!$decimal) {
    return FALSE;
  }
  switch ($direction) {
    case 'longitude':
      $coord_direction = $decimal < 0 ? 'W' : 'E';
      break;
    case 'latitude':
      $coord_direction = $decimal < 0 ? 'S' : 'N';
      break;
    default:
      return FALSE;
  }
  $coord_degrees = intval($decimal);
  $coord_minutes = abs(fmod($decimal, 1) * 60);
  return $coord_direction . ' ' . $coord_degrees . '° ' . $coord_minutes . '"';
}

/**
 * Helper function to return saved contact information.
 *
 * @return array
 *   All contact information saved from the settings form.
 */
function contactinfo_get_contactinfo() {

  // Get variable defaults.
  $default = contactinfo_defaults();

  // Get saved contact information.
  $contactinfo = variable_get('contactinfo', $default);

  // Merge default values and saved data to ensure all array keys are present.
  return array_merge($default, $contactinfo);
}

/**
 * Returns the default values for the site contact information.
 *
 * @return array
 *   Default values for all contact info keys.
 */
function contactinfo_defaults() {
  return array(
    'type' => 'personal',
    'fn_n' => array(
      'given-name' => '',
      'family-name' => '',
    ),
    'org' => '',
    'use_site_name' => 0,
    'tagline' => '',
    'use_site_slogan' => 0,
    'adr' => array(
      'street-address' => '',
      'extended-address' => '',
      'locality' => '',
      'region' => '',
      'postal-code' => '',
      'country-name' => '',
    ),
    'location' => array(
      'longitude' => '',
      'latitude' => '',
    ),
    'phone' => array(
      'voice' => '',
      'fax' => '',
    ),
    'email' => '',
  );
}

Functions

Namesort descending Description
contactinfo_block_info Implements hook_block_info().
contactinfo_block_view Implements hook_block_view().
contactinfo_contextual_links_view_alter Implements hook_contextual_links_view_alter().
contactinfo_coord_convert Helper function to convert longitude or latitude points.
contactinfo_defaults Returns the default values for the site contact information.
contactinfo_get_contactinfo Helper function to return saved contact information.
contactinfo_help Implements hook_help().
contactinfo_menu Implements hook_menu().
contactinfo_permission Implements hook_permission().
contactinfo_theme Implements hook_theme().
contactinfo_validate_email Validate an email address.
template_preprocess_contactinfo Theme preprocess function for the contact information block.
theme_contactinfo_admin_settings Theme function for the Contact Info settings form.