You are here

location_taxonomize_af.module in Location Taxonomize 7.2

File

location_taxonomize_af/location_taxonomize_af.module
View source
<?php

/**
 * @file
 * This is a 'source module' that adds a data source for the Location Taoxnomize
 * module. It implements the Location Taxonomize functionality for the 
 * Address Field module.
 */
require_once 'location_taxonomize_af.inc';

/**
 * Register this module as a source module for Location Taxonomize
 */
function location_taxonomize_af_location_taxonomize_source() {
  return array(
    'location_taxonomize_af' => 'Address Field',
  );
}

/**
 * Implements hook_location_taxonomize_initialize().
 */
function location_taxonomize_af_location_taxonomize_initialize($settings) {

  // Show a message making it clear that the field-specific enable checkbox needs to be selected.
  if ($settings['source'] == 'location_taxonomize_af') {
    drupal_set_message(t("NOTE: In order to actually taxonomize data from Address Field fields, you must also edit the individual fields and enable the 'Taxonomize locations from this field' checkbox."), 'warning');
  }
}

/**
 * Implements hook_field_widget_info_alter().
 * This is used to add a setting to the addressfield widget type that
 * determines whether the field is taxonomized
 */
function location_taxonomize_af_field_widget_info_alter(&$info) {

  // Add a setting to a widget type.
  $info['addressfield_standard']['settings'] += array(
    'location_taxonomize' => 0,
  );
}

/**
 * Implements hook_form_field_ui_field_edit_form_alter().
 * Here we add a form element on the addressfield widget settings form
 * to allow users to configure whether this field will be taxonomized
 * 
 */
function location_taxonomize_af_form_field_ui_field_edit_form_alter(&$form, $form_state, $form_id) {
  if ($form['#field']['type'] != 'addressfield') {
    return;
  }
  $settings = $form['#instance']['widget']['settings'];
  $enable = array(
    '#type' => 'checkbox',
    '#title' => t('Taxonomize locations from this field using Location Taxonomize'),
    '#default_value' => $settings['location_taxonomize'],
  );
  $form['instance']['widget']['settings']['location_taxonomize'] = $enable;
}

/**
 * Implements hook_field_widget_WIDGET_TYPE_form_alter().
 * Here we add a process callback for every widget that is set to be taxonomized
 */
function location_taxonomize_af_field_widget_addressfield_standard_form_alter(&$element, &$form_state, $context) {

  // check if this module is enabled as the current source
  if (_location_taxonomize_get_source() != LT_AF_MODULE_ID) {
    return;
  }

  // apply this only if we are on a node-edit form
  if (!isset($context['form']['#node_edit_form'])) {
    return;
  }

  // check field settings
  $taxonomize = $context['instance']['widget']['settings']['location_taxonomize'];

  // add a process function to addressfield widgets
  if ($taxonomize) {
    if (!in_array('location_taxonomize_af_process_address', $element['#process'])) {
      $element['#process'][] = 'location_taxonomize_af_process_address';
    }
  }
}

/**
 * A #process callback function for the addressfield widget
 * Here we make note of this field in the form_state array, and add a submit
 * callback to the form that will handle the processing.
 */
function location_taxonomize_af_process_address($element, &$form_state, &$form) {

  // add a submit handler to forms that have this widget
  if (!in_array('location_taxonomize_af_element_submitted', $form['#submit'])) {
    array_unshift($form['#submit'], 'location_taxonomize_af_element_submitted');
  }

  // make note that this field needs to be processed in the submit handler
  if (!isset($form_state['temporary']['addressfields']) || !in_array($element['#field_name'], $form_state['temporary']['addressfields'])) {
    $form_state['temporary']['addressfields'][] = $element['#field_name'];
  }
  return $element;
}

/**
 * A submit handler for forms that contain an addressfield
 */
function location_taxonomize_af_element_submitted($form, &$form_state) {
  $lang = 'und';

  // Act on values
  $fields = $form_state['temporary']['addressfields'];
  $items = array();
  foreach ($fields as $field) {
    $deltas = $form_state['values'][$field][$lang];
    $actual_delta = 0;
    foreach ($deltas as $delta) {

      // Add the Administrative Area name, if possible
      if (isset($form[$field][$lang][$actual_delta]['locality_block']['administrative_area']['#options'])) {
        $options = $form[$field][$lang][$actual_delta]['locality_block']['administrative_area']['#options'];
        $delta['administrative_area_name'] = $options[$delta['administrative_area']];
      }

      // Add the country name
      $delta['country_name'] = location_taxonomize_get_country_name($delta['country']);
      $items[] = $delta;
      $actual_delta++;
    }
  }

  // taxonomize
  if (!empty($items)) {
    location_taxonomize_taxonomize($items, $form, $form_state);
  }
}

