You are here

function _nat_sync_associations in Node Auto Term [NAT] 7

Same name and namespace in other branches
  1. 5 nat.module \_nat_sync_associations()
  2. 6.2 nat.admin.inc \_nat_sync_associations()
  3. 6 nat.admin.inc \_nat_sync_associations()
  4. 7.2 nat.admin.inc \_nat_sync_associations()

Synchronize NAT node-term relationships. Create associated terms for node where missing.

Parameters

$associations: Associative array denoting the node-vocabulary pair that is to be synced.

1 call to _nat_sync_associations()
nat_sync_form_submit in ./nat.admin.inc
Process NAT sync form submissions.

File

./nat.admin.inc, line 316
NAT module administrative forms.

Code

function _nat_sync_associations($associations) {
  $counter = 0;
  foreach ($associations as $association) {
    $association = explode('|', $association);

    // This query can possibly be improved.
    $result = db_query("SELECT n.nid FROM {node} n LEFT JOIN {nat} n1 ON (n.nid = n1.nid AND n1.vid = :vid) LEFT JOIN {nat} n2 ON (n.nid = n2.nid AND n2.nid <> n1.nid) WHERE n.type = :type AND n1.nid IS NULL", array(
      ':vid' => $association[1],
      ':type' => $association[0],
    ));
    foreach ($result as $node) {

      // We need to execute a node_load in order to load field information.
      $node = node_load($node->nid);

      // Add node title as terms.
      $terms = _nat_add_terms($node, array(
        $association[1],
      ));

      // Save node-term association in the NAT table.
      _nat_save_association($node->nid, $terms);
      $counter++;
    }
  }
  drupal_set_message(t('NAT sync complete: %count nodes synced.', array(
    '%count' => $counter,
  )));
}