You are here

function location_taxonomize_process_loc in Location Taxonomize 7

Determines if a location needs to be taxonomized. If so, it saves the appropriate terms Returns the number of terms added

Parameters

$obj - a location object:

2 calls to location_taxonomize_process_loc()
location_taxonomize_bulk_taxonomize_op in ./location_taxonomize.module
Runs the bulk taxonomize operation
location_taxonomize_locationapi in ./location_taxonomize.module
Implements hook_locationapi(). Used to save terms when locations are saved.

File

./location_taxonomize.module, line 126

Code

function location_taxonomize_process_loc($obj) {

  // get settings
  $settings = variable_get('location_taxonomize_settings');
  $vid = variable_get('location_taxonomize_vid');
  $hierarchy = _location_taxonomize_get_hierarchy();

  // holds the number of newly saved terms
  $saved = 0;

  // make sure the province_name and country_name fields are there and updated
  $location = _fix_loc_tmp($obj);
  $tids = array();

  // this loops through hierarchy levels and saves terms if necessary
  for ($hlevel = 0; $hlevel < count($hierarchy); $hlevel++) {
    $hlevel_name = $hierarchy[$hlevel];

    // set the term name
    $name = _location_taxonomize_create_term_name($hlevel_name, $location, $settings);

    // find if the term exists already or not
    if ($hlevel == 0) {
      $parentid = -1;
    }
    else {
      $parentid = $tids[$hlevel - 1];
    }
    $findterm = _find_term($name, $hlevel, $parentid);

    // save if necessary
    if (!$findterm) {
      $term = (object) array(
        'name' => $name,
        'vid' => $vid,
      );

      // add longname if necessary
      if ($settings['longname_enable']) {
        _location_taxonomy_add_longname($term, $hlevel, $location);
      }

      // set term parent
      if ($hlevel == 0) {
        $term->parent = array(
          0,
        );
      }
      else {
        $term->parent = array(
          $parentid,
        );
      }
      taxonomy_term_save($term);
      $tids[] = $term->tid;
      $saved++;
    }
    elseif ($findterm->tid) {
      $tids[] = $findterm->tid;
    }
  }
  return $saved;
}