You are here

function views_json_query_plugin_query_json::parse in Views JSON Query 7

Parse.

1 call to views_json_query_plugin_query_json::parse()
views_json_query_plugin_query_json::execute in ./views_json_query_plugin_query_json.inc
Execute.

File

./views_json_query_plugin_query_json.inc, line 271
Query plugin for views_json_query.

Class

views_json_query_plugin_query_json
@file Query plugin for views_json_query.

Code

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

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

  // Filter.
  foreach ($rows as $k => $row) {
    $check = TRUE;
    foreach ($view->build_info['query'] as $filter) {
      $l = is_object($row) ? $row->{$filter[0]} : $row[$filter[0]];
      $check = $this
        ->ops($filter[1], $l, $filter[2]);
      if (!$check) {
        break;
      }
    }
    if (!$check) {
      unset($rows[$k]);
    }
  }
  try {
    if ($this->pager
      ->use_count_query() || !empty($view->get_total_rows)) {
      if (!empty($this->options['total_items_apath'])) {
        $this->pager->total_items = $this
          ->apath($this->options['total_items_apath'], $ret);
      }
      else {

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

    // Deal with offset & limit.
    $offset = !empty($this->offset) ? intval($this->offset) : 0;
    $limit = !empty($this->limit) ? intval($this->limit) : 0;
    $rows = $limit ? array_slice($rows, $offset, $limit) : array_slice($rows, $offset);
    $result = array();
    foreach ($rows as $row) {
      $new_row = new stdClass();
      $new_row = $this
        ->parse_row(NULL, $row, $row);
      $result[] = (object) $new_row;
    }
    if (!empty($this->orderby)) {

      // Array reverse, because the most specific are first.
      foreach (array_reverse($this->orderby) as $orderby) {
        $orderby
          ->sort($result);
      }
    }
    $view->result = $result;
    $view->total_rows = count($result);
    $this->pager
      ->post_execute($view->result);
    return TRUE;
  } catch (Exception $e) {
    $view->result = array();
    if (!empty($view->live_preview)) {
      drupal_set_message(time());
      drupal_set_message($e
        ->getMessage(), 'error');
    }
    else {
      debug($e
        ->getMessage(), 'Views Json Backend');
    }
  }
}