You are here

entity_share_ui_client.endpoint.inc in Entity Share 7

Class for handling endpoints.

File

modules/entity_share_ui/modules/entity_share_ui_client/includes/entity_share_ui_client.endpoint.inc
View source
<?php

/**
 * @file
 * Class for handling endpoints.
 */

/**
 * Endpoint management.
 */
class EntityShareUiClientEndpoint {
  const TABLE_NAME = 'entity_share_endpoints';

  /**
   * Load an endpoint.
   *
   * @param mixed $eid
   *   Internal id or machine name.
   * @param bool $reset
   *   Bypass the static cache.
   *
   * @return mixed
   *   The endpoint array or FALSE.
   */
  public static function load($eid, $reset = FALSE) {
    if (is_array($eid)) {
      return self::loadAll($eid, $reset);
    }
    static $drupal_static_fast;
    if (!isset($drupal_static_fast)) {
      $drupal_static_fast['result'] =& drupal_static(__METHOD__);
    }
    $result =& $drupal_static_fast['result'];
    if (!isset($result[$eid]) || $reset) {
      $field_name = is_numeric($eid) ? 'eid' : 'name';
      $result[$eid] = db_select(self::TABLE_NAME, 'e')
        ->fields('e')
        ->condition($field_name, $eid, '=')
        ->execute()
        ->fetchAssoc();
    }
    return $result[$eid];
  }

  /**
   * Load all endpoints.
   *
   * @param array $eids
   *   Endpoint ids to load (optional).
   * @param bool $only_enabled
   *   Load only the enabled endpoints if TRUE, else load all endpoints.
   * @param bool $reset
   *   Bypass the static cache.
   *
   * @return mixed
   *   The endpoints arrays or FALSE.
   */
  public static function loadAll(array $eids = NULL, $only_enabled = FALSE, $reset = FALSE) {
    static $drupal_static_fast;
    if (!isset($drupal_static_fast)) {
      $drupal_static_fast['result'] =& drupal_static(__METHOD__);
    }
    $result =& $drupal_static_fast['result'];
    if (empty($eids)) {
      $eids = array(
        'ALL',
      );
    }
    $cache_key = implode('-', $eids);
    if (!isset($result[$cache_key]) || $reset) {
      $query = db_select(self::TABLE_NAME, 'e')
        ->fields('e');
      if (!empty($eids) && $eids[0] != 'ALL') {
        $query
          ->condition('eid', $eids, 'IN');
      }

      // Load only the enabled endpoints.
      if ($only_enabled) {
        $query
          ->condition('enabled', 1, '=');
      }
      $result[$cache_key] = $query
        ->execute()
        ->fetchAllAssoc('eid', PDO::FETCH_ASSOC);
    }
    return $result[$cache_key];
  }

  /**
   * Save an endpoint (create or update an endpoint).
   *
   * @param array $endpoint
   *   Datas of the endpoint.
   *
   * @code
   *  $endpoint = array(
   *    'name' => 'my_machine_name',
   *    'title' => 'My Endpoint 1',
   *    'description' => 'My Endpoint 1 description',
   *    'url' => 'http://server1/entity_share/rest_api'
   *  );
   * @endcode
   *
   * @return bool|int
   *   Success or not.
   */
  public static function save(array $endpoint) {
    if (empty($endpoint['password'])) {
      unset($endpoint['password']);
    }
    return drupal_write_record(self::TABLE_NAME, $endpoint, !empty($endpoint['eid']) ? 'eid' : array());
  }

  /**
   * Delete an endpoint.
   *
   * @param string $name
   *   Machine name.
   *
   * @return bool
   *   Success or not.
   */
  public static function delete($name) {
    $nb = db_delete(self::TABLE_NAME)
      ->condition('name', $name, '=')
      ->execute();
    return $nb > 0 ? TRUE : FALSE;
  }

}

Classes

Namesort descending Description
EntityShareUiClientEndpoint Endpoint management.