You are here

wsconfig.module in Web Service Data 7

Main module for wsconfig

File

modules/wsconfig/wsconfig.module
View source
<?php

define('WSCONFIG_DEFAULT_DEGRADED_BACKOFF', 0);

/**
 * @file
 * Main module for wsconfig
 */

/**
 * Implements hook_flush_caches().
 */
function wsconfig_flush_caches() {

  // @todo check for any new modules which register hook_wsconfig_connector_info().
  $connectors = module_invoke_all('wsconfig_connector_info');

  // @todo see if we should store each connector separately in its own table
  variable_set('wsconfig_connectors', $connectors);

  // @todo see if we should store each processor separately in its own table
  $processors = module_invoke_all('wsconfig_processor_info');
  variable_set('wsconfig_processors', $processors);
}

/**
 * Implements hook_modules_enabled().
 */
function wsconfig_modules_enabled($modules) {

  // @todo check for any new modules which register hook_wsconfig_connector_info().
  $connectors = module_invoke_all('wsconfig_connector_info');

  // @todo see if we should store each connector separately in its own table
  variable_set('wsconfig_connectors', $connectors);

  // @todo see if we should store each processor separately in its own table
  $processors = module_invoke_all('wsconfig_processor_info');
  variable_set('wsconfig_processors', $processors);
}

/**
 * Implements hook_modules_disabled().
 */
function wsconfig_modules_disabled($modules) {

  // Load list of processors
  $processors = module_invoke_all('wsconfig_processor_info');

  // Save them
  variable_set('wsconfig_processors', $processors);

  // @todo report on any fields/entities which were dependant on that processor
  // @todo check for any removed module which register hook_wsconfig_connector_info().
  // @todo check for any new modules which register hook_wsconfig_connector_info().
  $connectors = module_invoke_all('wsconfig_connector_info');

  // @todo see if we should store each connector separately in its own table
  // @todo should we use ctools API to define our API?
  variable_set('wsconfig_connectors', $connectors);

  // @todo Add report listing fields which no longer have a valid connector
}

/**
 * Implements hook_ctools_plugin_directory().
 */
function wsconfig_ctools_plugin_directory($module, $plugin) {
  return 'plugins/' . $plugin;
}

/**
 * Load the CRUD operations for a given connector
 *
 * @param string $id
 *  Name of the connector, typically the module name
 * @param string $op [optional]
 *  Specific CRUD operation to return
 * @return array|boolean
 *  Returns an array of defined CRUD operations, FALSE otherwise.
 */
function wsconfig_connector_crud($id, $op = NULL) {

  // @todo
}

/**
 * Implements hook_entity_info().
 */
function wsconfig_entity_info() {
  $entities = array(
    'wsconfig' => array(
      'label' => t('Web Service Configuration'),
      'entity class' => 'WsConfig',
      'controller class' => 'WsConfigController',
      'base table' => 'wsconfig',
      'fieldable' => FALSE,
      'exportable' => TRUE,
      'translation' => array(
        'locale' => TRUE,
      ),
      'entity keys' => array(
        'id' => 'wsconfig_id',
        'name' => 'name',
        'bundle' => 'type',
      ),
      'bundle keys' => array(
        'bundle' => 'type',
      ),
      'bundles' => array(),
      'load hook' => 'wsconfig_load',
      'view modes' => array(
        'full' => array(
          'label' => t('Default'),
          'custom settings' => FALSE,
        ),
      ),
      'label callback' => 'wsconfig_class_label',
      'uri callback' => 'wsconfig_uri',
      'module' => 'wsconfig',
      'access callback' => 'wsconfig_access',
      // Enable Entity API admin UI
      'admin ui' => array(
        'path' => 'admin/structure/wsconfig',
        'file' => 'wsconfig.admin.inc',
        'controller class' => 'WsConfigUIController',
        'menu wildcard' => '%wsconfig',
      ),
    ),
  );
  $entities['wsconfig_type'] = array(
    'label' => t('Web Service Configuration Type'),
    'entity class' => 'WsConfigType',
    'controller class' => 'WsConfigTypeController',
    'base table' => 'wsconfig_type',
    'fieldable' => FALSE,
    'bundle of' => 'wsconfig',
    'exportable' => TRUE,
    'entity keys' => array(
      'id' => 'id',
      'name' => 'type',
      'label' => 'label',
    ),
    'module' => 'wsconfig',
    // Enable the entity API's admin UI
    'admin ui' => array(
      'path' => 'admin/structure/wsconfig_types',
      'file' => 'wsconfig_type.admin.inc',
      'controller class' => 'WsConfigTypeUIController',
    ),
    'access callback' => 'wsconfig_type_access',
  );
  return $entities;
}

