You are here

class ProtectedPagesStorage in Protected Pages 8

Defines the protected page storage service.

Hierarchy

Expanded class hierarchy of ProtectedPagesStorage

7 files declare their use of ProtectedPagesStorage
ProtectedPagesAddForm.php in src/Form/ProtectedPagesAddForm.php
ProtectedPagesController.php in src/Controller/ProtectedPagesController.php
ProtectedPagesDeleteConfirmForm.php in src/Form/ProtectedPagesDeleteConfirmForm.php
ProtectedPagesEditForm.php in src/Form/ProtectedPagesEditForm.php
ProtectedPagesLoginForm.php in src/Form/ProtectedPagesLoginForm.php

... See full list

1 string reference to 'ProtectedPagesStorage'
protected_pages.services.yml in ./protected_pages.services.yml
protected_pages.services.yml
1 service uses ProtectedPagesStorage
protected_pages.storage in ./protected_pages.services.yml
Drupal\protected_pages\ProtectedPagesStorage

File

src/ProtectedPagesStorage.php, line 11

Namespace

Drupal\protected_pages
View source
class ProtectedPagesStorage {

  /**
   * The database connection to use.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * Constructs a new protected page storage service.
   *
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection to use.
   */
  public function __construct(Connection $connection) {
    $this->connection = $connection;
  }

  /**
   * Insert data into protected pages table.
   *
   * @param array $page_data
   *   An array containing all values.
   *
   * @return int
   *   The protected page id.
   */
  public function insertProtectedPage(array $page_data) {
    $query = $this->connection
      ->insert('protected_pages')
      ->fields([
      'password',
      'path',
    ])
      ->values($page_data);
    $pid = $query
      ->execute();
    return $pid;
  }

  /**
   * Updates data into protected pages table.
   *
   * @param array $page_data
   *   An array containing all values.
   * @param int $pid
   *   The protected page id.
   */
  public function updateProtectedPage(array $page_data, $pid) {
    $this->connection
      ->update('protected_pages')
      ->fields($page_data)
      ->condition('pid', $pid)
      ->execute();
  }

  /**
   * Delete protected page from database.
   *
   * @param int $pid
   *   The protected page id.
   */
  public function deleteProtectedPage($pid) {
    $this->connection
      ->delete('protected_pages')
      ->condition('pid', $pid)
      ->execute();
  }

  /**
   * Fetches protected page records from database.
   *
   * @param array $fields
   *   An array containing all fields.
   * @param array $query_conditions
   *   An array containing all conditions.
   * @param bool $get_single_field
   *   Boolean to check if functions needs to return one or multiple fields.
   *
   * @return mixed
   *   A single value or an array of results.
   */
  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;
  }

  /**
   * Fetches all protected pages records from database.
   */
  public function loadAllProtectedPages() {
    $results = $this->connection
      ->select('protected_pages', 'p')
      ->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
      ->fields('p')
      ->orderBy('p.pid', 'DESC')
      ->limit(20)
      ->execute()
      ->fetchAll();
    return $results;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ProtectedPagesStorage::$connection protected property The database connection to use.
ProtectedPagesStorage::deleteProtectedPage public function Delete protected page from database.
ProtectedPagesStorage::insertProtectedPage public function Insert data into protected pages table.
ProtectedPagesStorage::loadAllProtectedPages public function Fetches all protected pages records from database.
ProtectedPagesStorage::loadProtectedPage public function Fetches protected page records from database.
ProtectedPagesStorage::updateProtectedPage public function Updates data into protected pages table.
ProtectedPagesStorage::__construct public function Constructs a new protected page storage service.