You are here

getlocations_fields.module in Get Locations 7

Same filename and directory in other branches
  1. 7.2 modules/getlocations_fields/getlocations_fields.module

getlocations_fields.module @author Bob Hutchinson http://drupal.org/user/52366 @copyright GNU GPL

Defines getlocations fields type.

File

modules/getlocations_fields/getlocations_fields.module
View source
<?php

/**
 * @file
 * getlocations_fields.module
 * @author Bob Hutchinson http://drupal.org/user/52366
 * @copyright GNU GPL
 *
 * Defines getlocations fields type.
 */
define('GETLOCATIONS_FIELDS_PATH', drupal_get_path('module', 'getlocations_fields'));

/**
 * Implements hook_help().
 */
function getlocations_fields_help($path, $arg) {
  switch ($path) {
    case 'admin/help#getlocations':
      $output = '<p>' . t('Provides a getlocations geocoder field type.') . '</p>';
      return $output;
  }
}

/**
 * Implements hook_init().
 */
function getlocations_fields_init() {
  module_load_include('inc', 'getlocations_fields', 'getlocations_fields.functions');
  global $user;
  if ($user->uid > 0 && !getlocations_fields_column_check('data')) {
    drupal_set_message(t('You need to run update <a href="@url">now</a>.', array(
      '@url' => url('update.php'),
    )), 'error');
  }
}

/**
 * Implements hook_menu().
 */
function getlocations_fields_menu() {
  $items = array();
  $items[GETLOCATIONS_ADMIN_PATH . '/fields'] = array(
    'title' => 'Fields',
    'description' => 'Configure Getlocations fields',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'getlocations_fields_settings_form',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'getlocations_fields.admin.inc',
    'weight' => 2,
  );

  #  // ajax callback to fetch country code from full name

  #  $items['getlocations_fields/countryinfo'] = array(

  #    'page callback' => 'getlocations_fields_countryinfo',

  #    'access arguments' => array('access content'),

  #    'type' => MENU_CALLBACK,

  #  );

  // autocomplete paths
  $items['getlocations_fields/country_autocomplete'] = array(
    'page callback' => 'getlocations_fields_country_autocomplete',
    'access arguments' => array(
      'access content',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['getlocations_fields/province_autocomplete'] = array(
    'page callback' => 'getlocations_fields_province_autocomplete',
    'access arguments' => array(
      'access content',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['getlocations_fields/city_autocomplete'] = array(
    'page callback' => 'getlocations_fields_city_autocomplete',
    'access arguments' => array(
      'access content',
    ),
    'type' => MENU_CALLBACK,
  );
  if (module_exists('smart_ip')) {
    $items['getlocations_fields/smart_ip'] = array(
      'page callback' => 'getlocations_fields_smart_ip',
      'access arguments' => array(
        'access content',
      ),
      'type' => MENU_CALLBACK,
    );
  }
  return $items;
}

/**
 * Implements hook_views_api().
 */
function getlocations_fields_views_api() {
  return array(
    'api' => 3,
    'path' => GETLOCATIONS_FIELDS_PATH . '/views',
  );
}

/**
 * Implements hook_field_info().
 * Define Field API field types.
 *
 * @return
 *   An array whose keys are field type names and whose values are arrays
 *   describing the field type.
 */
function getlocations_fields_field_info() {
  $info = getlocations_fields_field_info_defaults();
  return $info;
}

/**
 * Implements hook_field_validate().
 * Validate this module's field data.
 *
 * This hook gives us a chance to validate content that's in our
 * field. We're really only interested in the $items parameter, since
 * it holds arrays representing content in the field we've defined.
 *
 * @param $entity_type
 *   The type of $entity.
 * @param $entity
 *   The entity for the operation.
 * @param $field
 *   The field structure for the operation.
 * @param $instance
 *   The instance structure for $field on $entity's bundle.
 * @param $langcode
 *   The language associated with $items.
 * @param $items
 *   $entity->{$field['field_name']}[$langcode], or an empty array if unset.
 * @param $errors
 *   The array of errors (keyed by field name, language code, and delta) that
 *   have already been reported for the entity.
 */
function getlocations_fields_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  if (!empty($items)) {
    $required = $instance['required'];
    $cardinality = $field['cardinality'];
    $count_items = count($items);
    $fieldnames = array(
      'name' => t('Name'),
      'street' => t('Street'),
      'additional' => t('Additional'),
      'city' => t('City/Town'),
      'province' => t('Province/State/County'),
      'postal_code' => t('Post code/Zip code'),
      'country' => t('Country'),
      'phone' => t('Phone'),
      'mobile' => t('Mobile'),
      'fax' => t('Fax'),
    );
    $item_count = 0;
    foreach ($items as $delta => $item) {
      if ($item['active']) {
        if (isset($item['delete_location']) && $item['delete_location']) {
          return;
        }

        // unlimited so don't error on the last one
        if ($cardinality == -1 && $item_count == $count_items - 1) {
          return;
        }
        if ($required && empty($item['latitude']) && empty($item['longitude'])) {
          $errors[$field['field_name']][$langcode][$delta][] = array(
            'error' => 'latlon_empty',
            'message' => t('Latitude and Longitude empty'),
            'field_name' => $field['field_name'],
            'lang' => $langcode,
            'delta' => $delta,
          );
        }
        if (isset($item['getlocations_required']) && $item['getlocations_required']) {
          $requireds = explode(',', $item['getlocations_required']);
          foreach ($requireds as $id) {
            if (isset($item[$id]) && empty($item[$id])) {
              $errors[$field['field_name']][$langcode][$delta][] = array(
                'error' => 'field_required_' . $id,
                'message' => t('The %tt field is required', array(
                  '%tt' => $fieldnames[$id],
                )),
              );
            }
          }
        }
      }
      $item_count++;
    }
  }
}

/**
 * Implements hook_field_insert().
 * Define custom insert behavior for this module's field types.
 *
 * @param $entity_type
 *   The type of $entity.
 * @param $entity
 *   The entity for the operation.
 * @param $field
 *   The field structure for the operation.
 * @param $instance
 *   The instance structure for $field on $entity's bundle.
 * @param $langcode
 *   The language associated with $items.
 * @param $items
 *   $entity->{$field['field_name']}[$langcode], or an empty array if unset.
 */
function getlocations_fields_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
  $settings = FALSE;
  if (isset($field['settings'])) {
    $settings = $field['settings'];
  }
  $settings['display_settings'] = getlocations_fields_get_display_settings($instance['display']['default']['settings']);
  if (!empty($items)) {
    foreach ($items as $key => $item) {
      if (empty($item['latitude']) || empty($item['longitude'])) {
        unset($items[$key]);
      }
    }
    $criteria = array();
    if ($entity_type == 'node') {
      $criteria = array(
        'field_name' => $field['field_name'],
        'vid' => $entity->vid,
        'nid' => $entity->nid,
      );
    }
    elseif ($entity_type == 'user' || $entity_type == 'profile2') {
      $criteria = array(
        'field_name' => $field['field_name'],
        'uid' => $entity->uid,
      );
    }
    elseif ($entity_type == 'comment') {

      //
      $criteria = array(
        'field_name' => $field['field_name'],
        'cid' => $entity->cid,
      );
    }
    elseif ($entity_type == 'taxonomy_term') {
      $criteria = array(
        'field_name' => $field['field_name'],
        'tid' => $entity->tid,
      );
    }
    if (!empty($criteria)) {
      $items = getlocations_fields_save_locations($items, $criteria, $settings, 'insert');
    }
  }
}

/**
 * Implements hook_field_update().
 * Define custom update behavior for this module's field types.
 *
 * @param $entity_type
 *   The type of $entity.
 * @param $entity
 *   The entity for the operation.
 * @param $field
 *   The field structure for the operation.
 * @param $instance
 *   The instance structure for $field on $entity's bundle.
 * @param $langcode
 *   The language associated with $items.
 * @param $items
 *   $entity->{$field['field_name']}[$langcode], or an empty array if unset.
 */
function getlocations_fields_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
  $settings = FALSE;
  if (isset($field['settings'])) {
    $settings = $field['settings'];
  }
  $mode = 'update';
  if (isset($entity->revision) && $entity->revision && (isset($entity->nid) && isset($entity->vid) && $entity->nid !== $entity->vid)) {
    $mode = 'insert';
  }
  $settings['display_settings'] = getlocations_fields_get_display_settings($instance['display']['default']['settings']);
  if (!empty($items)) {
    $criteria = array();

    // feeds sanity
    if (module_exists('feeds') && module_exists('getlocations_feeds') && isset($entity->feeds_item) && !$entity->feeds_item->is_new) {

      // feeds update, set active so that glid can be added later
      foreach ($items as $key => $item) {
        if (!isset($items[$key]['active'])) {
          $items[$key]['active'] = 1;
        }
      }
    }
    if ($entity_type == 'node') {
      $criteria = array(
        'field_name' => $field['field_name'],
        'vid' => $entity->vid,
        'nid' => $entity->nid,
      );
    }
    elseif ($entity_type == 'user' || $entity_type == 'profile2') {
      $criteria = array(
        'field_name' => $field['field_name'],
        'uid' => $entity->uid,
      );
    }
    elseif ($entity_type == 'comment') {
      $criteria = array(
        'field_name' => $field['field_name'],
        'cid' => $entity->cid,
      );
    }
    elseif ($entity_type == 'taxonomy_term') {
      $criteria = array(
        'field_name' => $field['field_name'],
        'tid' => $entity->tid,
      );
    }
    if (!empty($criteria)) {

      // look for delete_location in $items
      if (!$instance['required'] || $instance['required'] && count($items) > 1) {
        foreach ($items as $key => $item) {
          if (isset($item['delete_location']) && $item['delete_location'] > 0) {
            getlocations_fields_delete_record($items[$key], $criteria);
            unset($items[$key]);
          }
        }
      }
      $items = getlocations_fields_save_locations($items, $criteria, $settings, $mode);
    }
  }
}

/**
 * Implements hook_field_delete().
 * Define custom delete behavior for this module's field types.
 *
 * @param $entity_type
 *   The type of $entity.
 * @param $entity
 *   The entity for the operation.
 * @param $field
 *   The field structure for the operation.
 * @param $instance
 *   The instance structure for $field on $entity's bundle.
 * @param $langcode
 *   The language associated with $items.
 * @param $items
 *   $entity->{$field['field_name']}[$langcode], or an empty array if unset.
 */
function getlocations_fields_field_delete($entity_type, $entity, $field, $instance, $langcode, &$items) {
  if (!empty($items)) {
    $criteria = array();
    if ($entity_type == 'node') {
      $criteria = array(
        'field_name' => $field['field_name'],
        'nid' => $entity->nid,
        'vid' => $entity->vid,
      );
    }
    elseif ($entity_type == 'user' || $entity_type == 'profile2') {
      $criteria = array(
        'field_name' => $field['field_name'],
        'uid' => $entity->uid,
      );
    }
    elseif ($entity_type == 'comment') {
      $criteria = array(
        'field_name' => $field['field_name'],
        'cid' => $entity->cid,
      );
    }
    elseif ($entity_type == 'taxonomy_term') {
      $criteria = array(
        'field_name' => $field['field_name'],
        'tid' => $entity->tid,
      );
    }
    if (!empty($criteria)) {
      $items = getlocations_fields_save_locations($items, $criteria, FALSE, 'delete');
    }
  }
}

/**
 * Implements hook_field_delete_revision().
 * Define custom revision delete behavior for this module's field types.
 *
 * @param $entity_type
 *   The type of $entity.
 * @param $entity
 *   The entity for the operation.
 * @param $field
 *   The field structure for the operation.
 * @param $instance
 *   The instance structure for $field on $entity's bundle.
 * @param $langcode
 *   The language associated with $items.
 * @param $items
 *   $entity->{$field['field_name']}[$langcode], or an empty array if unset.
 */
function getlocations_fields_field_delete_revision($entity_type, $entity, $field, $instance, $langcode, &$items) {
  if (!empty($items)) {
    if ($entity_type == 'node') {
      $criteria = array(
        'field_name' => $field['field_name'],
        'vid' => $entity->vid,
      );
      $items = getlocations_fields_save_locations($items, $criteria, FALSE, 'delete_revision');
    }
  }
}

/**
 * Implements hook_field_load().
 * Define custom load behavior for this module's field types.
 * http://api.drupal.org/api/drupal/modules--field--field.api.php/function/hook_field_load/7
 *
 * @param $entity_type
 *   The type of $entity.
 * @param $entities
 *   Array of entities being loaded, keyed by entity ID.
 * @param $field
 *   The field structure for the operation.
 * @param $instances
 *   Array of instance structures for $field for each entity, keyed by entity ID.
 * @param $langcode
 *   The language code associated with $items.
 * @param $items
 *   Array of field values already loaded for the entities, keyed by entity ID.
 *   Store your changes in this parameter (passed by reference).
 * @param $age
 *   FIELD_LOAD_CURRENT to load the most recent revision for all fields, or
 *   FIELD_LOAD_REVISION to load the version indicated by each entity.
 */
function getlocations_fields_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
  foreach ($entities as $id => $entity) {
    foreach ($items[$id] as $delta => $item) {
      $location = array();

      // Load the location if it exists.
      // If we are previewing a new node it will not.
      if (!empty($item['glid'])) {
        $location = getlocations_fields_load_location($item['glid']);
      }

      // Combine the item with the location loaded from the database.
      // This will allow $item to display in the case of previewing a node.
      $items[$id][$delta] = array_merge($location, $item);
    }
  }
}

/**
 * Implements hook_field_settings_form().
 * Add settings to a field settings form.
 *
 * Invoked from field_ui_field_settings_form() to allow the module defining the
 * field to add global settings (i.e. settings that do not depend on the bundle
 * or instance) to the field settings form. If the field already has data, only
 * include settings that are safe to change.
 *
 * @todo: Only the field type module knows which settings will affect the
 * field's schema, but only the field storage module knows what schema
 * changes are permitted once a field already has data. Probably we need an
 * easy way for a field type module to ask whether an update to a new schema
 * will be allowed without having to build up a fake $prior_field structure
 * for hook_field_update_forbid().
 *
 * @param $field
 *   The field structure being configured.
 * @param $instance
 *   The instance structure being configured.
 * @param $has_data
 *   TRUE if the field already has data, FALSE if not.
 *
 * @return
 *   The form definition for the field settings.
 */
function getlocations_fields_field_settings_form($field, $instance, $has_data) {
  $settings = $field['settings'];
  if (empty($settings)) {
    $settings = getlocations_fields_field_info();
  }
  $form = array();
  $form += getlocations_fields_input_settings_form($settings);

  // validation for getlocations_fields_input_settings_form
  $form['input_address_width']['#element_validate'] = array(
    'element_validate_integer_positive',
  );
  $form['input_name_width']['#element_validate'] = array(
    'element_validate_integer_positive',
  );
  $form['input_street_width']['#element_validate'] = array(
    'element_validate_integer_positive',
  );
  $form['input_additional_width']['#element_validate'] = array(
    'element_validate_integer_positive',
  );
  $form['input_city_width']['#element_validate'] = array(
    'element_validate_integer_positive',
  );
  $form['input_province_width']['#element_validate'] = array(
    'element_validate_integer_positive',
  );
  $form['input_postal_code_width']['#element_validate'] = array(
    'element_validate_integer_positive',
  );
  $form['input_latitude_width']['#element_validate'] = array(
    'element_validate_integer_positive',
  );
  $form['input_longitude_width']['#element_validate'] = array(
    'element_validate_integer_positive',
  );
  $form['input_country_width']['#element_validate'] = array(
    'element_validate_integer_positive',
  );
  $form['input_phone_width']['#element_validate'] = array(
    'element_validate_integer_positive',
  );
  $form['input_mobile_width']['#element_validate'] = array(
    'element_validate_integer_positive',
  );
  $form['input_fax_width']['#element_validate'] = array(
    'element_validate_integer_positive',
  );
  $form['input_map_show'] = getlocations_element_map_checkbox(t('Show map on input form'), $settings['input_map_show']);
  $form['mapwidth'] = getlocations_element_map_tf(t('Input form Map width'), $settings['mapwidth'], t('The width of a map, as a CSS length or percentage. Examples: <em>50px</em>, <em>5em</em>, <em>2.5in</em>, <em>95%</em>'), 10, 10, TRUE);
  $form['mapwidth']['#element_validate'] = array(
    'getlocations_element_validate_dim',
  );
  $form['mapheight'] = getlocations_element_map_tf(t('Input form Map height'), $settings['mapheight'], t('The height of a map, as a CSS length or percentage. Examples: <em>50px</em>, <em>5em</em>, <em>2.5in</em>, <em>95%</em>'), 10, 10, TRUE);
  $form['mapheight']['#element_validate'] = array(
    'getlocations_element_validate_dim',
  );
  $form['latlong'] = getlocations_element_map_tf(t('Input form Map center'), $settings['latlong'], t('The default center coordinates of a map, expressed as a decimal latitude and longitude, separated by a comma. This must not be 0,0'), 40, 50, TRUE);
  $form['latlong']['#element_validate'] = array(
    'getlocations_element_validate_latlon',
  );

  // smart_ip latlon option
  if (module_exists('smart_ip')) {
    $form['use_smart_ip_latlon'] = getlocations_element_map_checkbox(t('Use Smart IP to set Latitude/Longitude'), $settings['use_smart_ip_latlon'], t('Use Smart IP module to set the default center coordinates.'));
  }
  else {
    $form['use_smart_ip_latlon'] = array(
      '#type' => 'value',
      '#value' => 0,
    );
  }

  // what3words
  $what3words_lic = variable_get('getlocations_what3words_lic', array(
    'key' => '',
    'url' => 'http://api.what3words.com',
  ));
  if ($what3words_lic['key']) {
    $form['what3words_collect'] = getlocations_element_map_checkbox(t('Collect What3Words'), $settings['what3words_collect'], '');
  }

  // input form marker
  $markers = getlocations_get_marker_titles();
  $form['input_map_marker'] = getlocations_element_map_marker(t('Input form Map marker'), $markers, $settings['input_map_marker']);
  $form['zoom'] = getlocations_element_map_zoom(t('Zoom'), $settings['zoom'], t('The default zoom level of a map.'));
  $form += getlocations_map_display_options_form($settings, FALSE, FALSE);
  unset($form['#theme']);
  $form['#theme'] = 'getlocations_fields_field_settings_form';
  return $form;
}

/**
 * Implements hook_field_is_empty().
 * Define what constitutes an empty item for a field type.
 * hook_field_is_emtpy() is where Drupal asks us if this field is empty.
 * Return TRUE if it does not contain data, FALSE if it does. This lets
 * the form API flag an error when required fields are empty.
 *
 * @param $item
 *   An item that may or may not be empty.
 * @param $field
 *   The field to which $item belongs.
 * @return
 *   TRUE if $field's type considers $item not to contain any data;
 *   FALSE otherwise.
 */
function getlocations_fields_field_is_empty($item, $field) {
  return FALSE;
}

/**
 * Implements hook_field_formatter_info().
 *
 * Declare information about a formatter.
 *
 * @return
 *   An array keyed by formatter name. Each element of the array is an associative
 *   array with these keys and values:
 *   - "label": The human-readable label for the formatter.
 *   - "field types": An array of field type names that can be displayed using
 *     this formatter.
 *
 * @see getlocations_fields_field_formatter_view()
 */
function getlocations_fields_field_formatter_info() {
  $formatters = array(
    'getlocations_fields_default' => array(
      'label' => t('Getlocations Field'),
      'field types' => array(
        'getlocations_fields',
      ),
      'settings' => getlocations_fields_field_formatter_info_defaults(),
    ),
  );
  return $formatters;
}

/**
 * Implements hook_field_formatter_view().
 * Build a renderable array for a field value.
 *
 * @param $entity_type
 *   The type of $entity.
 * @param $entity
 *   The entity being displayed.
 * @param $field
 *   The field structure.
 * @param $instance
 *   The field instance.
 * @param $langcode
 *   The language associated with $items.
 * @param $items
 *   Array of values for this field.
 * @param $display
 *   The display settings to use, as found in the 'display' entry of instance definitions.
 * @return
 *   A renderable array for the $items, as an array of child elements keyed
 *   by numeric indexes starting from 0.
 *
 * @see getlocations_fields_field_formatter_info()
 */
function getlocations_fields_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  drupal_add_css(GETLOCATIONS_FIELDS_PATH . '/getlocations_fields.css');
  $settings = $display['settings'];
  if (empty($settings)) {
    $settings = getlocations_fields_field_formatter_info_defaults();
  }

  // reset markers, in case there are any left from old method
  $getlocations_defaults = getlocations_defaults();
  $settings['node_map_marker'] = $getlocations_defaults['node_map_marker'];
  $settings['user_map_marker'] = $getlocations_defaults['user_map_marker'];
  $settings['vocabulary_map_marker'] = $getlocations_defaults['vocabulary_map_marker'];
  $settings['comment_map_marker'] = $getlocations_defaults['comment_map_marker'];
  $settings['term_map_marker'] = $getlocations_defaults['term_map_marker'];
  $field_name = '';
  if (isset($field['field_name'])) {
    $field_name = $field['field_name'];
  }
  if (empty($field_name) || !isset($entity->{$field_name}) || empty($entity->{$field_name})) {
    return;
  }
  $locations = array();
  $supported_entity_types = array(
    'node',
    'user',
    'taxonomy_term',
    'comment',
    'profile2',
  );
  $element = array();
  if (!in_array($entity_type, $supported_entity_types)) {
    return $element;
  }
  if ($entity_type == 'node') {
    $locations = getlocations_fields_load_locations($entity->vid, 'vid', $field_name);
    if (count($locations)) {
      $getlocations_node_marker = variable_get('getlocations_node_marker', array(
        'enable' => 0,
      ));
      if ($getlocations_node_marker['enable']) {
        if ($types = getlocations_get_types()) {
          foreach ($types as $type => $name) {
            $field_names = getlocations_get_fieldname2($type, $entity_type);
            foreach ($field_names as $f_name) {
              if ($field_name && $f_name == $field_name) {
                $mkey = 'node_marker__' . strtolower($type) . '__' . $field_name;
                if (isset($settings[$mkey])) {
                  $settings['map_marker'] = $settings[$mkey];
                }
                else {
                  $settings['map_marker'] = isset($getlocations_node_marker['content_type'][$type]['field_name'][$field_name]['map_marker']) ? $getlocations_node_marker['content_type'][$type]['field_name'][$field_name]['map_marker'] : $settings['node_map_marker'];
                }
              }
            }
          }
        }
      }
      else {
        $settings['map_marker'] = $settings['node_map_marker'];
      }

      // term marker. this will override node marker if there is one
      $settings['map_marker'] = getlocations_get_term_marker($entity->nid, $settings['map_marker']);
    }
  }
  elseif ($entity_type == 'taxonomy_term' && module_exists('taxonomy')) {
    $locations = getlocations_fields_load_locations($entity->tid, 'tid', $field_name);
    if (count($locations)) {
      $settings['map_marker'] = $settings['vocabulary_map_marker'];
      $getlocations_vocabulary_marker = variable_get('getlocations_vocabulary_marker', array(
        'enable' => 0,
      ));
      if ($getlocations_vocabulary_marker['enable']) {
        if ($types = getlocations_get_vocabularies()) {
          foreach ($types as $type => $name) {
            $f_name = getlocations_get_fieldname($type, $entity_type);
            if ($f_name == $field_name) {
              $mkey = 'vocabulary_marker_' . $field_name;
              if (isset($settings[$mkey])) {
                $settings['map_marker'] = $settings[$mkey];
              }
              else {
                $settings['map_marker'] = isset($getlocations_vocabulary_marker['vocabulary'][$type]['map_marker']) ? $getlocations_vocabulary_marker['vocabulary'][$type]['map_marker'] : $settings['vocabulary_map_marker'];
              }
            }
          }
        }
      }
      else {
        $settings['map_marker'] = $settings['vocabulary_map_marker'];
      }
    }
  }
  elseif ($entity_type == 'user' || $entity_type == 'profile2') {
    $locations = getlocations_fields_load_locations($entity->uid, 'uid', $field_name);
    if (count($locations)) {
      $settings['map_marker'] = $settings['user_map_marker'];
    }
  }
  elseif ($entity_type == 'comment') {
    $locations = getlocations_fields_load_locations($entity->cid, 'cid', $field_name);
    if (count($locations)) {
      $settings['map_marker'] = $settings['comment_map_marker'];
    }
  }
  switch ($display['type']) {
    case 'getlocations_fields_default':
      if (count($locations)) {

        #      foreach ($items as $delta => $item) {

        #        $element[$delta] = array(

        #          '#theme' => 'getlocations_fields_show',

        #          '#locations' => $locations,

        #          '#settings' => $settings,

        #        );

        #      }
        $element[0] = array(
          '#theme' => 'getlocations_fields_show',
          '#locations' => $locations,
          '#settings' => $settings,
        );
      }
  }
  return $element;
}

/**
 * Implements hook_field_formatter_settings_form().
 * Returns form elements for a formatter's settings.
 *
 * @param $field
 *   The field structure being configured.
 * @param $instance
 *   The instance structure being configured.
 * @param $view_mode
 *   The view mode being configured.
 * @param $form
 *   The (entire) configuration form array, which will usually have no use here.
 * @param $form_state
 *   The form state of the (entire) configuration form.
 *
 * @return
 *   The form elements for the formatter settings.
 */
function getlocations_fields_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  $getlocations_fields_paths = getlocations_fields_paths_get();
  drupal_add_js($getlocations_fields_paths['getlocations_fields_formatter_path']);
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $getlocations_fields_defaults = getlocations_fields_defaults();
  if (empty($settings)) {
    $settings = $getlocations_fields_defaults;
  }

  // $instance['entity_type'] contains node, user etc
  $entity_type = $instance['entity_type'];
  $element = array();

  // address display options
  $element += getlocations_fields_display_settings_form($settings);
  $element['display_mapwidth']['#element_validate'] = array(
    'getlocations_element_validate_dim',
  );
  $element['display_mapheight']['#element_validate'] = array(
    'getlocations_element_validate_dim',
  );

  // map display options
  $element += getlocations_map_display_options_form($settings, FALSE);

  // markeraction
  $element += getlocations_markeraction_form($settings);
  if (module_exists('getdirections')) {
    $element['getdirections_link'] = getlocations_element_map_checkbox(t('Link to Getdirections in bubble'), $settings['getdirections_link'], t('Include a link to the Getdirections page in InfoBubble/InfoWindow.'));
  }

  // views_search_marker etc
  if (module_exists('views')) {
    $element += getlocations_fields_views_search_form($settings);
  }

  // streetview overlay settings
  $element += getlocations_fields_sv_control_form($settings);
  unset($element['#theme']);
  $element['#theme'] = 'getlocations_fields_field_formatter_settings_form';
  return $element;
}

