You are here

function node_accessibility_load_problems in Node Accessibility 7

Loads the nodes problem data.

Parameters

array $conditions: (optional) An array with the following possible keys:

  • 'id' The unique id representing a specific problem.
  • 'nid' the node id.
  • 'vid' the node revision id.
  • 'test_id' a numeric value representing the id of the test the problem is associated with.
  • 'line' a numeric value representing the line number in which a problem applies to.
  • 'live_only' a boolean that specifies whether or not to restrict loading of problems to live content (active revision). (defaults to FALSE)
  • 'unlive_only' a boolean that specifies whether or not to restrict loading of problems to non-live content (active revision). (defaults to FALSE)
  • 'published_only' a boolean that specifies whether or not to restrict loading of problems to published content (active revision). (defaults to FALSE)
  • 'unpublished_only' a boolean that specifies whether or not to restrict loading of problems to unpublished content (active revision). (defaults to FALSE)
  • 'sort_by' the name of a column to sort by.
  • 'sort_order' the order in which to sort by ('asc' or 'desc').
  • 'node_columns' a boolean that specifies whether or not to load the node column fields in addition to the node accessibility problems fields. (defaults to FALSE)

string|null $keyed: (optional) A string matching one of the following: 'id' When this is NULL, the default behavior is to return the array exactly as it was returned by the database call. When this is a valid string, the key names of the returned array will use the specified key name.

Return value

array An array of database results.

2 calls to node_accessibility_load_problems()
node_accessibility_restructure_results in ./node_accessibility.module
This restructures an array of problems as returned from database calls.
node_accessibility_save_problem in ./node_accessibility.module
This stores a validation problem for a given node to the database.

File

./node_accessibility.module, line 821
Module file for the node accessibility project.

Code

function node_accessibility_load_problems($conditions = array(), $keyed = NULL) {
  if (!is_array($conditions)) {
    if (class_exists('cf_error')) {
      cf_error::invalid_array('conditions');
    }
    return array();
  }
  $query = db_select('node_accessibility_problems', 'nap');
  $query
    ->fields('nap');
  $sort_by = 'nap.nid';
  $sort_order = 'ASC';
  if (isset($conditions['sort_order'])) {
    switch ($conditions['sort_order']) {
      case 'ASC':
      case 'DESC':
        $sort_order = $conditions['sort_order'];
        break;
    }
  }
  if (isset($conditions['sort_by']) && is_string($conditions['sort_by'])) {
    switch ($conditions['sort_by']) {
      case 'id':
      case 'nid':
      case 'vid':
      case 'test_id':
      case 'line':
      case 'element':
        $sort_by = 'nap.' . $conditions['sort_by'];
        break;
      default:
        $sort_by = $conditions['sort_by'];
        break;
    }
  }
  $query
    ->orderBy($sort_by, $sort_order);
  $and = NULL;
  $joined = FALSE;
  if (isset($conditions['live_only']) && is_bool($conditions['live_only'])) {
    $query
      ->innerjoin('node', 'n', 'nap.vid = n.vid');
    $joined = TRUE;
  }
  else {
    if (isset($conditions['unlive_only']) && is_bool($conditions['unlive_only'])) {
      $query
        ->innerjoin('node', 'n', 'nap.nid = n.nid');
      $joined = TRUE;
      $and = db_and();
      $and
        ->where('NOT nap.vid = n.vid');
    }
  }
  if (isset($conditions['published_only']) && is_bool($conditions['published_only'])) {
    if (!$joined) {
      $query
        ->innerjoin('node', 'n', 'nap.nid = n.nid');
      $joined = TRUE;
    }
    if (is_null($and)) {
      $and = db_and();
    }
    $and
      ->condition('n.status', 1, '=');
  }
  else {
    if (isset($conditions['unpublished_only']) && is_bool($conditions['unpublished_only'])) {
      if (!$joined) {
        $query
          ->innerjoin('node', 'n', 'nap.nid = n.nid');
        $joined = TRUE;
      }
      if (is_null($and)) {
        $and = db_and();
      }
      $and
        ->condition('n.status', 0, '=');
    }
  }
  if (isset($conditions['id']) && is_numeric($conditions['id'])) {
    if (is_null($and)) {
      $and = db_and();
    }
    $and
      ->condition('nap.id', $conditions['id'], '=');
  }
  if (isset($conditions['nid']) && is_numeric($conditions['nid'])) {
    if (is_null($and)) {
      $and = db_and();
    }
    $and
      ->condition('nap.nid', $conditions['nid'], '=');
  }
  if (isset($conditions['vid']) && is_numeric($conditions['vid'])) {
    if (is_null($and)) {
      $and = db_and();
    }
    $and
      ->condition('nap.vid', $conditions['vid'], '=');
  }
  if (isset($conditions['test_id']) && is_numeric($conditions['test_id'])) {
    if (is_null($and)) {
      $and = db_and();
    }
    $and
      ->condition('nap.test_id', $conditions['test_id'], '=');
  }
  if (!empty($conditions['line'])) {
    if (is_null($and)) {
      $and = db_and();
    }
    $and
      ->condition('nap.line', $conditions['line'], '=');
  }
  if (is_object($and)) {
    $query
      ->condition($and);
  }
  if (isset($conditions['node_columns']) && is_bool($conditions['node_columns'])) {
    if (!$joined) {
      $query
        ->innerjoin('node', 'n', 'nap.nid = n.nid');
      $joined = TRUE;
    }
    $query
      ->fields('n');
  }
  if ($keyed === 'id') {
    $results = array();
    try {
      $records = $query
        ->execute();
    } catch (Exception $e) {
      if (class_exists('cf_error')) {
        cf_error::on_query_execution($e);
      }
      return array();
    }
    foreach ($records as $record) {
      if (!is_object($record)) {
        continue;
      }
      $results[$record->{$keyed}] = $record;
    }
    return $results;
  }
  try {
    return $query
      ->execute()
      ->fetchAll();
  } catch (Exception $e) {
    if (class_exists('cf_error')) {
      cf_error::on_query_execution($e);
    }
  }
  return array();
}