You are here

class clients_connection_drupal_services in Web Service Clients 6.2

Same name and namespace in other branches
  1. 7.3 connections/clients_drupal/clients_drupal.inc \clients_connection_drupal_services
  2. 7 backends/clients_drupal/clients_drupal.inc \clients_connection_drupal_services
  3. 7.2 connections/clients_drupal/clients_drupal.inc \clients_connection_drupal_services

Base class for Drupal client connections.

Hierarchy

Expanded class hierarchy of clients_connection_drupal_services

File

connections/clients_drupal/clients_drupal.inc, line 10
Contains classes for Client connections handlers.

View source
class clients_connection_drupal_services extends clients_connection_base {

  // ============================================ Base: Constructor.

  /**
   * Constructor method.
   *
   * @param $object
   *  An object of class stdClass returned from CTools.
   */
  function __construct($object) {

    // Call the base class to set the connection properties.
    parent::__construct($object);

    // Decrypt the password.
    $this->configuration['password'] = clients_drupal_decrypt($this->configuration['password']);
  }

  // ============================================ Base: Method calling.

  /**
   * Common helper for reacting to an error from an XMLRPC call.
   *
   * Gets the error from Drupal core's XMLRPC library, logs the error message,
   * and throws an exception, which should be caught by the module making use
   * of the Clients connection API.
   */
  function handleXmlrpcError() {

    // If the remote call resulted in an error, log it and throw an exception.
    $error = xmlrpc_error();
    if (is_object($error)) {
      watchdog('clients', 'Error calling method @method. Error was code @code with message "@message".', array(
        '@method' => $this->method,
        // Technically safe but who knows where this may come from, hence '@'.
        '@code' => $error->code,
        '@message' => $error->message,
      ));
      throw new Exception($error->message, $error->code);
    }
  }

  // ============================================ Base: Testing system.

  /**
   * Provide buttons for the connection testing page.
   *
   * @param $form_state
   *  This is passed in so you can set defaults based on user input.
   */
  function getTestOperations($form_state, $connection_name) {
    $buttons['connect'] = array(
      '#value' => t('Test connection'),
      '#type' => 'submit',
      //'#name' => 'connect', // wtf does this do?
      '#action_type' => 'method',
      '#action_submit' => 'testConnectionConnect',
      '#description' => t('Test the connection settings by calling system.connect on the remote server.'),
    );
    $buttons['login'] = array(
      '#value' => t('Test user login'),
      '#type' => 'submit',
      //'#name' => 'login',
      '#action_type' => 'method',
      '#action_submit' => 'testConnectionLogin',
      '#description' => t('Test the remote user settings and by calling user.login on the remote server.'),
    );
    $buttons['node_load'] = array(
      '#type' => 'fieldset',
    );
    $buttons['node_load']['nid'] = array(
      '#type' => 'textfield',
      '#title' => t('Node ID'),
      '#size' => 6,
      '#default_value' => isset($form_state['values']['buttons']['node_load']['nid']) ? $form_state['values']['buttons']['node_load']['nid'] : NULL,
    );
    $buttons['node_load']['button'] = array(
      '#value' => t('Test node retrieval'),
      '#type' => 'submit',
      //'#name' => 'login',

      // TODO: tidy up these method names!
      '#action_type' => 'method',
      '#action_submit' => 'testConnectionNodeLoad',
      '#action_validate' => 'testConnectionNodeLoadValidate',
      '#description' => t('Attempt to load a remote node.'),
    );
    return $buttons;
  }

  /**
   * Connection test button handler: basic connection.
   *
   * Connection test handlers should return the raw data they got back from the
   * connection for display to the user.
   */
  function testConnectionConnect(&$button_form_values) {
    try {

      // Call the connect method.
      $connect = $this
        ->callMethodArray('system.connect');
    } catch (Exception $e) {
      drupal_set_message(t('Could not connect to the remote site, got error message "@message".', array(
        '@message' => $e
          ->getMessage(),
      )), 'warning');

      //dsm($e);
      return;
    }
    if (is_array($connect) && isset($connect['user'])) {
      drupal_set_message(t('Sucessfully connected to the remote site.'));
    }
    else {
      drupal_set_message(t('Could not connect to the remote site.'), 'warning');
    }
    return $connect;
  }

  /**
   * Connection test button handler: user login.
   */
  function testConnectionLogin(&$button_form_values) {
    try {

      // Call the login method.
      $login = $this
        ->callMethodArray('user.login');

      // Eep. we need user details!!!
    } catch (Exception $e) {
      drupal_set_message(t('Could not log in to the remote site, got error message "@message".', array(
        '@message' => $e
          ->getMessage(),
      )), 'warning');

      //dsm($e);
      return;
    }
    if (is_array($login) && isset($login['user'])) {
      drupal_set_message(t('Sucessfully logged in to the remote site; got back details for user %user (uid @uid).', array(
        '%user' => $login['user']['name'],
        '@uid' => $login['user']['uid'],
      )));
    }
    else {
      drupal_set_message(t('Could not log in to the remote site.'), 'warning');
    }
    return $login;
  }

  /**
   * Connection test button validate handler: loading a node.
   */
  function testConnectionNodeLoadValidate(&$button_form_values) {
    if (empty($button_form_values['nid'])) {
      form_set_error('buttons][node_load][nid', t('Node id is required for the node retrieval test.'));
    }
  }

  /**
   * Connection test button handler: loading a node.
   *
   * Each version has to do its own thing; this is just here to avoid a WTF.
   */
  function testConnectionNodeLoad(&$button_form_values) {
  }

}

Members

Namesort descending Modifiers Type Description Overrides
clients_connection_base::$cid public property The connection id. Only set if this is stored in the database.
clients_connection_base::$configuration public property An array of further configuration options.
clients_connection_base::$endpoint public property The URL this connection connects to.
clients_connection_base::$name public property The machine name of the connection.
clients_connection_base::callMethod function Call a remote method.
clients_connection_base::callMethodArray function Call a remote method with an array of parameters. 3
clients_connection_base::connectionSettingsForm function Extra form elements specific to a class's edit form. 2
clients_connection_base::connectionSettingsForm_submit static function Submit handler for saving/updating connections of this class. 2
clients_connection_base::formatEndpoint function Format the connection's endpoint as a link. 2
clients_connection_drupal_services::getTestOperations function Provide buttons for the connection testing page. Overrides clients_connection_base::getTestOperations 1
clients_connection_drupal_services::handleXmlrpcError function Common helper for reacting to an error from an XMLRPC call.
clients_connection_drupal_services::testConnectionConnect function Connection test button handler: basic connection.
clients_connection_drupal_services::testConnectionLogin function Connection test button handler: user login.
clients_connection_drupal_services::testConnectionNodeLoad function Connection test button handler: loading a node. 2
clients_connection_drupal_services::testConnectionNodeLoadValidate function Connection test button validate handler: loading a node.
clients_connection_drupal_services::__construct function Constructor method. Overrides clients_connection_base::__construct