/**
 * Implements hook_field_formatter_settings_summary().
 * Returns a short summary for the current formatter settings of an instance.
 *
 *
 * If an empty result is returned, the formatter is assumed to have no
 * configurable settings, and no UI will be provided to display a settings
 * form.
 *
 *   The field structure.
 * @param $instance
 *   The instance structure.
 * @param $view_mode
 *   The view mode for which a settings summary is requested.
 *
 * @return
 *   A string containing a short summary of the formatter settings.
 */
function getlocations_fields_field_formatter_settings_summary($field, $instance, $view_mode) {
  $getlocations_fields_paths = getlocations_fields_paths_get();
  drupal_add_js($getlocations_fields_paths['getlocations_fields_formatter_path']);
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $entity_type = $instance['entity_type'];
  $summary = array();

  // map summary
  if ($settings['display_showmap']) {
    $summary[] = t('Show map: Yes');
    if ($settings['display_mapwidth'] && $settings['display_mapheight']) {
      $summary[] = t('Width: @w | Height: @h', array(
        '@w' => $settings['display_mapwidth'],
        '@h' => $settings['display_mapheight'],
      ));
    }
    if ($settings['display_onemap']) {
      $summary[] = t('Show all locations on one map');
    }
    if ($settings['controltype']) {
      $summary[] = t('Zoom control type: @c', array(
        '@c' => $settings['controltype'],
      ));
    }
    if ($settings['pancontrol']) {
      $summary[] = t('Show Pan control: Yes');
    }
    if ($settings['mtc']) {
      $summary[] = t('Map control type: @c', array(
        '@c' => $settings['mtc'],
      ));
    }
    if ($settings['maptype']) {
      $summary[] = t('Default map type: @c', array(
        '@c' => $settings['maptype'],
      ));
    }
    $types = array();
    foreach ($settings['baselayers'] as $key => $value) {
      if ($value) {
        $types[] = $key;
      }
    }
    if (count($types)) {
      $m = implode(', ', $types);
      $summary[] = t('Enabled map types: @m', array(
        '@m' => $m,
      ));
    }
    if ($settings['scale']) {
      $summary[] = t('Show scale: Yes');
    }
    if ($settings['overview']) {
      $summary[] = t('Show overview map: Yes');
    }
    if ($settings['scrollwheel']) {
      $summary[] = t('Enable scrollwheel zooming: Yes');
    }
    if ($settings['draggable']) {
      $summary[] = t('Enable dragging the map: Yes');
    }
    if ($settings['nokeyboard']) {
      $summary[] = t('Disable Keyboard shortcuts: Yes');
    }
    if ($settings['nodoubleclickzoom']) {
      $summary[] = t('Disable doubleclick zoom: Yes');
    }
    if ($settings['sv_show']) {
      $summary[] = t('Show streetview pegman: Yes');
    }
    if ($settings['sv_showfirst']) {
      $summary[] = t('Show streetview first: Yes');
    }
    if ($settings['fullscreen']) {
      $summary[] = t('Show Fullscreen button: Yes');
    }
    if ($settings['show_bubble_on_one_marker']) {
      $summary[] = t('Show bubble on one marker: Yes');
    }
    $info_display = array();
    if ($settings['trafficinfo']) {
      $info_display[] = t('Traffic');
    }
    if ($settings['bicycleinfo']) {
      $info_display[] = t('Bicycling');
    }
    if ($settings['transitinfo']) {
      $info_display[] = t('Transit');
    }
    if ($settings['poi_show']) {
      $info_display[] = t('Points of Interest');
    }
    if ($settings['transit_show']) {
      $info_display[] = t('Transit Points');
    }
    if (count($info_display)) {
      $summary[] = t("Enabled information layers:") . '<br />' . implode(', ', $info_display);
    }
    if ($entity_type == 'user' || $entity_type == 'profile2') {
      if ($settings['user_map_marker']) {
        $summary[] = t('User map marker: @c', array(
          '@c' => $settings['user_map_marker'],
        ));
      }
    }
    elseif ($entity_type == 'taxonomy_term') {
      if ($settings['vocabulary_map_marker']) {
        $summary[] = t('Vocabulary map marker: @c', array(
          '@c' => $settings['vocabulary_map_marker'],
        ));
      }
    }
    elseif ($entity_type == 'comment') {
      if ($settings['comment_map_marker']) {
        $summary[] = t('Comment map marker: @c', array(
          '@c' => $settings['comment_map_marker'],
        ));
      }
    }
    else {
      if ($settings['node_map_marker']) {
        $summary[] = t('Content map marker: @c', array(
          '@c' => $settings['node_map_marker'],
        ));
      }
    }
    if ($settings['map_backgroundcolor']) {
      $summary[] = t('Map background color: @c', array(
        '@c' => $settings['map_backgroundcolor'],
      ));
    }
    if ($settings['markeraction'] > 0) {
      if ($settings['markeraction'] == 1) {
        $msg = t('InfoWindow');
      }
      elseif ($settings['markeraction'] == 2) {
        $msg = t('InfoBubble');
      }
      else {
        $msg = t('Link');
      }
      if ($settings['markeractiontype'] == 2) {
        $msg2 = t('Mouse over');
      }
      else {
        $msg2 = t('Click');
      }
      $summary[] = t('Marker action: @a on @b', array(
        '@a' => $msg,
        '@b' => $msg2,
      ));
      if ($settings['markeraction_click_zoom'] > -1) {
        $summary[] = t('Zoom on Marker click: @a', array(
          '@a' => $settings['markeraction_click_zoom'],
        ));
      }
      if ($settings['markeraction_click_center'] == 1) {
        $summary[] = t('Center on Marker click: Yes');
      }
      elseif ($settings['markeraction_click_center'] == 2) {
        $summary[] = t('Pan to on Marker click: Yes');
      }
    }
    $summary[] = t('Zoom level: @z', array(
      '@z' => $settings['nodezoom'],
    ));
    if ($settings['polygons_enable']) {
      $summary[] = t('Enable polygons: Yes');
      if ($settings['polygons_clickable']) {
        $summary[] = t('Polygons clickable: Yes');
      }
    }
    if ($settings['rectangles_enable']) {
      $summary[] = t('Enable rectangles: Yes');
      if ($settings['rectangles_clickable']) {
        $summary[] = t('Rectangles clickable: Yes');
      }
    }
    if ($settings['circles_enable']) {
      $summary[] = t('Enable circles: Yes');
      if ($settings['circles_clickable']) {
        $summary[] = t('Circles clickable: Yes');
      }
    }
    if ($settings['polylines_enable']) {
      $summary[] = t('Enable polylines: Yes');
      if ($settings['polylines_clickable']) {
        $summary[] = t('Polylines clickable: Yes');
      }
    }

    // search_places
    if ($settings['search_places']) {
      $summary[] = t('Enable Search Places: Yes');
    }
  }
  else {
    $summary[] = t('Show map: No');
    if ($settings['display_maplink']) {
      $summary[] = t('Show map link: Yes');
    }
  }

  // lat/long
  if ($settings['display_latlong']) {
    $summary[] = t('Show Latitude/Longitude: Yes');
  }
  if ($settings['display_dms']) {
    $summary[] = t('Show Latitude/Longitude in Degrees, minutes, seconds');
  }
  if ($settings['display_geo_microformat']) {
    $summary[] = t('Show Latitude/Longitude in accordance with Geo Microformat markup');
  }

  // address summary
  $address_display = array();
  if ($settings['display_name']) {
    $address_display[] = t("Name");
  }
  if ($settings['display_street']) {
    $address_display[] = t("Street");
  }
  if ($settings['display_additional']) {
    $address_display[] = t("Additional");
  }
  if ($settings['display_city']) {
    $address_display[] = t("City");
  }
  if ($settings['display_province']) {
    $address_display[] = t("Province");
  }
  if ($settings['display_postal_code']) {
    $address_display[] = t("Postcode");
  }
  if ($settings['display_country']) {
    $address_display[] = t("Country");
  }
  if ($settings['display_phone']) {
    $address_display[] = t("Phone");
  }
  if ($settings['display_mobile']) {
    $address_display[] = t("Mobile");
  }
  if ($settings['display_fax']) {
    $address_display[] = t("Fax");
  }
  if (count($address_display)) {
    $summary[] = t("Show in Address:") . '<br />' . implode(', ', $address_display);
  }
  if ($settings['country_full']) {
    $summary[] = t('Display full country name: Yes');
  }
  return implode('<br />', $summary);
}

/**
 * Implements hook_field_widget_info().
 * Expose Field API widget types.
 *
 * @return
 *   An array describing the widget types implemented by the module.
 */
function getlocations_fields_field_widget_info() {
  $getlocations_fields_defaults = getlocations_fields_defaults();
  return array(
    'getlocations_fields' => array(
      'label' => t('Geocoder'),
      'field types' => array(
        'getlocations_fields',
      ),
      'settings' => array(
        'country' => $getlocations_fields_defaults['country'],
        'use_country_dropdown' => $getlocations_fields_defaults['use_country_dropdown'],
        'province_autocomplete' => $getlocations_fields_defaults['province_autocomplete'],
        'city_autocomplete' => $getlocations_fields_defaults['city_autocomplete'],
        'use_address' => $getlocations_fields_defaults['use_address'],
        'input_address_width' => $getlocations_fields_defaults['input_address_width'],
        'input_name_width' => $getlocations_fields_defaults['input_name_width'],
        'input_street_width' => $getlocations_fields_defaults['input_street_width'],
        'input_additional_width' => $getlocations_fields_defaults['input_additional_width'],
        'input_city_width' => $getlocations_fields_defaults['input_city_width'],
        'input_province_width' => $getlocations_fields_defaults['input_province_width'],
        'input_postal_code_width' => $getlocations_fields_defaults['input_postal_code_width'],
        'input_country_width' => $getlocations_fields_defaults['input_country_width'],
        'input_phone_width' => $getlocations_fields_defaults['input_phone_width'],
        'input_mobile_width' => $getlocations_fields_defaults['input_mobile_width'],
        'input_fax_width' => $getlocations_fields_defaults['input_fax_width'],
        'input_latitude_width' => $getlocations_fields_defaults['input_latitude_width'],
        'input_longitude_width' => $getlocations_fields_defaults['input_longitude_width'],
        'input_name_weight' => $getlocations_fields_defaults['input_name_weight'],
        'input_street_weight' => $getlocations_fields_defaults['input_street_weight'],
        'input_additional_weight' => $getlocations_fields_defaults['input_additional_weight'],
        'input_city_weight' => $getlocations_fields_defaults['input_city_weight'],
        'input_province_weight' => $getlocations_fields_defaults['input_province_weight'],
        'input_postal_code_weight' => $getlocations_fields_defaults['input_postal_code_weight'],
        'input_country_weight' => $getlocations_fields_defaults['input_country_weight'],
        'input_phone_weight' => $getlocations_fields_defaults['input_phone_weight'],
        'input_mobile_weight' => $getlocations_fields_defaults['input_mobile_weight'],
        'input_fax_weight' => $getlocations_fields_defaults['input_fax_weight'],
        'input_latitude_weight' => $getlocations_fields_defaults['input_latitude_weight'],
        'input_longitude_weight' => $getlocations_fields_defaults['input_longitude_weight'],
        'input_name_required' => $getlocations_fields_defaults['input_name_required'],
        'input_street_required' => $getlocations_fields_defaults['input_street_required'],
        'input_additional_required' => $getlocations_fields_defaults['input_additional_required'],
        'input_city_required' => $getlocations_fields_defaults['input_city_required'],
        'input_province_required' => $getlocations_fields_defaults['input_province_required'],
        'input_postal_code_required' => $getlocations_fields_defaults['input_postal_code_required'],
        'input_country_required' => $getlocations_fields_defaults['input_country_required'],
        'input_phone_required' => $getlocations_fields_defaults['input_phone_required'],
        'input_mobile_required' => $getlocations_fields_defaults['input_mobile_required'],
        'input_fax_required' => $getlocations_fields_defaults['input_fax_required'],
        'per_item_marker' => $getlocations_fields_defaults['per_item_marker'],
        'sv_enable' => $getlocations_fields_defaults['sv_enable'],
        'sv_showfirst' => $getlocations_fields_defaults['sv_showfirst'],
      ),
    ),
  );
}

/**
 * Implements hook_field_widget_form().
 * Return the form for a single field widget.
 *
 * @param $form
 *   The form structure where widgets are being attached to. This might be a
 *   full form structure, or a sub-element of a larger form.
 * @param $form_state
 *   An associative array containing the current state of the form.
 * @param $field
 *   The field structure.
 * @param $instance
 *   The field instance.
 * @param $langcode
 *   The language associated with $items.
 * @param $items
 *   Array of default values for this field.
 * @param $delta
 *   The order of this item in the array of subelements (0, 1, 2, etc).
 * @param $element
 *   A form element array containing basic properties for the widget.
 * @return
 *   The form elements for a single widget for this field.
 */
function getlocations_fields_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  switch ($instance['widget']['type']) {
    case 'getlocations_fields':

      // is this real or a demo in settings
      $active = TRUE;
      if (empty($element['#entity'])) {
        $active = FALSE;
      }
      $element['#delta'] = $delta;
      $field_name = '';
      if (isset($field['field_name'])) {
        $field_name = $field['field_name'];
        $items[$delta]['field_name'] = $field_name;
      }
      $cardinality = $field['cardinality'];
      $settings = $field['settings'];

      // Wrap in a fieldset for single fields
      if ($cardinality == 1) {
        $element['#type'] = 'fieldset';
        $element['#collapsible'] = TRUE;
        $element['#collapsed'] = FALSE;
      }

      // delete location
      if (isset($items[$delta]['glid']) && $items[$delta]['glid'] > 0) {
        $element['glid'] = array(
          '#type' => 'value',
          '#value' => $items[$delta]['glid'],
        );
        if (!$instance['required']) {
          $element['delete_location'] = array(
            '#type' => 'checkbox',
            '#title' => t('Delete'),
            '#default_value' => FALSE,
            '#description' => t('Check this box to delete this location.'),
          );
        }
      }

      // search field
      if ($settings['use_address']) {
        $element['address'] = array(
          '#type' => 'textfield',
          '#default_value' => isset($items[$delta]['address']) ? $items[$delta]['address'] : (isset($instance['default_value'][$delta]['address']) ? $instance['default_value'][$delta]['address'] : ''),
          '#size' => $settings['input_address_width'],
          '#title' => t('Search'),
          '#description' => t('Start typing an address and select from the dropdown.'),
          '#field_name' => $instance['field_name'],
          '#maxlength' => 255,
        );
        if ($instance['required']) {
          if ($active) {
            $element['address']['#required'] = TRUE;
          }
          else {
            $element['address']['#field_suffix'] = t('(Required)');
          }
        }
      }

      // TODO add a message, required form or not
      // smart_ip
      if ($active) {
        if (module_exists('smart_ip') && $settings['use_smart_ip_button']) {
          $element['smart_ip_button'] = array(
            '#markup' => '',
          );
          if (!empty($settings['input_smart_ip_button_weight'])) {
            $element['smart_ip_button']['#weight'] = $settings['input_smart_ip_button_weight'];
          }
        }
        if ($settings['use_geolocation_button']) {
          $element['geolocation_button'] = array(
            '#markup' => '',
          );
          if (!empty($settings['input_geolocation_button_weight'])) {
            $element['geolocation_button']['#weight'] = $settings['input_geolocation_button_weight'];
          }
        }
      }

      // loop over these as they are all the same
      $required = array();
      $inputarr = array(
        'name' => t('Name'),
        'street' => t('Street'),
        'additional' => t('Additional'),
        'city' => t('City/Town'),
        'province' => t('Province/State/County'),
        'postal_code' => t('Post code/Zip code'),
      );
      foreach ($inputarr as $id => $title) {
        if ($settings['input_' . $id . '_required'] == 4) {

          // hidden form
          $element[$id] = array(
            '#type' => 'hidden',
            '#value' => isset($items[$delta][$id]) ? $items[$delta][$id] : (isset($instance['default_value'][$delta][$id]) ? $instance['default_value'][$delta][$id] : ''),
          );
        }
        elseif ($settings['input_' . $id . '_required'] == 3) {
          $element[$id] = array(
            '#type' => 'hidden',
            '#value' => isset($items[$delta][$id]) ? $items[$delta][$id] : (isset($instance['default_value'][$delta][$id]) ? $instance['default_value'][$delta][$id] : ''),
          );
          $element[$id . '_display'] = array(
            '#markup' => isset($items[$delta][$id]) ? $items[$delta][$id] : (isset($instance['default_value'][$delta][$id]) ? $instance['default_value'][$delta][$id] : ''),
          );
        }
        else {
          $element[$id] = array(
            '#type' => 'textfield',
            '#default_value' => isset($items[$delta][$id]) ? htmlspecialchars_decode($items[$delta][$id], ENT_QUOTES) : (isset($instance['default_value'][$delta][$id]) ? htmlspecialchars_decode($instance['default_value'][$delta][$id], ENT_QUOTES) : ''),
            '#size' => $settings['input_' . $id . '_width'],
            '#title' => $title,
            '#maxlength' => 255,
          );

          // autocomplete for province
          if ($id == 'province' && $settings['province_autocomplete']) {
            $element[$id]['#autocomplete_path'] = 'getlocations_fields/province_autocomplete';
          }
          elseif ($id == 'city' && $settings['city_autocomplete']) {
            $element[$id]['#autocomplete_path'] = 'getlocations_fields/city_autocomplete';
          }
        }
        if ($settings['input_' . $id . '_required'] == 2) {
          $element[$id]['#disabled'] = TRUE;
        }
        elseif ($settings['input_' . $id . '_required'] == 1) {
          if ($active) {
            if ($cardinality == -1) {

              # needs to be replaced with a 'fake'
              $required[] = $id;
            }
            else {
              $element[$id]['#required'] = TRUE;
            }
          }
          else {
            $element[$id]['#field_suffix'] = t('(Required)');
          }
        }
        if (!empty($settings['input_' . $id . '_weight'])) {
          $element[$id]['#weight'] = $settings['input_' . $id . '_weight'];
        }
      }

      // country
      if ($settings['restrict_by_country'] && $settings['search_country']) {
        $default_country_value = isset($items[$delta]['country']) && $items[$delta]['country'] ? $items[$delta]['country'] : (isset($instance['default_value'][$delta]['country']) ? $instance['default_value'][$delta]['country'] : $settings['search_country']);
        $default_country_value_long = getlocations_get_country_name($default_country_value);
        $element['country'] = array(
          '#type' => 'hidden',
          '#value' => $default_country_value,
        );
        $element['country_display'] = array(
          '#markup' => $default_country_value_long,
        );
      }
      else {
        $default_country_value = isset($items[$delta]['country']) && $items[$delta]['country'] ? $items[$delta]['country'] : (isset($instance['default_value'][$delta]['country']) ? $instance['default_value'][$delta]['country'] : $settings['country']);
        $default_country_value_long = $default_country_value;
        if ($default_country_value && drupal_strlen($default_country_value) == 2) {
          $default_country_value_long = getlocations_get_country_name($default_country_value);
        }
        if ($settings['input_country_required'] == 4) {

          // hidden form
          $element['country'] = array(
            '#type' => 'hidden',
            '#value' => $default_country_value,
          );
        }
        elseif ($settings['input_country_required'] == 3) {

          // hidden form
          $element['country'] = array(
            '#type' => 'hidden',
            '#value' => $default_country_value,
          );
          $element['country_display'] = array(
            '#markup' => $default_country_value_long,
          );
        }
        else {
          if ($settings['use_country_dropdown'] == 1) {
            $only_continents = '';
            $only_countries = '';
            if (module_exists('countries')) {
              $only_continents = isset($settings['only_continents']) ? $settings['only_continents'] : '';
              $only_countries = isset($settings['only_countries']) ? $settings['only_countries'] : '';
            }
            $countries = getlocations_get_countries_list(TRUE, $only_continents, $only_countries);
            $countries = array_merge(array(
              '' => t('---Please select a country---'),
            ), $countries);
            $element['country'] = array(
              '#type' => 'select',
              '#default_value' => $default_country_value,
              '#options' => $countries,
              '#title' => t('Country'),
            );
          }
          else {
            $element['country'] = array(
              '#type' => 'textfield',
              '#default_value' => $default_country_value_long,
              '#autocomplete_path' => $settings['use_country_dropdown'] == 2 ? 'getlocations_fields/country_autocomplete' : '',
              '#size' => $settings['input_country_width'],
              '#title' => t('Country'),
              '#maxlength' => 255,
            );
          }
          if ($settings['input_country_required'] == 2) {
            $element['country']['#disabled'] = TRUE;
          }
          elseif ($settings['input_country_required'] == 1) {
            if ($active) {
              if ($cardinality == -1) {
                $required[] = 'country';
              }
              else {
                $element['country']['#required'] = TRUE;
              }
            }
            else {
              $element['country']['#field_suffix'] = t('(Required)');
            }
          }
        }
        if (!empty($settings['input_country_weight'])) {
          $element['country']['#weight'] = $settings['input_country_weight'];
        }
      }
      $inputarr = array(
        'phone' => t('Phone'),
        'mobile' => t('Mobile'),
        'fax' => t('Fax'),
      );
      foreach ($inputarr as $id => $title) {
        if ($settings['input_' . $id . '_required'] == 4) {

          // hidden form
          $element[$id] = array(
            '#type' => 'hidden',
            '#value' => isset($items[$delta][$id]) ? $items[$delta][$id] : (isset($instance['default_value'][$delta][$id]) ? $instance['default_value'][$delta][$id] : ''),
          );
        }
        elseif ($settings['input_' . $id . '_required'] == 3) {
          $element[$id] = array(
            '#type' => 'hidden',
            '#value' => isset($items[$delta][$id]) ? $items[$delta][$id] : (isset($instance['default_value'][$delta][$id]) ? $instance['default_value'][$delta][$id] : ''),
          );
          $element[$id . '_display'] = array(
            '#markup' => isset($items[$delta][$id]) ? $items[$delta][$id] : (isset($instance['default_value'][$delta][$id]) ? $instance['default_value'][$delta][$id] : ''),
          );
        }
        else {
          $element[$id] = array(
            '#type' => 'textfield',
            '#default_value' => isset($items[$delta][$id]) ? htmlspecialchars_decode($items[$delta][$id], ENT_QUOTES) : (isset($instance['default_value'][$delta][$id]) ? htmlspecialchars_decode($instance['default_value'][$delta][$id], ENT_QUOTES) : ''),
            '#size' => $settings['input_' . $id . '_width'],
            '#title' => $title,
            '#maxlength' => 255,
          );
        }
        if ($settings['input_' . $id . '_required'] == 2) {
          $element[$id]['#disabled'] = TRUE;
        }
        elseif ($settings['input_' . $id . '_required'] == 1) {
          if ($active) {
            if ($cardinality == -1) {

              # needs to be replaced with a 'fake'
              $required[] = $id;
            }
            else {
              $element[$id]['#required'] = TRUE;
            }
          }
          else {
            $element[$id]['#field_suffix'] = t('(Required)');
          }
        }
        if (!empty($settings['input_' . $id . '_weight'])) {
          $element[$id]['#weight'] = $settings['input_' . $id . '_weight'];
        }
      }

      // geobutton
      if ($active) {
        if ($settings['use_address'] < 2) {
          $element['geobutton'] = array(
            '#markup' => '',
          );
          if (!empty($settings['input_geobutton_weight'])) {
            $element['geobutton']['#weight'] = $settings['input_geobutton_weight'];
          }
        }
      }

      // Clear button
      if ($settings['use_clear_button']) {
        $element['clear_button'] = array(
          '#markup' => '',
        );
        if (!empty($settings['input_clear_button_weight'])) {
          $element['clear_button']['#weight'] = $settings['input_clear_button_weight'];
        }
      }

      // map
      $element['map'] = array(
        '#markup' => '',
      );
      if (!empty($settings['input_map_weight'])) {
        $element['map']['#weight'] = $settings['input_map_weight'];
      }

      // latitude
      $element['latitude'] = array(
        '#type' => 'textfield',
        '#default_value' => isset($items[$delta]['latitude']) ? $items[$delta]['latitude'] : (isset($instance['default_value'][$delta]['latitude']) ? $instance['default_value'][$delta]['latitude'] : NULL),
        '#size' => $settings['input_latitude_width'],
        '#title' => t('Latitude'),
        '#maxlength' => 20,
      );
      if (!empty($settings['input_latitude_weight'])) {
        $element['latitude']['#weight'] = $settings['input_latitude_weight'];
      }

      // longitude
      $element['longitude'] = array(
        '#type' => 'textfield',
        '#default_value' => isset($items[$delta]['longitude']) ? $items[$delta]['longitude'] : (isset($instance['default_value'][$delta]['longitude']) ? $instance['default_value'][$delta]['longitude'] : NULL),
        '#size' => $settings['input_longitude_width'],
        '#title' => t('Longitude'),
        '#maxlength' => 20,
      );
      if (!empty($settings['input_longitude_weight'])) {
        $element['longitude']['#weight'] = $settings['input_longitude_weight'];
      }

      // per_item_marker
      if ($settings['per_item_marker']) {
        $markers = getlocations_get_marker_titles();
        $markers = array_merge(array(
          '' => t('Default'),
        ), $markers);
        $element['marker'] = getlocations_element_map_marker(t('Map marker'), $markers, isset($items[$delta]['marker']) ? $items[$delta]['marker'] : (isset($instance['default_value'][$delta]['marker']) ? $instance['default_value'][$delta]['marker'] : ''));
        if (!empty($settings['input_marker_weight'])) {
          $element['marker']['#weight'] = $settings['input_marker_weight'];
        }
      }
      else {
        $element['marker'] = array(
          '#type' => 'value',
          '#value' => '',
        );
      }

      // streetview killswitch
      if (getlocations_fields_streetview_settings_allow()) {
        if (module_exists('views')) {
          $element['sv_enable'] = getlocations_element_map_checkbox(t('Enable Streetview'), isset($items[$delta]['sv_enable']) ? $items[$delta]['sv_enable'] : (isset($instance['default_value'][$delta]['sv_enable']) ? $instance['default_value'][$delta]['sv_enable'] : 1), t('Enables Streetview for this location in Views.'));
          $element['sv_enable']['#suffix'] = '<div id="wrap-streetview-enable">';
        }
        else {
          $element['sv_enable'] = array(
            '#type' => 'value',
            '#value' => 0,
          );
        }
        $element['sv_showfirst'] = getlocations_element_map_checkbox(t('Enable the Streetview overlay'), isset($items[$delta]['sv_showfirst']) ? $items[$delta]['sv_showfirst'] : (isset($instance['default_value'][$delta]['sv_showfirst']) ? $instance['default_value'][$delta]['sv_showfirst'] : 1), t('Enables Streetview on top of the initial map.'));
        if (module_exists('views')) {
          $element['sv_showfirst']['#suffix'] = '</div>';
        }
      }
      else {
        $element['sv_enable'] = array(
          '#type' => 'value',
          '#value' => 0,
        );
        $element['sv_showfirst'] = array(
          '#type' => 'value',
          '#value' => 0,
        );
      }

      // hidden fields, these are filled in by jQuery
      if (isset($items[$delta]['glid']) && $items[$delta]['glid'] > 0) {
        $d_mapzoom = isset($items[$delta]['mapzoom']) ? $items[$delta]['mapzoom'] : (isset($instance['default_value'][$delta]['mapzoom']) ? $instance['default_value'][$delta]['mapzoom'] : $settings['zoom']);
        $d_map_maptype = isset($items[$delta]['map_maptype']) ? $items[$delta]['map_maptype'] : (isset($instance['default_value'][$delta]['map_maptype']) ? $instance['default_value'][$delta]['map_maptype'] : $settings['maptype']);
        $d_sv_heading = isset($items[$delta]['sv_heading']) ? $items[$delta]['sv_heading'] : (isset($instance['default_value'][$delta]['sv_heading']) ? $instance['default_value'][$delta]['sv_heading'] : $settings['sv_heading']);
        $d_sv_zoom = isset($items[$delta]['sv_zoom']) ? $items[$delta]['sv_zoom'] : (isset($instance['default_value'][$delta]['sv_zoom']) ? $instance['default_value'][$delta]['sv_zoom'] : $settings['sv_zoom']);
        $d_sv_pitch = isset($items[$delta]['sv_pitch']) ? $items[$delta]['sv_pitch'] : (isset($instance['default_value'][$delta]['sv_pitch']) ? $instance['default_value'][$delta]['sv_pitch'] : $settings['sv_pitch']);
      }
      else {
        $d_mapzoom = $settings['zoom'];
        $d_map_maptype = $settings['maptype'];
        $d_sv_heading = $settings['sv_heading'];
        $d_sv_zoom = $settings['sv_zoom'];
        $d_sv_pitch = $settings['sv_pitch'];
      }
      $element['mapzoom'] = array(
        '#type' => 'textfield',
        '#default_value' => $d_mapzoom,
        '#prefix' => '<div class="js-hide">',
      );
      $element['map_maptype'] = array(
        '#type' => 'textfield',
        '#default_value' => $d_map_maptype,
      );
      $element['sv_heading'] = array(
        '#type' => 'textfield',
        '#default_value' => $d_sv_heading,
      );
      $element['sv_zoom'] = array(
        '#type' => 'textfield',
        '#default_value' => $d_sv_zoom,
      );
      $element['sv_pitch'] = array(
        '#type' => 'textfield',
        '#default_value' => $d_sv_pitch,
        '#suffix' => '</div>',
      );
      $element['active'] = array(
        '#type' => 'value',
        '#value' => $active,
      );
      if (count($required)) {
        $element['getlocations_required'] = array(
          '#type' => 'value',
          '#value' => implode(',', $required),
        );
      }
      $element['settings'] = array(
        '#type' => 'value',
        '#value' => $settings,
      );
      unset($element['#theme']);
      $element['#theme'] = 'getlocations_fields_field_widget_form';
      break;
  }
  return $element;
}

