You are here

views_service.module in Services 5

The module which exposes services related to views module

File

services/views_service/views_service.module
View source
<?php

/**
 * @file
 * The module which exposes services related to views module
 */

/**
 * Implementation of hook_help().
 */
function views_service_help($section) {
  switch ($section) {
    case 'admin/help#services_views':
      return t('<p>Provides view methods to services applications. Requires services.module.</p>');
    case 'admin/modules#description':
      return t('Provides view methods to services applications. Requires services.module.');
  }
}

/**
 * Implementation of hook_service().
 */
function views_service_service() {
  return array(
    // views.getView
    array(
      '#method' => 'views.getView',
      '#callback' => 'views_service_get_view',
      '#args' => array(
        array(
          '#name' => 'view_name',
          '#type' => 'string',
          '#description' => t('View name.'),
        ),
        array(
          '#name' => 'fields',
          '#type' => 'array',
          '#optional' => TRUE,
          '#description' => t('A list of fields to return.'),
        ),
        array(
          '#name' => 'args',
          '#type' => 'array',
          '#optional' => TRUE,
          '#description' => t('An array of arguments to pass to the view.'),
        ),
      ),
      array(
        '#name' => 'limit',
        '#type' => 'int',
        '#optional' => TRUE,
        '#description' => t('How many elements should be build by page'),
      ),
      array(
        '#name' => 'page',
        '#type' => 'int',
        '#optional' => TRUE,
        '#description' => t('If you use pager, which page to start'),
      ),
      array(
        '#name' => 'offset',
        '#type' => 'int',
        '#optional' => TRUE,
        '#description' => t('If pager is false, skip the first $offset results.'),
      ),
      array(
        '#name' => 'filters',
        '#type' => 'array',
        '#optional' => TRUE,
        '#description' => t('An array of exposed filter ops and values to use with the exposed filter system'),
      ),
      '#return' => 'array',
      '#help' => t('Retrieves a view defined in views.module.'),
    ),
    // views.exportView
    array(
      '#method' => 'views.exportView',
      '#callback' => 'views_service_export_view',
      '#access arguments' => array(
        'administer views',
      ),
      '#args' => array(
        'string',
      ),
      '#args' => array(
        array(
          '#name' => 'view_name',
          '#type' => 'string',
          '#description' => t('View name.'),
        ),
      ),
      '#return' => 'string',
      '#help' => t('Exports the code of a view, same as the output you would get from the Export tab.'),
    ),
    // views.importView
    array(
      '#method' => 'views.importView',
      '#callback' => 'views_service_import_view',
      '#access arguments' => array(
        'administer views',
      ),
      '#args' => array(
        'string',
      ),
      '#args' => array(
        array(
          '#name' => 'view_export',
          '#type' => 'string',
          '#description' => t('Code from a Views->Export.'),
        ),
      ),
      '#return' => 'int',
      '#help' => t('Imports a view through code, equivalent to using the Import tab in the views admin.'),
    ),
  );
}

/**
 * Get a view from the database.
 */
function views_service_get_view($view_name, $fields = array(), $args = array(), $limit = 0, $page = 0, $offset = 0, $filters = NULL) {
  $view = views_get_view($view_name);
  if (is_null($view)) {
    return services_error('View does not exist.');
  }

  // Check access
  if (!views_access($view)) {
    return services_error('You do not have access to this view.');
  }
  $result = views_build_view('result', $view, $args, FALSE, $limit, $page, $offset, $filters);
  while ($node = db_fetch_object($result['result'])) {
    $nodes[] = services_node_load(node_load(array(
      'nid' => $node->nid,
    )), $fields);
  }
  return $nodes;
}

/**
 * Export a view.
 */
function views_service_export_view($view_name) {
  $view = views_get_view($view_name);
  if (is_null($view)) {
    return services_error('View does not exist.');
  }
  return views_create_view_code($view_name);
}

/**
 * Import a view.
 */
function views_service_import_view($view_export) {
  views_load_cache();
  ob_start();
  eval($view_export);
  ob_end_clean();

  // Views exports don't contain vids, therefore we have to
  // check and see if the view already exists. If so, save
  // the existing vid into our imported view object. Otherwise
  // _views_save_view() will treat this as an insert rather than
  // as an update.
  $existing_view = views_get_view($view->name);
  if ($existing_view) {
    $view->vid = $existing_view->vid;
  }
  views_sanitize_view($view);
  $vid = _views_save_view($view);
  return $vid;
}

Functions

Namesort descending Description
views_service_export_view Export a view.
views_service_get_view Get a view from the database.
views_service_help Implementation of hook_help().
views_service_import_view Import a view.
views_service_service Implementation of hook_service().