You are here

function location_taxonomize_process_item in Location Taxonomize 7.2

Determines if a location needs to be taxonomized. If so, it saves the appropriate terms Returns an array containing two items: the first is the number of new terms saved, and the second is an array of all the terms that correspond to this object, including ones that were added and ones that already existed

Parameters

$obj - a location object:

2 calls to location_taxonomize_process_item()
location_taxonomize_taxonomize in ./location_taxonomize.module
location_taxonomize_taxonomize_bulk in ./location_taxonomize.module

File

./location_taxonomize.module, line 180

Code

function location_taxonomize_process_item($obj) {

  // make sure we are enabled
  if (!location_taxonomize_enabled()) {
    return NULL;
  }

  // get settings
  $settings = variable_get('location_taxonomize_settings');
  $vid = variable_get('location_taxonomize_vid');
  $hierarchy = _location_taxonomize_get_hierarchy();
  $longname_main = $settings['longname_enable'] && $settings['longname']['main'];

  // holds the number of newly saved terms
  $saved = 0;
  $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
    if ($longname_main) {
      $name = location_taxonomize_make_longname($hlevel, $obj);
      $longname = $name;
    }
    else {
      $name = _location_taxonomize_create_term_name($hlevel_name, $obj, $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'] && !$longname_main) {
        _location_taxonomy_add_longname($term, $hlevel, $obj);
      }

      // 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 array(
    'saved' => $saved,
    'tids' => $tids,
  );
}