You are here

clients_drupal.module in Web Service Clients 6

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

Drupal Services plugin for Clients module @author Django Beatty - adub

File

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

/**
 * @file
 * Drupal Services plugin for Clients module
 * @author Django Beatty - adub
 */

/**
 * Contains class extending Clients ClientsServicesBase
 */
require_once 'clients_drupal.inc';

/**
 * 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_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_clients_connection_edit()
 */
function clients_drupal_clients_connection_edit($formvals) {
  if ($formvals['type'] == variable_get('clients_drupal_type', 'Drupal Services')) {
    $formvals['type'] = variable_get('clients_drupal_type', 'clients_drupal');

    // prepare pass for serialized storage
    if (empty($formvals['configuration']['password'])) {

      // need to load connection and set password to original if blank
      $original = clients_connection_load((int) $formvals['cid']);
      $formvals['configuration']['password'] = $original->configuration['password'];
    }
    $formvals['configuration']['password'] = clients_drupal_encrypt($formvals['configuration']['password']);
    return $formvals;
  }
}

/**
 * Implementation of hook_clients_connection_save()
 */
function clients_drupal_clients_connection_save($values) {
  if ($values['type'] == variable_get('clients_drupal_type', 'Drupal Services')) {
    $values['configuration']['password'] = clients_drupal_encrypt($values['configuration']['password']);
  }
  return $values;
}

/**
 * Implementation of hook_menu()
 */
function clients_drupal_menu() {
  $items = array();
  $items['admin/settings/clients/connections/drupal'] = array(
    'title' => 'Add Drupal connection',
    'description' => 'Add Drupal connection',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'clients_drupal_config',
      'add',
    ),
    'access arguments' => array(
      'clients_drupal admin',
    ),
    'weight' => 10,
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/settings/clients/drupal/admin'] = array(
    'title' => 'Configure Drupal',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'clients_drupal_admin',
    ),
    'access arguments' => array(
      'clients_drupal admin',
    ),
    'weight' => 10,
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/settings/clients/connections/drupal/%/edit'] = array(
    'title' => 'Edit Drupal connection',
    'description' => 'Edit Drupal connection',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'clients_drupal_config',
      6,
      5,
    ),
    'access arguments' => array(
      'clients_drupal admin',
    ),
    'weight' => 10,
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * @return array Form
 */
function clients_drupal_config(&$form_state, $op, $cid = '') {
  $form = array();
  if ($cid) {
    $connection = clients_connection_load((int) $cid);
    $form['cid'] = array(
      '#type' => 'value',
      '#value' => $cid,
    );
  }
  $form['type'] = array(
    '#type' => 'value',
    '#value' => variable_get('clients_drupal_type', 'Drupal Services'),
  );
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Connection name'),
    '#default_value' => $cid ? $connection->name : '',
    '#size' => 50,
    '#maxlength' => 100,
    '#description' => t('Must be unique, any characters allowed'),
    '#required' => TRUE,
  );
  $form['endpoint'] = array(
    '#type' => 'textfield',
    '#title' => t('Connection endpoint'),
    '#default_value' => $cid ? $connection->endpoint : '',
    '#size' => 50,
    '#maxlength' => 100,
    '#description' => t('Remote service URL e.g. http://mysite.com/services/xmlrpc'),
    '#required' => TRUE,
  );
  $form['configuration'] = array(
    '#type' => 'fieldset',
    '#title' => t('Configuration'),
    '#collapsible' => FALSE,
    '#tree' => TRUE,
  );
  $form['configuration']['domain'] = array(
    '#type' => 'textfield',
    '#title' => t('Domain'),
    '#default_value' => $cid ? $connection->configuration['domain'] : '',
    '#size' => 50,
    '#maxlength' => 100,
    '#description' => t('This should be same as the \'Domain\' field used by the Services authentication key on the server you are connecting to.'),
    '#required' => TRUE,
  );
  $form['configuration']['servicekey'] = array(
    '#type' => 'textfield',
    '#title' => t('Service key'),
    '#default_value' => $cid ? $connection->configuration['servicekey'] : '',
    '#size' => 50,
    '#maxlength' => 40,
    '#attributes' => array(
      'autocomplete' => 'off',
    ),
    '#description' => t('This should be same as the \'Key\' field used by the Services authentication key on the server you are connecting to.'),
    '#required' => TRUE,
  );
  $form['configuration']['username'] = array(
    '#type' => 'textfield',
    '#title' => t('Service username'),
    '#default_value' => $cid ? $connection->configuration['username'] : '',
    '#size' => 30,
    '#maxlength' => 60,
    '#attributes' => array(
      'autocomplete' => 'off',
    ),
    '#description' => t('This should be same as the username on the server you are connecting to.'),
    '#required' => TRUE,
  );
  $password_desc = $cid ? t('This should be same as the password on the server you are connecting to. Leave blank unless you need to change this.') : 'This should be same as the password on the server you are connecting to.';
  $form['configuration']['password'] = array(
    '#type' => 'password',
    '#title' => t('Service password'),
    '#size' => 30,
    '#maxlength' => 60,
    '#attributes' => array(
      'autocomplete' => 'off',
    ),
    '#description' => $password_desc,
    '#required' => $op == 'add',
  );
  $form['configuration']['methods_enabled'] = array(
    '#type' => 'textarea',
    '#title' => t('Services'),
    '#default_value' => $cid ? $connection->configuration['methods_enabled'] : '',
    '#description' => t('List of Drupal services on remote servers that you want to enable here (e.g. views.service). Resources can select from this list. One per line.'),
  );
  $form['configuration']['views_enabled'] = array(
    '#type' => 'textarea',
    '#title' => t('Views'),
    '#default_value' => $cid ? $connection->configuration['views_enabled'] : '',
    '#description' => t('List of Drupal views on remote servers. Resources can select from this list. One per line.'),
  );
  if ($op == 'add') {
    $form['buttons']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Add connection'),
    );
    $form['#submit'] = array(
      'clients_drupal_add_submit_handler',
    );
  }
  elseif ($op == 'edit') {
    $form['buttons']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
    );
    $form['#submit'] = array(
      'clients_drupal_edit_submit_handler',
    );
  }
  return $form;
}

