You are here

class clients_connection_drupal_services_5 in Web Service Clients 7

Same name and namespace in other branches
  1. 6.2 connections/clients_drupal/clients_drupal.inc \clients_connection_drupal_services_5
  2. 7.3 connections/clients_drupal/clients_drupal.inc \clients_connection_drupal_services_5
  3. 7.2 connections/clients_drupal/clients_drupal.inc \clients_connection_drupal_services_5

Drupal client for services on a Drupal 5 site.

Works with Services 5.x-0.92.

We extend from the Services 6.x-2.x class as not much actually changes these versions between when it comes to making calls.

Hierarchy

Expanded class hierarchy of clients_connection_drupal_services_5

File

backends/clients_drupal/clients_drupal.inc, line 626
Defines methods and calls to Drupal services

View source
class clients_connection_drupal_services_5 extends clients_connection_drupal_services_6_2 {

  /**
   * Call a remote method.
   *
   * TODO: REFACTOR this to look more like the static methods above --
   * separate methods for getuser, connect, etc etc.
   *
   * @param $method
   *  The name of the remote method to call.
   * @param
   *  All other parameters are passed to the remote method.
   *  Note that the D5 version of Services does not seem to respect optional parameters; you
   *  should pass in defaults (eg an empty string or 0) instead of omitting a parameter.
   *
   * @return
   *  Whatever is returned from the remote site.
   */
  function callMethod($method) {

    //dsm($this);

    //dsm($method);
    $config = $this->configuration;
    $connect = xmlrpc($this->endpoint, 'system.connect');
    $session_id = $connect['sessid'];

    // We may want to call only system.connect for testing purposes.
    if ($method == 'system.connect') {
      return $connect;
    }

    // Log in
    // Get the API key-related arguments.
    $key_args = $this
      ->xmlrpc_key_args('user.login');

    //dsm($key_args);

    // Build the array of connection arguments we need to log in.
    $username = $this->configuration['username'];
    $password = $this->configuration['password'];
    $login_args = array_merge(array(
      $this->endpoint,
      'user.login',
    ), $key_args, array(
      $session_id,
    ), array(
      $username,
      $password,
    ));

    // Call the xmlrpc method with our array of arguments. This accounts for
    // whether we use a key or not, and the extra parameters to pass to the method.
    $login = call_user_func_array('xmlrpc', $login_args);
    $login_session_id = $login['sessid'];

    // If the requested method is user.login, we're done.
    if ($method == 'user.login') {
      return $login;
    }

    //dsm($login);

    // Get all the arguments this function has been passed.
    $function_args = func_get_args();

    // Slice out the ones that are arguments to the method call: everything past
    // the 1st argument.
    $method_args = array_slice($function_args, 1);

    // The node.load method on D5 is an evil special case because it's defined
    // to not use an API key.
    if ($method == 'node.load') {

      // Be nice. Let the caller specify just the nid, and provide the
      // empty default for the optional fields parameter.
      if (count($method_args) == 1) {
        $method_args[] = array();
      }

      // Be nice part 2: the number one (in my experience) cause of lost
      // hours on Services is the way XMLRPC and/or services get their
      // knickers in a twist when they want an integer but think they've got
      // a string because they're too damn stupid to try casting.
      // So cast the nid here, since we're already in a special case for this
      // method anyway.
      $method_args[0] = (int) $method_args[0];

      // Build the array of connection arguments for the method we want to call.
      $xmlrpc_args = array_merge(array(
        $this->endpoint,
        $method,
      ), array(
        $login_session_id,
      ), $method_args);

      //dsm($xmlrpc_args);

      // Call the xmlrpc method with our array of arguments.
      $result = call_user_func_array('xmlrpc', $xmlrpc_args);
      if ($result === FALSE) {

        //dsm('error');
        return xmlrpc_error();
      }
      return $result;
    }

    // Get the API key-related arguments.
    $key_args = $this
      ->xmlrpc_key_args($method);

    // Build the array of connection arguments for the method we want to call.
    $xmlrpc_args = array_merge(array(
      $this->endpoint,
      $method,
    ), $key_args, array(
      $login_session_id,
    ), $method_args);

    // Call the xmlrpc method with our array of arguments.
    $result = call_user_func_array('xmlrpc', $xmlrpc_args);
    if ($result === FALSE) {

      //dsm('error');
      return xmlrpc_error();
    }
    return $result;
  }

  /**
   * 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, $cid) {

    // Just making the inheritance explicit for debugging... ;)
    return parent::getTestOperations($form_state, $cid);
  }

  /**
   * Connection test button handler: loading a node.
   *
   * This uses a different method on Services 5.x-0.92.
   */
  function testConnectionNodeLoad(&$button_form_values) {
    $nid = $button_form_values['nid'];
    $node = $this
      ->callMethod('node.load', $nid);
    if (is_array($node) && isset($node['nid'])) {
      drupal_set_message(t('Sucessfully retrieved node %title (nid @nid).', array(
        '%title' => $node['title'],
        '@nid' => $node['nid'],
      )));
    }
    else {
      drupal_set_message(t('Could not retrieve a node from the remote site.'), 'warning');
    }
    return $node;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
clients_connection_base::$cid public property
clients_connection_base::$configuration public property
clients_connection_base::$endpoint public property
clients_connection_base::$name public property
clients_connection_base::doCall protected static function Takes variable number of params after cacheid.
clients_connection_drupal_services::call public static function Executes call and processes data Overrides clients_connection_base::call
clients_connection_drupal_services::connect public static function Use for testing
clients_connection_drupal_services::connectionSettingsForm static function Form builder for adding or editing connections of this class.
clients_connection_drupal_services::connectionSettingsForm_submit static function Submit handler for saving/updating connections of this class. Overrides clients_connection_base::connectionSettingsForm_submit
clients_connection_drupal_services::fetch protected static function Gets raw data from service call
clients_connection_drupal_services::getToken public static function Prepares a hashed token for the service, based on current time, the required service and config values; serviceKey and serviceDomain t
clients_connection_drupal_services::getUser public static function Connects to Drupal Services and logs in the user provided in the config. Returns a session for the user. @todo needs error catching in case service is down
clients_connection_drupal_services::__construct function Constructor method. Overrides clients_connection_base::__construct
clients_connection_drupal_services_5::callMethod function Call a remote method. Overrides clients_connection_drupal_services_6_2::callMethod
clients_connection_drupal_services_5::getTestOperations function Provide buttons for the connection testing page. Overrides clients_connection_drupal_services_6_2::getTestOperations
clients_connection_drupal_services_5::testConnectionNodeLoad function Connection test button handler: loading a node. Overrides clients_connection_drupal_services_6_2::testConnectionNodeLoad
clients_connection_drupal_services_6_2::testConnectionConnect function Connection test button handler: basic connection.
clients_connection_drupal_services_6_2::testConnectionLogin function Connection test button handler: user login.
clients_connection_drupal_services_6_2::testConnectionNodeLoadValidate function Connection test button validate handler: loading a node.
clients_connection_drupal_services_6_2::xmlrpc_key_args function Helper function to get key-related method arguments for the XMLRPC call.