/**
 * Label callback that refers to the entity classes label method.
 */
function wsconfig_class_label($entity) {
  if (isset($entity)) {
    return $entity
      ->label();
  }
  else {
    return "";
  }
}

/**
 * Implements hook_entity_info_alter().
 */
function wsconfig_entity_info_alter(&$entity_info) {
  foreach (wsconfig_get_types() as $type => $info) {
    $entity_info['wsconfig']['bundles'][$type] = array(
      'label' => $info->label,
      'admin' => array(
        'path' => 'admin/structure/wsconfig_types/%wsconfig_type',
        'real path' => 'admin/structure/wsconfig_types/' . $type,
        'bundle argument' => 4,
        'access arguments' => array(
          'administer wsconfig types',
        ),
      ),
    );
  }
}

/**
 * Implements hook_permission().
 */
function wsconfig_permission() {
  $permissions = array(
    'administer wsconfig types' => array(
      'title' => t('Administer web service configuration types'),
      'description' => t('Create and delete fields for web service configuration types.'),
    ),
    'administer wsconfig' => array(
      'title' => t('Administer web service configurations'),
      'description' => t('Edit and delete all web service configurations.'),
    ),
  );

  // Generate permissions
  foreach (wsconfig_get_types() as $type) {
    $type_name = check_plain($type->type);
    $permissions += array(
      "edit any {$type_name} wsconfig" => array(
        'title' => t('%type_name: Edit any wsconfig of this type.', array(
          '%type_name' => $type->label,
        )),
      ),
      "view any {$type_name} wsconfig" => array(
        'title' => t('%type_name: View any wsconfig of this type.', array(
          '%type_name' => $type->label,
        )),
      ),
    );
  }
  return $permissions;
}

/**
 * Implements hook_theme().
 */
function wsconfig_theme($existing, $type, $theme, $path) {
  return array(
    'wsconfig_add_list' => array(
      'variables' => array(
        'content' => array(),
      ),
      'file' => 'wsconfig.admin.inc',
    ),
    'wsconfig' => array(
      'render element' => 'elements',
      'template' => 'wsconfig',
    ),
    'wsconfig_sample_data' => array(
      'variables' => array(
        'wsconfig_sample_data',
        'wsconfig' => NULL,
      ),
      'template' => 'wsconfig-sample-data',
    ),
  );
}

/**
 * Implements hook_form_alter().
 * Sorts the WS Config type overview table.
 */
function wsconfig_form_wsconfig_type_overview_form_alter(&$form, &$form_state) {
  usort($form['table']['#rows'], '_wsconfig_form_overview_sort');
  $name = array_shift($form['table']['#header']);
  array_unshift($form['table']['#header'], $name, t('Active'));
  $types = wsconfig_get_types();
  foreach ($form['table']['#rows'] as $key => $row) {
    $type = $types[$row[0]['data']['#name']];
    if ($type
      ->isDisabled()) {
      $degraded = $type
        ->getDegraded();
      if ($degraded) {
        $active = t('Degraded - Calls to WSConfigs using this type will not be run.  Service will be automatically reenabled in %seconds.', array(
          '%seconds' => $degraded,
        ));
      }
      else {
        $active = t('Disabled - Calls to WSConfigs using this type will not be run.');
      }
    }
    else {
      $active = t('Enabled');
    }
    $name = array_shift($form['table']['#rows'][$key]);
    array_unshift($form['table']['#rows'][$key], $name, $active);
  }
}

/**
 * Implements hook_form_alter().
 * Sorts the WS Config overview table.
 */
function wsconfig_form_wsconfig_overview_form_alter(&$form, &$form_state) {
  usort($form['table']['#rows'], '_wsconfig_form_overview_sort');
}

/**
 * Sorting for the overview pages.
 */
function _wsconfig_form_overview_sort($a, $b) {
  return strcasecmp($a[0]['data']['#label'], $b[0]['data']['#label']);
}

