You are here

function node_tools_get_nodes in Module Grants 6.4

Same name and namespace in other branches
  1. 6.3 node_tools/node_tools.module \node_tools_get_nodes()

Retrieve a list of nodes or revisions accessible to the logged-in user via the supplied operation.

@todo Allow paging, improve performance

Parameters

$op: Operation, one of 'view', 'update' or 'delete'

$is_published: 1 to return only published content 0 to return only content that isn't published -1 (default) no filter, return content regardles of publication status

$creator_uid: Only return content created by the user with the supplied id. Defaults to -1, which means don't care who the creator is.

$modifier_uid: Only return content last modified by the user with the supplied id. Defaults to -1, which means don't care who last modifed the node.

$is_moderated: TRUE to return only content of types that are subject to moderation FALSE to return only content that isn't subject to moderation -1 (default) no filter, return content regardles of moderation flag

$is_pending: Boolean indicating whether only nodes pending publication should be returned; a pending node is defined as a node that has a revision newer than the current OR a node with a single revision that is not published.

$max: Maximum number of nodes to be returned, defaults to 1000

$order_by_override: "ORDER BY ..." clause to be added, defaults to "timestamp DESC".

Return value

An array of node objects each containing nid, content type, published flag, creator-id, title+vid+modifier-id+timestamp of the current revision, plus taxonomy term(s) and workflow state, if these modules are installed and enabled.

1 call to node_tools_get_nodes()
module_grants_monitor_accessible_content_summary in module_grants_monitor/module_grants_monitor.pages.inc
Return as a themed table a content summary of the site filtered by the access rights of the logged-in user.

File

node_tools/node_tools.module, line 133
Generic reusable functions involving node objects.

Code

function node_tools_get_nodes($op, $is_published = -1, $creator_uid = -1, $modifier_uid = -1, $is_moderated = -1, $is_pending = FALSE, $max = 1000, $order_by_override = NULL) {

  //drupal_set_message("op='$op', is_published='$is_published', creator=$creator_uid, modifier=$modifier_uid, is_moderated='$is_moderated', is_pending='$is_pending', max=$max, show_msg='$show_message', order=$order_by_override", 'warning');
  $sql_select = 'SELECT n.nid, r.vid, n.uid AS creator_uid, r.uid, n.type, n.status, r.title, r.timestamp';

  // Join on current revision (vid) except when looking for pending revisions
  $sql_from = ' FROM {node} n INNER JOIN {node_revisions} r ' . ($is_pending ? 'ON n.nid=r.nid' : 'ON n.vid=r.vid');
  $sql_where = $is_published < 0 ? '' : " WHERE n.status={$is_published}";
  if ($creator_uid >= 0) {
    $sql_where = empty($sql_where) ? " WHERE n.uid={$creator_uid}" : $sql_where . " AND n.uid={$creator_uid}";
  }
  if ($modifier_uid >= 0) {
    $sql_where = empty($sql_where) ? " WHERE r.uid={$modifier_uid}" : $sql_where . " AND r.uid={$modifier_uid}";
  }
  if ($is_pending) {
    $sql_where = empty($sql_where) ? ' WHERE' : $sql_where . ' AND';
    $sql_where .= ' (r.vid>n.vid OR (n.status=0 AND (SELECT COUNT(vid) FROM {node_revisions} WHERE nid=n.nid)=1))';
  }
  $sql_order = " ORDER BY " . (empty($order_by_override) ? _node_tools_extract_order_clause_from_URI() : $order_by_override);
  $include_taxonomy_terms = module_exists('taxonomy') && count(taxonomy_get_vocabularies()) > 0 && variable_get("show_taxonomy_terms", TRUE);
  $include_workflow_state = module_exists('workflow') && count(workflow_get_all()) > 0;
  if ($include_taxonomy_terms) {
    $sql_select .= ', td.name AS term';
    $sql_from .= ' LEFT JOIN {term_node} tn ON r.vid=tn.vid LEFT JOIN {term_data} td ON tn.tid=td.tid';
  }
  if ($include_workflow_state) {
    $sql_select .= ', ws.state';
    $sql_from .= ' LEFT JOIN {workflow_node} wn ON wn.nid=n.nid LEFT JOIN {workflow_states} ws ON wn.sid=ws.sid';
  }
  $sql = $sql_select . $sql_from . $sql_where . $sql_order;

  //drupal_set_message($sql);
  $node_query_result = db_query_range($sql, 0, $max);
  $nodes = array();
  while ($node = db_fetch_object($node_query_result)) {
    $filter = $is_moderated < 0 || $is_moderated == node_tools_content_is_moderated($node->type);
    $access = module_exists('module_grants') ? module_grants_node_access($op, $node) : node_access($op, $node);
    if ($filter && $access) {
      if (empty($nodes[$node->nid])) {
        $nodes[$node->nid] = $node;
      }
      elseif ($include_taxonomy_terms && !empty($node->term)) {

        // When $is_pending==TRUE more dan one revision may be returned, so
        // need to check for duplicate terms.
        $existing_node = $nodes[$node->nid];
        if (strpos($existing_node->term, $node->term) === FALSE) {

          // Bit of a quick & dirty -- goes wrong if a term is substr of another
          $existing_node->term .= "/{$node->term}";
        }
      }
    }
  }
  return $nodes;
}