You are here

getlocations_location_taxonomize.module in Get Locations 7

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

This is a 'source module' that adds a data source for the Location Taxonomize module. It implements the Location Taxonomize functionality for the Getlocations_fields module.

File

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

/**
 * @file
 * getlocations_location_taxonomize.module
 * @author Bob Hutchinson http://drupal.org/user/52366
 * @copyright GNU GPL
 *
 * This is a 'source module' that adds a data source for the Location Taxonomize
 * module. It implements the Location Taxonomize functionality for the
 * Getlocations_fields module.
 */

/**
 * Implements hook_init().
 */
function getlocations_location_taxonomize_init() {
  module_load_include('inc', 'getlocations_location_taxonomize', 'getlocations_location_taxonomize');
}

/**
 * Implements hook_help().
 */
function getlocations_location_taxonomize_help($path, $arg) {
  switch ($path) {
    case 'admin/help#getlocations_location_taxonomize':
      $output = '<p>' . t('Provides getlocations_fields as a data source to the location_taxonomize module.') . '</p>';
      return $output;
  }
}

/**
 * Register this module as a source module for Location Taxonomize
 */
function getlocations_location_taxonomize_location_taxonomize_source() {
  return array(
    'getlocations_location_taxonomize' => 'Getlocations Fields Location Taxonomize',
  );
}

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

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

/**
 * Implements hook_form_field_ui_field_edit_form_alter().
 * Here we add a form element on the getlocations_fields widget settings form
 * to allow users to configure whether this field will be taxonomized
 *
 */
function getlocations_location_taxonomize_form_field_ui_field_edit_form_alter(&$form, $form_state, $form_id) {
  if ($form['#field']['type'] == 'getlocations_fields') {
    $settings = $form['#instance']['widget']['settings'];
    $enable = array(
      '#type' => 'checkbox',
      '#title' => t('Taxonomize locations from this field using Getlocations Fields 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 getlocations_location_taxonomize_field_widget_getlocations_fields_form_alter(&$element, &$form_state, $context) {

  // apply this only if we are in the right module and on a node-edit form
  if (_location_taxonomize_get_source() == LT_GETLOC_MODULE_ID && isset($context['form']['#node_edit_form'])) {

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

    // add a process function to getlocations_fields widget
    if ($taxonomize) {

      // notice avoidance
      if (!isset($element['#process'])) {
        $element['#process'] = array();
      }
      if (!in_array('getlocations_location_taxonomize_process_address', $element['#process'])) {
        $element['#process'][] = 'getlocations_location_taxonomize_process_address';
      }
    }
  }
}

/**
 * A #process callback function for the getlocations_fields 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 getlocations_location_taxonomize_process_address($element, &$form_state, &$form) {

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

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

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

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

      // Add the country name
      $delta = getlocations_location_taxonomize_fixup($delta);
      $items[] = $delta;
    }
  }

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

/**
 * Runs the bulk taxonomize operation
 */
function getlocations_location_taxonomize_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-getlocations_fields fields
    foreach ($fields as $field => $data) {
      if ($data['type'] != 'getlocations_fields') {
        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
getlocations_location_taxonomize_bulk_taxonomize_op Runs the bulk taxonomize operation
getlocations_location_taxonomize_element_submitted A submit handler for forms that contain an getlocations_fields widget
getlocations_location_taxonomize_field_widget_getlocations_fields_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
getlocations_location_taxonomize_field_widget_info_alter Implements hook_field_widget_info_alter(). This is used to add a setting to the getlocations_fields widget type that determines whether the field is taxonomized
getlocations_location_taxonomize_form_field_ui_field_edit_form_alter Implements hook_form_field_ui_field_edit_form_alter(). Here we add a form element on the getlocations_fields widget settings form to allow users to configure whether this field will be taxonomized
getlocations_location_taxonomize_help Implements hook_help().
getlocations_location_taxonomize_init Implements hook_init().
getlocations_location_taxonomize_location_taxonomize_source Register this module as a source module for Location Taxonomize
getlocations_location_taxonomize_process_address A #process callback function for the getlocations_fields 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.