/**
 * Implements hook_field_widget_error().
 * Flag a field-level validation error.
 *
 * @param $element
 *   An array containing the form element for the widget. The error needs to be
 *   flagged on the right sub-element, according to the widget's internal structure.
 * @param $error
 *   An associative array, as returned by getlocations_fields_field_validate().
 * @param $form
 *   The form structure where field elements are attached to. This might be a
 *   full form structure, or a sub-element of a larger form.
 * @param $form_state
 *   An associative array containing the current state of the form.
 */
function getlocations_fields_field_widget_error($element, $error, $form, &$form_state) {
  switch ($error['error']) {
    case 'latlon_empty':
      unset($form_state['values'][$error['field_name']][$error['lang']][$error['delta']]);
      break;
    case 'field_required_name':
      form_error($element['name'], $error['message']);
      break;
    case 'field_required_street':
      form_error($element['street'], $error['message']);
      break;
    case 'field_required_additional':
      form_error($element['additional'], $error['message']);
      break;
    case 'field_required_city':
      form_error($element['city'], $error['message']);
      break;
    case 'field_required_province':
      form_error($element['province'], $error['message']);
      break;
    case 'field_required_postal_code':
      form_error($element['postal_code'], $error['message']);
      break;
    case 'field_required_country':
      form_error($element['country'], $error['message']);
      break;
    case 'field_required_phone':
      form_error($element['phone'], $error['message']);
      break;
    case 'field_required_mobile':
      form_error($element['mobile'], $error['message']);
      break;
    case 'field_required_fax':
      form_error($element['fax'], $error['message']);
      break;
  }
}

/**
 * input map
 * @param $settings
 *    The map settings
 *
 * @param $active
 *    Determine wether this is from the edit form or just a demo on the settings
 */
function getlocations_fields_getmap($settings, $active) {
  $getlocations_defaults = getlocations_defaults();

  // we do not want these on an input map
  $getlocations_defaults['trafficinfo'] = 0;
  $getlocations_defaults['bicycleinfo'] = 0;
  $getlocations_defaults['transitinfo'] = 0;
  $getlocations_defaults['markeraction'] = 0;
  $getlocations_defaults['markeraction_click_zoom'] = -1;
  $getlocations_defaults['markeraction_click_center'] = 0;
  $getlocations_defaults['sv_show'] = 0;
  $getlocations_defaults['show_maplinks'] = 0;
  $getlocations_defaults['minzoom_map'] = -1;
  $getlocations_defaults['maxzoom_map'] = -1;
  $getlocations_defaults['fullscreen'] = 0;
  $getlocations_defaults['polygons_enable'] = 0;
  $getlocations_defaults['rectangles_enable'] = 0;
  $getlocations_defaults['circles_enable'] = 0;
  $getlocations_defaults['polylines_enable'] = 0;
  $getlocations_defaults['show_bubble_on_one_marker'] = 0;
  $getlocations_defaults['search_places'] = 0;
  if ($active) {
    $getlocations_defaults['places'] = 1;
  }
  $getlocations_defaults['latlong'] = $settings['latlong'];
  if (module_exists('smart_ip') && $settings['use_smart_ip_latlon']) {
    $lla = getlocations_fields_smart_ip_get();
    if ($lla && is_array($lla)) {
      if ($latlon = getlocations_latlon_check($lla['latitude'] . ',' . $lla['longitude'])) {
        $getlocations_defaults['latlong'] = $latlon;
      }
    }
  }
  $getlocations_defaults['zoom'] = $settings['zoom'];
  $getlocations_defaults['controltype'] = $settings['controltype'];
  $getlocations_defaults['zoomcontrolposition'] = $settings['zoomcontrolposition'];
  $getlocations_defaults['pancontrol'] = $settings['pancontrol'];
  $getlocations_defaults['pancontrolposition'] = $settings['pancontrolposition'];
  $getlocations_defaults['mtc'] = $settings['mtc'];
  $getlocations_defaults['maptype'] = $settings['maptype'];
  $getlocations_defaults['mapcontrolposition'] = $settings['mapcontrolposition'];
  $getlocations_defaults['baselayers'] = $settings['baselayers'];
  $getlocations_defaults['scale'] = $settings['scale'];
  $getlocations_defaults['scalecontrolposition'] = $settings['scalecontrolposition'];
  $getlocations_defaults['overview'] = $settings['overview'];
  $getlocations_defaults['overview_opened'] = $settings['overview_opened'];
  $getlocations_defaults['scrollwheel'] = $settings['scrollwheel'];
  $getlocations_defaults['draggable'] = $settings['draggable'];
  $getlocations_defaults['input_map_marker'] = $settings['input_map_marker'];
  $getlocations_defaults['use_address'] = $settings['use_address'];
  $getlocations_defaults['map_backgroundcolor'] = $settings['map_backgroundcolor'];
  $getlocations_defaults['input_map_show'] = $settings['input_map_show'];

  #$getlocations_defaults['use_jsapi']       = $settings['use_geolocation_button'];
  $getlocations_defaults['geocoder_enable'] = $settings['geocoder_enable'];
  $mapid = getlocations_setup_map($getlocations_defaults);
  drupal_add_css(GETLOCATIONS_FIELDS_PATH . '/getlocations_fields.css');
  $aggr = getlocations_aggr_get() ? TRUE : FALSE;
  $getlocations_fields_paths = getlocations_fields_paths_get();
  $js_opts = array();
  $js_opts['weight'] = $getlocations_defaults['getlocations_js_weight'] + 30;
  $js_opts['type'] = 'file';
  $js_opts['preprocess'] = $aggr;
  $latlons = array();
  if ($active) {
    drupal_add_js($getlocations_fields_paths['getlocations_fields_path'], $js_opts);
    $settings['nodezoom'] = $getlocations_defaults['nodezoom'];
    getlocations_fields_js_settings_do($settings, $mapid);
  }
  else {

    // demo mode
    drupal_add_js($getlocations_fields_paths['getlocations_fields_preview_path'], $js_opts);
    $getlocations_defaults['nodezoom'] = $getlocations_defaults['zoom'];
  }
  $minmaxes = '';
  getlocations_js_settings_do($getlocations_defaults, $latlons, $minmaxes, $mapid, $active);
  $map = theme('getlocations_show', array(
    'width' => $settings['mapwidth'],
    'height' => $settings['mapheight'],
    'defaults' => $getlocations_defaults,
    'mapid' => $mapid,
    'latlons' => $latlons,
    'minmaxes' => $minmaxes,
    'type' => '',
    'node' => '',
  ));
  return array(
    $map,
    $mapid,
  );
}

/**
 * Used by theme for display output.
 */
function getlocations_fields_getmap_show($settings, $lls = '', $minmaxes = '') {
  $getlocations_defaults = getlocations_defaults();
  $getlocations_defaults['controltype'] = $settings['controltype'];
  $getlocations_defaults['pancontrol'] = $settings['pancontrol'];
  $getlocations_defaults['mtc'] = $settings['mtc'];
  $getlocations_defaults['maptype'] = $settings['maptype'];
  $getlocations_defaults['baselayers'] = $settings['baselayers'];
  $getlocations_defaults['scale'] = $settings['scale'];
  $getlocations_defaults['overview'] = $settings['overview'];
  $getlocations_defaults['overview_opened'] = $settings['overview_opened'];
  $getlocations_defaults['scrollwheel'] = $settings['scrollwheel'];
  $getlocations_defaults['draggable'] = $settings['draggable'];
  $getlocations_defaults['sv_show'] = $settings['sv_show'];
  $getlocations_defaults['sv_showfirst'] = $settings['sv_showfirst'];
  $getlocations_defaults['sv_heading'] = $settings['sv_heading'];
  $getlocations_defaults['sv_zoom'] = $settings['sv_zoom'];
  $getlocations_defaults['sv_pitch'] = $settings['sv_pitch'];
  $getlocations_defaults['trafficinfo'] = $settings['trafficinfo'];
  $getlocations_defaults['trafficinfo_state'] = $settings['trafficinfo_state'];
  $getlocations_defaults['bicycleinfo'] = $settings['bicycleinfo'];
  $getlocations_defaults['bicycleinfo_state'] = $settings['bicycleinfo_state'];
  $getlocations_defaults['transitinfo'] = $settings['transitinfo'];
  $getlocations_defaults['transitinfo_state'] = $settings['transitinfo_state'];
  $getlocations_defaults['poi_show'] = $settings['poi_show'];
  $getlocations_defaults['transit_show'] = $settings['transit_show'];
  $getlocations_defaults['node_map_marker'] = $settings['node_map_marker'];
  $getlocations_defaults['user_map_marker'] = $settings['user_map_marker'];
  $getlocations_defaults['vocabulary_map_marker'] = $settings['vocabulary_map_marker'];
  $getlocations_defaults['comment_map_marker'] = $settings['comment_map_marker'];
  $getlocations_defaults['markeraction'] = $settings['markeraction'];
  $getlocations_defaults['markeractiontype'] = $settings['markeractiontype'];
  $getlocations_defaults['markeraction_click_zoom'] = $settings['markeraction_click_zoom'];
  $getlocations_defaults['markeraction_click_center'] = $settings['markeraction_click_center'];
  $getlocations_defaults['nodezoom'] = $settings['nodezoom'];
  $getlocations_defaults['map_backgroundcolor'] = $settings['map_backgroundcolor'];
  $getlocations_defaults['show_maplinks'] = $settings['show_maplinks'];
  $getlocations_defaults['minzoom_map'] = $settings['minzoom_map'];
  $getlocations_defaults['maxzoom_map'] = $settings['maxzoom_map'];
  $getlocations_defaults['fullscreen'] = $settings['fullscreen'];
  $getlocations_defaults['fullscreen_controlposition'] = $settings['fullscreen_controlposition'];
  $getlocations_defaults['show_bubble_on_one_marker'] = $settings['show_bubble_on_one_marker'];
  $getlocations_defaults['polygons_enable'] = $settings['polygons_enable'];
  $getlocations_defaults['polygons_strokecolor'] = $settings['polygons_strokecolor'];
  $getlocations_defaults['polygons_strokeopacity'] = $settings['polygons_strokeopacity'];
  $getlocations_defaults['polygons_strokeweight'] = $settings['polygons_strokeweight'];
  $getlocations_defaults['polygons_fillcolor'] = $settings['polygons_fillcolor'];
  $getlocations_defaults['polygons_fillopacity'] = $settings['polygons_fillopacity'];
  $getlocations_defaults['polygons_coords'] = $settings['polygons_coords'];
  $getlocations_defaults['polygons_clickable'] = $settings['polygons_clickable'];
  $getlocations_defaults['polygons_message'] = $settings['polygons_message'];
  $getlocations_defaults['rectangles_enable'] = $settings['rectangles_enable'];
  $getlocations_defaults['rectangles_strokecolor'] = $settings['rectangles_strokecolor'];
  $getlocations_defaults['rectangles_strokeopacity'] = $settings['rectangles_strokeopacity'];
  $getlocations_defaults['rectangles_strokeweight'] = $settings['rectangles_strokeweight'];
  $getlocations_defaults['rectangles_fillcolor'] = $settings['rectangles_fillcolor'];
  $getlocations_defaults['rectangles_fillopacity'] = $settings['rectangles_fillopacity'];
  $getlocations_defaults['rectangles_coords'] = $settings['rectangles_coords'];
  $getlocations_defaults['rectangles_clickable'] = $settings['rectangles_clickable'];
  $getlocations_defaults['rectangles_message'] = $settings['rectangles_message'];
  $getlocations_defaults['rectangles_apply'] = $settings['rectangles_apply'];
  $getlocations_defaults['rectangles_dist'] = $settings['rectangles_dist'];
  $getlocations_defaults['circles_enable'] = $settings['circles_enable'];
  $getlocations_defaults['circles_strokecolor'] = $settings['circles_strokecolor'];
  $getlocations_defaults['circles_strokeopacity'] = $settings['circles_strokeopacity'];
  $getlocations_defaults['circles_strokeweight'] = $settings['circles_strokeweight'];
  $getlocations_defaults['circles_fillcolor'] = $settings['circles_fillcolor'];
  $getlocations_defaults['circles_fillopacity'] = $settings['circles_fillopacity'];
  $getlocations_defaults['circles_coords'] = $settings['circles_coords'];
  $getlocations_defaults['circles_clickable'] = $settings['circles_clickable'];
  $getlocations_defaults['circles_message'] = $settings['circles_message'];
  $getlocations_defaults['circles_radius'] = $settings['circles_radius'];
  $getlocations_defaults['circles_apply'] = $settings['circles_apply'];
  $getlocations_defaults['polylines_enable'] = $settings['polylines_enable'];
  $getlocations_defaults['polylines_strokecolor'] = $settings['polylines_strokecolor'];
  $getlocations_defaults['polylines_strokeopacity'] = $settings['polylines_strokeopacity'];
  $getlocations_defaults['polylines_strokeweight'] = $settings['polylines_strokeweight'];
  $getlocations_defaults['polylines_coords'] = $settings['polylines_coords'];
  $getlocations_defaults['polylines_clickable'] = $settings['polylines_clickable'];
  $getlocations_defaults['polylines_message'] = $settings['polylines_message'];
  $getlocations_defaults['search_places'] = $settings['search_places'];
  $getlocations_defaults['search_places_size'] = $settings['search_places_size'];
  $getlocations_defaults['search_places_position'] = $settings['search_places_position'];
  $getlocations_defaults['search_places_label'] = $settings['search_places_label'];
  $getlocations_defaults['search_places_placeholder'] = $settings['search_places_placeholder'];
  $getlocations_defaults['search_places_dd'] = $settings['search_places_dd'];
  $getlocations_defaults['search_places_list'] = $settings['search_places_list'];
  $getlocations_defaults['kml_group'] = $settings['kml_group'];
  $getlocations_defaults['geojson_enable'] = $settings['geojson_enable'];
  $getlocations_defaults['geojson_data'] = $settings['geojson_data'];
  $getlocations_defaults['geojson_options'] = $settings['geojson_options'];
  $getlocations_defaults['nokeyboard'] = $settings['nokeyboard'];
  $getlocations_defaults['nodoubleclickzoom'] = $settings['nodoubleclickzoom'];
  $getlocations_defaults['zoomcontrolposition'] = $settings['zoomcontrolposition'];
  $getlocations_defaults['mapcontrolposition'] = $settings['mapcontrolposition'];
  $getlocations_defaults['pancontrolposition'] = $settings['pancontrolposition'];
  $getlocations_defaults['scalecontrolposition'] = $settings['scalecontrolposition'];
  $getlocations_defaults['svcontrolposition'] = $settings['svcontrolposition'];
  $getlocations_defaults['sv_addresscontrol'] = $settings['sv_addresscontrol'];

  // sv overlay controls
  $getlocations_defaults['sv_addresscontrolposition'] = $settings['sv_addresscontrolposition'];
  $getlocations_defaults['sv_pancontrol'] = $settings['sv_pancontrol'];
  $getlocations_defaults['sv_pancontrolposition'] = $settings['sv_pancontrolposition'];
  $getlocations_defaults['sv_zoomcontrol'] = $settings['sv_zoomcontrol'];
  $getlocations_defaults['sv_zoomcontrolposition'] = $settings['sv_zoomcontrolposition'];
  $getlocations_defaults['sv_linkscontrol'] = $settings['sv_linkscontrol'];
  $getlocations_defaults['sv_imagedatecontrol'] = $settings['sv_imagedatecontrol'];
  $getlocations_defaults['sv_scrollwheel'] = $settings['sv_scrollwheel'];
  $getlocations_defaults['sv_clicktogo'] = $settings['sv_clicktogo'];
  $getlocations_defaults['highlight_enable'] = $settings['highlight_enable'];
  $getlocations_defaults['highlight_strokecolor'] = $settings['highlight_strokecolor'];
  $getlocations_defaults['highlight_strokeopacity'] = $settings['highlight_strokeopacity'];
  $getlocations_defaults['highlight_strokeweight'] = $settings['highlight_strokeweight'];
  $getlocations_defaults['highlight_fillcolor'] = $settings['highlight_fillcolor'];
  $getlocations_defaults['highlight_fillopacity'] = $settings['highlight_fillopacity'];
  $getlocations_defaults['highlight_radius'] = $settings['highlight_radius'];
  $getlocations_defaults['getdirections_link'] = $settings['getdirections_link'];
  $getlocations_defaults['show_search_distance'] = $settings['show_search_distance'];
  $getlocations_defaults['views_search_marker_enable'] = $settings['views_search_marker_enable'];
  $getlocations_defaults['views_search_marker'] = $settings['views_search_marker'];
  $getlocations_defaults['views_search_marker_toggle'] = $settings['views_search_marker_toggle'];
  $getlocations_defaults['views_search_marker_toggle_active'] = $settings['views_search_marker_toggle_active'];
  $getlocations_defaults['views_search_radshape_enable'] = $settings['views_search_radshape_enable'];
  $getlocations_defaults['views_search_radshape_strokecolor'] = $settings['views_search_radshape_strokecolor'];
  $getlocations_defaults['views_search_radshape_strokeopacity'] = $settings['views_search_radshape_strokeopacity'];
  $getlocations_defaults['views_search_radshape_strokeweight'] = $settings['views_search_radshape_strokeweight'];
  $getlocations_defaults['views_search_radshape_fillcolor'] = $settings['views_search_radshape_fillcolor'];
  $getlocations_defaults['views_search_radshape_fillopacity'] = $settings['views_search_radshape_fillopacity'];
  $getlocations_defaults['views_search_radshape_toggle'] = $settings['views_search_radshape_toggle'];
  $getlocations_defaults['views_search_radshape_toggle_active'] = $settings['views_search_radshape_toggle_active'];
  $getlocations_defaults['views_search_center'] = $settings['views_search_center'];
  $getlocations_defaults['gps_button'] = $settings['gps_button'];
  $getlocations_defaults['gps_button_label'] = $settings['gps_button_label'];
  $getlocations_defaults['gps_marker'] = $settings['gps_marker'];
  $getlocations_defaults['gps_marker_title'] = $settings['gps_marker_title'];
  $getlocations_defaults['gps_bubble'] = $settings['gps_bubble'];
  $getlocations_defaults['gps_geocode'] = $settings['gps_geocode'];
  $getlocations_defaults['gps_center'] = $settings['gps_center'];
  $getlocations_defaults['gps_type'] = $settings['gps_type'];
  $getlocations_defaults['gps_zoom'] = $settings['gps_zoom'];

  // not ideal but required for preview
  $getlocations_defaults['places'] = 1;

  // onemap
  if ($settings['display_onemap']) {
    $latlons = $lls;
  }
  else {
    $latlons[] = $lls;
  }
  $mapid = getlocations_setup_map($getlocations_defaults);
  getlocations_js_settings_do($getlocations_defaults, $latlons, $minmaxes, $mapid);
  return theme('getlocations_show', array(
    'width' => $settings['display_mapwidth'],
    'height' => $settings['display_mapheight'],
    'defaults' => $getlocations_defaults,
    'mapid' => $mapid,
    'latlons' => $latlons,
    'minmaxes' => $minmaxes,
    'type' => '',
    'node' => '',
  ));
}

/**
 * @param array $locations
 *  An array of locations to be saved
 *
 * @param array $criteria
 *  Relationship information
 *
 * @param string $mode
 *  The action to be carried out
 *
 * @return array $locations
 *
 */
