You are here

services_uuid.module in Services Client 7.2

Same filename and directory in other branches
  1. 7 services_uuid/services_uuid.module

File

services_uuid/services_uuid.module
View source
<?php

/**
 * Implementation of hook_services_resources()
 * Defines the UUID resource fo the services module
 *
 * @return array
 */
function services_uuid_services_resources() {
  $uuid_resource = array(
    'uuid' => array(
      'retrieve' => array(
        'callback' => '_services_uuid_resource_retrieve',
        'help' => t('Get the nid for a given UUID.'),
        'args' => array(
          array(
            'name' => 'type',
            'optional' => FALSE,
            'source' => array(
              'path' => 0,
            ),
            'type' => 'string',
            'description' => 'The data type for which to retrieve the UUID',
          ),
          array(
            'name' => 'uuid',
            'optional' => FALSE,
            'source' => array(
              'param' => 'uuid',
            ),
            'type' => 'string',
            'description' => 'The uuid for which to get the object id (nid, uid, etc...)',
          ),
        ),
        'access callback' => '_services_uuid_resource_access',
        'access arguments' => array(
          'view',
        ),
        'access arguments append' => TRUE,
      ),
    ),
  );
  return $uuid_resource;
}

/**
 * Function to handle actual retrieval of the xID for the UUID
 *
 * @param $type
 * @param $uuid
 * @return int
 */
function _services_uuid_resource_retrieve($type, $uuid) {

  // Latest version of uuid module (alpha6)
  if (function_exists('entity_get_id_by_uuid')) {
    $ids = entity_get_id_by_uuid($type, array(
      $uuid,
    ));
    return reset($ids);
  }
  else {
    switch ($type) {
      case 'node':
        $nid = uuid_node_find($uuid);
        return $nid;
      case 'user':
        $uid = uuid_user_find($uuid);
        return $uid;
      case 'taxonomy':
        $tid = uuid_taxonomy_term_find($uuid);
        return $tid;
      case 'role':
        $rid = uuid_role_find($uuid);
        return $rid;
      case 'comment':
        $cid = uuid_comment_find($uuid);
        return $cid;
      case 'file':
        $fid = uuid_file_find($uuid);
        return $fid;
      default:
        return services_error(t('Unsupported UUID resource %type type.', array(
          '%type' => $type,
        )), 406);
    }
  }
}

/**
 * Access callback to handle permissions for retrieving UUIDs
 *
 * @param string $op
 * @param array $args
 * @return boolean
 */
function _services_uuid_resource_access($op = 'view', $args = array()) {
  if ($op == 'view') {
    switch ($args[0]) {
      case 'node':
        $nid = _services_uuid_resource_retrieve($args[0], $args[1]);
        if ($nid) {
          $node = node_load($nid);
          return node_access('view', $node);
        }
        else {
          return TRUE;
        }
        break;
      case 'user':
        module_load_include('inc', 'services', 'resources/user_resource');
        $uid = _services_uuid_resource_retrieve($args[0], $args[1]);
        if ($uid) {
          return _user_resource_access('view', array(
            $uid,
          ));
        }
        else {
          return TRUE;
        }
      case 'taxonomy':
        return user_access('access content');
      case 'comment':
        return user_access('access comments');
      case 'file':
        module_load_include('inc', 'services', 'resources/file_resource');
        $fid = _services_uuid_resource_retrieve($args[0], $args[1]);
        return _file_resource_access('view', array(
          $fid,
        ));
    }
  }
}

Functions

Namesort descending Description
services_uuid_services_resources Implementation of hook_services_resources() Defines the UUID resource fo the services module
_services_uuid_resource_access Access callback to handle permissions for retrieving UUIDs
_services_uuid_resource_retrieve Function to handle actual retrieval of the xID for the UUID