/**
 * Runs the bulk taxonomize operation
 */
function location_taxonomize_af_bulk_taxonomize_op($form_state, &$context) {

  // initialize progress, max, and current if this is the first iteration
  if (!isset($context['sandbox']['progress'])) {
    $field_refs = array();

    // get all fields
    $fields = field_info_fields();

    // take out all non-addressfield fields
    foreach ($fields as $field => $data) {
      if ($data['type'] != 'addressfield') {
        unset($fields[$field]);
      }
    }

    // collect references to all the fields for which taxonomize is enabled
    foreach ($fields as $field) {
      foreach ($field['bundles'] as $entity => $bundles) {
        foreach ($bundles as $bundle) {
          $instance = field_info_instance($entity, $field['field_name'], $bundle);
          if ($instance['widget']['settings']['location_taxonomize'] == 1) {
            $field_refs[] = array(
              $entity,
              $bundle,
              $field['field_name'],
            );
          }
        }
      }
    }

    // collect node ids
    $final = array();
    $count = 0;
    foreach ($field_refs as $field) {
      $e_type = $field[0];
      $bundle = $field[1];
      $field_name = $field[2];
      $replace = array(
        ':etype' => $e_type,
        ':bundle' => $bundle,
      );
      $table = 'field_data_' . $field_name;
      $result = db_query("SELECT entity_id FROM {$table} WHERE entity_type = :etype AND bundle = :bundle ORDER BY entity_id ASC", $replace);
      $result_array = $result
        ->fetchAllAssoc('entity_id');
      foreach ($result_array as $e) {
        if ($e_type == 'node') {
          $id = $e->entity_id;
          $final[$id][] = $field_name;
          $count++;
        }
      }
    }
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['max'] = $count;
    $context['sandbox']['current'] = 0;
    $context['results']['added'] = 0;
    $context['results']['processed'] = 0;
    $context['sandbox']['fields'] = $final;
    if ($count == 0) {
      return;
    }
  }

  // maximum 10 addresses per function iteration
  $limit = 10;

  // iterate through the next group of addresses
  $i = 0;
  $saved = 0;
  $taxonomize = array();
  if (!empty($context['sandbox']['fields'])) {
    foreach ($context['sandbox']['fields'] as $nid => $fields) {
      $node = node_load($nid);
      foreach ($fields as $field) {
        $items = field_get_items('node', $node, $field);
        foreach ($items as $address) {

          // Add the country name
          $address['country_name'] = location_taxonomize_get_country_name($address['country']);
          $taxonomize[$nid][] = $address;
        }
        $i++;
        array_shift($context['sandbox']['fields'][$nid]);
        if (empty($context['sandbox']['fields'][$nid])) {
          unset($context['sandbox']['fields'][$nid]);
        }
      }
      if ($i > $limit) {
        break;
      }
    }

    // taxonomize
    $results = location_taxonomize_taxonomize_bulk($taxonomize);
    $saved = $results['saved'];
  }
  $added = $saved;
  $context['results']['added'] += $added;
  $context['sandbox']['progress'] += $i;
  $context['message'] = t('Processing address');
  $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  $context['sandbox']['current'] = $context['sandbox']['progress'];
  $context['results']['processed'] = $context['sandbox']['progress'];
}

Functions

Namesort descending Description
location_taxonomize_af_bulk_taxonomize_op Runs the bulk taxonomize operation
location_taxonomize_af_element_submitted A submit handler for forms that contain an addressfield
location_taxonomize_af_field_widget_addressfield_standard_form_alter Implements hook_field_widget_WIDGET_TYPE_form_alter(). Here we add a process callback for every widget that is set to be taxonomized
location_taxonomize_af_field_widget_info_alter Implements hook_field_widget_info_alter(). This is used to add a setting to the addressfield widget type that determines whether the field is taxonomized
location_taxonomize_af_form_field_ui_field_edit_form_alter Implements hook_form_field_ui_field_edit_form_alter(). Here we add a form element on the addressfield widget settings form to allow users to configure whether this field will be taxonomized
location_taxonomize_af_location_taxonomize_initialize Implements hook_location_taxonomize_initialize().
location_taxonomize_af_location_taxonomize_source Register this module as a source module for Location Taxonomize
location_taxonomize_af_process_address A #process callback function for the addressfield widget Here we make note of this field in the form_state array, and add a submit callback to the form that will handle the processing.