You are here

public function ViewsJsonQuery::parse in Views Json Source 8

Same name and namespace in other branches
  1. 1.x src/Plugin/views/query/ViewsJsonQuery.php \Drupal\views_json_source\Plugin\views\query\ViewsJsonQuery::parse()

Parse.

1 call to ViewsJsonQuery::parse()
ViewsJsonQuery::execute in src/Plugin/views/query/ViewsJsonQuery.php
Executes the query and fills the associated view object with according values.

File

src/Plugin/views/query/ViewsJsonQuery.php, line 324

Class

ViewsJsonQuery
Base query handler for views_json_source.

Namespace

Drupal\views_json_source\Plugin\views\query

Code

public function parse(ViewExecutable &$view, $data) {
  $ret = json_decode($data->contents, TRUE);
  if (!$ret) {
    return FALSE;
  }

  // Get rows.
  $ret = $this
    ->apath($this->options['row_apath'], $ret) ?? [];

  // Get group operator.
  $filter_group = $view->display_handler
    ->getOption('filter_groups');
  $group_conditional_operator = $filter_group['groups'][1] ?? "AND";

  // Filter.
  foreach ($ret as $k => $row) {
    $check = TRUE;
    foreach ($view->build_info['query'] as $filter) {

      // Filter only when value is present.
      if (!empty($filter[0])) {
        $l = $row[$filter[0]];
        $check = $this
          ->ops($filter[1], $l, $filter[2]);
        if ($group_conditional_operator === "AND") {

          // With AND condition.
          if (!$check) {
            break;
          }
        }
        elseif ($group_conditional_operator === "OR") {

          // With OR conditions.
          if ($check) {
            break;
          }
        }
      }
    }
    if (!$check) {
      unset($ret[$k]);
    }
  }
  try {
    if ($view->pager
      ->useCountQuery() || !empty($view->get_total_rows)) {

      // Hackish execute_count_query implementation.
      $view->pager->total_items = count($ret);
      if (!empty($view->pager->options['offset'])) {
        $view->pager->total_items -= $view->pager->options['offset'];
      }
      $view->pager
        ->updatePageInfo();
    }
    if (!empty($this->orderby)) {

      // Array reverse, because the most specific are first.
      foreach (array_reverse($this->orderby) as $orderby) {
        $this
          ->sort($ret, $orderby['field'], $orderby['order']);
      }
    }

    // Deal with offset & limit.
    $offset = !empty($this->offset) ? intval($this->offset) : 0;
    $limit = !empty($this->limit) ? intval($this->limit) : 0;
    $ret = $limit ? array_slice($ret, $offset, $limit) : array_slice($ret, $offset);
    $result = [];
    if ($this->options['single_payload']) {
      $result[] = $this
        ->parseRow(NULL, $ret, $ret);
    }
    else {
      foreach ($ret as $row) {
        $new_row = $this
          ->parseRow(NULL, $row, $row);
        $result[] = $new_row;
      }
    }
    foreach ($result as $row) {
      $view->result[] = new ResultRow($row);
    }

    // Re-index array.
    $index = 0;
    foreach ($view->result as &$row) {
      $row->index = $index++;
    }
    $view->total_rows = count($result);
    $view->pager
      ->postExecute($view->result);
    return TRUE;
  } catch (\Exception $e) {
    $view->result = [];
    if (!empty($view->live_preview)) {
      \Drupal::messenger()
        ->addMessage(time());
      \Drupal::messenger()
        ->addMessage($e
        ->getMessage(), 'error');
    }
    else {
      \Drupal::messenger()
        ->addMessage($e
        ->getMessage());
    }
  }
}