You are here

clients_drupal.module in Web Service Clients 7

Same filename and directory in other branches
  1. 6 backends/clients_drupal/clients_drupal.module

Drupal Services plugin for Clients module

File

backends/clients_drupal/clients_drupal.module
View source
<?php

/**
 * @file
 * Drupal Services plugin for Clients module
 */

/**
 * Implementation of hook_help()
 * @param path which path of the site we're displaying help
 * @param arg array that holds the current path as would be returned from arg() function
 * @return help text for the path
 */
function clients_drupal_help($path, $arg) {
  $output = '';
  switch ($path) {
    case "admin/help#clients_drupal":
      $output = '<p>' . t("Clients - Drupal Services.") . '</p>';
      break;
  }
  return $output;
}

/**
 * Implementation of hook_clients_connection_type_info().
 *
 * Define the connection type we provide.
 */
function clients_drupal_clients_connection_type_info() {
  return array(
    // Key by machine name. Used as the base for hooks.
    'drupal_services' => array(
      'label' => t('Drupal Services'),
    ),
    'drupal_services_5' => array(
      'label' => t('Drupal Services 5'),
    ),
    'drupal_services_6_2' => array(
      'label' => t('Drupal Services 6.x-2.x'),
    ),
  );
}

/**
 * Implementation of hook_perm()
 * @TODO
 * @return array An array of valid permissions for the clients_drupal module
 */
function clients_drupal_perm() {
  return array(
    'clients_drupal admin',
  );
}

/**
 * Implementation of hook_menu()
 */
