Index: modules/node_reference/node_reference.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/cck/modules/node_reference/node_reference.module,v
retrieving revision 1.16
diff -u -p -r1.16 node_reference.module
--- modules/node_reference/node_reference.module	10 Nov 2009 11:30:24 -0000	1.16
+++ modules/node_reference/node_reference.module	22 Nov 2009 18:38:13 -0000
@@ -126,7 +126,7 @@ function node_reference_field($op, $node
               $errors[$field['field_name']][$langcode][$delta][] = array(
                 'error' => 'valid_nid',
                 'message' => t("%name: this post can't be referenced.", array('%name' => t($field['widget']['label']))),
-              );              
+              );
             }
           }
         }
@@ -427,7 +427,7 @@ function node_reference_field_widget(&$f
  */
 function nodereference_field_widget_error($element, $error) {
   form_error($element['nid'], $error['message']);
-} 	  	 
+}
 
 /**
  * Value for a node_reference autocomplete element.
@@ -588,7 +588,8 @@ function node_reference_options_validate
 function node_reference_autocomplete_validate($element, &$form_state) {
   $field_name = $element['#field_name'];
   $bundle = $element['#bundle'];
-  $field = field_fields($field_name, $bundle);
+  // $field = field_fields($field_name, $bundle);
+  $field = field_info_fields($bundle, $field_name);
   $field_key  = $element['#columns'][0];
   $delta = $element['#delta'];
   $value = $element['#value'][$field_key];
@@ -689,48 +690,62 @@ function _node_reference_potential_refer
  * referenceable nodes defined by content types.
  */
 function _node_reference_potential_references_standard($field, $string = '', $match = 'contains', $ids = array(), $limit = NULL) {
-  $related_types = array();
-  $where = array();
-  $args = array();
 
-  if (is_array($field['settings']['referenceable_types'])) {
-    foreach (array_filter($field['settings']['referenceable_types']) as $related_type) {
-      $related_types[] = "n.type = '%s'";
-      $args[] = $related_type;
-    }
+  // Avoid useless work
+  if (!count($field['settings']['referenceable_types'])) {
+    return array();
   }
 
-  $where[] = implode(' OR ', $related_types);
+  $q = db_select('node', 'n');
+  $node_nid_alias   = $q->addField('n', 'nid');
+  $node_title_alias = $q->addField('n', 'title', 'node_title');
+  $node_type_alias  = $q->addField('n', 'type',  'node_type');
+  $q->addTag('node_access');
 
-  if (!count($related_types)) {
-    return array();
+  if (is_array($field['settings']['referenceable_types'])) {
+    $q->condition('n.type', $field['settings']['referenceable_types'], 'IN');
   }
 
   if ($string !== '') {
-    $match_operators = array(
-      'contains' => "LIKE '%%%s%%'",
-      'equals' => "= '%s'",
-      'starts_with' => "LIKE '%s%%'",
-    );
-    $where[] = 'n.title '. (isset($match_operators[$match]) ? $match_operators[$match] : $match_operators['contains']);
-    $args[] = $string;
+    $args = array();
+    switch ($match) {
+      case 'contains':
+        $title_clause = 'n.title LIKE :match';
+        $args['match'] = '%' . $string . '%';
+        break;
+
+      case 'starts_with':
+        $title_clause = 'n.title LIKE :match';
+        $args['match'] = $string . '%';
+        break;
+
+      case 'equals':
+      default: // no match type or incorrect match type: use "="
+        $title_clause = 'n.title = :match';
+        $args['match'] = $string;
+        break;
+    }
+    $q->where($title_clause, $args);
   }
   elseif ($ids) {
-    $where[] = 'n.nid IN (' . db_placeholders($ids) . ')';
-    $args = array_merge($args, $ids);
+    $q = $q->condition($node_nid_alias, $ids, 'IN', $ids);
+  }
+
+  $q->orderBy($node_title_alias)
+    ->orderBy($node_type_alias);
+
+  if ($limit) {
+    $q->range(0, $limit);
   }
 
-  $where_clause = $where ? 'WHERE ('. implode(') AND (', $where) .')' : '';
-  $sql = db_rewrite_sql("SELECT n.nid, n.title AS node_title, n.type AS node_type FROM {node} n $where_clause ORDER BY n.title, n.type");
-  $result = $limit ? db_query_range($sql, $args, 0, $limit) : db_query($sql, $args);
+  $result = $q->execute();
   $references = array();
-  while ($node = db_fetch_object($result)) {
+  foreach ($result->fetchAll() as $node) {
     $references[$node->nid] = array(
-      'title' => $node->node_title,
+      'title'    => $node->node_title,
       'rendered' => check_plain($node->node_title),
     );
   }
-
   return $references;
 }
 
@@ -738,8 +753,9 @@ function _node_reference_potential_refer
  * Menu callback; Retrieve a pipe delimited string of autocomplete suggestions for existing users
  */
 function node_reference_autocomplete($field_name, $string = '') {
-  $fields = content_fields();
+  $fields = field_info_fields(); // content_fields();
   $field = $fields[$field_name];
+
   $match = isset($field['widget']['autocomplete_match']) ? $field['widget']['autocomplete_match'] : 'contains';
   $matches = array();
 
@@ -748,7 +764,7 @@ function node_reference_autocomplete($fi
     // Add a class wrapper for a few required CSS overrides.
     $matches[$row['title'] ." [nid:$id]"] = '<div class="reference-autocomplete">'. $row['rendered'] . '</div>';
   }
-  drupal_json($matches);
+  drupal_json_output($matches);
 }
 
 /**
@@ -828,4 +844,4 @@ function node_reference_field_settings_f
     '#disabled' => $has_data,
   );
   return $form;
-}
\ No newline at end of file
+}
