<?php

/**
 * @file getlocations_fields_entity.module
 * @author Bob Hutchinson http://drupal.org/user/52366
 * @copyright GNU GPL
 *
 * Provides information about Getlocations Fields to Entity module.
 * Thanks to rbruhn, http://drupal.org/user/1037754 for this code
 * see issue http://drupal.org/node/1661362
 *
 */

/**
 * Implements hook_field_info_alter().
 */
function getlocations_fields_entity_field_info_alter(&$field_info) {
  $field_info['getlocations_fields']['property_type'] = 'getlocations_fields';
  $field_info['getlocations_fields']['property_callbacks'][] = 'getlocations_fields_entity_getlocations_fields_property_callback';
}

/**
 * Property callback for getlocations fields.
 */
function getlocations_fields_entity_getlocations_fields_property_callback(&$info, $entity_type, $field, $instance, $field_type) {
  $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$field['field_name']];
  $property['getter callback'] = 'entity_metadata_field_verbatim_get';
  $property['setter callback'] = 'entity_metadata_field_verbatim_set';

  foreach (getlocations_fields_entity_key_map() as $key => $item) {
    $property['property info'][$key] = array(
      'type' => $item['type'],
      'label' => $item['label'],
      'description' => $item['description'],
      'getter callback' => 'entity_property_verbatim_get',
      'setter callback' => 'entity_property_verbatim_set',
    );
  }

  unset($property['query callback']);
}

/**
 * Helper function that returns getlocations_fields key map array
 */
function getlocations_fields_entity_key_map() {
  $keys = array(
    'glid' => array(
      'type' => 'integer',
      'label' => t('glid'),
      'description' => t('Getlocations Fields ID field')
    ),
    'name' => array(
      'type' => 'text',
      'label' => t('Name'),
      'description' => t('Getlocations Fields name field')
    ),
    'street' => array(
      'type' => 'text',
      'label' => t('Street'),
      'description' => t('GetLocations Fields street field')
    ),
    'additional' => array(
      'type' => 'text',
      'label' => t('Additional'),
      'description' => t('Getlocations Fields additional field')
    ),
    'city' => array(
      'type' => 'text',
      'label' => t('City'),
      'description' => t('Getlocations Fields city field')
    ),
    'province' => array(
      'type' => 'text',
      'label' => t('Province'),
      'description' => t('Getlocations Fields province field')
    ),
    'postal_code' => array(
      'type' => 'text',
      'label' => t('Postal code'),
      'description' => t('Getlocations Fields postal code field')
    ),
    'country' => array(
      'type' => 'text',
      'label' => t('Country'),
      'description' => t('Getlocations Fields country field')
    ),
    'address' => array(
      'type' => 'text',
      'label' => t('Address'),
      'description' => t('Getlocations Fields address field')
    ),
    'latitude' => array(
      'type' => 'decimal',
      'label' => t('Latitude'),
      'description' => t('Getlocations Fields latitude field')
    ),
    'longitude' => array(
      'type' => 'decimal',
      'label' => t('Longitude'),
      'description' => t('Getlocations Fields longitude field')
    ),
    'marker' => array(
      'type' => 'text',
      'label' => t('Marker'),
      'description' => t('Getlocations Fields marker field')
    ),
  );
  if (getlocations_fields_column_check('data')) {
    $keys['data'] = array(
      'type' => 'text',
      'label' => t('Data'),
      'description' => t('Getlocations Fields serialized data field')
    );
  }
  return $keys;
}
