You are here

function lineage_update_term_r in Taxonomy Lineage 7

Same name and namespace in other branches
  1. 5 lineage.module \lineage_update_term_r()
  2. 6 lineage.module \lineage_update_term_r()
1 call to lineage_update_term_r()
lineage_update_term in ./lineage.module

File

./lineage.module, line 221
lineage.module Module code for taxonomy term hierarchy lineage.

Code

function lineage_update_term_r($term, $base, $tids = array()) {

  // Extend the base.
  $base['base'] .= lineage_string($term);

  // Table wants NO NULL, so just make sure.
  $base['depth'] = intval($base['depth']);

  // Update the hierarchy for the current tid.
  db_delete('taxonomy_term_lineage')
    ->condition('tid', $term->tid)
    ->execute();
  db_insert('taxonomy_term_lineage')
    ->fields(array(
    'tid' => $term->tid,
    'lineage' => trim($base['base']),
    'depth' => $base['depth'],
  ))
    ->execute();
  $base['depth']++;

  // Mark that we've done this one to prevent looping.
  $tids[$term->tid] = TRUE;

  // Update all the children.
  $query = db_select('taxonomy_term_hierarchy', 'th');
  $query
    ->join('taxonomy_term_data', 'td', 'td.tid = th.tid');
  $query
    ->fields('td', array(
    'tid',
    'name',
    'weight',
    'vid',
  ));
  $query
    ->condition('th.parent', $term->tid, '=');
  $result = $query
    ->execute();
  foreach ($result as $child) {

    // loop protection, just in case.
    if (!isset($tids[$child->tid])) {
      $tids = lineage_update_term_r($child, $base, $tids);
    }
  }
  return $tids;
}