You are here

protected function BrightcoveVideoSearch::queryVideos in Brightcove Video Connect 3.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/Search/BrightcoveVideoSearch.php \Drupal\brightcove\Plugin\Search\BrightcoveVideoSearch::queryVideos()

Query the matching video entities from the database.

Return value

array An array of matching video entities.

1 call to BrightcoveVideoSearch::queryVideos()
BrightcoveVideoSearch::execute in src/Plugin/Search/BrightcoveVideoSearch.php
Executes the search.

File

src/Plugin/Search/BrightcoveVideoSearch.php, line 175

Class

BrightcoveVideoSearch
Executes a keyword search for videos against the {brightcove_video} table.

Namespace

Drupal\brightcove\Plugin\Search

Code

protected function queryVideos() {

  // Get query from the storage.
  $query = $this->brightcoveVideoStorage
    ->getQuery();

  // Escape for LIKE matching.
  $keys = $this->database
    ->escapeLike($this->keywords);

  // Replace wildcards with MySQL/PostgreSQL wildcards.
  $keys = preg_replace('!\\*+!', '%', $keys);
  $like = "%{$keys}%";
  $query
    ->condition($query
    ->orConditionGroup()
    ->condition('name', $like, 'LIKE')
    ->condition('description', $like, 'LIKE')
    ->condition('long_description', $like, 'LIKE')
    ->condition('related_link__title', $like, 'LIKE'));

  // Restrict user's access based on video status.
  // If the user cannot view the published nor the unpublished videos then the
  // user would get an access denied to the page so this case shouldn't be
  // checked here.
  $statuses = [];
  if ($this->currentUser
    ->hasPermission('view published brightcove videos')) {
    $statuses[] = BrightcoveVideoInterface::PUBLISHED;
  }
  if ($this->currentUser
    ->hasPermission('view unpublished brightcove videos')) {
    $statuses[] = BrightcoveVideoInterface::NOT_PUBLISHED;
  }

  // Sanity condition, SQL query doesn't like empty lists so add a NULL value
  // if the user does not have either of the required permissions, this should
  // never happen though.
  if (empty($statuses)) {
    $statuses[] = NULL;
  }
  $query
    ->condition('status', $statuses, 'IN');
  $ids = $query
    ->sort('created', 'DESC')
    ->pager(static::RESULT_LIMIT)
    ->execute();
  return $this->brightcoveVideoStorage
    ->loadMultiple($ids);
}