/**
 * Submit handler
 * @return array Form
 */
function clients_drupal_add_submit_handler($form, &$form_state) {
  clients_connection_save($form_state['values']);
}

/**
 * Submit handler
 * @return array Form
 */
function clients_drupal_edit_submit_handler($form, &$form_state) {
  clients_connection_edit($form_state['values']);
}

/**
 * Implementation of hook_validate()
 */
function clients_drupal_add_validate($form, &$form_state) {
  if (clients_connection_load($form['name']['#value'])) {
    form_set_error('name', 'A service by this name already exists!');
  }
  $connection = new stdClass();
  $connection->name = $form['name']['#value'];
  $connection->endpoint = $form['endpoint']['#value'];
  $connection->domain = $form['configuration']['domain']['#value'];
  $connection->servicekey = $form['configuration']['servicekey']['#value'];
  $connection->username = $form['configuration']['username']['#value'];
  $connection->password = $form['configuration']['password']['#value'];
  $testconnect = ClientsServicesDrupal::connect($connection);
  if (!is_array($testconnect) || !isset($testconnect['sessid'])) {
    form_set_error('endpoint', "Couldn't connect");
  }
  else {
    $testuser = ClientsServicesDrupal::getUser($connection);
    if (!is_array($testuser) || !isset($testuser['sessid'])) {
      form_set_error('username', isset($testuser->message) ? $testuser->message : "Couldn't log in");
    }
  }
}

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

    // or ***
    $connection->operations = l(t('Edit'), 'admin/settings/clients/connections/drupal/' . $connection->cid . '/edit') . ' | ' . l(t('Delete'), 'admin/settings/clients/connections/' . $connection->cid . '/delete');
  }
}

/**
 * Implementation of hook_clients_service_options
 */
function clients_drupal_clients_service_options($connection, $wrapper, $wrapper_values, $resource) {
  if ($connection->type != variable_get('clients_drupal_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 == variable_get('clients_drupal_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 == variable_get('clients_drupal_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
 */
function clients_drupal_clients_call($connection, $serviceOptions) {
  if ($connection->type == variable_get('clients_drupal_type', 'Drupal Services')) {
    $serviceConfig = clients_connection_load((int) $connection->cid);
    return ClientsServicesDrupal::call($serviceConfig, $serviceOptions);
  }
}

/**
 * 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 != variable_get('clients_drupal_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', ''),
    '#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', '');
  if ($encryption_method == 'no_encryption') {
    return $val;
  }
  else {
    return module_invoke($encryption_method, 'clients_drupal_decrypt', $val);
  }
}

Functions

Namesort descending Description
clients_drupal_add_submit_handler Submit handler
clients_drupal_add_validate Implementation of hook_validate()
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_edit Implementation of hook_clients_connection_edit()
clients_drupal_clients_connection_load Implementation of hook_clients_connection_load
clients_drupal_clients_connection_save Implementation of hook_clients_connection_save()
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_config
clients_drupal_decrypt
clients_drupal_edit_submit_handler Submit handler
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