/**
 * Determines whether the given user has access to a wsconfig.
 *
 * @param string $op
 *   The operation being performed. One of 'view', 'update', 'create', 'delete'
 *   or just 'edit' (being the same as 'create' or 'update').
 * @param object $wsconfig [optional]
 *   Optionally a model or a model type to check access for. If nothing is
 *   given, access for all models is determined.
 * @param object $account [optional]
 *   The user to check for. Leave it to NULL to check for the global user.
 * @return boolean
 *   Return TRUE if the account has access, FALSE otherwise.
 */
function wsconfig_access($op, $wsconfig = NULL, $account = NULL) {
  if (user_access('administer wsconfig', $account)) {
    return TRUE;
  }
  if (isset($wsconfig) && ($type_name = $wsconfig->type)) {
    $op = $op == 'view' ? 'view' : 'edit';
    if (user_access("{$op} any {$type_name} wsconfig", $account)) {
      return TRUE;
    }
  }
  return FALSE;
}

/**
 * Get the list of wsconfig types
 */
function wsconfig_get_types($type_name = NULL) {
  $types = entity_load_multiple_by_name('wsconfig_type', isset($type_name) ? array(
    $type_name,
  ) : FALSE);
  return isset($type_name) ? reset($types) : $types;
}

/**
 * Access callback for the entity API.
 */
function wsconfig_type_access($op, $type = NULL, $account = NULL) {
  return user_access('administer wsconfig types', $account);
}

/**
 * Menu argument loader; Load a wsconfig type by string.
 *
 * @param $type
 *   The machine-readable name of a wsconfig type to load.
 * @return
 *   A model type array or FALSE if $type does not exist.
 */
function wsconfig_type_load($type) {
  return wsconfig_get_types($type);
}

/**
 * Create an new instance of WsConfigType
 *
 * @param array $values [optional]
 *  Default configuration settings
 * @return object
 *  Returns an instance of WsConfigType
 */
function wsconfig_type_create($values = array()) {
  return entity_get_controller('wsconfig_type')
    ->create($values);
}

/**
 * Saves a wsconfig type to the db.
 */
function wsconfig_type_save(WsConfigType $type) {
  $type
    ->save();
}

/**
 * Deletes a wsconfig type from the db.
 */
function wsconfig_type_delete(WsConfigType $type) {
  $type
    ->delete();
}

/**
 * Gets the list of web service configurations keyed by serial id
 */
function wsconfig_get_list() {
  $result = _wsconfig_get_list();
  $list = array();
  while ($record = $result
    ->fetchObject()) {
    $list[$record->wsconfig_id] = $record->title;
  }
  return $list;
}

/**
 * Get list of web service configurations keyed by machine-name
 */
function wsconfig_get_list_by_name() {
  $result = _wsconfig_get_list();
  $list = array();
  while ($record = $result
    ->fetchObject()) {
    $list[$record->name] = $record->title;
  }
  return $list;
}

/**
 * Gets the list of web service configurations with tokens
 */
function wsconfig_get_list_tokens($wsconfig = "") {

  // @todo add caching layer?
  $query = db_select('wsconfig', 'w');
  $query
    ->fields('w', array(
    'name',
    'data',
  ))
    ->orderBy('name', 'DESC');
  if (!empty($wsconfig)) {
    $query
      ->condition('name', $wsconfig);
  }
  $result = $query
    ->execute();
  $list = array();
  while ($record = $result
    ->fetchObject()) {
    $data = unserialize($record->data);
    $tokens = array();
    foreach ($data as $calltitle => $call) {
      $matches = array();
      if (is_string($call)) {
        preg_match_all("/%[a-zA-Z0-9_]+/", $call, $matches);
        if (count($matches[0]) > 0) {
          $calltitle = explode('_data_method', $calltitle);
          $list[$record->name][$calltitle[0]] = $matches[0];
        }
      }
    }
  }
  return $list;
}

/**
 * Fetch a wsconfig object. Make sure that the wildcard you choose
 * in the model entity definition fits the function name here.
 *
 * @param $wsconfig_id
 *   Integer specifying the wsconfig id.
 * @param $reset
 *   A boolean indicating that the internal cache should be reset.
 * @return
 *   A fully-loaded $wsconfig object or FALSE if it cannot be loaded.
 *
 * @see wsconfig_load_multiple()
 */
