You are here

function _nat_sync_associations in Node Auto Term [NAT] 5

Same name and namespace in other branches
  1. 6.2 nat.admin.inc \_nat_sync_associations()
  2. 6 nat.admin.inc \_nat_sync_associations()
  3. 7.2 nat.admin.inc \_nat_sync_associations()
  4. 7 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.module
Process NAT sync form submissions.

File

./nat.module, line 685
NAT - node auto term - is a helper module that automatically creates a term using the same title as a node.

Code

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

    // This query can possibly be improved.
    $result = db_query("SELECT n.nid, n.type, n.title, nr.body FROM {node} n INNER JOIN {node_revisions} nr USING (vid) LEFT JOIN {nat} n1 ON (n.nid = n1.nid AND n1.vid = %d) LEFT JOIN {nat} n2 ON (n.nid = n2.nid AND n2.nid != n1.nid) WHERE n.type = '%s' AND n1.nid IS NULL", $association[1], $association[0]);
    while ($node = db_fetch_array($result)) {

      // Copying over the node body is optional.
      $body = isset($nat_config['body'][$node['type']]) ? $node['body'] : '';

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

      // 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,
  )));
}