You are here

class clients_connection_drupal_services_7_3 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_7_3
  2. 7.2 connections/clients_drupal/clients_drupal.inc \clients_connection_drupal_services_7_3

Drupal client for services on a Drupal 7 site for Services 7.x-3.x.

Hierarchy

Expanded class hierarchy of clients_connection_drupal_services_7_3

File

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

View source
class clients_connection_drupal_services_7_3 extends clients_connection_drupal_services {

  // ============================================ D7-3: Connection form methods.

  /**
   * Extra form elements specific to a class's edit form.
   *
   * This is the same pattern as node_form() -- just ignore the object behind
   * the curtain ;)
   *
   * @param $form_state
   *  The form state from the main form, which you probably don't need anyway.
   *
   * @return
   *  A FormAPI form array. This will be merged in with basic data and the
   *  submit button added.
   *
   * @see clients_connection_form()
   * @see clients_connection_form()
   * @see clients_connection_form_submit()
   */
  function connectionSettingsForm(&$form_state) {
    $form = array();
    $form['endpoint'] = array(
      '#type' => 'textfield',
      '#title' => t('Connection endpoint'),
      '#default_value' => $this->new ? '' : $this->endpoint,
      '#size' => 50,
      '#maxlength' => 100,
      '#description' => t('Remote service URL e.g. http://mysite.com/path/to/xmlrpc'),
      '#required' => TRUE,
    );
    $form['configuration'] = array(
      '#type' => 'fieldset',
      '#title' => t('Configuration'),
      '#collapsible' => FALSE,
      '#tree' => TRUE,
    );
    $form['configuration']['username'] = array(
      '#type' => 'textfield',
      '#title' => t('Service username'),
      '#default_value' => $this->new ? '' : $this->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 = $this->new ? t('This should be same as the password on the server you are connecting to.') : t('This should be same as the password on the server you are connecting to. Leave blank unless you need to change this.');
    $form['configuration']['password'] = array(
      '#type' => 'password',
      '#title' => t('Service password'),
      '#size' => 30,
      '#maxlength' => 60,
      '#attributes' => array(
        'autocomplete' => 'off',
      ),
      '#description' => $password_desc,
      '#required' => $this->new,
    );
    return $form;
  }

  /**
   * Submit handler for saving/updating connections of this class.
   *
   * @see clients_connection_form_submit()
   */
  static function connectionSettingsForm_submit($form, &$form_state) {
    $old_connection = $form_state['values']['old_connection'];

    // Check whether we're editing or adding a new connection.
    if ($old_connection->new) {
      $form_state['values']['configuration']['password'] = clients_drupal_encrypt($form_state['values']['configuration']['password']);
    }
    else {

      // Prepare password for serialized storage
      if (empty($form_state['values']['configuration']['password'])) {

        // Set password to original if blank.
        $form_state['values']['configuration']['password'] = $old_connection->configuration['password'];
      }
      $form_state['values']['configuration']['password'] = clients_drupal_encrypt($form_state['values']['configuration']['password']);
    }
  }
  function formatEndpoint($url) {
    return $url;
  }

  // ============================================ D7-3: Connection API.

  /**
   * Call a remote method.
   *
   * @param $method
   *  The name of the remote method to call.
   * @param $method_params
   *  An array of parameters to 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 callMethodArray($method, $method_params = array()) {
    $this->method = $method;

    // Connect to the remote system service to get an initial session id to log in with.
    $connect = xmlrpc($this->endpoint, 'system.connect');
    $session_id = $connect['sessid'];
    $this
      ->handleXmlrpcError();

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

    // Log in and get the user's session ID.
    $username = $this->configuration['username'];
    $password = $this->configuration['password'];
    $login = xmlrpc($this->endpoint, 'user.login', $username, $password);
    $login_session_id = $login['sessid'];
    $this
      ->handleXmlrpcError();

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

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

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

    // Throw an exception for errors from the remote call.
    $this
      ->handleXmlrpcError();
    return $result;
  }

  // ============================================ D7-3: Testing system.

  /**
   * Connection test button handler: loading a node: Services 7.x-3.x.
   */
  function testConnectionNodeLoad(&$button_form_values) {

    // Must be cast to integer for faffiness of XMLRPC and Services.
    $nid = (int) $button_form_values['nid'];
    try {
      $node = $this
        ->callMethodArray('node.retrieve', array(
        $nid,
      ));
    } catch (Exception $e) {
      drupal_set_message(t('Could not retrieve a node from the remote site, got error message "@message".', array(
        '@message' => $e
          ->getMessage(),
      )), 'warning');

      //dsm($e);
      return;
    }
    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 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_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::testConnectionNodeLoadValidate function Connection test button validate handler: loading a node.
clients_connection_drupal_services::__construct function Constructor method. Overrides clients_connection_base::__construct
clients_connection_drupal_services_7_3::callMethodArray function Call a remote method. Overrides clients_connection_base::callMethodArray
clients_connection_drupal_services_7_3::connectionSettingsForm function Extra form elements specific to a class's edit form. Overrides clients_connection_base::connectionSettingsForm
clients_connection_drupal_services_7_3::connectionSettingsForm_submit static function Submit handler for saving/updating connections of this class. Overrides clients_connection_base::connectionSettingsForm_submit
clients_connection_drupal_services_7_3::formatEndpoint function Format the connection's endpoint as a link. Overrides clients_connection_base::formatEndpoint
clients_connection_drupal_services_7_3::testConnectionNodeLoad function Connection test button handler: loading a node: Services 7.x-3.x. Overrides clients_connection_drupal_services::testConnectionNodeLoad