You are here

public function ProtectedPagesStorage::loadProtectedPage in Protected Pages 8

Fetches protected page records from database.

Parameters

array $fields: An array containing all fields.

array $query_conditions: An array containing all conditions.

bool $get_single_field: Boolean to check if functions needs to return one or multiple fields.

Return value

mixed A single value or an array of results.

File

src/ProtectedPagesStorage.php, line 87

Class

ProtectedPagesStorage
Defines the protected page storage service.

Namespace

Drupal\protected_pages

Code

public function loadProtectedPage(array $fields = [], array $query_conditions = [], $get_single_field = FALSE) {
  $select = $this->connection
    ->select('protected_pages');
  if (count($fields)) {
    $select
      ->fields('protected_pages', $fields);
  }
  else {
    $select
      ->fields('protected_pages');
  }
  if (count($query_conditions)) {
    if (isset($query_conditions['or']) && count($query_conditions['or'])) {
      $conditions = new Condition('OR');
      foreach ($query_conditions['or'] as $condition) {
        $conditions
          ->condition($condition['field'], $condition['value'], $condition['operator']);
      }
      $select
        ->condition($conditions);
    }
    if (isset($query_conditions['and']) && count($query_conditions['and'])) {
      foreach ($query_conditions['and'] as $condition) {
        $select
          ->condition($condition['field'], $condition['value'], $condition['operator']);
      }
    }
    if (isset($query_conditions['general']) && count($query_conditions['general'])) {
      foreach ($query_conditions['general'] as $condition) {
        $select
          ->condition($condition['field'], $condition['value'], $condition['operator']);
      }
    }
  }
  if ($get_single_field) {
    $select
      ->range(0, 1);
    $result = $select
      ->execute()
      ->fetchField();
  }
  else {
    $result = $select
      ->execute()
      ->fetchAll();
  }
  return $result;
}