function clients_drupal_menu() {
  $items = array();

  // Client provider modules may add settings beneath the Settings tab.
  $items['admin/settings/clients/settings/drupal'] = array(
    'title' => 'Configure Drupal',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'clients_drupal_admin',
    ),
    'access arguments' => array(
      'clients_drupal admin',
    ),
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

/**
 * Implementation of hook_clients_connection_load
 */
function clients_drupal_clients_connection_load(&$connection) {
  if ($connection->type == 'drupal_services') {
    $connection->configuration['password'] = clients_drupal_decrypt($connection->configuration['password']);

    // or ***
  }
}

/**
 * Implementation of hook_clients_service_options
 */
function clients_drupal_clients_service_options($connection, $wrapper, $wrapper_values, $resource) {

  // TODO: kill this -- see TODO on clients_service_options().
  if ($connection->type != 'drupal_services') {
    return;
  }
  $form = array();
  $connection = clients_connection_load((int) $connection->cid);
  $methods = array_map('trim', explode("\n", trim($connection->configuration['methods_enabled'])));
  $methods = array_combine($methods, $methods);
  $options_selected = isset($wrapper_values['options']) ? $wrapper_values['options'] : (isset($resource->rid) ? $resource->configuration['options'] : array());

  // if we change connection and method doesn't exist, reset
  if (!in_array($options_selected['method'], $methods)) {
    $options_selected['method'] = key($methods);
  }
  $form['method'] = array(
    '#type' => 'select',
    '#title' => t('Method'),
    '#default_value' => $options_selected['method'],
    '#options' => $methods,
    '#description' => t('Choose method'),
    '#required' => TRUE,
    '#ahah' => array(
      'path' => ahah_helper_path(array(
        $wrapper,
      )),
      'wrapper' => $wrapper,
      'method' => 'replace',
      'effect' => 'fade',
    ),
  );
  if ($options_selected['method'] == 'views.get') {
    $views = array_map('trim', explode("\n", trim($connection->configuration['views_enabled'])));
    $views = array_combine($views, $views);
    $form['view'] = array(
      '#type' => 'select',
      '#title' => t('View'),
      '#default_value' => $options_selected['view'],
      '#options' => $views,
      '#description' => t('Choose view'),
    );
    $form['arguments']['first'] = array(
      '#type' => 'textfield',
      '#title' => t('Argument'),
      '#default_value' => $options_selected['arguments']['first'],
      '#size' => 10,
      '#maxlength' => 30,
    );
    $form['offset'] = array(
      '#type' => 'textfield',
      '#title' => t('Offset'),
      '#default_value' => $options_selected['offset'],
      '#size' => 3,
      '#maxlength' => 4,
    );
    $form['limit'] = array(
      '#type' => 'textfield',
      '#title' => t('Limit'),
      '#default_value' => $options_selected['limit'],
      '#size' => 3,
      '#maxlength' => 4,
    );
  }
  else {

    /**
     * @todo some 'method not supported' handling
     */
  }
  return $form;
}

/**
 * Implementation of hook_clients_setparams
 */
function clients_drupal_clients_setparams(&$resource, $arg) {
  $connection = clients_connection_load((int) $resource->cid);

  // need to make static?
  if ($connection->type == 'drupal_services') {
    foreach ($arg as $key => $value) {

      // doesn't support multiple args...
      if ($key == 'argument') {
        $resource->configuration['options']['arguments']['first'] = str_replace(' ', '+', $value);
      }
      else {
        $resource->configuration['options'][$key] = str_replace(' ', '+', $value);
      }
    }
  }
}

/**
 * Implementation of hook_clients_arguments
 */
function clients_drupal_clients_arguments($connection) {

  //  $connection = clients_connection_load((int)$source['connection']);
  if ($connection->type == 'drupal_services') {
    $arg = new stdClass();
    $arg->name = 'argument';
    $arg->title = t('Argument');
    $arg->help = t('Argument to pass to resource');
    return array(
      $arg,
    );
  }
}

/**
 * Implementation of hook_clients_call
 *
 * TODO: rename to hook_clients_call_resource
 */
function clients_drupal_clients_call($connection, $serviceOptions) {
  if ($connection->type == 'drupal_services') {
    $serviceConfig = clients_connection_load((int) $connection->cid);
    return ClientsServicesDrupal::call($serviceConfig, $serviceOptions);
  }
}

/**
 * Caller function. This gets us into the class.
 */
function drupal_services_clients_call($connection, $method, $parameters = array()) {
  return ClientsServicesDrupal::call_method($connection, $method, $parameters);
}

/**
 * Implementation of hook_clients_fields - refactor as inc file?
 * Allows us to get field for a specific resource. Default fields are set at the connection type level here and additional custom field will be defined per resource (@todo). This will allow adding of remote cck fields (for example) for a specific resource (e.g. a certain view). This function will need to aggregate these with the base ones. (Otherwise this function is used to return all available fields.)
 */
function clients_drupal_clients_fields($resource = NULL) {
  if ($resource) {
    $connection = clients_connection_load((int) $resource->cid);
    if ($connection->type != 'drupal_services') {
      return;
    }
    $result = clients_call($resource);
    if (isset($result[0]->data[0])) {
      $fields = array_keys($result[0]->data[0]);
      $fieldset = array();
      foreach ($fields as $field) {
        $fieldset[$field] = array(
          'name' => $field,
          'description' => '',
        );
      }
      return $fieldset;
    }
    else {
      drupal_set_message('No fields were returned');
      return;
    }
  }

  // need to define behaviour if no resource specified - should this display:
  // - no fields
  // - a preset list of fields (from each service module)
  // - all possible fields from currently defined resources
  return array();
}

/**
 * @return array Form
 */
function clients_drupal_admin() {
  $form = array();
  $options = clients_drupal_encryption_methods();
  $form['clients_drupal_encryption_method'] = array(
    '#type' => 'select',
    '#title' => t('Remote password encryption method'),
    '#default_value' => variable_get('clients_drupal_encryption_method', 'no_encryption'),
    '#options' => $options,
    '#required' => TRUE,
  );
  return system_settings_form($form);
}
function clients_drupal_encryption_methods() {
  $options = module_invoke_all('clients_drupal_encryption_methods');
  $options['no_encryption'] = t('No encryption');
  return $options;
}
function clients_drupal_encrypt($val) {
  $encryption_method = variable_get('clients_drupal_encryption_method', 'no_encryption');
  if ($encryption_method == 'no_encryption') {
    return $val;
  }
  else {
    return module_invoke($encryption_method, 'clients_drupal_encrypt', $val);
  }
}
function clients_drupal_decrypt($val) {
  $encryption_method = variable_get('clients_drupal_encryption_method', 'no_encryption');
  if ($encryption_method == 'no_encryption') {
    return $val;
  }
  else {
    return module_invoke($encryption_method, 'clients_drupal_decrypt', $val);
  }
}

Functions

Namesort descending Description
clients_drupal_admin
clients_drupal_clients_arguments Implementation of hook_clients_arguments
clients_drupal_clients_call Implementation of hook_clients_call
clients_drupal_clients_connection_load Implementation of hook_clients_connection_load
clients_drupal_clients_connection_type_info Implementation of hook_clients_connection_type_info().
clients_drupal_clients_fields Implementation of hook_clients_fields - refactor as inc file? Allows us to get field for a specific resource. Default fields are set at the connection type level here and additional custom field will be defined per resource (@todo). This will allow…
clients_drupal_clients_service_options Implementation of hook_clients_service_options
clients_drupal_clients_setparams Implementation of hook_clients_setparams
clients_drupal_decrypt
clients_drupal_encrypt
clients_drupal_encryption_methods
clients_drupal_help Implementation of hook_help()
clients_drupal_menu Implementation of hook_menu()
clients_drupal_perm Implementation of hook_perm() @TODO
drupal_services_clients_call Caller function. This gets us into the class.