You are here

function nodereferrer_referrers in NodeReferrer 6

Same name and namespace in other branches
  1. 5 nodereferrer.module \nodereferrer_referrers()
  2. 7 nodereferrer.module \nodereferrer_referrers()

Get an array of referrer nids, by node.type & field.type

Parameters

nid: the nid we want to find referres for

fieldnames: array of fieldnames to be checked for referrers

nodetypes: array of node types to be checked for referrers

translations: boolean if true, also return nodes that referrer to translations of the given node

3 calls to nodereferrer_referrers()
nodereferrer_field in ./nodereferrer.module
Implementation of hook_field().
nodereferrer_nodeapi in ./nodereferrer.module
Implementation of hook_nodeapi().
nodereferrer_referrers_with_translations in views/nodereferrer.views.inc
Given a node id, field names and content names, returns the list of nodes that refer to the given node. This takes translation settings into account

File

./nodereferrer.module, line 367
Defines a field type for backlinking referencing nodes.

Code

function nodereferrer_referrers($nid, $fieldnames = array(), $nodetypes = array(), $translations = 0, $order = 'DESC') {
  if ($nodetypes) {
    $filter_nodetypes = "AND n.type IN ('" . implode("', '", $nodetypes) . "')";
  }
  else {
    $filter_nodetypes = '';
  }
  $fields = content_fields();

  // Set default values of fieldnames.
  if (!count($fieldnames)) {
    $fieldnames = array_keys($fields);
  }

  // For Postgresql compatibility ordered fields should be in SELECT too.
  $select_field = '';
  switch ($order) {
    case 'TITLE_ASC':
      $order = 'n.title ASC';
      break;
    case 'TITLE_DESC':
      $order = 'n.title DESC';
      break;
    case 'ASC':
    case 'CREATED_ASC':
      $order = 'n.created ASC';
      $select_field = ', n.created';
      break;
    default:
    case 'DESC':
    case 'CREATED_DESC':
      $order = 'n.created DESC';
      $select_field = ', n.created';
      break;
  }
  $values = array();
  foreach ($fieldnames as $fieldname) {
    if ($fields[$fieldname]['type'] == 'nodereference') {
      $db_info = content_database_info($fields[$fieldname]);
      if ($translations) {
        $query = "SELECT       n.nid, n.vid, n.title {$select_field}\n                  FROM         {" . $db_info['table'] . "} nr\n                  INNER JOIN   {node} current_node ON current_node.nid = %d\n                  INNER JOIN   {node} n ON n.vid = nr.vid AND n.status = 1 " . $filter_nodetypes . "\n                  LEFT JOIN    {node} translations ON current_node.tnid > 0 AND translations.tnid = current_node.tnid\n                  WHERE        (current_node.tnid = 0 AND nr." . $db_info['columns']['nid']['column'] . " = current_node.nid)\n                               OR\n                               (current_node.tnid > 0 AND nr." . $db_info['columns']['nid']['column'] . " = translations.nid)\n                  ORDER BY     " . $order;
      }
      else {
        $query = "SELECT       n.nid, n.vid, n.title {$select_field}\n                  FROM         {" . $db_info['table'] . "} nr\n                  INNER JOIN   {node} n ON n.vid = nr.vid AND n.status = 1 " . $filter_nodetypes . "\n                  WHERE        nr." . $db_info['columns']['nid']['column'] . " = %d\n                  ORDER BY     " . $order;
      }
      $query = db_rewrite_sql($query);
      $result = db_query($query, $nid);
      while ($value = db_fetch_array($result)) {

        // avoid duplicate referrers by using nid as key
        $values[$value['nid']]['nid'] = $value['nid'];
        $values[$value['nid']]['vid'] = $value['vid'];
        $values[$value['nid']]['title'] = $value['title'];

        //Store the nodereference fieldnames in the nodereferrer data in the node,

        //so that it can be looked up without needing to load the referring node.
        $values[$value['nid']]['referrers'][$fieldname] = $fieldname;
      }
    }
  }
  return $values;
}