function getlocations_fields_save_locations($locations, $criteria, $settings, $mode) {
  if (isset($locations) && is_array($locations) && !empty($criteria) && is_array($criteria)) {

    // deal with $settings
    if ($settings && isset($settings['map_settings_allow'])) {
      $map_settings_allow = $settings['map_settings_allow'];
    }
    else {
      $map_settings_allow = getlocations_fields_map_settings_allow();
    }
    if ($settings && isset($settings['streetview_settings_allow'])) {
      $streetview_settings_allow = $settings['streetview_settings_allow'];
    }
    else {
      $streetview_settings_allow = getlocations_fields_streetview_settings_allow();
    }
    foreach (array_keys($locations) as $key) {

      // remove empties
      if (empty($locations[$key]['latitude']) && empty($locations[$key]['longitude'])) {
        unset($locations[$key]);
        continue;
      }
      else {

        // normalize
        $locations[$key]['latitude'] = getlocations_normalizelat($locations[$key]['latitude']);
        $locations[$key]['longitude'] = getlocations_normalizelng($locations[$key]['longitude']);
      }

      // dealing with imports
      if (!isset($locations[$key]['name'])) {
        $locations[$key]['name'] = '';
      }
      if (!isset($locations[$key]['street'])) {
        $locations[$key]['street'] = '';
      }
      if (!isset($locations[$key]['additional'])) {
        $locations[$key]['additional'] = '';
      }
      if (!isset($locations[$key]['city'])) {
        $locations[$key]['city'] = '';
      }
      if (!isset($locations[$key]['province'])) {
        $locations[$key]['province'] = '';
      }
      if (!isset($locations[$key]['postal_code'])) {
        $locations[$key]['postal_code'] = '';
      }
      if (!isset($locations[$key]['country'])) {
        $locations[$key]['country'] = '';
      }
      if (!isset($locations[$key]['marker'])) {
        $locations[$key]['marker'] = '';
      }
      if (!isset($locations[$key]['data'])) {
        $locations[$key]['data'] = '';
      }
      $data = array();

      // set defaults
      $dvals = getlocations_fields_data_keys('d');
      foreach ($dvals as $k => $v) {
        $data['data'][$k] = $v;
      }
      if (isset($locations[$key]['phone'])) {
        $locations[$key]['phone'] = trim($locations[$key]['phone']);
        $data['data']['phone'] = check_plain($locations[$key]['phone']);
      }
      if (isset($locations[$key]['mobile'])) {
        $locations[$key]['mobile'] = trim($locations[$key]['mobile']);
        $data['data']['mobile'] = check_plain($locations[$key]['mobile']);
      }
      if (isset($locations[$key]['fax'])) {
        $locations[$key]['fax'] = trim($locations[$key]['fax']);
        $data['data']['fax'] = check_plain($locations[$key]['fax']);
      }

      // streetview_settings
      $data['data']['streetview_settings_allow'] = $streetview_settings_allow;
      if ($streetview_settings_allow) {
        if (isset($locations[$key]['sv_heading']) && is_numeric($locations[$key]['sv_heading'])) {
          $ph = trim($locations[$key]['sv_heading']);

          // sanity check
          while ($ph < 0) {
            $ph = $ph + 360;
          }
          while ($ph > 360) {
            $ph = $ph - 360;
          }
          $data['data']['sv_heading'] = intval($ph);
        }
        if (isset($locations[$key]['sv_zoom']) && is_numeric($locations[$key]['sv_zoom'])) {
          $locations[$key]['sv_zoom'] = trim($locations[$key]['sv_zoom']);

          // sanity check
          if ($locations[$key]['sv_zoom'] > 5) {
            $locations[$key]['sv_zoom'] = 5;
          }
          if ($locations[$key]['sv_zoom'] < 0) {
            $locations[$key]['sv_zoom'] = 0;
          }
          $data['data']['sv_zoom'] = intval($locations[$key]['sv_zoom']);
        }
        if (isset($locations[$key]['sv_pitch']) && is_numeric($locations[$key]['sv_pitch'])) {
          $locations[$key]['sv_pitch'] = trim($locations[$key]['sv_pitch']);

          // sanity check
          $locations[$key]['sv_pitch'] = getlocations_normalizelat($locations[$key]['sv_pitch']);
          $data['data']['sv_pitch'] = intval($locations[$key]['sv_pitch']);
        }
        if (isset($locations[$key]['sv_showfirst']) && is_numeric($locations[$key]['sv_showfirst'])) {
          $data['data']['sv_showfirst'] = intval($locations[$key]['sv_showfirst']);
        }
        if (isset($locations[$key]['sv_enable']) && is_numeric($locations[$key]['sv_enable'])) {
          $data['data']['sv_enable'] = intval($locations[$key]['sv_enable']);
        }

        // display_settings
        if (isset($settings['display_settings'])) {
          foreach ($settings['display_settings'] as $k => $v) {
            $data['data'][$k] = $v;
          }
        }
      }

      // map_settings
      $data['data']['map_settings_allow'] = $map_settings_allow;
      if ($map_settings_allow) {
        if (isset($locations[$key]['mapzoom']) && is_numeric($locations[$key]['mapzoom'])) {
          $locations[$key]['mapzoom'] = trim($locations[$key]['mapzoom']);

          // sanity check
          if ($locations[$key]['mapzoom'] > 21) {
            $locations[$key]['mapzoom'] = 21;
          }
          if ($locations[$key]['mapzoom'] < 0) {
            $locations[$key]['mapzoom'] = 0;
          }
          $data['data']['mapzoom'] = intval($locations[$key]['mapzoom']);
        }
        if (isset($locations[$key]['map_maptype'])) {
          $data['data']['map_maptype'] = check_plain($locations[$key]['map_maptype']);
        }
      }

      // what3words
      $what3words_lic = variable_get('getlocations_what3words_lic', array(
        'key' => '',
        'url' => 'http://api.what3words.com',
      ));
      if ($what3words_lic['key'] && $settings['what3words_collect']) {
        $ll = $locations[$key]['latitude'] . ',' . $locations[$key]['longitude'];
        $ll = getlocations_latlon_check($ll);
        if ($ll) {
          $jd = json_decode(getlocations_cb_w3w_get($ll, 'position'));
          $locations[$key]['what3words'] = implode('.', $jd->words);
          $data['data']['what3words'] = $locations[$key]['what3words'];
        }
      }
      if (count($data)) {
        $locations[$key]['data'] = $data;
      }
      if ($mode == 'insert') {
        if ($locations[$key]['latitude'] && $locations[$key]['longitude']) {
          $result = getlocations_fields_insert_record($locations[$key], $criteria);
          if ($result) {
            $locations[$key]['glid'] = $result;
          }
          else {
            unset($locations[$key]);
            continue;
          }
        }
        else {
          unset($locations[$key]);
          continue;
        }
      }
      elseif ($mode == 'update') {
        if (isset($locations[$key]['active']) && $locations[$key]['active']) {

          // check for elusive glid in feed imports
          if (!isset($locations[$key]['glid']) || !is_numeric($locations[$key]['glid'])) {

            // eek
            $loc = getlocations_fields_load_locations($criteria['nid'], 'nid');
            $locations[$key]['glid'] = $loc[$key]['glid'];
          }
          $result = getlocations_fields_update_record($locations[$key], $criteria);
          if ($result && !isset($locations[$key]['glid'])) {
            $locations[$key]['glid'] = $result;
          }
        }
      }
      elseif ($mode == 'delete') {
        getlocations_fields_delete_record($locations[$key], $criteria);
        unset($locations[$key]);
      }
      elseif ($mode == 'delete_revision') {
        getlocations_fields_delete_revision_record($locations[$key], $criteria);
        unset($locations[$key]);
      }
    }
    return $locations;
  }
}

/**
 * Create a location
 * @param array $location
 *  The new location
 *
 * @param array $relations
 *  Relationship information
 *
 */
function getlocations_fields_insert_record($location, $relations) {
  $noaddress = t('Enter an address');
  if (!isset($location['address']) || $location['address'] == $noaddress) {
    $location['address'] = '';
  }
  if (empty($location['latitude']) && empty($location['longitude'])) {
    return FALSE;
  }
  $query = db_insert('getlocations_fields');
  $query
    ->fields(array(
    'name' => getlocations_apoclean($location['name']),
    'street' => getlocations_apoclean($location['street']),
    'additional' => getlocations_apoclean($location['additional']),
    'city' => getlocations_apoclean($location['city']),
    'province' => getlocations_apoclean($location['province']),
    'postal_code' => getlocations_apoclean($location['postal_code']),
    'country' => check_plain($location['country']),
    'address' => getlocations_apoclean($location['address']),
    'latitude' => (double) $location['latitude'],
    'longitude' => (double) $location['longitude'],
    'marker' => $location['marker'] ? $location['marker'] : '',
    'data' => !empty($location['data']) ? serialize($location['data']) : '',
  ));
  $result = $query
    ->execute();

  // $result should contain glid
  // new insert id
  if ($result) {
    $relations['glid'] = $result;
    getlocations_fields_insert_record_relations($relations);
    return $result;
  }
  return FALSE;
}

/**
 * Insert the relationship information
 * @param array $relations
 *
 */
function getlocations_fields_insert_record_relations($relations) {
  db_insert('getlocations_fields_entities')
    ->fields($relations)
    ->execute();
}

/**
 * Create a location
 * @param array $location
 *  The location
 *
 * @param array $relations
 *  Relationship information
 *
 */
function getlocations_fields_update_record($location, $relations) {
  if (!isset($location['glid'])) {

    // this record does not exist yet
    $result = getlocations_fields_insert_record($location, $relations);
    return $result;
  }
  $noaddress = t('Enter an address');
  if (!isset($location['address']) || $location['address'] == $noaddress) {
    $location['address'] = '';
  }
  if (empty($location['latitude']) && empty($location['longitude'])) {
    return FALSE;
  }
  $num_updated = db_update('getlocations_fields')
    ->fields(array(
    'name' => getlocations_apoclean($location['name']),
    'street' => getlocations_apoclean($location['street']),
    'additional' => getlocations_apoclean($location['additional']),
    'city' => getlocations_apoclean($location['city']),
    'province' => getlocations_apoclean($location['province']),
    'postal_code' => getlocations_apoclean($location['postal_code']),
    'country' => check_plain($location['country']),
    'address' => getlocations_apoclean($location['address']),
    'latitude' => (double) $location['latitude'],
    'longitude' => (double) $location['longitude'],
    'marker' => $location['marker'] ? $location['marker'] : '',
    'data' => !empty($location['data']) ? serialize($location['data']) : '',
  ))
    ->condition('glid', $location['glid'])
    ->execute();
  getlocations_fields_update_record_relations($relations, $location['glid']);
}

/**
 * @param array $relations
 *  Relationship information
 *
 * @param int $glid
 *  The location ID
 *
 */
function getlocations_fields_update_record_relations($relations, $glid) {
  $num_updated = db_update('getlocations_fields_entities')
    ->fields($relations)
    ->condition('glid', $glid)
    ->execute();
}

/**
 * Create a location
 * @param array $location
 *  The location
 *
 * @param array $relations
 *  Relationship information
 *
 */
function getlocations_fields_delete_record($location, $relations) {
  if (isset($location['glid'])) {
    db_delete('getlocations_fields')
      ->condition('glid', $location['glid'])
      ->execute();
    getlocations_fields_delete_record_relations($relations, $location['glid']);
  }
}

/**
 * @param array $relations
 *  Relationship information
 *
 * @param int $glid
 *  The location ID
 *
 */
function getlocations_fields_delete_record_relations($relations, $glid) {
  db_delete('getlocations_fields_entities')
    ->condition('glid', $glid)
    ->execute();
}

/**
 * Create a location
 * @param array $location
 *  The location
 *
 * @param array $relations
 *  Relationship information
 *
 */
function getlocations_fields_delete_revision_record($location, $relations) {
  if (isset($location['glid']) && $location['glid']) {
    $glid = $location['glid'];
    getlocations_fields_delete_revision_record_relations($relations, $glid);

    // now see if there are any left for glid
    $query = db_select('getlocations_fields', 'f');
    $query
      ->fields('e', array(
      'nid',
      'vid',
      'uid',
      'tid',
      'cid',
    ));
    $query
      ->join('getlocations_fields_entities', 'e', 'f.glid=e.glid');
    $query
      ->condition('e.glid', $glid);
    $rows = $query
      ->execute();
    $ct = 0;
    foreach ($rows as $row) {
      if ($row->nid || $row->vid || $row->uid || $row->tid || $row->cid) {
        $ct++;
      }
    }
    if (!$ct) {
      db_delete('getlocations_fields')
        ->condition('glid', $glid)
        ->execute();
    }
  }
}

/**
 * @param array $relations
 *  Relationship information
 *
 * @param int $glid
 *  The location ID
 *
 */
function getlocations_fields_delete_revision_record_relations($relations, $glid) {
  $query = db_delete('getlocations_fields_entities')
    ->condition('glid', $glid);
  foreach ($relations as $key => $value) {
    $query
      ->condition($key, $value);
  }
  $query
    ->execute();
}

/**
 * @param int $id
 *  Primary identifier of the content type the location is linked to
 *
 * @param string $key
 *  The name of the primary identifier
 *
 * @return array $locations
 *  The locations associated with the content type
 *
 */
function getlocations_fields_load_locations($id, $key = 'vid', $field_name = '') {

  // $key can be vid, nid, uid, tid, cid
  global $user;
  $roles = $user->roles;
  $locations = array();
  if ($id) {
    module_load_include('inc', 'getlocations_fields', 'getlocations_fields.functions');
    $getlocations_fields_defaults = getlocations_fields_defaults();
    $fields = array(
      'glid',
      'name',
      'street',
      'additional',
      'city',
      'province',
      'postal_code',
      'country',
      'address',
      'latitude',
      'longitude',
      'marker',
    );
    if (getlocations_fields_column_check('data')) {
      $fields[] = 'data';
    }
    $query = db_select('getlocations_fields', 'f');
    $query
      ->fields('f', $fields);
    $query
      ->fields('e', array(
      'nid',
      'vid',
      'uid',
      'tid',
      'cid',
      'field_name',
    ));
    $query
      ->join('getlocations_fields_entities', 'e', 'f.glid=e.glid');

    // we need to limit on status for nid, vid, uid and cid but allow admin
    if (!in_array('administrator', $roles)) {
      if ($key == 'nid') {
        $query
          ->join('node', 'n', 'n.nid = e.nid');
        $query
          ->condition('e.nid', $id)
          ->condition('n.status', 0, '>');
      }
      elseif ($key == 'vid') {
        $query
          ->join('node_revision', 'n', 'n.vid = e.vid');
        $query
          ->condition('e.vid', $id)
          ->condition('n.status', 0, '>');
      }
      elseif ($key == 'uid') {
        $query
          ->join('users', 'n', 'n.uid = e.uid');
        $query
          ->condition('e.uid', $id)
          ->condition('n.status', 0, '>');
      }
      elseif (module_exists('comment') && $key == 'cid') {
        $query
          ->join('comment', 'n', 'n.cid = e.cid');
        $query
          ->condition('e.cid', $id)
          ->condition('n.status', 0, '>');
      }
    }
    if ($field_name) {
      $query
        ->join('field_config', 'c', 'e.field_name = c.field_name');
      $query
        ->condition('e.field_name', $field_name);
    }
    $query
      ->condition('e.' . $key, $id);
    $rows = $query
      ->execute();
    $ct = 0;
    foreach ($rows as $row) {
      $locations[$ct]['glid'] = $row->glid;
      $locations[$ct]['lid'] = $row->glid;
      $locations[$ct]['name'] = $row->name;
      $locations[$ct]['street'] = $row->street;
      $locations[$ct]['additional'] = $row->additional;
      $locations[$ct]['city'] = $row->city;
      $locations[$ct]['province'] = $row->province;
      $locations[$ct]['province_name'] = $row->province;
      $locations[$ct]['postal_code'] = $row->postal_code;
      if ($getlocations_fields_defaults['country_full'] && drupal_strlen($row->country) == 2) {
        $locations[$ct]['country_name'] = getlocations_get_country_name($row->country);
      }
      else {
        $locations[$ct]['country_name'] = $row->country;
      }
      $locations[$ct]['country'] = $row->country;
      $locations[$ct]['address'] = $row->address;
      $locations[$ct]['latitude'] = $row->latitude;
      $locations[$ct]['longitude'] = $row->longitude;
      $locations[$ct]['marker'] = $row->marker;
      $data = isset($row->data) && !empty($row->data) ? unserialize($row->data) : '';
      $keys = getlocations_fields_data_keys('d');
      if (is_array($data) && isset($data['data']['map_settings_allow'])) {
        $map_settings_allow = $data['data']['map_settings_allow'];
      }
      else {
        $map_settings_allow = getlocations_fields_map_settings_allow();
      }
      foreach ($keys as $key => $dval) {
        $locations[$ct][$key] = $dval;
        if (is_array($data) && isset($data['data'][$key])) {

          #$do = TRUE;
          if (!$map_settings_allow && ($key == 'mapzoom' || $key == 'map_maptype')) {

            #$do = FALSE;
            continue;
          }

          #if ($do) {
          $locations[$ct][$key] = $data['data'][$key];

          #}
        }
      }

      // what3words
      $what3words_lic = variable_get('getlocations_what3words_lic', array(
        'key' => '',
        'url' => 'http://api.what3words.com',
      ));
      if ($what3words_lic['key'] && is_array($data) && isset($data['data']['what3words'])) {
        $locations[$ct]['what3words'] = $data['data']['what3words'];
      }
      $locations[$ct]['nid'] = $row->nid;
      $locations[$ct]['vid'] = $row->vid;
      $locations[$ct]['uid'] = $row->uid;
      $locations[$ct]['tid'] = $row->tid;
      $locations[$ct]['cid'] = $row->cid;
      $locations[$ct]['field_name'] = $row->field_name;
      if ($row->nid) {
        $locations[$ct]['type'] = 'node';
      }
      elseif ($row->uid) {
        $locations[$ct]['type'] = 'user';
      }
      elseif ($row->tid) {
        $locations[$ct]['type'] = 'vocabulary';
      }
      elseif ($row->cid) {
        $locations[$ct]['type'] = 'comment';
      }
      $ct++;
    }
  }
  return $locations;
}

/**
 * @param int $glid
 *  Location ID
 */
function getlocations_fields_load_location($glid) {
  global $user;
  $roles = $user->roles;
  $location = array();
  if ($glid) {
    module_load_include('inc', 'getlocations_fields', 'getlocations_fields.functions');
    $getlocations_fields_defaults = getlocations_fields_defaults();
    $fields = array(
      'glid',
      'name',
      'street',
      'additional',
      'city',
      'province',
      'postal_code',
      'country',
      'address',
      'latitude',
      'longitude',
      'marker',
    );
    if (getlocations_fields_column_check('data')) {
      $fields[] = 'data';
    }
    $query = db_select('getlocations_fields', 'f');
    $query
      ->fields('f', $fields);
    $query
      ->fields('e', array(
      'nid',
      'vid',
      'uid',
      'tid',
      'cid',
    ));
    $query
      ->join('getlocations_fields_entities', 'e', 'f.glid=e.glid');

    #    if (! in_array('administrator', $roles)) {

    #      if ($key == 'nid') {

    #        $query->join('node', 'n', 'n.nid = e.nid');

    #        $query->condition('e.nid', $id)->condition('n.status', 0, '>');

    #      }

    #      elseif ($key == 'vid') {

    #        $query->join('node_revision', 'n', 'n.vid = e.vid');

    #        $query->condition('e.vid', $id)->condition('n.status', 0, '>');

    #      }

    #      elseif ($key == 'uid') {

    #        $query->join('users', 'n', 'n.uid = e.uid');

    #        $query->condition('e.uid', $id)->condition('n.status', 0, '>');

    #      }

    #      elseif (module_exists('comment') && $key == 'cid') {

    #        $query->join('comment', 'n', 'n.cid = e.cid');

    #        $query->condition('e.cid', $id)->condition('n.status', 0, '>');

    #      }

    #    }
    $query
      ->condition('f.glid', $glid);
    $row = $query
      ->execute()
      ->fetchObject();
    if ($row) {
      $location['glid'] = $row->glid;
      $location['lid'] = $row->glid;
      $location['name'] = $row->name;
      $location['street'] = $row->street;
      $location['additional'] = $row->additional;
      $location['city'] = $row->city;
      $location['province'] = $row->province;
      $location['province_name'] = $row->province;
      $location['postal_code'] = $row->postal_code;
      if ($getlocations_fields_defaults['country_full'] && drupal_strlen($row->country) == 2) {
        $location['country_name'] = getlocations_get_country_name($row->country);
      }
      else {
        $location['country_name'] = $row->country;
      }
      $location['country'] = $row->country;
      $location['address'] = $row->address;
      $location['latitude'] = $row->latitude;
      $location['longitude'] = $row->longitude;
      $location['marker'] = $row->marker;
      $data = isset($row->data) && !empty($row->data) ? unserialize($row->data) : '';
      $keys = getlocations_fields_data_keys('d');
      if (is_array($data) && isset($data['data']['map_settings_allow'])) {
        $map_settings_allow = $data['data']['map_settings_allow'];
      }
      else {
        $map_settings_allow = getlocations_fields_map_settings_allow();
      }
      foreach ($keys as $key => $dval) {
        $location[$key] = $dval;
        if (is_array($data) && isset($data['data'][$key])) {
          if (!$map_settings_allow && ($key == 'mapzoom' || $key == 'map_maptype')) {
            continue;
          }
          $location[$key] = $data['data'][$key];
        }
      }

      // what3words
      $what3words_lic = variable_get('getlocations_what3words_lic', array(
        'key' => '',
        'url' => 'http://api.what3words.com',
      ));
      if ($what3words_lic['key'] && is_array($data) && isset($data['data']['what3words'])) {
        $location['what3words'] = $data['data']['what3words'];
      }
      $location['nid'] = $row->nid;
      $location['vid'] = $row->vid;
      $location['uid'] = $row->uid;
      $location['tid'] = $row->tid;
      $location['cid'] = $row->cid;
      if ($row->nid) {
        $location['type'] = 'node';
      }
      elseif ($row->uid) {
        $location['type'] = 'user';
      }
      elseif ($row->tid) {
        $location['type'] = 'vocabulary';
      }
      elseif ($row->cid) {
        $location['type'] = 'comment';
      }
    }
  }
  return $location;
}

/**
 * @param array $defaults
 *  Settings
 *
 * @param string $mapid
 *  Unique map identifier used in javascript to allow multiple maps
 *
 */
function getlocations_fields_js_settings_do($defaults, $mapid) {
  $settings = array(
    $mapid => array(
      'nodezoom' => $defaults['nodezoom'],
      'map_marker' => $defaults['input_map_marker'],
      'use_address' => $defaults['use_address'],
      'latlon_warning' => $defaults['latlon_warning'],
      'autocomplete_bias' => $defaults['autocomplete_bias'],
      'restrict_by_country' => $defaults['restrict_by_country'],
      'search_country' => $defaults['search_country'],
      'smart_ip_path' => url("getlocations_fields/smart_ip"),
      'street_num_pos' => $defaults['street_num_pos'],
      'sv_heading' => $defaults['sv_heading'],
      'sv_zoom' => $defaults['sv_zoom'],
      'sv_pitch' => $defaults['sv_pitch'],
      'sv_enable' => $defaults['sv_enable'],
      'sv_showfirst' => $defaults['sv_showfirst'],
      'map_settings_allow' => $defaults['map_settings_allow'],
      'streetview_settings_allow' => $defaults['streetview_settings_allow'],
    ),
  );
  drupal_add_js(array(
    'getlocations_fields' => $settings,
  ), 'setting');
}

/**
 * @return array $defaults
 *  Settings
 */
