You are here

services_views.module in Services Views 6

Same filename and directory in other branches
  1. 7 services_views.module

Provides a generic but powerful API for web services.

File

services_views.module
View source
<?php

/**
 * @file
 *  Provides a generic but powerful API for web services.
 */

/**
 * Implementation of hook_views_api().
 */
function services_views_views_api() {
  return array(
    'api' => 2.0,
  );
}

/**
 * Implementation of hook_services_resources().
 */
function services_views_services_resources() {
  $resources['views'] = array();
  $resources['views']['retrieve'] = array(
    'help' => 'Retrieves a view.',
    'file' => array(
      'type' => 'inc',
      'module' => 'services_views',
      'name' => 'services_views.resource',
    ),
    'callback' => 'services_views_retrieve',
    'access callback' => 'services_views_access',
    'access arguments' => array(
      'view',
    ),
    'access arguments append' => TRUE,
    'args' => array(
      'view_name' => array(
        'name' => 'view_name',
        'type' => 'string',
        'description' => 'The name of the view to get.',
        'source' => array(
          'path' => '0',
        ),
        'optional' => FALSE,
      ),
      'display_id' => array(
        'name' => 'display_id',
        'type' => 'string',
        'description' => 'The display ID of the view to get.',
        'source' => array(
          'param' => 'display_id',
        ),
        'optional' => TRUE,
        'default value' => 'default',
      ),
      'args' => array(
        'name' => 'args',
        'type' => 'array',
        'description' => 'A list of arguments to pass to the view.',
        'source' => array(
          'param' => 'args',
        ),
        'optional' => TRUE,
        'default value' => array(),
      ),
      'offset' => array(
        'name' => 'offset',
        'type' => 'int',
        'description' => 'The number of the entry for the page begin with.',
        'source' => array(
          'param' => 'offset',
        ),
        'optional' => TRUE,
        'default value' => 0,
      ),
      'limit' => array(
        'name' => 'limit',
        'type' => 'int',
        'description' => 'The total number of entries to list.',
        'source' => array(
          'param' => 'limit',
        ),
        'optional' => TRUE,
        'default value' => 10,
      ),
      'theme_output' => array(
        'name' => 'theme_output',
        'type' => 'bool',
        'description' => 'Whether to return the raw data results or style the results.',
        'source' => array(
          'param' => 'theme_output',
        ),
        'optional' => TRUE,
        'default value' => FALSE,
      ),
      'filters' => array(
        'name' => 'filters',
        'type' => 'array',
        'description' => 'A list of filters to pass to the view.  These are defined by the exposed filters on your view.  Example call: <code>/views/your_view?filters[nid]=12345</code>',
        'source' => array(
          'param' => 'filters',
        ),
        'optional' => TRUE,
        'default value' => array(),
      ),
    ),
  );
  return $resources;
}

/**
 * Check the access permission to a given views.
 *
 * @param $op
 *  String. The operation that's going to be performed.
 * @param $args
 *  Array. The arguments that will be passed to the callback.
 * @return
 *  Boolean. TRUE if the user is allowed to load the given view.
 */
function services_views_access($op = 'view', $args = array()) {
  switch ($op) {
    case 'view':
      $view = views_get_view($args['view_name']);
      if (empty($view)) {
        return services_error(t('View @view could not be found', array(
          '@view' => $args['view_name'],
        )), 404);
      }
      if (!isset($view->display[$args['display_id']])) {
        return services_error(t('Display @display on view @view could not be found', array(
          '@display' => $args['display_id'],
          '@view' => $args['view_name'],
        )), 404);
      }
      return $view
        ->access($args['display_id']);
  }
}

Functions

Namesort descending Description
services_views_access Check the access permission to a given views.
services_views_services_resources Implementation of hook_services_resources().
services_views_views_api Implementation of hook_views_api().