function wsconfig_load($wsconfig_id, $reset = FALSE) {
  $wsconfigs = wsconfig_load_multiple(array(
    $wsconfig_id,
  ), array(), $reset);
  return reset($wsconfigs);
}

/**
 * Fetch a wsconfig object. Make sure that the wildcard you choose
 * in the model entity definition fits the function name here.
 *
 * @param $name
 *   Machine name of the wsconfig
 * @param $reset
 *   A boolean indicating that the internal cache should be reset.
 * @return
 *   A fully-loaded $wsconfig object or FALSE if it cannot be loaded.
 *
 * @see wsconfig_load()
 */
function wsconfig_load_by_name($name, $reset = FALSE) {
  $id =& drupal_static(__FUNCTION__ . ':' . $name);
  if ($reset or !$id) {
    $query = new EntityFieldQuery();
    $result = $query
      ->entityCondition('entity_type', 'wsconfig')
      ->propertyCondition('name', $name)
      ->execute();

    // Check the results
    if (!isset($result['wsconfig']) or !is_array($result['wsconfig'])) {
      return FALSE;
    }
    $ids = array_keys($result['wsconfig']);
    $id = reset($ids);
  }

  // Load the entity
  return wsconfig_load($id, $reset);
}

/**
 * Load multiple wsconfigs based on certain conditions.
 *
 * @param $wsconfig_ids
 *   An array of wsconfig IDs.
 * @param $conditions
 *   An array of conditions to match against the {wsconfig} table.
 * @param $reset
 *   A boolean indicating that the internal cache should be reset.
 * @return
 *   An array of wsconfig objects, indexed by wsconfig_id.
 *
 * @see entity_load()
 * @see model_load()
 */
function wsconfig_load_multiple($wsconfig_ids = array(), $conditions = array(), $reset = FALSE) {
  return entity_load('wsconfig', $wsconfig_ids, $conditions, $reset);
}

/**
 * Check if a wsconfig exists using a machine name
 *
 * @param string $name
 *  Machine name to check for
 * @return boolean
 *  Returns TRUE if a config exists with that name, FALSE otherwise.
 */
function wsconfig_exists_by_name($name) {
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', 'wsconfig')
    ->propertyCondition('name', $name)
    ->count();
  $result = $query
    ->execute();
  return $result > 0;
}

/**
 * Deletes a wsconfig
 */
function wsconfig_delete(WsConfig $wsconfig) {
  $wsconfig
    ->delete();
}

/**
 * Delete multiple models.
 *
 * @param array $wsconfig_ids
 *  An array of wsconfig IDs.
 */
function wsconfig_delete_multiple(array $wsconfig_ids) {
  entity_get_controller('wsconfig')
    ->delete($wsconfig_ids);
}

/**
 * Create a wsconfig object.
 */
function wsconfig_create($values = array()) {
  return entity_get_controller('wsconfig')
    ->create($values);
}

/**
 * Saves a wsconfig to the database.
 *
 * @param $wsconfig
 *   The wsconfig object.
 */
function wsconfig_save(WsConfig $wsconfig) {
  return $wsconfig
    ->save();
}

/**
 * URI callback for models
 */
function wsconfig_uri(WsConfig $wsconfig) {
  return array(
    'path' => 'admin/structure/wsconfig/' . $wsconfig->wsconfig_id,
  );
}

/**
 * Menu title callback for showing individual entities
 */
function wsconfig_page_title(WsConfig $wsconfig) {
  return $wsconfig->title;
}

/**
 * Sets up content to show an individual model
 * @todo - get rid of drupal_set_title();
 */
function wsconfig_page_view($wsconfig, $view_mode = 'full') {
  $controller = entity_get_controller('wsconfig');
  $content = $controller
    ->view(array(
    $wsconfig->wsconfig_id => $wsconfig,
  ));
  drupal_set_title($wsconfig->title);
  return $content;
}

/**
 * Get a list of processors which support fields
 *
 * @param array $types [optional]
 *  List of field types
 * @return array
 *  Returns an array of key/value pairs of classnames and display names for
 *  supported processors.
 */