function getlocations_fields_defaults() {
  $getlocations_defaults = getlocations_defaults();
  $dvals = getlocations_fields_data_keys('d');
  $defaults = array(
    'country' => variable_get('site_default_country', ''),
    'display_mapwidth' => $getlocations_defaults['width'],
    'display_mapheight' => $getlocations_defaults['height'],
    'nodezoom' => $getlocations_defaults['nodezoom'],
    'minzoom_map' => $getlocations_defaults['minzoom_map'],
    'maxzoom_map' => $getlocations_defaults['maxzoom_map'],
    'map_backgroundcolor' => $getlocations_defaults['map_backgroundcolor'],
    'show_maplinks' => $getlocations_defaults['show_maplinks'],
    'fullscreen' => $getlocations_defaults['fullscreen'],
    'fullscreen_controlposition' => $getlocations_defaults['fullscreen_controlposition'],
    'show_bubble_on_one_marker' => $getlocations_defaults['show_bubble_on_one_marker'],
    'display_showmap' => 1,
    'display_maplink' => 1,
    'display_latlong' => 1,
    'display_dms' => 0,
    'display_geo_microformat' => 0,
    'display_onemap' => 0,
    'display_name' => 1,
    'display_street' => 1,
    'display_additional' => 1,
    'display_city' => 1,
    'display_province' => 1,
    'display_postal_code' => 1,
    'display_country' => 1,
    'display_phone' => 1,
    'display_mobile' => 1,
    'display_fax' => 1,
    'country_full' => 1,
    'use_address' => 1,
    'input_address_width' => 40,
    'input_name_width' => 40,
    'input_street_width' => 40,
    'input_additional_width' => 40,
    'input_city_width' => 40,
    'input_province_width' => 40,
    'input_postal_code_width' => 40,
    'input_country_width' => 40,
    'input_phone_width' => 20,
    'input_mobile_width' => 20,
    'input_fax_width' => 20,
    'input_latitude_width' => 20,
    'input_longitude_width' => 20,
    'input_name_required' => 0,
    'input_street_required' => 0,
    'input_additional_required' => 0,
    'input_city_required' => 0,
    'input_province_required' => 0,
    'input_postal_code_required' => 0,
    'input_country_required' => 0,
    'input_phone_required' => 0,
    'input_mobile_required' => 0,
    'input_fax_required' => 0,
    'input_name_weight' => 0,
    'input_street_weight' => 0,
    'input_additional_weight' => 0,
    'input_city_weight' => 0,
    'input_province_weight' => 0,
    'input_postal_code_weight' => 0,
    'input_country_weight' => 0,
    'input_phone_weight' => 0,
    'input_mobile_weight' => 0,
    'input_fax_weight' => 0,
    'input_latitude_weight' => 0,
    'input_longitude_weight' => 0,
    'input_map_weight' => 0,
    'input_marker_weight' => 0,
    'input_geobutton_weight' => 0,
    'input_smart_ip_button_weight' => 0,
    'input_geolocation_button_weight' => 0,
    'input_clear_button_weight' => 0,
    'use_clear_button' => 1,
    'use_smart_ip_button' => 0,
    'use_geolocation_button' => 0,
    'use_country_dropdown' => 1,
    'only_continents' => '',
    'only_countries' => '',
    'province_autocomplete' => 0,
    'city_autocomplete' => 0,
    'per_item_marker' => 0,
    'street_num_pos' => 1,
    'latlon_warning' => 0,
    'map_settings_allow' => 0,
    'streetview_settings_allow' => 0,
    'sv_heading' => $dvals['sv_heading'],
    'sv_zoom' => $dvals['sv_zoom'],
    'sv_pitch' => $dvals['sv_pitch'],
    'sv_enable' => $dvals['sv_enable'],
    'sv_showfirst' => $getlocations_defaults['sv_showfirst'],
    'autocomplete_bias' => 0,
    'restrict_by_country' => 0,
    'search_country' => variable_get('site_default_country', ''),
    'use_smart_ip_latlon' => 0,
    'columns' => array(
      'glid' => t('Glid'),
      'name' => t('Name'),
      'street' => t('Street'),
      'additional' => t('Additional'),
      'city' => t('City'),
      'province' => t('Province'),
      'postal_code' => t('Post code'),
      'country' => t('Country'),
      'latitude' => t('Latitude'),
      'longitude' => t('Longitude'),
      'marker' => t('Marker'),
      'data' => t('Data'),
    ),
    'polygons_enable' => $getlocations_defaults['polygons_enable'],
    'polygons_strokecolor' => $getlocations_defaults['polygons_strokecolor'],
    'polygons_strokeopacity' => $getlocations_defaults['polygons_strokeopacity'],
    'polygons_strokeweight' => $getlocations_defaults['polygons_strokeweight'],
    'polygons_fillcolor' => $getlocations_defaults['polygons_fillcolor'],
    'polygons_fillopacity' => $getlocations_defaults['polygons_fillopacity'],
    'polygons_coords' => $getlocations_defaults['polygons_coords'],
    'polygons_clickable' => $getlocations_defaults['polygons_clickable'],
    'polygons_message' => $getlocations_defaults['polygons_message'],
    'rectangles_enable' => $getlocations_defaults['rectangles_enable'],
    'rectangles_strokecolor' => $getlocations_defaults['rectangles_strokecolor'],
    'rectangles_strokeopacity' => $getlocations_defaults['rectangles_strokeopacity'],
    'rectangles_strokeweight' => $getlocations_defaults['rectangles_strokeweight'],
    'rectangles_fillcolor' => $getlocations_defaults['rectangles_fillcolor'],
    'rectangles_fillopacity' => $getlocations_defaults['rectangles_fillopacity'],
    'rectangles_coords' => $getlocations_defaults['rectangles_coords'],
    'rectangles_clickable' => $getlocations_defaults['rectangles_clickable'],
    'rectangles_message' => $getlocations_defaults['rectangles_message'],
    'rectangles_apply' => $getlocations_defaults['rectangles_apply'],
    'rectangles_dist' => $getlocations_defaults['rectangles_dist'],
    'circles_enable' => $getlocations_defaults['circles_enable'],
    'circles_strokecolor' => $getlocations_defaults['circles_strokecolor'],
    'circles_strokeopacity' => $getlocations_defaults['circles_strokeopacity'],
    'circles_strokeweight' => $getlocations_defaults['circles_strokeweight'],
    'circles_fillcolor' => $getlocations_defaults['circles_fillcolor'],
    'circles_fillopacity' => $getlocations_defaults['circles_fillopacity'],
    'circles_coords' => $getlocations_defaults['circles_coords'],
    'circles_clickable' => $getlocations_defaults['circles_clickable'],
    'circles_message' => $getlocations_defaults['circles_message'],
    'circles_radius' => $getlocations_defaults['circles_radius'],
    'circles_apply' => $getlocations_defaults['circles_apply'],
    'polylines_enable' => $getlocations_defaults['polylines_enable'],
    'polylines_strokecolor' => $getlocations_defaults['polylines_strokecolor'],
    'polylines_strokeopacity' => $getlocations_defaults['polylines_strokeopacity'],
    'polylines_strokeweight' => $getlocations_defaults['polylines_strokeweight'],
    'polylines_coords' => $getlocations_defaults['polylines_coords'],
    'polylines_clickable' => $getlocations_defaults['polylines_clickable'],
    'polylines_message' => $getlocations_defaults['polylines_message'],
    'search_places' => $getlocations_defaults['search_places'],
    'search_places_size' => $getlocations_defaults['search_places_size'],
    'search_places_position' => $getlocations_defaults['search_places_position'],
    'search_places_label' => $getlocations_defaults['search_places_label'],
    'search_places_placeholder' => $getlocations_defaults['search_places_placeholder'],
    'search_places_dd' => $getlocations_defaults['search_places_dd'],
    'search_places_list' => $getlocations_defaults['search_places_list'],
    'jquery_colorpicker_enabled' => $getlocations_defaults['jquery_colorpicker_enabled'],
    'geojson_enable' => $getlocations_defaults['geojson_enable'],
    'geojson_data' => $getlocations_defaults['geojson_data'],
    'geojson_options' => $getlocations_defaults['geojson_options'],
    'nokeyboard' => $getlocations_defaults['nokeyboard'],
    'nodoubleclickzoom' => $getlocations_defaults['nodoubleclickzoom'],
    'zoomcontrolposition' => $getlocations_defaults['zoomcontrolposition'],
    'mapcontrolposition' => $getlocations_defaults['mapcontrolposition'],
    'pancontrolposition' => $getlocations_defaults['pancontrolposition'],
    'scalecontrolposition' => $getlocations_defaults['scalecontrolposition'],
    'svcontrolposition' => $getlocations_defaults['svcontrolposition'],
    'sv_addresscontrol' => $getlocations_defaults['sv_addresscontrol'],
    // sv overlay controls
    'sv_addresscontrolposition' => $getlocations_defaults['sv_addresscontrolposition'],
    'sv_pancontrol' => $getlocations_defaults['sv_pancontrol'],
    'sv_pancontrolposition' => $getlocations_defaults['sv_pancontrolposition'],
    'sv_zoomcontrol' => $getlocations_defaults['sv_zoomcontrol'],
    'sv_zoomcontrolposition' => $getlocations_defaults['sv_zoomcontrolposition'],
    'sv_linkscontrol' => $getlocations_defaults['sv_linkscontrol'],
    'sv_imagedatecontrol' => $getlocations_defaults['sv_imagedatecontrol'],
    'sv_scrollwheel' => $getlocations_defaults['sv_scrollwheel'],
    'sv_clicktogo' => $getlocations_defaults['sv_clicktogo'],
    'input_map_show' => 1,
    'highlight_enable' => $getlocations_defaults['highlight_enable'],
    'highlight_strokecolor' => $getlocations_defaults['highlight_strokecolor'],
    'highlight_strokeopacity' => $getlocations_defaults['highlight_strokeopacity'],
    'highlight_strokeweight' => $getlocations_defaults['highlight_strokeweight'],
    'highlight_fillcolor' => $getlocations_defaults['highlight_fillcolor'],
    'highlight_fillopacity' => $getlocations_defaults['highlight_fillopacity'],
    'highlight_radius' => $getlocations_defaults['highlight_radius'],
    'getdirections_link' => $getlocations_defaults['getdirections_link'],
    'show_search_distance' => $getlocations_defaults['show_search_distance'],
    'views_search_marker_enable' => $getlocations_defaults['views_search_marker_enable'],
    'views_search_marker' => $getlocations_defaults['views_search_marker'],
    'views_search_marker_toggle' => $getlocations_defaults['views_search_marker_toggle'],
    'views_search_marker_toggle_active' => $getlocations_defaults['views_search_marker_toggle_active'],
    'views_search_radshape_enable' => $getlocations_defaults['views_search_radshape_enable'],
    'views_search_radshape_strokecolor' => $getlocations_defaults['views_search_radshape_strokecolor'],
    'views_search_radshape_strokeopacity' => $getlocations_defaults['views_search_radshape_strokeopacity'],
    'views_search_radshape_strokeweight' => $getlocations_defaults['views_search_radshape_strokeweight'],
    'views_search_radshape_fillcolor' => $getlocations_defaults['views_search_radshape_fillcolor'],
    'views_search_radshape_fillopacity' => $getlocations_defaults['views_search_radshape_fillopacity'],
    'views_search_radshape_toggle' => $getlocations_defaults['views_search_radshape_toggle'],
    'views_search_radshape_toggle_active' => $getlocations_defaults['views_search_radshape_toggle_active'],
    'views_search_center' => $getlocations_defaults['views_search_center'],
    'gps_button' => 0,
    'gps_button_label' => $getlocations_defaults['gps_button_label'],
    'gps_marker' => $getlocations_defaults['gps_marker'],
    'gps_marker_title' => $getlocations_defaults['gps_marker_title'],
    'gps_bubble' => $getlocations_defaults['gps_bubble'],
    'gps_geocode' => $getlocations_defaults['gps_geocode'],
    'gps_center' => $getlocations_defaults['gps_center'],
    'gps_type' => $getlocations_defaults['gps_type'],
    'gps_zoom' => $getlocations_defaults['gps_zoom'],
    'geocoder_enable' => $getlocations_defaults['geocoder_enable'],
  );
  foreach ($getlocations_defaults['baselayers'] as $key => $layer) {
    $defaults['baselayers'][$key] = $layer;
  }
  foreach ($getlocations_defaults['kml_group'] as $key => $layer) {
    $defaults['kml_group'][$key] = $layer;
  }

  // what3words
  $defaults['what3words_display'] = 0;
  $defaults['what3words_collect'] = 0;
  $getlocations_fields_defaults = variable_get('getlocations_fields_defaults', $defaults);
  $newdefaults = getlocations_adjust_vars($defaults, $getlocations_fields_defaults);
  return $newdefaults;
}

/**
 * @return array $defaults
 *  Settings for the Field module
 */
function getlocations_fields_field_info_defaults() {
  $getlocations_defaults = getlocations_defaults();
  $getlocations_fields_defaults = getlocations_fields_defaults();
  $defaults = array(
    'getlocations_fields' => array(
      'label' => t('Getlocations Fields'),
      'description' => t('Provide Getlocations Fields.'),
      'default_formatter' => 'getlocations_fields_default',
      'default_widget' => 'getlocations_fields',
      'settings' => array(
        'mapwidth' => $getlocations_defaults['width'],
        'mapheight' => $getlocations_defaults['height'],
        'latlong' => $getlocations_defaults['latlong'],
        'zoom' => $getlocations_defaults['zoom'],
        'minzoom_map' => $getlocations_defaults['minzoom_map'],
        'maxzoom_map' => $getlocations_defaults['maxzoom_map'],
        'controltype' => $getlocations_defaults['controltype'],
        'pancontrol' => $getlocations_defaults['pancontrol'],
        'mtc' => $getlocations_defaults['mtc'],
        'maptype' => $getlocations_defaults['maptype'],
        'scale' => $getlocations_defaults['scale'],
        'overview' => $getlocations_defaults['overview'],
        'overview_opened' => $getlocations_defaults['overview_opened'],
        'scrollwheel' => $getlocations_defaults['scrollwheel'],
        'draggable' => $getlocations_defaults['draggable'],
        'map_marker' => $getlocations_defaults['node_map_marker'],
        'node_map_marker' => $getlocations_defaults['node_map_marker'],
        'user_map_marker' => $getlocations_defaults['user_map_marker'],
        'vocabulary_map_marker' => $getlocations_defaults['vocabulary_map_marker'],
        'comment_map_marker' => $getlocations_defaults['comment_map_marker'],
        'input_map_marker' => $getlocations_defaults['input_map_marker'],
        'use_address' => $getlocations_fields_defaults['use_address'],
        'input_address_width' => $getlocations_fields_defaults['input_address_width'],
        'input_name_width' => $getlocations_fields_defaults['input_name_width'],
        'input_street_width' => $getlocations_fields_defaults['input_street_width'],
        'input_additional_width' => $getlocations_fields_defaults['input_additional_width'],
        'input_city_width' => $getlocations_fields_defaults['input_city_width'],
        'input_province_width' => $getlocations_fields_defaults['input_province_width'],
        'input_postal_code_width' => $getlocations_fields_defaults['input_postal_code_width'],
        'input_country_width' => $getlocations_fields_defaults['input_country_width'],
        'input_phone_width' => $getlocations_fields_defaults['input_phone_width'],
        'input_mobile_width' => $getlocations_fields_defaults['input_mobile_width'],
        'input_fax_width' => $getlocations_fields_defaults['input_fax_width'],
        'input_latitude_width' => $getlocations_fields_defaults['input_latitude_width'],
        'input_longitude_width' => $getlocations_fields_defaults['input_longitude_width'],
        'input_name_required' => $getlocations_fields_defaults['input_name_required'],
        'input_street_required' => $getlocations_fields_defaults['input_street_required'],
        'input_additional_required' => $getlocations_fields_defaults['input_additional_required'],
        'input_city_required' => $getlocations_fields_defaults['input_city_required'],
        'input_province_required' => $getlocations_fields_defaults['input_province_required'],
        'input_postal_code_required' => $getlocations_fields_defaults['input_postal_code_required'],
        'input_country_required' => $getlocations_fields_defaults['input_country_required'],
        'input_phone_required' => $getlocations_fields_defaults['input_phone_required'],
        'input_mobile_required' => $getlocations_fields_defaults['input_mobile_required'],
        'input_fax_required' => $getlocations_fields_defaults['input_fax_required'],
        'input_name_weight' => $getlocations_fields_defaults['input_name_weight'],
        'input_street_weight' => $getlocations_fields_defaults['input_street_weight'],
        'input_additional_weight' => $getlocations_fields_defaults['input_additional_weight'],
        'input_city_weight' => $getlocations_fields_defaults['input_city_weight'],
        'input_province_weight' => $getlocations_fields_defaults['input_province_weight'],
        'input_postal_code_weight' => $getlocations_fields_defaults['input_postal_code_weight'],
        'input_country_weight' => $getlocations_fields_defaults['input_country_weight'],
        'input_phone_weight' => $getlocations_fields_defaults['input_phone_weight'],
        'input_mobile_weight' => $getlocations_fields_defaults['input_mobile_weight'],
        'input_fax_weight' => $getlocations_fields_defaults['input_fax_weight'],
        'input_map_weight' => $getlocations_fields_defaults['input_map_weight'],
        'input_marker_weight' => $getlocations_fields_defaults['input_marker_weight'],
        'input_latitude_weight' => $getlocations_fields_defaults['input_latitude_weight'],
        'input_longitude_weight' => $getlocations_fields_defaults['input_longitude_weight'],
        'input_geobutton_weight' => $getlocations_fields_defaults['input_geobutton_weight'],
        'input_smart_ip_button_weight' => $getlocations_fields_defaults['input_smart_ip_button_weight'],
        'input_geolocation_button_weight' => $getlocations_fields_defaults['input_geolocation_button_weight'],
        'use_smart_ip_button' => $getlocations_fields_defaults['use_smart_ip_button'],
        'input_clear_button_weight' => $getlocations_fields_defaults['input_clear_button_weight'],
        'use_clear_button' => $getlocations_fields_defaults['use_clear_button'],
        'use_country_dropdown' => $getlocations_fields_defaults['use_country_dropdown'],
        'only_continents' => $getlocations_fields_defaults['only_continents'],
        'only_countries' => $getlocations_fields_defaults['only_countries'],
        'province_autocomplete' => $getlocations_fields_defaults['province_autocomplete'],
        'city_autocomplete' => $getlocations_fields_defaults['city_autocomplete'],
        'country' => $getlocations_fields_defaults['country'],
        'per_item_marker' => $getlocations_fields_defaults['per_item_marker'],
        'street_num_pos' => $getlocations_fields_defaults['street_num_pos'],
        'latlon_warning' => $getlocations_fields_defaults['latlon_warning'],
        'map_settings_allow' => $getlocations_fields_defaults['map_settings_allow'],
        'streetview_settings_allow' => $getlocations_fields_defaults['streetview_settings_allow'],
        'sv_heading' => $getlocations_fields_defaults['sv_heading'],
        'sv_zoom' => $getlocations_fields_defaults['sv_zoom'],
        'sv_pitch' => $getlocations_fields_defaults['sv_pitch'],
        'sv_enable' => $getlocations_fields_defaults['sv_enable'],
        'sv_showfirst' => $getlocations_fields_defaults['sv_showfirst'],
        'use_geolocation_button' => $getlocations_fields_defaults['use_geolocation_button'],
        'autocomplete_bias' => $getlocations_fields_defaults['autocomplete_bias'],
        'restrict_by_country' => $getlocations_fields_defaults['restrict_by_country'],
        'search_country' => $getlocations_fields_defaults['search_country'],
        'map_backgroundcolor' => $getlocations_fields_defaults['map_backgroundcolor'],
        'use_smart_ip_latlon' => $getlocations_fields_defaults['use_smart_ip_latlon'],
        'show_maplinks' => $getlocations_defaults['show_maplinks'],
        'fullscreen' => $getlocations_defaults['fullscreen'],
        'fullscreen_controlposition' => $getlocations_defaults['fullscreen_controlposition'],
        'show_bubble_on_one_marker' => $getlocations_defaults['show_bubble_on_one_marker'],
        'polygons_enable' => $getlocations_fields_defaults['polygons_enable'],
        'polygons_strokecolor' => $getlocations_fields_defaults['polygons_strokecolor'],
        'polygons_strokeopacity' => $getlocations_fields_defaults['polygons_strokeopacity'],
        'polygons_strokeweight' => $getlocations_fields_defaults['polygons_strokeweight'],
        'polygons_fillcolor' => $getlocations_fields_defaults['polygons_fillcolor'],
        'polygons_fillopacity' => $getlocations_fields_defaults['polygons_fillopacity'],
        'polygons_coords' => $getlocations_fields_defaults['polygons_coords'],
        'polygons_clickable' => $getlocations_fields_defaults['polygons_clickable'],
        'polygons_message' => $getlocations_fields_defaults['polygons_message'],
        'rectangles_enable' => $getlocations_fields_defaults['rectangles_enable'],
        'rectangles_strokecolor' => $getlocations_fields_defaults['rectangles_strokecolor'],
        'rectangles_strokeopacity' => $getlocations_fields_defaults['rectangles_strokeopacity'],
        'rectangles_strokeweight' => $getlocations_fields_defaults['rectangles_strokeweight'],
        'rectangles_fillcolor' => $getlocations_fields_defaults['rectangles_fillcolor'],
        'rectangles_fillopacity' => $getlocations_fields_defaults['rectangles_fillopacity'],
        'rectangles_coords' => $getlocations_fields_defaults['rectangles_coords'],
        'rectangles_clickable' => $getlocations_fields_defaults['rectangles_clickable'],
        'rectangles_message' => $getlocations_fields_defaults['rectangles_message'],
        'rectangles_apply' => $getlocations_fields_defaults['rectangles_apply'],
        'rectangles_dist' => $getlocations_fields_defaults['rectangles_dist'],
        'circles_enable' => $getlocations_fields_defaults['circles_enable'],
        'circles_strokecolor' => $getlocations_fields_defaults['circles_strokecolor'],
        'circles_strokeopacity' => $getlocations_fields_defaults['circles_strokeopacity'],
        'circles_strokeweight' => $getlocations_fields_defaults['circles_strokeweight'],
        'circles_fillcolor' => $getlocations_fields_defaults['circles_fillcolor'],
        'circles_fillopacity' => $getlocations_fields_defaults['circles_fillopacity'],
        'circles_coords' => $getlocations_fields_defaults['circles_coords'],
        'circles_clickable' => $getlocations_fields_defaults['circles_clickable'],
        'circles_message' => $getlocations_fields_defaults['circles_message'],
        'circles_radius' => $getlocations_fields_defaults['circles_radius'],
        'circles_apply' => $getlocations_fields_defaults['circles_apply'],
        'polylines_enable' => $getlocations_fields_defaults['polylines_enable'],
        'polylines_strokecolor' => $getlocations_fields_defaults['polylines_strokecolor'],
        'polylines_strokeopacity' => $getlocations_fields_defaults['polylines_strokeopacity'],
        'polylines_strokeweight' => $getlocations_fields_defaults['polylines_strokeweight'],
        'polylines_coords' => $getlocations_fields_defaults['polylines_coords'],
        'polylines_clickable' => $getlocations_fields_defaults['polylines_clickable'],
        'polylines_message' => $getlocations_fields_defaults['polylines_message'],
        'search_places' => $getlocations_fields_defaults['search_places'],
        'search_places_size' => $getlocations_fields_defaults['search_places_size'],
        'search_places_position' => $getlocations_fields_defaults['search_places_position'],
        'search_places_label' => $getlocations_fields_defaults['search_places_label'],
        'search_places_placeholder' => $getlocations_fields_defaults['search_places_placeholder'],
        'search_places_dd' => $getlocations_fields_defaults['search_places_dd'],
        'search_places_list' => $getlocations_fields_defaults['search_places_list'],
        'kml_group' => $getlocations_fields_defaults['kml_group'],
        'jquery_colorpicker_enabled' => $getlocations_fields_defaults['jquery_colorpicker_enabled'],
        'geojson_enable' => $getlocations_fields_defaults['geojson_enable'],
        'geojson_data' => $getlocations_fields_defaults['geojson_data'],
        'geojson_options' => $getlocations_fields_defaults['geojson_options'],
        'nokeyboard' => $getlocations_fields_defaults['nokeyboard'],
        'nodoubleclickzoom' => $getlocations_fields_defaults['nodoubleclickzoom'],
        'zoomcontrolposition' => $getlocations_fields_defaults['zoomcontrolposition'],
        'mapcontrolposition' => $getlocations_fields_defaults['mapcontrolposition'],
        'pancontrolposition' => $getlocations_fields_defaults['pancontrolposition'],
        'scalecontrolposition' => $getlocations_fields_defaults['scalecontrolposition'],
        'svcontrolposition' => $getlocations_fields_defaults['svcontrolposition'],
        'sv_addresscontrol' => $getlocations_fields_defaults['sv_addresscontrol'],
        // sv overlay controls
        'sv_addresscontrolposition' => $getlocations_fields_defaults['sv_addresscontrolposition'],
        'sv_pancontrol' => $getlocations_fields_defaults['sv_pancontrol'],
        'sv_pancontrolposition' => $getlocations_fields_defaults['sv_pancontrolposition'],
        'sv_zoomcontrol' => $getlocations_fields_defaults['sv_zoomcontrol'],
        'sv_zoomcontrolposition' => $getlocations_fields_defaults['sv_zoomcontrolposition'],
        'sv_linkscontrol' => $getlocations_fields_defaults['sv_linkscontrol'],
        'sv_imagedatecontrol' => $getlocations_fields_defaults['sv_imagedatecontrol'],
        'sv_scrollwheel' => $getlocations_fields_defaults['sv_scrollwheel'],
        'sv_clicktogo' => $getlocations_fields_defaults['sv_clicktogo'],
        'input_map_show' => $getlocations_fields_defaults['input_map_show'],
        'highlight_enable' => $getlocations_fields_defaults['highlight_enable'],
        'highlight_strokecolor' => $getlocations_fields_defaults['highlight_strokecolor'],
        'highlight_strokeopacity' => $getlocations_fields_defaults['highlight_strokeopacity'],
        'highlight_strokeweight' => $getlocations_fields_defaults['highlight_strokeweight'],
        'highlight_fillcolor' => $getlocations_fields_defaults['highlight_fillcolor'],
        'highlight_fillopacity' => $getlocations_fields_defaults['highlight_fillopacity'],
        'highlight_radius' => $getlocations_fields_defaults['highlight_radius'],
        'getdirections_link' => $getlocations_fields_defaults['getdirections_link'],
        'show_search_distance' => $getlocations_fields_defaults['show_search_distance'],
        'views_search_marker_enable' => $getlocations_fields_defaults['views_search_marker_enable'],
        'views_search_marker' => $getlocations_fields_defaults['views_search_marker'],
        'views_search_marker_toggle' => $getlocations_fields_defaults['views_search_marker_toggle'],
        'views_search_marker_toggle_active' => $getlocations_fields_defaults['views_search_marker_toggle_active'],
        'views_search_radshape_enable' => $getlocations_fields_defaults['views_search_radshape_enable'],
        'views_search_radshape_strokecolor' => $getlocations_fields_defaults['views_search_radshape_strokecolor'],
        'views_search_radshape_strokeopacity' => $getlocations_fields_defaults['views_search_radshape_strokeopacity'],
        'views_search_radshape_strokeweight' => $getlocations_fields_defaults['views_search_radshape_strokeweight'],
        'views_search_radshape_fillcolor' => $getlocations_fields_defaults['views_search_radshape_fillcolor'],
        'views_search_radshape_fillopacity' => $getlocations_fields_defaults['views_search_radshape_fillopacity'],
        'views_search_radshape_toggle' => $getlocations_fields_defaults['views_search_radshape_toggle'],
        'views_search_radshape_toggle_active' => $getlocations_fields_defaults['views_search_radshape_toggle_active'],
        'views_search_center' => $getlocations_fields_defaults['views_search_center'],
        'gps_button' => $getlocations_fields_defaults['gps_button'],
        'gps_button_label' => $getlocations_fields_defaults['gps_button_label'],
        'gps_marker' => $getlocations_fields_defaults['gps_marker'],
        'gps_marker_title' => $getlocations_fields_defaults['gps_marker_title'],
        'gps_bubble' => $getlocations_fields_defaults['gps_bubble'],
        'gps_geocode' => $getlocations_fields_defaults['gps_geocode'],
        'gps_center' => $getlocations_fields_defaults['gps_center'],
        'gps_type' => $getlocations_fields_defaults['gps_type'],
        'gps_zoom' => $getlocations_fields_defaults['gps_zoom'],
        'geocoder_enable' => $getlocations_fields_defaults['geocoder_enable'],
        'what3words_collect' => $getlocations_fields_defaults['what3words_collect'],
      ),
    ),
  );
  foreach ($getlocations_defaults['baselayers'] as $key => $layer) {
    $defaults['getlocations_fields']['settings']['baselayers'][$key] = $layer;
  }
  return $defaults;
}

/**
 * @return array $defaults
 *  Settings for the Field formatter
 */
