You are here

function services_views_retrieve in Services Views 6

Same name and namespace in other branches
  1. 7 services_views.resource.inc \services_views_retrieve()

Callback for retrieving views resources.

Parameters

$view_name: String. The views name.

$display_id: String (optional). The views display name.

$offset: Integer (optional). An offset integer for paging.

$limit: Integer (optional). A limit integer for paging.

$args: Array (optional). A list of arguments to pass to the view.

$return_type: String (optional). Whether to return the raw data results (FALSE), the entire views object ('view') or themed results ('theme').

$args: Array (optional). A list of exposed filters to pass to the view.

Return value

Array. The views return.

1 string reference to 'services_views_retrieve'
services_views_services_resources in ./services_views.module
Implementation of hook_services_resources().

File

./services_views.resource.inc, line 28
Callbacks for services module resource hooks.

Code

function services_views_retrieve($view_name, $display_id = 'default', $args = array(), $offset = 0, $limit = 10, $return_type = FALSE, $filters = array()) {
  $result = array();
  $view = views_get_view($view_name);

  // Put all arguments and filters and then execute.
  $view
    ->set_arguments($args, FALSE);
  $view
    ->set_exposed_input($filters);
  $view
    ->set_offset($offset);

  // Support for Views 2
  if (method_exists($view, 'set_use_pager')) {

    // If offset is set we can't have a user pager.
    if (empty($offset)) {
      $view
        ->set_use_pager(TRUE);
      $view
        ->set_items_per_page($limit);
    }
    else {

      // Disable the user pager.
      $view
        ->set_use_pager(FALSE);
    }
  }
  else {
    $view
      ->set_items_per_page($limit);
  }
  if (empty($return_type)) {
    $view
      ->set_display($display_id);
    $view
      ->pre_execute();
    $view
      ->execute();
    $result = $view->result;
    $row_plugin = $view->display_handler
      ->get_option('row_plugin');

    // If row plugin is node, then we should load each node
    if ($row_plugin == 'node') {
      $nodes = array();
      foreach ($view->result as $row) {
        $nodes[] = services_node_load(node_load($row->nid));
      }
      $result = $nodes;
    }
    elseif ($row_plugin == 'fields') {
      $items = array();
      foreach ($view->result as $row) {
        $item = new stdClass();
        foreach ($view->field as $field_id => $field) {
          if (!empty($field->options['label'])) {
            $field_label = strtolower($field->options['label']);
          }
          elseif ($field->field_alias) {
            $field_label = $field->field_alias;
          }
          else {
            $field_label = $field_id;
          }
          $field
            ->pre_render(array(
            $row,
          ));
          $item->{$field_label} = $field
            ->render($row);
        }
        $items[] = $item;
      }
      if ($items) {
        $result = $items;
      }
    }
  }
  elseif ($return_type == 'view') {
    $view
      ->set_display($display_id);
    $view
      ->execute();
    return $view;
  }
  else {

    // We want to keep the result an array.
    $result[] = $view
      ->preview($display_id);
  }
  return $result;
}