You are here

function gmap_taxonomy_reassign_marker in GMap Module 7.2

Same name and namespace in other branches
  1. 5 gmap_taxonomy.module \gmap_taxonomy_reassign_marker()
  2. 6.2 gmap_taxonomy.module \gmap_taxonomy_reassign_marker()
  3. 6 gmap_taxonomy.module \gmap_taxonomy_reassign_marker()
  4. 7 gmap_taxonomy.module \gmap_taxonomy_reassign_marker()

Reassign markers associated with a term that's going away.

@todo move this to GmapTaxonomyCRUDi class

2 calls to gmap_taxonomy_reassign_marker()
gmap_taxonomy_taxonomy_term_delete in ./gmap_taxonomy.module
Implements hook_taxonomy_term_delete().
gmap_taxonomy_taxonomy_term_update in ./gmap_taxonomy.module
Implements hook_taxonomy_term_update().

File

./gmap_taxonomy.module, line 341
GMap Taxonomy Markers

Code

function gmap_taxonomy_reassign_marker($tid, $deletion = FALSE) {
  $nids = array();
  if ($deletion) {
    $result = db_query('SELECT nid FROM {gmap_taxonomy_node} WHERE tid = :tid', array(
      ':tid' => $tid,
    ));
    foreach ($result as $node) {
      $nids[] = $node->nid;
    }
  }
  else {
    $result = db_query('SELECT nid FROM {taxonomy_index} WHERE tid = :tid', array(
      ':tid' => $tid,
    ));
    foreach ($result as $node) {
      $nids[] = $node->nid;
    }
  }
  foreach ($nids as $nid) {
    $markers = db_query('SELECT t.tid, gt.marker FROM {taxonomy_index} r INNER JOIN {gmap_taxonomy_term} gt ON r.tid = gt.tid INNER JOIN {taxonomy_term_data} t ON r.tid = t.tid INNER JOIN {taxonomy_vocabulary} v ON t.vid = v.vid WHERE r.nid = :nid ORDER BY v.weight DESC, t.weight DESC, t.name DESC', array(
      ':nid' => $nid,
    ));
    if ($marker = $markers
      ->fetchObject()) {

      // Fallback found.
      db_update('gmap_taxonomy_node')
        ->fields(array(
        'tid' => $marker->tid,
        'marker' => $marker->marker,
      ))
        ->condition('nid', $nid)
        ->execute();
    }
    else {

      // No replacement marker, delete the row.
      db_delete('gmap_taxonomy_node')
        ->condition('nid', $nid)
        ->execute();
    }
  }

  // Clear cache to keep data updated.
  cache_clear_all('gmap_taxonomy_markers_data', 'cache');
}