function getlocations_fields_field_formatter_info_defaults() {
  $getlocations_defaults = getlocations_defaults();
  $getlocations_fields_defaults = getlocations_fields_defaults();
  $defaults = array(
    'display_mapwidth' => $getlocations_defaults['width'],
    'display_mapheight' => $getlocations_defaults['height'],
    'display_showmap' => $getlocations_fields_defaults['display_showmap'],
    'display_maplink' => $getlocations_fields_defaults['display_maplink'],
    'display_latlong' => $getlocations_fields_defaults['display_latlong'],
    'display_dms' => $getlocations_fields_defaults['display_dms'],
    'display_geo_microformat' => $getlocations_fields_defaults['display_geo_microformat'],
    'display_onemap' => $getlocations_fields_defaults['display_onemap'],
    'what3words_display' => $getlocations_fields_defaults['what3words_display'],
    'controltype' => $getlocations_defaults['controltype'],
    'pancontrol' => $getlocations_defaults['pancontrol'],
    'mtc' => $getlocations_defaults['mtc'],
    'maptype' => $getlocations_defaults['maptype'],
    'scale' => $getlocations_defaults['scale'],
    'overview' => $getlocations_defaults['overview'],
    'overview_opened' => $getlocations_defaults['overview_opened'],
    'scrollwheel' => $getlocations_defaults['scrollwheel'],
    'draggable' => $getlocations_defaults['draggable'],
    'sv_show' => $getlocations_defaults['sv_show'],
    'sv_showfirst' => $getlocations_fields_defaults['sv_showfirst'],
    'sv_heading' => $getlocations_defaults['sv_heading'],
    'sv_zoom' => $getlocations_defaults['sv_zoom'],
    'sv_pitch' => $getlocations_defaults['sv_pitch'],
    'trafficinfo' => $getlocations_defaults['trafficinfo'],
    'trafficinfo_state' => $getlocations_defaults['trafficinfo_state'],
    'bicycleinfo' => $getlocations_defaults['bicycleinfo'],
    'bicycleinfo_state' => $getlocations_defaults['bicycleinfo_state'],
    'transitinfo' => $getlocations_defaults['transitinfo'],
    'transitinfo_state' => $getlocations_defaults['transitinfo_state'],
    'poi_show' => $getlocations_defaults['poi_show'],
    'transit_show' => $getlocations_defaults['transit_show'],
    'map_marker' => $getlocations_defaults['node_map_marker'],
    'node_map_marker' => $getlocations_defaults['node_map_marker'],
    'user_map_marker' => $getlocations_defaults['user_map_marker'],
    'vocabulary_map_marker' => $getlocations_defaults['vocabulary_map_marker'],
    'comment_map_marker' => $getlocations_defaults['comment_map_marker'],
    'markeraction' => $getlocations_defaults['markeraction'],
    'markeractiontype' => $getlocations_defaults['markeractiontype'],
    'markeraction_click_zoom' => $getlocations_defaults['markeraction_click_zoom'],
    'markeraction_click_center' => $getlocations_defaults['markeraction_click_center'],
    'map_backgroundcolor' => $getlocations_defaults['map_backgroundcolor'],
    'show_maplinks' => $getlocations_defaults['show_maplinks'],
    'display_name' => $getlocations_fields_defaults['display_name'],
    'display_street' => $getlocations_fields_defaults['display_street'],
    'display_additional' => $getlocations_fields_defaults['display_additional'],
    'display_city' => $getlocations_fields_defaults['display_city'],
    'display_province' => $getlocations_fields_defaults['display_province'],
    'display_postal_code' => $getlocations_fields_defaults['display_postal_code'],
    'display_country' => $getlocations_fields_defaults['display_country'],
    'display_phone' => $getlocations_fields_defaults['display_phone'],
    'display_mobile' => $getlocations_fields_defaults['display_mobile'],
    'display_fax' => $getlocations_fields_defaults['display_fax'],
    'country_full' => $getlocations_fields_defaults['country_full'],
    'per_item_marker' => $getlocations_fields_defaults['per_item_marker'],
    'nodezoom' => $getlocations_fields_defaults['nodezoom'],
    'minzoom_map' => $getlocations_defaults['minzoom_map'],
    'maxzoom_map' => $getlocations_defaults['maxzoom_map'],
    'fullscreen' => $getlocations_defaults['fullscreen'],
    'fullscreen_controlposition' => $getlocations_defaults['fullscreen_controlposition'],
    'show_bubble_on_one_marker' => $getlocations_defaults['show_bubble_on_one_marker'],
    'polygons_enable' => $getlocations_defaults['polygons_enable'],
    'polygons_strokecolor' => $getlocations_defaults['polygons_strokecolor'],
    'polygons_strokeopacity' => $getlocations_defaults['polygons_strokeopacity'],
    'polygons_strokeweight' => $getlocations_defaults['polygons_strokeweight'],
    'polygons_fillcolor' => $getlocations_defaults['polygons_fillcolor'],
    'polygons_fillopacity' => $getlocations_defaults['polygons_fillopacity'],
    'polygons_coords' => $getlocations_defaults['polygons_coords'],
    'polygons_clickable' => $getlocations_defaults['polygons_clickable'],
    'polygons_message' => $getlocations_defaults['polygons_message'],
    'rectangles_enable' => $getlocations_defaults['rectangles_enable'],
    'rectangles_strokecolor' => $getlocations_defaults['rectangles_strokecolor'],
    'rectangles_strokeopacity' => $getlocations_defaults['rectangles_strokeopacity'],
    'rectangles_strokeweight' => $getlocations_defaults['rectangles_strokeweight'],
    'rectangles_fillcolor' => $getlocations_defaults['rectangles_fillcolor'],
    'rectangles_fillopacity' => $getlocations_defaults['rectangles_fillopacity'],
    'rectangles_coords' => $getlocations_defaults['rectangles_coords'],
    'rectangles_clickable' => $getlocations_defaults['rectangles_clickable'],
    'rectangles_message' => $getlocations_defaults['rectangles_message'],
    'rectangles_apply' => $getlocations_defaults['rectangles_apply'],
    'rectangles_dist' => $getlocations_defaults['rectangles_dist'],
    'circles_enable' => $getlocations_defaults['circles_enable'],
    'circles_strokecolor' => $getlocations_defaults['circles_strokecolor'],
    'circles_strokeopacity' => $getlocations_defaults['circles_strokeopacity'],
    'circles_strokeweight' => $getlocations_defaults['circles_strokeweight'],
    'circles_fillcolor' => $getlocations_defaults['circles_fillcolor'],
    'circles_fillopacity' => $getlocations_defaults['circles_fillopacity'],
    'circles_coords' => $getlocations_defaults['circles_coords'],
    'circles_clickable' => $getlocations_defaults['circles_clickable'],
    'circles_message' => $getlocations_defaults['circles_message'],
    'circles_radius' => $getlocations_defaults['circles_radius'],
    'circles_apply' => $getlocations_defaults['circles_apply'],
    'polylines_enable' => $getlocations_defaults['polylines_enable'],
    'polylines_strokecolor' => $getlocations_defaults['polylines_strokecolor'],
    'polylines_strokeopacity' => $getlocations_defaults['polylines_strokeopacity'],
    'polylines_strokeweight' => $getlocations_defaults['polylines_strokeweight'],
    'polylines_coords' => $getlocations_defaults['polylines_coords'],
    'polylines_clickable' => $getlocations_defaults['polylines_clickable'],
    'polylines_message' => $getlocations_defaults['polylines_message'],
    'search_places' => $getlocations_defaults['search_places'],
    'search_places_size' => $getlocations_defaults['search_places_size'],
    'search_places_position' => $getlocations_defaults['search_places_position'],
    'search_places_label' => $getlocations_defaults['search_places_label'],
    'search_places_placeholder' => $getlocations_defaults['search_places_placeholder'],
    'search_places_dd' => $getlocations_defaults['search_places_dd'],
    'search_places_list' => $getlocations_defaults['search_places_list'],
    'kml_group' => $getlocations_defaults['kml_group'],
    'jquery_colorpicker_enabled' => $getlocations_defaults['jquery_colorpicker_enabled'],
    'geojson_enable' => $getlocations_defaults['geojson_enable'],
    'geojson_data' => $getlocations_defaults['geojson_data'],
    'geojson_options' => $getlocations_defaults['geojson_options'],
    'nokeyboard' => $getlocations_defaults['nokeyboard'],
    'nodoubleclickzoom' => $getlocations_defaults['nodoubleclickzoom'],
    'zoomcontrolposition' => $getlocations_defaults['zoomcontrolposition'],
    'mapcontrolposition' => $getlocations_defaults['mapcontrolposition'],
    'pancontrolposition' => $getlocations_defaults['pancontrolposition'],
    'scalecontrolposition' => $getlocations_defaults['scalecontrolposition'],
    'svcontrolposition' => $getlocations_defaults['svcontrolposition'],
    'sv_addresscontrol' => $getlocations_fields_defaults['sv_addresscontrol'],
    // sv overlay controls
    'sv_addresscontrolposition' => $getlocations_fields_defaults['sv_addresscontrolposition'],
    'sv_pancontrol' => $getlocations_fields_defaults['sv_pancontrol'],
    'sv_pancontrolposition' => $getlocations_fields_defaults['sv_pancontrolposition'],
    'sv_zoomcontrol' => $getlocations_fields_defaults['sv_zoomcontrol'],
    'sv_zoomcontrolposition' => $getlocations_fields_defaults['sv_zoomcontrolposition'],
    'sv_linkscontrol' => $getlocations_fields_defaults['sv_linkscontrol'],
    'sv_imagedatecontrol' => $getlocations_fields_defaults['sv_imagedatecontrol'],
    'sv_scrollwheel' => $getlocations_fields_defaults['sv_scrollwheel'],
    'sv_clicktogo' => $getlocations_fields_defaults['sv_clicktogo'],
    'highlight_enable' => $getlocations_defaults['highlight_enable'],
    'highlight_strokecolor' => $getlocations_defaults['highlight_strokecolor'],
    'highlight_strokeopacity' => $getlocations_defaults['highlight_strokeopacity'],
    'highlight_strokeweight' => $getlocations_defaults['highlight_strokeweight'],
    'highlight_fillcolor' => $getlocations_defaults['highlight_fillcolor'],
    'highlight_fillopacity' => $getlocations_defaults['highlight_fillopacity'],
    'highlight_radius' => $getlocations_defaults['highlight_radius'],
    'getdirections_link' => $getlocations_defaults['getdirections_link'],
    'show_search_distance' => $getlocations_defaults['show_search_distance'],
    'views_search_marker_enable' => $getlocations_defaults['views_search_marker_enable'],
    'views_search_marker' => $getlocations_defaults['views_search_marker'],
    'views_search_marker_toggle' => $getlocations_defaults['views_search_marker_toggle'],
    'views_search_marker_toggle_active' => $getlocations_defaults['views_search_marker_toggle_active'],
    'views_search_radshape_enable' => $getlocations_defaults['views_search_radshape_enable'],
    'views_search_radshape_strokecolor' => $getlocations_defaults['views_search_radshape_strokecolor'],
    'views_search_radshape_strokeopacity' => $getlocations_defaults['views_search_radshape_strokeopacity'],
    'views_search_radshape_strokeweight' => $getlocations_defaults['views_search_radshape_strokeweight'],
    'views_search_radshape_fillcolor' => $getlocations_defaults['views_search_radshape_fillcolor'],
    'views_search_radshape_fillopacity' => $getlocations_defaults['views_search_radshape_fillopacity'],
    'views_search_radshape_toggle' => $getlocations_defaults['views_search_radshape_toggle'],
    'views_search_radshape_toggle_active' => $getlocations_defaults['views_search_radshape_toggle_active'],
    'views_search_center' => $getlocations_defaults['views_search_center'],
    'gps_button' => 0,
    'gps_button_label' => $getlocations_defaults['gps_button_label'],
    'gps_marker' => $getlocations_defaults['gps_marker'],
    'gps_marker_title' => $getlocations_defaults['gps_marker_title'],
    'gps_bubble' => $getlocations_defaults['gps_bubble'],
    'gps_geocode' => $getlocations_defaults['gps_geocode'],
    'gps_center' => $getlocations_defaults['gps_center'],
    'gps_type' => $getlocations_defaults['gps_type'],
    'gps_zoom' => $getlocations_defaults['gps_zoom'],
    'geocoder_enable' => $getlocations_defaults['geocoder_enable'],
  );
  foreach ($getlocations_defaults['baselayers'] as $key => $layer) {
    $defaults['baselayers'][$key] = $layer;
  }
  return $defaults;
}

/**
 * input settings form for sharing
 *
 * @param array $defaults
 *  Settings
 *
 * @return array $form
 *
 */
function getlocations_fields_input_settings_form($defaults) {
  $getlocations_fields_paths = getlocations_fields_paths_get();
  drupal_add_js($getlocations_fields_paths['getlocations_fields_admin_path']);
  drupal_add_css(GETLOCATIONS_FIELDS_PATH . '/getlocations_fields.css');
  $form = array();

  // input form defaults
  $form['use_address'] = getlocations_element_dd(t('Search options'), $defaults['use_address'], array(
    '0' => t('No Search box'),
    '1' => t('Search box with Geocode button'),
    '2' => t('Search box with Automatic Geocoding'),
  ), t("With 'No Search box' you must fill in the address then use the Geocode button to find the location.<br />With 'Search box with Geocode button' you can fill in the Search box then use the Geocode button to find the location.<br />With 'Search box with Automatic Geocoding' the Geocoding will be done when the Search has been selected."));
  $form['input_address_width'] = getlocations_element_map_tf(t('Search box width'), $defaults['input_address_width'], t('The width of the Google Autocomplete search textbox. Must be a positive number.'), 10, 10, TRUE);
  $form['input_address_width']['#prefix'] = '<div id="wrap-input_address_width">';
  $form['input_address_width']['#suffix'] = '</div>';
  $form['autocomplete_bias'] = getlocations_element_map_checkbox(t('Enable Viewport bias'), $defaults['autocomplete_bias'], t('Bias the Google Autocomplete results to the area on the map.'));

  // country restriction
  $form['restrict_by_country'] = getlocations_element_map_checkbox(t('Restrict by country'), $defaults['restrict_by_country'], t('Restrict searches to the country set below. Works with Google Autocomplete. This will override all other country settings.'));
  $form['restrict_by_country']['#suffix'] = '<div id="getlocations_fields_search_country">';
  $form['search_country'] = getlocations_fields_element_country($defaults['search_country'], t('Search country'), FALSE);
  $form['search_country']['#suffix'] = '</div>';
  if (module_exists('smart_ip')) {
    $form['use_smart_ip_button'] = getlocations_element_map_checkbox(t('Provide IP based geocoding option'), $defaults['use_smart_ip_button'], t('Use Smart IP module to provide a location.'));
  }
  $form['use_geolocation_button'] = getlocations_element_map_checkbox(t('Provide Browser based geocoding option'), $defaults['use_geolocation_button'], t('Use the browser to find a location.'));

  // geocoder-js
  $form += getlocations_geocoder_form($defaults);
  $form['use_clear_button'] = getlocations_element_map_checkbox(t('Provide Clear button'), $defaults['use_clear_button'], t('Provide a button to empty the address.'));
  $form['input_name_width'] = getlocations_element_map_tf(t('Name box width'), $defaults['input_name_width'], t('The width of the name textbox. Must be a positive number.'), 10, 10, TRUE);
  $form['input_name_required'] = getlocations_fields_element_opts(t('Name box settings'), $defaults['input_name_required'], '');
  $form['input_name_weight'] = getlocations_fields_element_weight(t('Name box position'), $defaults['input_name_weight'], '');
  $form['input_street_width'] = getlocations_element_map_tf(t('Street box width'), $defaults['input_street_width'], t('The width of the street textbox. Must be a positive number.'), 10, 10, TRUE);
  $form['input_street_required'] = getlocations_fields_element_opts(t('Street box settings'), $defaults['input_street_required'], '');
  $form['input_street_weight'] = getlocations_fields_element_weight(t('Street box position'), $defaults['input_street_weight'], '');
  $form['input_additional_width'] = getlocations_element_map_tf(t('Additional box width'), $defaults['input_additional_width'], t('The width of the additional textbox. Must be a positive number.'), 10, 10, TRUE);
  $form['input_additional_required'] = getlocations_fields_element_opts(t('Additional box settings'), $defaults['input_additional_required'], '');
  $form['input_additional_weight'] = getlocations_fields_element_weight(t('Additional box position'), $defaults['input_additional_weight'], '');
  $form['input_city_width'] = getlocations_element_map_tf(t('City box width'), $defaults['input_city_width'], t('The width of the city textbox. Must be a positive number.'), 10, 10, TRUE);
  $form['input_city_required'] = getlocations_fields_element_opts(t('City box settings'), $defaults['input_city_required'], '');
  $form['input_city_weight'] = getlocations_fields_element_weight(t('City box position'), $defaults['input_city_weight'], '');
  $form['input_province_width'] = getlocations_element_map_tf(t('Province box width'), $defaults['input_province_width'], t('The width of the province/county/state textbox. Must be a positive number.'), 10, 10, TRUE);
  $form['input_province_required'] = getlocations_fields_element_opts(t('Province box settings'), $defaults['input_province_required'], '');
  $form['input_province_weight'] = getlocations_fields_element_weight(t('Province box position'), $defaults['input_province_weight'], '');
  $form['input_postal_code_width'] = getlocations_element_map_tf(t('Postal code box width'), $defaults['input_postal_code_width'], t('The width of the post code textbox. Must be a positive number.'), 10, 10, TRUE);
  $form['input_postal_code_required'] = getlocations_fields_element_opts(t('Postal code box settings'), $defaults['input_postal_code_required'], '');
  $form['input_postal_code_weight'] = getlocations_fields_element_weight(t('Post code box position'), $defaults['input_postal_code_weight'], '');
  $form['input_phone_width'] = getlocations_element_map_tf(t('Phone box width'), $defaults['input_phone_width'], t('The width of the phone textbox. Must be a positive number.'), 10, 10, TRUE);
  $form['input_phone_required'] = getlocations_fields_element_opts(t('Phone box settings'), $defaults['input_phone_required'], '');
  $form['input_phone_weight'] = getlocations_fields_element_weight(t('Phone box position'), $defaults['input_phone_weight'], '');
  $form['input_mobile_width'] = getlocations_element_map_tf(t('Mobile box width'), $defaults['input_mobile_width'], t('The width of the mobile textbox. Must be a positive number.'), 10, 10, TRUE);
  $form['input_mobile_required'] = getlocations_fields_element_opts(t('Mobile box settings'), $defaults['input_mobile_required'], '');
  $form['input_mobile_weight'] = getlocations_fields_element_weight(t('Mobile box position'), $defaults['input_mobile_weight'], '');
  $form['input_fax_width'] = getlocations_element_map_tf(t('Fax box width'), $defaults['input_fax_width'], t('The width of the fax textbox. Must be a positive number.'), 10, 10, TRUE);
  $form['input_fax_required'] = getlocations_fields_element_opts(t('Fax box settings'), $defaults['input_fax_required'], '');
  $form['input_fax_weight'] = getlocations_fields_element_weight(t('Fax box position'), $defaults['input_fax_weight'], '');
  $form['input_latitude_width'] = getlocations_element_map_tf(t('Latitude box width'), $defaults['input_latitude_width'], t('The width of the latitude textbox. Must be a positive number.'), 10, 10, TRUE);
  $form['input_latitude_weight'] = getlocations_fields_element_weight(t('Latitude box position'), $defaults['input_latitude_weight'], '');
  $form['input_longitude_width'] = getlocations_element_map_tf(t('Longitude box width'), $defaults['input_longitude_width'], t('The width of the longitude textbox. Must be a positive number.'), 10, 10, TRUE);
  $form['input_longitude_weight'] = getlocations_fields_element_weight(t('Longitude box position'), $defaults['input_longitude_weight'], '');
  $form['input_map_weight'] = getlocations_fields_element_weight(t('Map position'), $defaults['input_map_weight'], '');
  $form['input_geobutton_weight'] = getlocations_fields_element_weight(t('Geocode button position'), $defaults['input_geobutton_weight'], '');
  if (module_exists('smart_ip')) {
    $form['input_smart_ip_button_weight'] = getlocations_fields_element_weight(t('Smart IP button position'), $defaults['input_smart_ip_button_weight'], '');
  }
  $form['input_geolocation_button_weight'] = getlocations_fields_element_weight(t('Geolocation button position'), $defaults['input_geolocation_button_weight'], '');
  $form['input_clear_button_weight'] = getlocations_fields_element_weight(t('Clear button position'), $defaults['input_clear_button_weight'], '');
  $form['input_country_width'] = getlocations_element_map_tf(t('Country box width'), $defaults['input_country_width'], t('The width of the country textbox. Must be a positive number.'), 10, 10, TRUE);
  $form['input_country_required'] = getlocations_fields_element_opts(t('Country box settings'), $defaults['input_country_required'], '');
  $form['input_country_weight'] = getlocations_fields_element_weight(t('Country box position'), $defaults['input_country_weight'], '');

  // city_autocomplete
  $form['city_autocomplete'] = getlocations_element_dd(t('City input method'), $defaults['city_autocomplete'], array(
    0 => t('Normal textfield'),
    1 => t('Autocomplete'),
  ), t('Autocomplete works on existing records so it works best if you already have some.'));

  // province_autocomplete
  $form['province_autocomplete'] = getlocations_element_dd(t('Province/State/County input method'), $defaults['province_autocomplete'], array(
    0 => t('Normal textfield'),
    1 => t('Autocomplete'),
  ), t('Autocomplete works on existing records so it works best if you already have some.'));
  $form['country'] = getlocations_fields_element_country($defaults['country'], t('Default country'), FALSE);

  // country dropdown
  $form['use_country_dropdown'] = getlocations_element_dd(t('Country input method'), $defaults['use_country_dropdown'], array(
    1 => t('Dropdown'),
    0 => t('Normal textfield'),
    2 => t('Autocomplete'),
  ), t('Use Dropdown if you need Locale support.'));
  if (module_exists('countries')) {
    $continents = variable_get('countries_continents', countries_get_default_continents());
    unset($continents['UN']);
    $form['only_continents'] = getlocations_element_dd(t('Limit to Continents'), $defaults['only_continents'], $continents, t('Limit to Continents only applies to Country dropdown.'), TRUE);
    $form['only_countries'] = getlocations_element_map_tf(t('Limit to Countries'), $defaults['only_countries'], t('Limit to Countries only applies to Country dropdown. This should be a comma delimited list of two letter ISO codes, no spaces.'), 30, 255);
  }

  // per_item_marker
  $form['per_item_marker'] = getlocations_element_map_checkbox(t('Allow per location markers'), $defaults['per_item_marker'], t('Allow each location to have a marker selected.'));
  $form['input_marker_weight'] = getlocations_fields_element_weight(t('Marker select position'), $defaults['input_marker_weight'], '');

  // street_num_pos
  $form['street_num_pos'] = getlocations_element_dd(t('Street number position'), $defaults['street_num_pos'], array(
    1 => t('Before'),
    2 => t('After'),
  ), t('Placement of street number, if available.'));

  // latlon_warning
  $form['latlon_warning'] = getlocations_element_map_checkbox(t('Enable empty lat/lon popup warning'), $defaults['latlon_warning'], t('Enables a javascript popup that warns users that they have not geocoded the address. Only use this if you have a fixed number of addresses.'));
  $form['map_settings_allow'] = getlocations_element_map_checkbox(t('Per location map settings'), $defaults['map_settings_allow'], t('Allow zoom and map type set per location. Disabling this will use the defaults instead.'));
  $form['streetview_settings_allow'] = getlocations_element_map_checkbox(t('Per location Streetview settings'), $defaults['streetview_settings_allow'], t('Allow Streetview per location. Disabling this will disable Streetview.'));
  return $form;
}

/**
 * display settings form for sharing
 *
 * @param array $defaults
 *  Settings
 *
 * @return array $form
 *
 */
function getlocations_fields_display_settings_form($defaults) {
  $form = array();

  // display defaults
  $form['display_showmap'] = getlocations_element_map_checkbox(t('Show map in display'), $defaults['display_showmap'], '');
  $form['display_maplink'] = getlocations_element_map_checkbox(t('Show link to map in display'), $defaults['display_maplink'], t('Only applicable when the map is not being displayed.'));
  $form['display_mapwidth'] = getlocations_element_map_tf(t('Map width'), $defaults['display_mapwidth'], t('The width of a map, as a CSS length or percentage. Examples: <em>50px</em>, <em>5em</em>, <em>2.5in</em>, <em>95%</em>'), 10, 10, TRUE);
  $form['display_mapheight'] = getlocations_element_map_tf(t('Map height'), $defaults['display_mapheight'], t('The height of a map, as a CSS length or percentage. Examples: <em>50px</em>, <em>5em</em>, <em>2.5in</em>, <em>95%</em>'), 10, 10, TRUE);
  $form['display_latlong'] = getlocations_element_map_checkbox(t('Show Latitude/Longitude in display'), $defaults['display_latlong'], '');
  $form['display_dms'] = getlocations_element_map_checkbox(t('Show Latitude/Longitude in Degrees, minutes, seconds'), $defaults['display_dms'], '');
  $form['display_geo_microformat'] = getlocations_element_dd(t('Show Latitude/Longitude in accordance with Geo Microformat or Microdata markup'), $defaults['display_geo_microformat'], array(
    0 => t('None'),
    1 => t('Microformat'),
    2 => t('Microdata'),
  ), t('See the <a href="@a" target="_blank">Microformats wiki</a> or <a href="@b" target="_blank">Microdata standards</a> for more information', array(
    '@a' => url('http://microformats.org/wiki/geo'),
    '@b' => url('http://www.w3.org/TR/microdata/'),
  )));
  $form['display_onemap'] = getlocations_element_map_checkbox(t('Show all markers on one map'), $defaults['display_onemap'], '');
  $form['display_name'] = getlocations_element_map_checkbox(t('Show Name in display'), $defaults['display_name'], '');
  $form['display_street'] = getlocations_element_map_checkbox(t('Show Street in display'), $defaults['display_street'], '');
  $form['display_additional'] = getlocations_element_map_checkbox(t('Show Additional in display'), $defaults['display_additional'], '');
  $form['display_city'] = getlocations_element_map_checkbox(t('Show City in display'), $defaults['display_city'], '');
  $form['display_province'] = getlocations_element_map_checkbox(t('Show Province/state/county in display'), $defaults['display_province'], '');
  $form['display_postal_code'] = getlocations_element_map_checkbox(t('Show Postal code in display'), $defaults['display_postal_code'], '');
  $form['display_country'] = getlocations_element_map_checkbox(t('Show Country in display'), $defaults['display_country'], '');
  $form['country_full'] = getlocations_element_map_checkbox(t('Display full country name'), $defaults['country_full'], '');
  $form['display_phone'] = getlocations_element_map_checkbox(t('Show Phone in display'), $defaults['display_phone'], '');
  $form['display_mobile'] = getlocations_element_map_checkbox(t('Show Mobile in display'), $defaults['display_mobile'], '');
  $form['display_fax'] = getlocations_element_map_checkbox(t('Show Fax in display'), $defaults['display_fax'], '');

  // what3words
  $what3words_lic = variable_get('getlocations_what3words_lic', array(
    'key' => '',
    'url' => 'http://api.what3words.com',
  ));
  if ($what3words_lic['key']) {
    $form['what3words_display'] = getlocations_element_map_checkbox(t('Show What3Words in display'), $defaults['what3words_display'], '');
  }
  else {
    $form['what3words_display'] = array(
      '#type' => 'value',
      '#value' => 0,
    );
  }
  $form['nodezoom'] = getlocations_element_map_zoom(t('Zoom'), $defaults['nodezoom'], t('The zoom level for a display map.'));
  $form['minzoom_map'] = getlocations_element_map_zoom_map(t('Minimum Zoom'), $defaults['minzoom_map'], t('The Minimum zoom level for a display map.'));
  $form['maxzoom_map'] = getlocations_element_map_zoom_map(t('Maximum Zoom'), $defaults['maxzoom_map'], t('The Maximum zoom level for a display map.'));
  return $form;
}

/**
 * Implements hook_modules_uninstalled().
 *
 * Cleans up entries related to taxonomy or comment modules
 */
function getlocations_fields_modules_uninstalled($modules) {
  $identifier = '';
  foreach ($modules as $module) {
    if ($module == 'taxonomy') {
      $identifier = 'tid';
    }
    elseif ($module == 'comment') {
      $identifier = 'cid';
    }
    elseif ($module == 'profile2') {
      $identifier = 'uid';
    }
    if ($identifier) {
      $instances = array();

      // field_info_instances() is unuseable so roll our own
      $query = db_select('field_config', 'f');
      $query
        ->fields('i', array(
        'field_name',
        'entity_type',
        'bundle',
      ));
      $query
        ->join('field_config_instance', 'i', 'f.id=i.field_id');
      $query
        ->condition('f.module', 'getlocations_fields')
        ->condition('f.type', 'getlocations_fields')
        ->condition('i.entity_type', $module);
      $result = $query
        ->execute();
      if ($result) {
        $ct = 0;
        foreach ($result as $row) {
          $instances[$ct]['field_name'] = $row->field_name;
          $instances[$ct]['entity_type'] = $row->entity_type;
          $instances[$ct]['bundle'] = $row->bundle;
          $ct++;
        }
      }
      if (count($instances)) {
        $glids = array();
        foreach ($instances as $k => $instance) {
          $query = db_select('getlocations_fields_entities', 'e');
          $query
            ->fields('e', array(
            'glid',
          ));
          $query
            ->condition('e.' . $identifier, 0, '>')
            ->condition('e.field_name', $instance['field_name']);
          $result2 = $query
            ->execute();
          if ($result2) {
            foreach ($result2 as $row) {
              $glids[] = $row->glid;
            }
          }
          field_delete_instance($instance);
        }
        if (count($glids)) {
          foreach ($glids as $glid) {
            getlocations_fields_delete_record(array(
              'glid' => $glid,
            ), '');
          }
        }
        field_purge_batch(1000);

        // for some reason the record in field_config_instance does not get deleted
        // even though it is set to be deleted, so do it now.
        db_delete('field_config_instance')
          ->condition('entity_type', $module)
          ->execute();
      }
    }
  }
}

