You are here

public static function ProblemsStorage::load_problems in Node Accessibility 8

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.
  • 'test_severity' a numeric value representing the severity of the problem.
  • '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.

3 calls to ProblemsStorage::load_problems()
ProblemsStorage::restructure_results in src/ProblemsStorage.php
This restructures an array of problems as returned from database calls.
ProblemsStorage::save_problem in src/ProblemsStorage.php
This stores a validation problem for a given node to the database.
ValidateFormBase::buildForm in src/Form/ValidateFormBase.php
Form constructor.

File

src/ProblemsStorage.php, line 191

Class

ProblemsStorage
Class DatabaseStorage.

Namespace

Drupal\node_accessibility

Code

public static function load_problems($conditions = [], $keyed = NULL) {
  if (!is_array($conditions)) {
    return [];
  }
  $query = \Drupal::database()
    ->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 'test_severity':
      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 = new Condition('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 = new Condition('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 = new Condition('AND');
      }
      $and
        ->condition('n.status', 0, '=');
    }
  }
  if (isset($conditions['id']) && is_numeric($conditions['id'])) {
    if (is_null($and)) {
      $and = new Condition('AND');
    }
    $and
      ->condition('nap.id', $conditions['id'], '=');
  }
  if (isset($conditions['nid']) && is_numeric($conditions['nid'])) {
    if (is_null($and)) {
      $and = new Condition('AND');
    }
    $and
      ->condition('nap.nid', $conditions['nid'], '=');
  }
  if (isset($conditions['vid']) && is_numeric($conditions['vid'])) {
    if (is_null($and)) {
      $and = new Condition('AND');
    }
    $and
      ->condition('nap.vid', $conditions['vid'], '=');
  }
  if (isset($conditions['test_id']) && is_numeric($conditions['test_id'])) {
    if (is_null($and)) {
      $and = new Condition('AND');
    }
    $and
      ->condition('nap.test_id', $conditions['test_id'], '=');
  }
  if (isset($conditions['test_severity']) && is_numeric($conditions['test_severity'])) {
    if (is_null($and)) {
      $and = new Condition('AND');
    }
    $and
      ->condition('nap.test_severity', $conditions['test_severity'], '=');
  }
  if (!empty($conditions['line'])) {
    if (is_null($and)) {
      $and = new Condition('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 = [];
    try {
      $records = $query
        ->execute();
    } catch (Exception $e) {
      \Drupal::logger('node_accessibility')
        ->error("Failed to load problems.");
      return [];
    } catch (Error $e) {
      \Drupal::logger('node_accessibility')
        ->error("Failed to load problems.");
      return [];
    }
    foreach ($records as $record) {
      if (!is_object($record)) {
        continue;
      }
      $results[$record->{$keyed}] = $record;
    }
    return $results;
  }
  try {
    return $query
      ->execute()
      ->fetchAll();
  } catch (Exception $e) {
    \Drupal::logger('node_accessibility')
      ->error("Failed to load problems.");
  } catch (Error $e) {
    \Drupal::logger('node_accessibility')
      ->error("Failed to load problems.");
  }
  return [];
}