function wsconfig_get_field_processors($types = array()) {
  $field_processors = array();
  $defined_proc = variable_get('wsconfig_processors', array());
  foreach ($defined_proc as $classname => $value) {
    if (!empty($value['fields'])) {
      foreach ($value['fields'] as $type => $displaytext) {
        if (!empty($types) && in_array($type, $types) || empty($types)) {
          $field_processors[$classname] = $displaytext;
        }
      }
    }
  }
  return $field_processors;
}

/**
 * Get a list of processors which return forms
 *
 * @return array
 *  Returns an array of key/value pairs of classnames and display names for
 *  supported processors.
 */
function wsconfig_get_form_processors() {
  $form_processors = array();
  $defined_proc = variable_get('wsconfig_processors', array());
  foreach ($defined_proc as $classname => $value) {
    if (isset($value['form'])) {
      $form_processors[$classname] = $value['form'];
    }
  }
  return $form_processors;
}

/**
 * Get a list of processors which return data
 *
 * @return array
 *  Returns an array of key/value pairs of classnames and display names for
 *  supported processors.
 */
function wsconfig_get_data_processors() {
  $form_processors = array();
  $defined_proc = variable_get('wsconfig_processors', array());
  foreach ($defined_proc as $classname => $value) {
    if (isset($value['data'])) {
      $form_processors[$classname] = $value['data'];
    }
  }
  return $form_processors;
}

/**
 * Generate list of web service configurations
 */
function _wsconfig_get_list() {

  // @todo add caching layer?
  $query = db_select('wsconfig', 'w');
  $query
    ->fields('w', array(
    'wsconfig_id',
    'title',
    'name',
  ))
    ->orderBy('name', 'DESC');
  $result = $query
    ->execute();
  return $result;
}

Functions

Namesort descending Description
wsconfig_access Determines whether the given user has access to a wsconfig.
wsconfig_class_label Label callback that refers to the entity classes label method.
wsconfig_connector_crud Load the CRUD operations for a given connector
wsconfig_create Create a wsconfig object.
wsconfig_ctools_plugin_directory Implements hook_ctools_plugin_directory().
wsconfig_delete Deletes a wsconfig
wsconfig_delete_multiple Delete multiple models.
wsconfig_entity_info Implements hook_entity_info().
wsconfig_entity_info_alter Implements hook_entity_info_alter().
wsconfig_exists_by_name Check if a wsconfig exists using a machine name
wsconfig_flush_caches Implements hook_flush_caches().
wsconfig_form_wsconfig_overview_form_alter Implements hook_form_alter(). Sorts the WS Config overview table.
wsconfig_form_wsconfig_type_overview_form_alter Implements hook_form_alter(). Sorts the WS Config type overview table.
wsconfig_get_data_processors Get a list of processors which return data
wsconfig_get_field_processors Get a list of processors which support fields
wsconfig_get_form_processors Get a list of processors which return forms
wsconfig_get_list Gets the list of web service configurations keyed by serial id
wsconfig_get_list_by_name Get list of web service configurations keyed by machine-name
wsconfig_get_list_tokens Gets the list of web service configurations with tokens
wsconfig_get_types Get the list of wsconfig types
wsconfig_load Fetch a wsconfig object. Make sure that the wildcard you choose in the model entity definition fits the function name here.
wsconfig_load_by_name Fetch a wsconfig object. Make sure that the wildcard you choose in the model entity definition fits the function name here.
wsconfig_load_multiple Load multiple wsconfigs based on certain conditions.
wsconfig_modules_disabled Implements hook_modules_disabled().
wsconfig_modules_enabled Implements hook_modules_enabled().
wsconfig_page_title Menu title callback for showing individual entities
wsconfig_page_view Sets up content to show an individual model @todo - get rid of drupal_set_title();
wsconfig_permission Implements hook_permission().
wsconfig_save Saves a wsconfig to the database.
wsconfig_theme Implements hook_theme().
wsconfig_type_access Access callback for the entity API.
wsconfig_type_create Create an new instance of WsConfigType
wsconfig_type_delete Deletes a wsconfig type from the db.
wsconfig_type_load Menu argument loader; Load a wsconfig type by string.
wsconfig_type_save Saves a wsconfig type to the db.
wsconfig_uri URI callback for models
_wsconfig_form_overview_sort Sorting for the overview pages.
_wsconfig_get_list Generate list of web service configurations

Constants