// backward compat
function getlocations_fields_get_countries_list($upper = TRUE) {
  $countries = getlocations_get_countries_list($upper);
  return $countries;
}
function getlocations_fields_paths_get($reset = FALSE, $min = FALSE) {
  $jq_upd = getlocations_check_jquery_version(TRUE);
  if ($min) {
    $defaults = array(
      'getlocations_fields_path' => GETLOCATIONS_FIELDS_PATH . '/js/getlocations_fields.min.js',
      'getlocations_fields_preview_path' => GETLOCATIONS_FIELDS_PATH . '/js/getlocations_fields_preview.min.js',
      'getlocations_fields_streetview_path' => GETLOCATIONS_FIELDS_PATH . '/js/getlocations_fields_streetview.min.js',
      'getlocations_fields_admin_path' => GETLOCATIONS_FIELDS_PATH . ($jq_upd ? '/js/getlocations_fields_admin_1.min.js' : '/js/getlocations_fields_admin.min.js'),
      'getlocations_fields_formatter_path' => GETLOCATIONS_FIELDS_PATH . ($jq_upd ? '/js/getlocations_fields_formatter_1.min.js' : '/js/getlocations_fields_formatter.min.js'),
      'getlocations_fields_views_path' => GETLOCATIONS_FIELDS_PATH . ($jq_upd ? '/js/getlocations_fields_views_1.min.js' : '/js/getlocations_fields_views.min.js'),
      'getlocations_fields_search_views_path' => GETLOCATIONS_FIELDS_PATH . '/js/getlocations_fields_search_views.min.js',
    );
  }
  else {
    $defaults = array(
      'getlocations_fields_path' => GETLOCATIONS_FIELDS_PATH . '/js/getlocations_fields.js',
      'getlocations_fields_preview_path' => GETLOCATIONS_FIELDS_PATH . '/js/getlocations_fields_preview.js',
      'getlocations_fields_streetview_path' => GETLOCATIONS_FIELDS_PATH . '/js/getlocations_fields_streetview.js',
      'getlocations_fields_admin_path' => GETLOCATIONS_FIELDS_PATH . ($jq_upd ? '/js/getlocations_fields_admin_1.js' : '/js/getlocations_fields_admin.js'),
      'getlocations_fields_formatter_path' => GETLOCATIONS_FIELDS_PATH . ($jq_upd ? '/js/getlocations_fields_formatter_1.js' : '/js/getlocations_fields_formatter.js'),
      'getlocations_fields_views_path' => GETLOCATIONS_FIELDS_PATH . ($jq_upd ? '/js/getlocations_fields_views_1.js' : '/js/getlocations_fields_views.js'),
      'getlocations_fields_search_views_path' => GETLOCATIONS_FIELDS_PATH . '/js/getlocations_fields_search_views.js',
    );
  }
  if ($reset || $min) {
    $getlocations_fields_paths = $defaults;
  }
  else {
    $settings = variable_get('getlocations_fields_paths', $defaults);
    $getlocations_fields_paths = getlocations_adjust_vars($defaults, $settings);
  }
  return $getlocations_fields_paths;
}
function getlocations_fields_column_check($field) {
  if ($result = db_field_exists('getlocations_fields', $field)) {
    return TRUE;
  }
  return FALSE;
}
function getlocations_fields_data_keys($type = 'dd') {
  if ($type == 'dd') {

    // for dropdowns
    $keys = array(
      'phone' => t('Phone'),
      'mobile' => t('Mobile'),
      'fax' => t('Fax'),
      'sv_heading' => t('Heading'),
      'sv_zoom' => t('Zoom'),
      'sv_pitch' => t('Pitch'),
      'mapzoom' => t('Map zoom'),
      'map_maptype' => t('Map type'),
      'sv_enable' => t('Streetview Enable'),
      'sv_showfirst' => t('Showfirst Enable'),
    );
  }
  elseif ($type == 'd') {

    // default values
    $keys = array(
      'phone' => '',
      'mobile' => '',
      'fax' => '',
      'sv_heading' => 0,
      'sv_zoom' => 1,
      'sv_pitch' => 0,
      'mapzoom' => 12,
      'map_maptype' => 'Map',
      'map_settings_allow' => 0,
      'sv_enable' => 0,
      'sv_showfirst' => 0,
    );
    $keys += getlocations_fields_get_display_settings_defaults();
  }
  return $keys;
}
function getlocations_fields_svinfo($location) {
  $sv_defaults = getlocations_fields_data_keys('d');
  $sv_info = array(
    'sv_heading' => isset($location['sv_heading']) ? $location['sv_heading'] : $sv_defaults['sv_heading'],
    'sv_zoom' => isset($location['sv_zoom']) ? $location['sv_zoom'] : $sv_defaults['sv_zoom'],
    'sv_pitch' => isset($location['sv_pitch']) ? $location['sv_pitch'] : $sv_defaults['sv_pitch'],
    'sv_showfirst' => isset($location['sv_showfirst']) ? $location['sv_showfirst'] : $sv_defaults['sv_showfirst'],
    'sv_addresscontrol' => isset($location['sv_addresscontrol']) ? $location['sv_addresscontrol'] : $sv_defaults['sv_addresscontrol'],
    'sv_addresscontrolposition' => isset($location['sv_addresscontrolposition']) ? $location['sv_addresscontrolposition'] : $sv_defaults['sv_addresscontrolposition'],
    'sv_pancontrol' => isset($location['sv_pancontrol']) ? $location['sv_pancontrol'] : $sv_defaults['sv_pancontrol'],
    'sv_pancontrolposition' => isset($location['sv_pancontrolposition']) ? $location['sv_pancontrolposition'] : $sv_defaults['sv_pancontrolposition'],
    'sv_zoomcontrol' => isset($location['sv_zoomcontrol']) ? $location['sv_zoomcontrol'] : $sv_defaults['sv_zoomcontrol'],
    'sv_zoomcontrolposition' => isset($location['sv_zoomcontrolposition']) ? $location['sv_zoomcontrolposition'] : $sv_defaults['sv_zoomcontrolposition'],
    'sv_linkscontrol' => isset($location['sv_linkscontrol']) ? $location['sv_linkscontrol'] : $sv_defaults['sv_linkscontrol'],
    'sv_imagedatecontrol' => isset($location['sv_imagedatecontrol']) ? $location['sv_imagedatecontrol'] : $sv_defaults['sv_imagedatecontrol'],
    'sv_scrollwheel' => isset($location['sv_scrollwheel']) ? $location['sv_scrollwheel'] : $sv_defaults['sv_scrollwheel'],
    'sv_clicktogo' => isset($location['sv_clicktogo']) ? $location['sv_clicktogo'] : $sv_defaults['sv_clicktogo'],
  );
  return $sv_info;
}
function getlocations_fields_mapinfo($location) {
  $defaults = getlocations_fields_data_keys('d');
  $info = array(
    'mapzoom' => isset($location['mapzoom']) ? $location['mapzoom'] : $defaults['mapzoom'],
    'map_maptype' => isset($location['map_maptype']) ? $location['map_maptype'] : $defaults['map_maptype'],
  );
  return $info;
}
function getlocations_fields_map_settings_allow() {
  $getlocations_fields_defaults = getlocations_fields_defaults();
  if ($getlocations_fields_defaults['map_settings_allow']) {
    return TRUE;
  }
  return FALSE;
}
function getlocations_fields_streetview_settings_allow() {
  $getlocations_fields_defaults = getlocations_fields_defaults();
  if ($getlocations_fields_defaults['streetview_settings_allow']) {
    return TRUE;
  }
  return FALSE;
}
function getlocations_fields_sv_control_form($settings) {
  $controlpositions = getlocations_controlpositions();
  $form = array();
  $form['sv_addresscontrol'] = getlocations_element_map_checkbox(t('Show Streetview address'), $settings['sv_addresscontrol']);
  $form['sv_addresscontrol']['#suffix'] = '<div id="wrap-getlocations-sv-addresscontrol">';
  $form['sv_addresscontrolposition'] = getlocations_element_dd(t('Position of Streetview address display'), $settings['sv_addresscontrolposition'], $controlpositions);
  $form['sv_addresscontrolposition']['#suffix'] = '</div>';
  $form['sv_pancontrol'] = getlocations_element_map_checkbox(t('Show Streetview Pan Control'), $settings['sv_pancontrol']);
  $form['sv_pancontrol']['#suffix'] = '<div id="wrap-getlocations-sv-pancontrol">';
  $form['sv_pancontrolposition'] = getlocations_element_dd(t('Position of Streetview Pan Control'), $settings['sv_pancontrolposition'], $controlpositions);
  $form['sv_pancontrolposition']['#suffix'] = '</div>';
  $form['sv_zoomcontrol'] = getlocations_element_map_zoom_controltype($settings['sv_zoomcontrol'], t('Streetview Zoom Control type'));
  $form['sv_zoomcontrol']['#suffix'] = '<div id="wrap-getlocations-sv-zoomcontrol">';
  $form['sv_zoomcontrolposition'] = getlocations_element_dd(t('Position of Streetview Zoom Control'), $settings['sv_zoomcontrolposition'], $controlpositions);
  $form['sv_zoomcontrolposition']['#suffix'] = '</div>';
  $form['sv_linkscontrol'] = getlocations_element_map_checkbox(t('Enable Streetview links'), $settings['sv_linkscontrol']);
  $form['sv_imagedatecontrol'] = getlocations_element_map_checkbox(t('Show Streetview image date'), $settings['sv_imagedatecontrol']);
  $form['sv_scrollwheel'] = getlocations_element_map_checkbox(t('Enable Streetview scrollwheel'), $settings['sv_scrollwheel']);
  $form['sv_clicktogo'] = getlocations_element_map_checkbox(t('Enable Streetview clickToGo'), $settings['sv_clicktogo'], t('Enable navigating Streetview by clicking on it.'));
  return $form;
}
function getlocations_fields_get_display_settings($settings) {
  $display_settings_keys = getlocations_fields_get_display_settings_defaults();
  $display_settings = array();
  foreach ($display_settings_keys as $key => $dval) {
    $display_settings[$key] = isset($settings[$key]) ? $settings[$key] : $dval;
  }
  return $display_settings;
}
function getlocations_fields_get_display_settings_defaults() {
  $settings = array(
    'sv_addresscontrol' => 1,
    'sv_addresscontrolposition' => '',
    'sv_pancontrol' => 1,
    'sv_pancontrolposition' => '',
    'sv_zoomcontrol' => 'default',
    'sv_zoomcontrolposition' => '',
    'sv_linkscontrol' => 1,
    'sv_imagedatecontrol' => 0,
    'sv_scrollwheel' => 1,
    'sv_clicktogo' => 1,
  );
  return $settings;
}

// Theming functions

/**
 * Implements hook_theme().
 *
 * This lets us tell Drupal about our theme functions and their arguments.
 */
function getlocations_fields_theme() {
  return array(
    'getlocations_fields_show' => array(
      'variables' => array(
        'locations' => '',
        'settings' => '',
      ),
    ),
    'getlocations_fields_field_settings_form' => array(
      'render element' => 'form',
    ),
    'getlocations_fields_field_formatter_settings_form' => array(
      'render element' => 'form',
    ),
    'getlocations_fields_field_widget_form' => array(
      'render element' => 'form',
    ),
    'getlocations_fields_settings_form' => array(
      'render element' => 'form',
    ),
    'getlocations_fields_distance' => array(
      'variables' => array(
        'distance' => '',
        'units' => 'km',
      ),
    ),
  );
}

/**
 * @param array $variables
 *
 * @return
 *   Returns $output
 *
 */
function theme_getlocations_fields_show($variables) {
  $locations = $variables['locations'];
  $settings = $variables['settings'];
  if ($settings['display_onemap']) {
    $minmaxes = array(
      'minlat' => 0,
      'minlon' => 0,
      'maxlat' => 0,
      'maxlon' => 0,
    );
  }
  else {
    $minmaxes = '';
  }
  $output = '';
  $location_ct = 0;
  foreach ($locations as $location) {
    $output .= '<div class="location vcard">';
    if (!empty($location['name']) && $settings['display_name']) {
      $output .= '<h4>' . $location['name'] . '</h4>';
    }
    $output .= '<div class="adr">';
    if (!empty($location['street']) && $settings['display_street']) {
      $output .= '<div class="street-address">' . $location['street'];
      if (!empty($location['additional']) && $settings['display_additional']) {
        $output .= ' ' . '<span class="extended-address">' . $location['additional'] . '</span>';
      }
      $output .= '</div>';
    }
    if (!empty($location['city']) && $settings['display_city']) {
      $output .= '<span class="locality">' . $location['city'] . '</span>';
      if (!empty($location['province']) && $settings['display_province']) {
        $output .= ",&nbsp;";
      }
      else {
        $output .= "&nbsp;";
      }
    }
    if (!empty($location['province']) && $settings['display_province']) {
      $output .= '<span class="region">' . $location['province'] . '</span>';
      if (!empty($location['postal_code']) && $settings['display_postal_code']) {
        $output .= "&nbsp;";
      }
    }
    if (!empty($location['postal_code']) && $settings['display_postal_code']) {
      $output .= '<span class="postal-code">' . drupal_strtoupper($location['postal_code']) . '</span>';
    }
    if (!empty($location['country']) && $settings['display_country']) {
      if ($settings['country_full'] && drupal_strlen($location['country']) == 2) {

        // this is a 2 letter code, we want the full name
        $countries = getlocations_get_countries_list();
        $country = $countries[$location['country']];
        if (empty($country)) {
          $country = $location['country'];
        }
      }
      else {
        $country = $location['country'];
      }
      $output .= '<div class="country-name">' . $country . '</div>';
    }
    $output .= '</div>';
    if ($location['phone'] && $settings['display_phone'] || $location['mobile'] && $settings['display_mobile'] || $location['fax'] && $settings['display_fax']) {
      $output .= '<div class="phones">';
      if ($location['phone'] && $settings['display_phone']) {
        $output .= '<div class="phone"><span class="phone_label">' . t('Phone') . '</span>' . $location['phone'] . '</div>';
      }
      if ($location['mobile'] && $settings['display_mobile']) {
        $output .= '<div class="mobile"><span class="mobile_label">' . t('Mobile') . '</span>' . $location['mobile'] . '</div>';
      }
      if ($location['fax'] && $settings['display_fax']) {
        $output .= '<div class="fax"><span class="fax_label">' . t('Fax') . '</span>' . $location['fax'] . '</div>';
      }
      $output .= '</div>';
    }
    $output .= '</div>';

    // what3words
    $what3words_lic = variable_get('getlocations_what3words_lic', array(
      'key' => '',
      'url' => 'http://api.what3words.com',
    ));
    if ($what3words_lic['key'] && isset($location['what3words']) && $location['what3words'] && $settings['what3words_display']) {
      $output .= '<div class="w3w"><span class="w3w_label">' . t('What3Words') . '</span>' . $location['what3words'] . '</div>';
    }
    if ($settings['display_latlong']) {
      $lat = $location['latitude'];
      $lng = $location['longitude'];
      if ($settings['display_geo_microformat'] > 0) {
        $lat_dms = theme('getlocations_latitude_dms', array(
          'latitude' => $lat,
        ));
        $lng_dms = theme('getlocations_longitude_dms', array(
          'longitude' => $lng,
        ));
        if ($settings['display_geo_microformat'] == 1) {
          $output .= '<div class="geo">';
          $output .= '<abbr class="latitude" title="' . $lat . '">' . $lat_dms . '</abbr> ';
          $output .= '<abbr class="longitude" title="' . $lng . '">' . $lng_dms . '</abbr>';
          $output .= '</div>';
        }
        else {
          $output .= '<div itemprop="geo" itemscope itemtype="http://schema.org/GeoCoordinates">';
          $output .= t('Latitude') . '&nbsp;' . $lat_dms . '<br />';
          $output .= t('Longitude') . '&nbsp;' . $lng_dms;
          $output .= '<meta itemprop="latitude" content="' . $lat . '" />';
          $output .= '<meta itemprop="longitude" content="' . $lng . '" />';
          $output .= '</div>';
        }
        $output .= '<br />';
      }
      else {
        if ($settings['display_dms']) {
          $lat = theme('getlocations_latitude_dms', array(
            'latitude' => $lat,
          ));
          $lng = theme('getlocations_longitude_dms', array(
            'longitude' => $lng,
          ));
        }
        $nomap = '';
        if (!$settings['display_showmap']) {
          $nomap = '_nomap';
        }
        $output .= '<div class="getlocations_fields_latlon_wrapper_themed' . $nomap . '">';
        $output .= '<div class="getlocations_fields_lat_wrapper_themed' . $nomap . '">';
        $output .= t('Latitude') . ":&nbsp;" . $lat . '</div>';
        $output .= '<div class="getlocations_fields_lon_wrapper_themed' . $nomap . '">';
        $output .= t('Longitude') . ":&nbsp;" . $lng . '</div>';
        $output .= '</div>';
        $output .= '<br />';
      }
    }

    // smuggle these in from location data
    // map_settings_allow
    if (isset($location['map_settings_allow'])) {
      $map_settings_allow = $location['map_settings_allow'];
    }
    else {
      $map_settings_allow = getlocations_fields_map_settings_allow();
    }
    if ($map_settings_allow) {
      if (isset($location['mapzoom']) && is_numeric($location['mapzoom'])) {
        $settings['nodezoom'] = $location['mapzoom'];
      }
      if (isset($location['map_maptype']) && $location['map_maptype']) {
        $settings['maptype'] = $location['map_maptype'];
      }
    }

    // streetview
    if (isset($location['streetview_settings_allow'])) {
      $streetview_settings_allow = $location['streetview_settings_allow'];
    }
    else {
      $streetview_settings_allow = getlocations_fields_streetview_settings_allow();
    }
    if ($streetview_settings_allow) {
      if (isset($location['sv_enable'])) {
        $settings['sv_enable'] = $location['sv_enable'] == 1 ? 1 : 0;
      }
      if (isset($location['sv_showfirst'])) {
        $settings['sv_showfirst'] = $location['sv_showfirst'] == 1 ? 1 : 0;
      }
      if (isset($location['sv_heading']) && is_numeric($location['sv_heading'])) {
        $settings['sv_heading'] = $location['sv_heading'];
      }
      if (isset($location['sv_zoom']) && is_numeric($location['sv_zoom'])) {
        $settings['sv_zoom'] = $location['sv_zoom'];
      }
      if (isset($location['sv_pitch']) && is_numeric($location['sv_pitch'])) {
        $settings['sv_pitch'] = $location['sv_pitch'];
      }
      $settings += getlocations_fields_get_display_settings($location);
    }
    else {
      $settings['sv_enable'] = 0;
      $settings['sv_showfirst'] = 0;
    }
    if ($location['longitude'] && $location['latitude']) {
      if ($settings['display_showmap']) {
        $name = htmlspecialchars_decode(isset($location['name']) && $location['name'] ? strip_tags($location['name']) : (isset($location['title']) && $location['title'] ? strip_tags($location['title']) : ''), ENT_QUOTES);
        $base = '';
        if ($location['nid']) {
          $base = 'nid';
        }
        elseif ($location['uid']) {
          $base = 'uid';
        }
        elseif ($location['tid']) {
          $base = 'tid';
        }
        elseif ($location['cid']) {
          $base = 'cid';
        }
        $marker = $settings['map_marker'];
        if ($base == 'nid') {

          // term markers, nodes only
          $marker = getlocations_get_term_marker($location['nid'], $marker);
        }

        // per location marker
        if (isset($location['marker']) && !empty($location['marker'])) {
          $marker = $location['marker'];
        }

        // onemap
        if ($settings['display_onemap']) {
          $latlons[] = array(
            $location['latitude'],
            $location['longitude'],
            $location['glid'],
            $name,
            $marker,
            $base,
            '',
            '',
          );
          $minmaxes = getlocations_do_minmaxes($location_ct, $location, $minmaxes);
        }
        else {
          $latlons = array(
            $location['latitude'],
            $location['longitude'],
            $location['glid'],
            $name,
            $marker,
            $base,
            '',
            '',
          );
          $map = getlocations_fields_getmap_show($settings, $latlons);
          $output .= $map;
        }
      }
      else {
        if ($settings['display_maplink']) {
          if (count($locations) > 1) {
            $output .= '<p>' . l(t('Show on a map'), 'getlocations/lids/' . $location['glid']) . '</p>';
          }
          else {
            if ($location['nid']) {
              $output .= '<p>' . l(t('Show on a map'), 'getlocations/node/' . $location['nid']) . '</p>';
            }
            elseif ($location['uid']) {
              $output .= '<p>' . l(t('Show on a map'), 'getlocations/user/' . $location['uid']) . '</p>';
            }
            elseif ($location['tid']) {
              $output .= '<p>' . l(t('Show on a map'), 'getlocations/term/' . $location['tid']) . '</p>';
            }
            elseif ($location['cid']) {
              $output .= '<p>' . l(t('Show on a map'), 'getlocations/comment/' . $location['cid']) . '</p>';
            }
          }
        }
      }
      $location_ct++;
    }
  }

  // onemap
  if ($settings['display_showmap'] && $settings['display_onemap'] && isset($latlons)) {
    if ($location_ct < 2) {
      unset($minmaxes);
      $minmaxes = '';
    }
    $map = getlocations_fields_getmap_show($settings, $latlons, $minmaxes);
    $output .= $map;
  }
  return $output;
}

/**
 * @param array $variables
 *
 * @return
 *   Returns $output
 *
 */
function theme_getlocations_fields_field_settings_form($variables) {
  $form = $variables['form'];
  $output = '';

  // try table
  $header = array();
  $no_striping = TRUE;
  $rows = array(
    array(
      'data' => array(
        array(
          'data' => $form['input_name_width'],
          'class' => array(
            'getlocations_fields_input_cell1',
          ),
        ),
        array(
          'data' => $form['input_name_weight'],
          'class' => array(
            'getlocations_fields_input_cell2',
          ),
        ),
        array(
          'data' => $form['input_name_required'],
          'class' => array(
            'getlocations_fields_input_cell3',
          ),
        ),
      ),
      'no_striping' => $no_striping,
    ),
    array(
      'data' => array(
        array(
          'data' => $form['input_street_width'],
          'class' => array(
            'getlocations_fields_input_cell1',
          ),
        ),
        array(
          'data' => $form['input_street_weight'],
          'class' => array(
            'getlocations_fields_input_cell2',
          ),
        ),
        array(
          'data' => $form['input_street_required'],
          'class' => array(
            'getlocations_fields_input_cell3',
          ),
        ),
      ),
      'no_striping' => $no_striping,
    ),
    array(
      'data' => array(
        array(
          'data' => $form['input_additional_width'],
          'class' => array(
            'getlocations_fields_input_cell1',
          ),
        ),
        array(
          'data' => $form['input_additional_weight'],
          'class' => array(
            'getlocations_fields_input_cell2',
          ),
        ),
        array(
          'data' => $form['input_additional_required'],
          'class' => array(
            'getlocations_fields_input_cell3',
          ),
        ),
      ),
      'no_striping' => $no_striping,
    ),
    array(
      'data' => array(
        array(
          'data' => $form['input_city_width'],
          'class' => array(
            'getlocations_fields_input_cell1',
          ),
        ),
        array(
          'data' => $form['input_city_weight'],
          'class' => array(
            'getlocations_fields_input_cell2',
          ),
        ),
        array(
          'data' => $form['input_city_required'],
          'class' => array(
            'getlocations_fields_input_cell3',
          ),
        ),
      ),
      'no_striping' => $no_striping,
    ),
    array(
      'data' => array(
        array(
          'data' => $form['input_province_width'],
          'class' => array(
            'getlocations_fields_input_cell1',
          ),
        ),
        array(
          'data' => $form['input_province_weight'],
          'class' => array(
            'getlocations_fields_input_cell2',
          ),
        ),
        array(
          'data' => $form['input_province_required'],
          'class' => array(
            'getlocations_fields_input_cell3',
          ),
        ),
      ),
      'no_striping' => $no_striping,
    ),
    array(
      'data' => array(
        array(
          'data' => $form['input_postal_code_width'],
          'class' => array(
            'getlocations_fields_input_cell1',
          ),
        ),
        array(
          'data' => $form['input_postal_code_weight'],
          'class' => array(
            'getlocations_fields_input_cell2',
          ),
        ),
        array(
          'data' => $form['input_postal_code_required'],
          'class' => array(
            'getlocations_fields_input_cell3',
          ),
        ),
      ),
      'no_striping' => $no_striping,
    ),
    array(
      'data' => array(
        array(
          'data' => $form['input_country_width'],
          'class' => array(
            'getlocations_fields_input_cell1',
          ),
        ),
        array(
          'data' => $form['input_country_weight'],
          'class' => array(
            'getlocations_fields_input_cell2',
          ),
        ),
        array(
          'data' => $form['input_country_required'],
          'class' => array(
            'getlocations_fields_input_cell3',
          ),
        ),
      ),
      'no_striping' => $no_striping,
    ),
    array(
      'data' => array(
        array(
          'data' => $form['input_phone_width'],
          'class' => array(
            'getlocations_fields_input_cell1',
          ),
        ),
        array(
          'data' => $form['input_phone_weight'],
          'class' => array(
            'getlocations_fields_input_cell2',
          ),
        ),
        array(
          'data' => $form['input_phone_required'],
          'class' => array(
            'getlocations_fields_input_cell3',
          ),
        ),
      ),
      'no_striping' => $no_striping,
    ),
    array(
      'data' => array(
        array(
          'data' => $form['input_mobile_width'],
          'class' => array(
            'getlocations_fields_input_cell1',
          ),
        ),
        array(
          'data' => $form['input_mobile_weight'],
          'class' => array(
            'getlocations_fields_input_cell2',
          ),
        ),
        array(
          'data' => $form['input_mobile_required'],
          'class' => array(
            'getlocations_fields_input_cell3',
          ),
        ),
      ),
      'no_striping' => $no_striping,
    ),
    array(
      'data' => array(
        array(
          'data' => $form['input_fax_width'],
          'class' => array(
            'getlocations_fields_input_cell1',
          ),
        ),
        array(
          'data' => $form['input_fax_weight'],
          'class' => array(
            'getlocations_fields_input_cell2',
          ),
        ),
        array(
          'data' => $form['input_fax_required'],
          'class' => array(
            'getlocations_fields_input_cell3',
          ),
        ),
      ),
      'no_striping' => $no_striping,
    ),
    array(
      'data' => array(
        array(
          'data' => $form['input_latitude_width'],
          'class' => array(
            'getlocations_fields_input_cell1',
          ),
        ),
        array(
          'data' => $form['input_latitude_weight'],
          'class' => array(
            'getlocations_fields_input_cell2',
          ),
        ),
        array(
          'data' => '',
          'class' => array(
            'getlocations_fields_input_cell3',
          ),
        ),
      ),
      'no_striping' => $no_striping,
    ),
    array(
      'data' => array(
        array(
          'data' => $form['input_longitude_width'],
          'class' => array(
            'getlocations_fields_input_cell1',
          ),
        ),
        array(
          'data' => $form['input_longitude_weight'],
          'class' => array(
            'getlocations_fields_input_cell2',
          ),
        ),
        array(
          'data' => '',
          'class' => array(
            'getlocations_fields_input_cell3',
          ),
        ),
      ),
      'no_striping' => $no_striping,
    ),
  );
  $attributes = array(
    'class' => array(
      'getlocations_fields_input_table',
    ),
  );
  $table = theme('table', array(
    'header' => $header,
    'rows' => $rows,
    'attributes' => $attributes,
  ));
  $form['input_name_width'] = array(
    '#markup' => $table,
  );
  unset($form['input_name_required']);
  unset($form['input_name_weight']);
  unset($form['input_street_width']);
  unset($form['input_street_required']);
  unset($form['input_street_weight']);
  unset($form['input_additional_width']);
  unset($form['input_additional_required']);
  unset($form['input_additional_weight']);
  unset($form['input_city_width']);
  unset($form['input_city_required']);
  unset($form['input_city_weight']);
  unset($form['input_province_width']);
  unset($form['input_province_required']);
  unset($form['input_province_weight']);
  unset($form['input_postal_code_width']);
  unset($form['input_postal_code_required']);
  unset($form['input_postal_code_weight']);
  unset($form['input_country_width']);
  unset($form['input_country_required']);
  unset($form['input_country_weight']);
  unset($form['input_latitude_width']);
  unset($form['input_latitude_weight']);
  unset($form['input_longitude_width']);
  unset($form['input_longitude_weight']);
  unset($form['input_phone_width']);
  unset($form['input_phone_required']);
  unset($form['input_phone_weight']);
  unset($form['input_mobile_width']);
  unset($form['input_mobile_required']);
  unset($form['input_mobile_weight']);
  unset($form['input_fax_width']);
  unset($form['input_fax_required']);
  unset($form['input_fax_weight']);
  $prefix = '<fieldset class="form-wrapper">';
  $prefix .= '<legend><span class="fieldset-legend">' . t('Input form settings') . '</span></legend>';
  $prefix .= '<div class="fieldset-wrapper">';
  $prefix .= '<div class="fieldset-description">' . t('Configure the input form.') . '</div>';
  $form['use_address']['#prefix'] = $prefix;
  if (module_exists('countries')) {
    $form['only_countries']['#suffix'] = '</div></fieldset>';
  }
  else {
    $form['use_country_dropdown']['#suffix'] = '</div></fieldset>';
  }
  $prefix = '<fieldset class="form-wrapper">';
  $prefix .= '<legend><span class="fieldset-legend">' . t('Input map settings') . '</span></legend>';
  $prefix .= '<div class="fieldset-wrapper">';
  $prefix .= '<div class="fieldset-description">' . t('Configure the input map.') . '</div>';
  $form['input_map_show']['#prefix'] = $prefix;
  $form['draggable']['#suffix'] = '</div></fieldset>';
  if (isset($form['input_map_marker'])) {
    $link = getlocations_markerpicker_link($form['input_map_marker']['#id'], 'i');
    $form['input_map_marker']['#field_suffix'] = '&nbsp;' . $link;
  }
  $output .= drupal_render_children($form);
  return $output;
}

/**
 * The display settings form
 *
 * @param array $variables
 *
 * @return
 *   Returns $output
 *
 */
function theme_getlocations_fields_field_formatter_settings_form($variables) {
  $form = $variables['form'];
  $output = '';
  $form['display_showmap']['#prefix'] = '<fieldset class="getlocations_fieldset form-wrapper"><legend><span class="fieldset-legend">' . t('Address display settings') . '</span></legend><div class="fieldset-wrapper">';
  $form['display_fax']['#suffix'] = '</div></fieldset>';
  $form['sv_addresscontrol']['#prefix'] = '<fieldset class="getlocations_fieldset collapsible ' . (getlocations_fields_streetview_settings_allow() ? '' : 'collapsed') . ' form-wrapper"><legend><span class="fieldset-legend">' . t('Streetview overlay settings') . '</span></legend><div class="fieldset-wrapper">';
  $form['sv_clicktogo']['#suffix'] = '</div></fieldset>';
  $output .= drupal_render_children($form);
  return $output;
}

/**
 * The input form
 *
 * @param array $variables
 *
 * @return
 *   Returns $output
 *
 */
function theme_getlocations_fields_field_widget_form($variables) {
  $form = $variables['form'];
  $output = '';
  $settings = $form['settings']['#value'];
  unset($form['settings']);
  $active = $form['active']['#value'];
  list($map, $mapid) = getlocations_fields_getmap($settings, $active);

  // attributes
  $fieldlist = array(
    'name',
    'street',
    'additional',
    'city',
    'province',
    'postal_code',
    'country',
    'latitude',
    'longitude',
    'mapzoom',
    'sv_heading',
    'sv_zoom',
    'sv_pitch',
    'map_maptype',
    'sv_enable',
    'sv_showfirst',
  );
  foreach ($fieldlist as $field) {
    if (isset($form[$field])) {
      $form[$field]['#attributes'] = array(
        'class' => array(
          'getlocations_' . $field . '_' . $mapid,
        ),
      );
    }
  }
  if (isset($form['address'])) {
    $form['address']['#attributes'] = array(
      'id' => $active ? 'getlocations_address_' . $mapid : 'getlocations_default_address',
      'placeholder' => t('Enter an address'),
    );
  }
  if ($mapid !== NULL && isset($form['geobutton'])) {
    $geobutton = '<p><input type="button" value="' . t('Geocode this address') . '" title="' . t('Get the latitude and longitude for this address') . '" id="getlocations_geocodebutton_' . $mapid . '" class="form-submit" /></p>';
    $form['geobutton']['#markup'] = $geobutton;
  }
  if ($mapid !== NULL && isset($form['smart_ip_button']) && module_exists('smart_ip')) {
    $smart_ip_button = '<p><input type="button" value="' . t('Use current user position') . '" title="' . t('Get the latitude and longitude for this address from the user IP') . '" id="getlocations_smart_ip_button_' . $mapid . '" class="form-submit" /></p>';
    $form['smart_ip_button']['#markup'] = $smart_ip_button;
  }

  // html5 and google
  if (getlocations_is_mobile() && $mapid !== NULL && isset($form['geolocation_button'])) {
    $geolocation_button = '<p><input type="button" value="' . t('Find Location') . '" title="' . t('Get the latitude and longitude for this address from the browser') . '" id="getlocations_geolocation_button_' . $mapid . '" class="form-submit" />&nbsp;<span id="getlocations_geolocation_status_' . $mapid . '" ></span></p>';
    $geolocation_button .= '<p>Use the browser to find your current location</p>';
    $form['geolocation_button']['#markup'] = $geolocation_button;
  }

  // Clear button
  if (isset($form['clear_button'])) {
    $clear_button = '<p><input type="button" value="' . t('Clear') . '" title="' . t('Clear the address on the form') . '" id="getlocations_clear_button_' . $mapid . '" class="form-submit" /></p>';
    $form['clear_button']['#markup'] = $clear_button;
  }
  $form['latitude']['#prefix'] = '<div class="getlocations_fields_latlon_wrapper clearfix"><div class="getlocations_fields_lat_wrapper">';
  $form['latitude']['#suffix'] = '</div>';
  $form['longitude']['#prefix'] = '<div class="getlocations_fields_lon_wrapper">';
  $form['longitude']['#suffix'] = '</div></div>';
  if (isset($form['marker']) && $form['marker']['#type'] != 'value') {

    // colorbox marker
    $link = getlocations_markerpicker_link($form['marker']['#id'], 'i');
    $form['marker']['#field_suffix'] = '&nbsp;' . $link;
    $form['marker']['#prefix'] = '<div class="getlocations_fields_marker_wrapper">';
    $form['marker']['#suffix'] = '</div>';
  }
  $form['city']['#prefix'] = '<div class="getlocations_fields_city_wrapper">';
  $form['city']['#suffix'] = '</div>';
  $form['province']['#prefix'] = '<div class="getlocations_fields_province_wrapper">';
  $form['province']['#suffix'] = '</div>';
  $form['country']['#prefix'] = '<div class="getlocations_fields_country_wrapper">';
  if (isset($form['country_display'])) {
    $form['country_display']['#suffix'] = '</div>';
  }
  else {
    $form['country']['#suffix'] = '</div>';
  }
  $msg = t('To use the map to find a location, drag the marker or click on the map.');
  if (isset($settings['map_settings_allow'])) {
    $map_settings_allow = $settings['map_settings_allow'];
  }
  else {
    $map_settings_allow = getlocations_fields_map_settings_allow();
  }
  if ($map_settings_allow) {
    $msg .= '<br />' . t('You can also set the map type and zoom');
  }
  if (!$active) {
    $msg = t('A preview of the current map settings.') . '<br />' . t('You can adjust the Map center, Zoom and map type by changing the map.') . '<br />' . t('For all other changes use the form. Remember to Save when you are done.');
  }
  $form['map']['#prefix'] = '<div class="getlocations_fields_map_wrapper">';
  $form['map']['#suffix'] = '</div>';
  if ($settings['use_address'] < 1) {
    $form['map']['#markup'] = '<p>' . t('If you leave the address empty you can Geocode by moving the marker on the map') . '</p>';
  }
  else {
    $form['map']['#markup'] = '';
  }
  $form['map']['#markup'] .= '<p>' . $msg . '</p><div class="getlocations_fields_map">' . $map . '</div>';
  if (isset($settings['streetview_settings_allow'])) {
    $streetview_settings_allow = $settings['streetview_settings_allow'];
  }
  else {
    $streetview_settings_allow = getlocations_fields_streetview_settings_allow();
  }
  if ($active && $streetview_settings_allow) {
    $form['map']['#markup'] .= '<div id="getlocations_streetview_setup_' . $mapid . '">';
    $form['map']['#markup'] .= '<input type="button" value="' . t('Streetview Setup') . '" title="' . t('Configure streetview heading, zoom and pitch for this map.') . '" id="getlocations_streetview_setupbutton_' . $mapid . '" class="form-submit" />';
    $form['map']['#markup'] .= '</div>';
  }

  // wrap 'fake' required in a div
  if ($mapid !== NULL && isset($form['getlocations_required'])) {
    $requireds = explode(',', $form['getlocations_required']['#value']);
    foreach ($requireds as $id) {
      $form[$id]['#prefix'] = '<div class="getlocations_required_' . $id . '_' . $mapid . '">';
      $form[$id]['#suffix'] = '</div>';
    }
    unset($form['getlocations_required']);
  }
  $output .= drupal_render_children($form);
  return $output;
}

/**
 * The settings form
 *
 * @param array $variables
 *
 * @return
 *   Returns $output
 *
 */
function theme_getlocations_fields_settings_form($variables) {
  $form = $variables['form'];
  $output = '';

  // try table
  $header = array();
  $no_striping = TRUE;
  $rows = array(
    array(
      'data' => array(
        array(
          'data' => $form['getlocations_fields_defaults']['input_name_width'],
          'class' => array(
            'getlocations_fields_input_cell1',
          ),
        ),
        array(
          'data' => $form['getlocations_fields_defaults']['input_name_weight'],
          'class' => array(
            'getlocations_fields_input_cell2',
          ),
        ),
        array(
          'data' => $form['getlocations_fields_defaults']['input_name_required'],
          'class' => array(
            'getlocations_fields_input_cell3',
          ),
        ),
      ),
      'no_striping' => $no_striping,
    ),
    array(
      'data' => array(
        array(
          'data' => $form['getlocations_fields_defaults']['input_street_width'],
          'class' => array(
            'getlocations_fields_input_cell1',
          ),
        ),
        array(
          'data' => $form['getlocations_fields_defaults']['input_street_weight'],
          'class' => array(
            'getlocations_fields_input_cell2',
          ),
        ),
        array(
          'data' => $form['getlocations_fields_defaults']['input_street_required'],
          'class' => array(
            'getlocations_fields_input_cell3',
          ),
        ),
      ),
      'no_striping' => $no_striping,
    ),
    array(
      'data' => array(
        array(
          'data' => $form['getlocations_fields_defaults']['input_additional_width'],
          'class' => array(
            'getlocations_fields_input_cell1',
          ),
        ),
        array(
          'data' => $form['getlocations_fields_defaults']['input_additional_weight'],
          'class' => array(
            'getlocations_fields_input_cell2',
          ),
        ),
        array(
          'data' => $form['getlocations_fields_defaults']['input_additional_required'],
          'class' => array(
            'getlocations_fields_input_cell3',
          ),
        ),
      ),
      'no_striping' => $no_striping,
    ),
    array(
      'data' => array(
        array(
          'data' => $form['getlocations_fields_defaults']['input_city_width'],
          'class' => array(
            'getlocations_fields_input_cell1',
          ),
        ),
        array(
          'data' => $form['getlocations_fields_defaults']['input_city_weight'],
          'class' => array(
            'getlocations_fields_input_cell2',
          ),
        ),
        array(
          'data' => $form['getlocations_fields_defaults']['input_city_required'],
          'class' => array(
            'getlocations_fields_input_cell3',
          ),
        ),
      ),
      'no_striping' => $no_striping,
    ),
    array(
      'data' => array(
        array(
          'data' => $form['getlocations_fields_defaults']['input_province_width'],
          'class' => array(
            'getlocations_fields_input_cell1',
          ),
        ),
        array(
          'data' => $form['getlocations_fields_defaults']['input_province_weight'],
          'class' => array(
            'getlocations_fields_input_cell2',
          ),
        ),
        array(
          'data' => $form['getlocations_fields_defaults']['input_province_required'],
          'class' => array(
            'getlocations_fields_input_cell3',
          ),
        ),
      ),
      'no_striping' => $no_striping,
    ),
    array(
      'data' => array(
        array(
          'data' => $form['getlocations_fields_defaults']['input_postal_code_width'],
          'class' => array(
            'getlocations_fields_input_cell1',
          ),
        ),
        array(
          'data' => $form['getlocations_fields_defaults']['input_postal_code_weight'],
          'class' => array(
            'getlocations_fields_input_cell2',
          ),
        ),
        array(
          'data' => $form['getlocations_fields_defaults']['input_postal_code_required'],
          'class' => array(
            'getlocations_fields_input_cell3',
          ),
        ),
      ),
      'no_striping' => $no_striping,
    ),
    array(
      'data' => array(
        array(
          'data' => $form['getlocations_fields_defaults']['input_country_width'],
          'class' => array(
            'getlocations_fields_input_cell1',
          ),
        ),
        array(
          'data' => $form['getlocations_fields_defaults']['input_country_weight'],
          'class' => array(
            'getlocations_fields_input_cell2',
          ),
        ),
        array(
          'data' => $form['getlocations_fields_defaults']['input_country_required'],
          'class' => array(
            'getlocations_fields_input_cell3',
          ),
        ),
      ),
      'no_striping' => $no_striping,
    ),
    array(
      'data' => array(
        array(
          'data' => $form['getlocations_fields_defaults']['input_phone_width'],
          'class' => array(
            'getlocations_fields_input_cell1',
          ),
        ),
        array(
          'data' => $form['getlocations_fields_defaults']['input_phone_weight'],
          'class' => array(
            'getlocations_fields_input_cell2',
          ),
        ),
        array(
          'data' => $form['getlocations_fields_defaults']['input_phone_required'],
          'class' => array(
            'getlocations_fields_input_cell3',
          ),
        ),
      ),
      'no_striping' => $no_striping,
    ),
    array(
      'data' => array(
        array(
          'data' => $form['getlocations_fields_defaults']['input_mobile_width'],
          'class' => array(
            'getlocations_fields_input_cell1',
          ),
        ),
        array(
          'data' => $form['getlocations_fields_defaults']['input_mobile_weight'],
          'class' => array(
            'getlocations_fields_input_cell2',
          ),
        ),
        array(
          'data' => $form['getlocations_fields_defaults']['input_mobile_required'],
          'class' => array(
            'getlocations_fields_input_cell3',
          ),
        ),
      ),
      'no_striping' => $no_striping,
    ),
    array(
      'data' => array(
        array(
          'data' => $form['getlocations_fields_defaults']['input_fax_width'],
          'class' => array(
            'getlocations_fields_input_cell1',
          ),
        ),
        array(
          'data' => $form['getlocations_fields_defaults']['input_fax_weight'],
          'class' => array(
            'getlocations_fields_input_cell2',
          ),
        ),
        array(
          'data' => $form['getlocations_fields_defaults']['input_fax_required'],
          'class' => array(
            'getlocations_fields_input_cell3',
          ),
        ),
      ),
      'no_striping' => $no_striping,
    ),
    array(
      'data' => array(
        array(
          'data' => $form['getlocations_fields_defaults']['input_latitude_width'],
          'class' => array(
            'getlocations_fields_input_cell1',
          ),
        ),
        array(
          'data' => $form['getlocations_fields_defaults']['input_latitude_weight'],
          'class' => array(
            'getlocations_fields_input_cell2',
          ),
        ),
        array(
          'data' => '',
          'class' => array(
            'getlocations_fields_input_cell3',
          ),
        ),
      ),
      'no_striping' => $no_striping,
    ),
    array(
      'data' => array(
        array(
          'data' => $form['getlocations_fields_defaults']['input_longitude_width'],
          'class' => array(
            'getlocations_fields_input_cell1',
          ),
        ),
        array(
          'data' => $form['getlocations_fields_defaults']['input_longitude_weight'],
          'class' => array(
            'getlocations_fields_input_cell2',
          ),
        ),
        array(
          'data' => '',
          'class' => array(
            'getlocations_fields_input_cell3',
          ),
        ),
      ),
      'no_striping' => $no_striping,
    ),
  );
  $attributes = array(
    'class' => array(
      'getlocations_fields_input_table',
    ),
  );
  $table = theme('table', array(
    'header' => $header,
    'rows' => $rows,
    'attributes' => $attributes,
  ));
  $form['getlocations_fields_defaults']['input_name_width'] = array(
    '#markup' => $table,
  );
  unset($form['getlocations_fields_defaults']['input_name_required']);
  unset($form['getlocations_fields_defaults']['input_name_weight']);
  unset($form['getlocations_fields_defaults']['input_street_width']);
  unset($form['getlocations_fields_defaults']['input_street_required']);
  unset($form['getlocations_fields_defaults']['input_street_weight']);
  unset($form['getlocations_fields_defaults']['input_additional_width']);
  unset($form['getlocations_fields_defaults']['input_additional_required']);
  unset($form['getlocations_fields_defaults']['input_additional_weight']);
  unset($form['getlocations_fields_defaults']['input_city_width']);
  unset($form['getlocations_fields_defaults']['input_city_required']);
  unset($form['getlocations_fields_defaults']['input_city_weight']);
  unset($form['getlocations_fields_defaults']['input_province_width']);
  unset($form['getlocations_fields_defaults']['input_province_required']);
  unset($form['getlocations_fields_defaults']['input_province_weight']);
  unset($form['getlocations_fields_defaults']['input_postal_code_width']);
  unset($form['getlocations_fields_defaults']['input_postal_code_required']);
  unset($form['getlocations_fields_defaults']['input_postal_code_weight']);
  unset($form['getlocations_fields_defaults']['input_country_width']);
  unset($form['getlocations_fields_defaults']['input_country_required']);
  unset($form['getlocations_fields_defaults']['input_country_weight']);
  unset($form['getlocations_fields_defaults']['input_latitude_width']);
  unset($form['getlocations_fields_defaults']['input_latitude_weight']);
  unset($form['getlocations_fields_defaults']['input_longitude_width']);
  unset($form['getlocations_fields_defaults']['input_longitude_weight']);
  unset($form['getlocations_fields_defaults']['input_phone_width']);
  unset($form['getlocations_fields_defaults']['input_phone_required']);
  unset($form['getlocations_fields_defaults']['input_phone_weight']);
  unset($form['getlocations_fields_defaults']['input_mobile_width']);
  unset($form['getlocations_fields_defaults']['input_mobile_required']);
  unset($form['getlocations_fields_defaults']['input_mobile_weight']);
  unset($form['getlocations_fields_defaults']['input_fax_width']);
  unset($form['getlocations_fields_defaults']['input_fax_required']);
  unset($form['getlocations_fields_defaults']['input_fax_weight']);
  $prefix = '<fieldset class="collapsible form-wrapper">';
  $prefix .= '<legend><span class="fieldset-legend">' . t('Default Input form settings') . '</span></legend>';
  $prefix .= '<div class="fieldset-wrapper">';
  $prefix .= '<div class="fieldset-description">' . t('Configure the default input form.') . '</div>';
  $form['getlocations_fields_defaults']['use_address']['#prefix'] = $prefix;
  $form['getlocations_fields_defaults']['use_country_dropdown']['#suffix'] = '</div></fieldset>';
  $prefix = '<fieldset class="collapsible form-wrapper">';
  $prefix .= '<legend><span class="fieldset-legend">' . t('Default Address display settings') . '</span></legend>';
  $prefix .= '<div class="fieldset-wrapper">';
  $prefix .= '<div class="fieldset-description">' . t('Configure the default address display.') . '</div>';
  $form['getlocations_fields_defaults']['display_showmap']['#prefix'] = $prefix;
  $form['getlocations_fields_defaults']['display_fax']['#suffix'] = '</div></fieldset>';
  $output .= drupal_render_children($form);
  return $output;
}
function theme_getlocations_fields_distance($variables) {
  $distance = $variables['distance'];
  $shortunit = $variables['shortunit'];

  // also available
  // $longunit = $variables['longunit'];
  $output = '';
  $output .= number_format($distance, 2) . ' ' . $shortunit;
  return $output;
}

/**
 * Theme preprocess function for getlocations_fields_distance.
 */
function template_preprocess_getlocations_fields_distance(&$variables) {
  $units = $variables['units'];
  unset($variables['units']);
  $variables['shortunit'] = $units;
  $variables['longunit'] = getlocations_get_unit_names($units, 'plurals');
  $variables['distance'] = (double) $variables['distance'];
}

Functions

Namesort descending Description
getlocations_fields_column_check
getlocations_fields_data_keys
getlocations_fields_defaults
getlocations_fields_delete_record Create a location
getlocations_fields_delete_record_relations
getlocations_fields_delete_revision_record Create a location
getlocations_fields_delete_revision_record_relations
getlocations_fields_display_settings_form display settings form for sharing
getlocations_fields_field_delete Implements hook_field_delete(). Define custom delete behavior for this module's field types.
getlocations_fields_field_delete_revision Implements hook_field_delete_revision(). Define custom revision delete behavior for this module's field types.
getlocations_fields_field_formatter_info Implements hook_field_formatter_info().
getlocations_fields_field_formatter_info_defaults
getlocations_fields_field_formatter_settings_form Implements hook_field_formatter_settings_form(). Returns form elements for a formatter's settings.
getlocations_fields_field_formatter_settings_summary Implements hook_field_formatter_settings_summary(). Returns a short summary for the current formatter settings of an instance.
getlocations_fields_field_formatter_view Implements hook_field_formatter_view(). Build a renderable array for a field value.
getlocations_fields_field_info Implements hook_field_info(). Define Field API field types.
getlocations_fields_field_info_defaults
getlocations_fields_field_insert Implements hook_field_insert(). Define custom insert behavior for this module's field types.
getlocations_fields_field_is_empty Implements hook_field_is_empty(). Define what constitutes an empty item for a field type. hook_field_is_emtpy() is where Drupal asks us if this field is empty. Return TRUE if it does not contain data, FALSE if it does. This lets the form API flag an…
getlocations_fields_field_load Implements hook_field_load(). Define custom load behavior for this module's field types. http://api.drupal.org/api/drupal/modules--field--field.api.php/function/...
getlocations_fields_field_settings_form Implements hook_field_settings_form(). Add settings to a field settings form.
getlocations_fields_field_update Implements hook_field_update(). Define custom update behavior for this module's field types.
getlocations_fields_field_validate Implements hook_field_validate(). Validate this module's field data.
getlocations_fields_field_widget_error Implements hook_field_widget_error(). Flag a field-level validation error.
getlocations_fields_field_widget_form Implements hook_field_widget_form(). Return the form for a single field widget.
getlocations_fields_field_widget_info Implements hook_field_widget_info(). Expose Field API widget types.
getlocations_fields_getmap input map
getlocations_fields_getmap_show Used by theme for display output.
getlocations_fields_get_countries_list
getlocations_fields_get_display_settings
getlocations_fields_get_display_settings_defaults
getlocations_fields_help Implements hook_help().
getlocations_fields_init Implements hook_init().
getlocations_fields_input_settings_form input settings form for sharing
getlocations_fields_insert_record Create a location
getlocations_fields_insert_record_relations Insert the relationship information
getlocations_fields_js_settings_do
getlocations_fields_load_location
getlocations_fields_load_locations
getlocations_fields_mapinfo
getlocations_fields_map_settings_allow
getlocations_fields_menu Implements hook_menu().
getlocations_fields_modules_uninstalled Implements hook_modules_uninstalled().
getlocations_fields_paths_get
getlocations_fields_save_locations
getlocations_fields_streetview_settings_allow
getlocations_fields_svinfo
getlocations_fields_sv_control_form
getlocations_fields_theme Implements hook_theme().
getlocations_fields_update_record Create a location
getlocations_fields_update_record_relations
getlocations_fields_views_api Implements hook_views_api().
template_preprocess_getlocations_fields_distance Theme preprocess function for getlocations_fields_distance.
theme_getlocations_fields_distance
theme_getlocations_fields_field_formatter_settings_form The display settings form
theme_getlocations_fields_field_settings_form
theme_getlocations_fields_field_widget_form The input form
theme_getlocations_fields_settings_form The settings form
theme_getlocations_fields_show

Constants

Namesort descending Description
GETLOCATIONS_FIELDS_PATH @file getlocations_fields.module @author Bob Hutchinson http://drupal.org/user/52366 @copyright GNU GPL