Skip to content

Instantly share code, notes, and snippets.

@arturo-c
Created November 22, 2011 15:11
Show Gist options
  • Save arturo-c/1385888 to your computer and use it in GitHub Desktop.
Save arturo-c/1385888 to your computer and use it in GitHub Desktop.
<?php
/**
* @file
* Loads dependencies and defines the apci_groups resource
*/
module_load_include('inc', 'og_services', 'og_services');
module_load_include('inc', 'services_views', 'services_views.resource');
module_load_include('inc', 'apci_public_api_services', 'helpers');
function apci_public_api_services_groups_resource_definition() {
$response = new GroupResponse;
$apci_groups_resource = array(
'apci_groups' => array(
'retrieve' => array(
'file' => array('type' => 'inc', 'module' => 'apci_public_api_services', 'name' => 'resources/groups_resource'),
'callback' => '_apci_api_groups_retrieve',
'help' => 'Get Group information by Group ID',
'args' => array(
array(
'name' => 'uuid',
'optional' => FALSE,
'source' => array('path' => 0),
'type' => 'string',
'description' => 'The uuid of the group to get',
),
array(
'name' => 'fields',
'optional' => TRUE,
'type' => 'string',
'description' => 'The fields to get.',
'default value' => '*',
'source' => array('param' => 'fields'),
),
array(
'name' => 'modified',
'optional' => TRUE,
'type' => 'string',
'description' => 'Only return group object if updated since the supplied if-modified-since date/time.',
'default value' => NULL,
'source' => array('headers' => 'IF_MODIFIED_SINCE'),
),
),// args
'response' => $response->getResponse('group', 'view'),
// Anyone can look at groups, so this stays public. Further filtering in callback.
'access callback' => 'services_access_menu',
// 'access arguments' => array('retrieve'),
// 'access arguments append' => TRUE,
),
'create' => array(
'file' => array('type' => 'inc', 'module' => 'apci_public_api_services', 'name' => 'resources/groups_resource'),
'callback' => '_apci_api_groups_create',
'help' => 'Create Groups',
'args' => array(
array(
'name' => 'title',
'optional' => FALSE,
'source' => 'data',
'description' => 'Title of the group',
'type' => 'string',
),
array(
'name' => 'description',
'optional' => FALSE,
'source' => 'data',
'description' => 'Description of the group',
'type' => 'string',
),
array(
'name' => 'location',
'optional' => FALSE,
'source' => 'data',
'description' => 'Location of the group',
'type' => 'array',
'default value' => NULL,
),
array(
'name' => 'category',
'optional' => FALSE,
'source' => 'data',
'description' => 'Categories of the group [0] = primary, [1] = secondary',
'type' => 'array',
),
array(
'name' => 'group_type',
'optional' => TRUE,
'source' => 'data',
'description' => 'Further define your group by specifying your group type.',
'type' => 'string',
'default value' => 'team',
),
array(
'name' => 'web_address',
'optional' => TRUE,
'source' => 'data',
'description' => 'The address after http://www.allplayers.com/g/',
'type' => 'string',
'default value' => NULL,
),
array(
'name' => 'status',
'optional' => TRUE,
'source' => 'data',
'description' => 'Is the group active or inactive. (active by default)',
'type' => 'string',
'default value' => 'active',
),
array(
'name' => 'groupmates_enabled',
'optional' => TRUE,
'source' => 'data',
'description' => 'Are groupmates enabled on this group?',
'type' => 'string',
'default value' => TRUE,
),
),
'access callback' => '_apci_api_groups_access',
'access arguments' => array('create'),
'access arguments append' => TRUE,
'services_docs access arguments' => array('services_docs'),
),
'update' => array(
'file' => array('type' => 'inc', 'module' => 'apci_public_api_services', 'name' => 'resources/groups_resource'),
'callback' => '_apci_api_groups_update',
'help' => 'Update Groups',
'args' => array(
array(
'name' => 'uuid',
'optional' => FALSE,
'source' => array('path' => 0),
'description' => 'UUID of the group being updated',
'type' => 'string',
),
array(
'name' => 'title',
'optional' => TRUE,
'source' => 'data',
'description' => 'Title of the group',
'type' => 'string',
'default value' => NULL,
),
array(
'name' => 'description',
'optional' => TRUE,
'source' => 'data',
'description' => 'Description of the group',
'type' => 'string',
'default value' => NULL,
),
array(
'name' => 'location',
'optional' => TRUE,
'source' => 'data',
'description' => 'Location of the group',
'type' => 'array',
'default value' => NULL,
),
array(
'name' => 'category',
'optional' => TRUE,
'source' => 'data',
'description' => 'Categories of the group [0] = primary, [1] = secondary',
'type' => 'array',
'default value' => NULL,
),
array(
'name' => 'status',
'optional' => TRUE,
'source' => 'data',
'description' => 'Is the group active or inactive. (active by default)',
'type' => 'string',
'default value' => NULL
),
array(
'name' => 'groupmates_enabled',
'optional' => TRUE,
'source' => 'data',
'description' => 'Are groupmates enabled on this group?',
'type' => 'string',
'default value' => 'TRUE',
),
),
'access callback' => '_apci_api_groups_access',
'access arguments' => array('update'),
'access arguments append' => TRUE,
'services_docs access arguments' => array('services_docs'),
),
'targeted_actions' => array(
'join' => array(
'help' => 'Subscribe a user to a group',
'file' => array('type' => 'inc', 'module' => 'apci_public_api_services', 'name' => 'resources/groups_resource'),
'callback' => '_apci_api_groups_subscribe_user',
'access callback' => '_apci_api_groups_access',
'access arguments' => array('join'),
'access arguments append' => TRUE,
'args' => array(
array(
'name' => 'group_uuid',
'type' => 'string',
'description' => 'A uuid of a group on the system',
'source' => array('path' => '0'),
'optional' => false,
),
array(
'name' => 'uuid',
'type' => 'string',
'description' => 'A uuid of a user on the system',
'source' => array('path' => '2'),
'optional' => false,
),
), // args
), // join
'leave' => array(
'help' => 'Unsubscribe a user to a group',
'file' => array('type' => 'inc', 'module' => 'apci_public_api_services', 'name' => 'resources/groups_resource'),
'callback' => '_apci_api_groups_unsubscribe_user',
'access callback' => '_apci_api_groups_access',
'access arguments' => array('leave'),
'access arguments append' => TRUE,
'args' => array(
array(
'name' => 'group_uuid',
'type' => 'string',
'description' => 'A uuid of a group on the system',
'source' => array('path' => '0'),
'optional' => false,
),
array(
'name' => 'uuid',
'type' => 'string',
'description' => 'A uuid of a user on the system',
'source' => array('path' => '2'),
'optional' => false,
),
), // args
), // leave
'register' => array(
'help' => 'Register a user to a group.',
'file' => array('type' => 'inc', 'module' => 'apci_public_api_services', 'name' => 'resources/groups_resource'),
'callback' => '_apci_api_groups_register_user',
'access callback' => '_apci_api_groups_access',
'access arguments' => array('register'),
'access arguments append' => TRUE,
'args' => array(
array(
'name' => 'group_uuid',
'type' => 'string',
'description' => 'A uuid of a group on the system',
'source' => array('path' => '0'),
'optional' => false,
),
array(
'name' => 'uuid',
'type' => 'string',
'description' => 'A uuid of a user on the system',
'source' => array('path' => '2'),
'optional' => false,
),
array(
'name' => 'role_uuid',
'type' => 'string',
'description' => 'A uuid of a role on the system',
'source' => 'data',
'optional' => true,
),
array(
'name' => 'fields',
'type' => 'array',
'description' => 'An array of additional required fields',
'source' => 'data',
'optional' => true,
),
), // args
), // register
'addrole' => array(
'file' => array('type' => 'inc', 'module' => 'apci_public_api_services', 'name' => 'resources/groups_resource'),
'callback' => '_apci_api_groups_addrole',
'access callback' => '_apci_api_groups_access',
'access arguments' => array('addrole'),
'access arguments append' => TRUE,
'args' => array(
array(
'name' => 'group_uuid',
'optional' => FALSE,
'description' => 'A uuid of a group on the system',
'type' => 'string',
'source' => array('path' => 0),
),
array(
'name' => 'uuid',
'type' => 'string',
'description' => 'A uuid of a user on the system',
'source' => array('path' => '2'),
'optional' => FALSE,
),
array(
'name' => 'role_uuid',
'type' => 'string',
'description' => 'A uuid of a role in that group',
'source' => array('path' => '3'),
'optional' => FALSE,
),
array(
'name' => 'options',
'type' => 'struct',
'description' => 'Options related to role add, such as should_pay and payment_method.',
'source' => 'data',
'optional' => TRUE,
),
), // args
), // add role
), // targeted actions
'index' => array(
'file' => array('type' => 'inc', 'module' => 'apci_public_api_services', 'name' => 'resources/groups_resource'),
'callback' => '_apci_api_groups_index',
'help' => 'Search Groups',
'args' => array(
array(
'name' => 'search',
'optional' => TRUE,
'type' => 'string',
'description' => 'Search terms.',
'default value' => '*',
'source' => array('param' => 'search'),
),
array(
'name' => 'distance',
'optional' => TRUE,
'type' => 'string',
'description' => 'Distance from postal code (format: distance[postal_code]=00000&distance[search_distance]=100&distance[search_units]=mile).',
'default value' => '*',
'source' => array('param' => 'distance'),
),
array(
'name' => 'fields',
'optional' => TRUE,
'type' => 'string',
'description' => 'The fields to get.',
'default value' => '*',
'source' => array('param' => 'fields'),
),
array(
'name' => 'limit',
'optional' => TRUE,
'type' => 'int',
'description' => 'Limit the amount of groups returned, default 10',
'default value' => 10,
'source' => array('param' => 'limit'),
),
array(
'name' => 'offset',
'optional' => TRUE,
'type' => 'int',
'description' => 'Offset the list of groups returned, default 0',
'default value' => 0,
'source' => array('param' => 'offset'),
),
array(
'name' => 'sort',
'optional' => TRUE,
'type' => 'string',
'description' => 'Sort the groups returned, default activity level',
'default value' => 'activity',
'source' => array('param' => 'sort'),
),
array(
'name' => 'modified',
'optional' => TRUE,
'type' => 'string',
'description' => 'Return only groups created or updated since the supplied unix timestamp.',
'default value' => NULL,
'source' => array('param' => 'modified'),
),
),
'response' => $response->getResponse('group', 'view'),
// Anyone can search for groups without being logged in.
'access callback' => 'services_access_menu',
'access arguments append' => FALSE,
),// index
'relationships' => array(
'albums' => array(
'help' => 'retrieve a list of albums for this group',
'file' => array('type' => 'inc', 'module' => 'apci_public_api_services', 'name' => 'resources/groups_resource'),
'callback' => '_apci_api_groups_albums_retrieve',
'args' => array(
array(
'name' => 'uuid',
'optional' => FALSE,
'source' => array('path' => 0),
'type' => 'string',
'description' => 'The uuid of the group that the albums are for',
),// uid
array(
'name' => 'fields',
'optional' => TRUE,
'type' => 'string',
'description' => 'The fields to get.',
'default value' => '*',
'source' => array('param' => 'fields'),
),// fields
array(
'name' => 'limit',
'optional' => TRUE,
'type' => 'int',
'description' => 'Limit of albums.',
'default value' => 10,
'source' => array('param' => 'limit'),
),// limit
),// args
'response' => $response->getResponse('album', 'view'),
// Anyone can look at albums, further filtering done in callback.
'access callback' => 'services_access_menu', // dummy
'access arguments' => array('view'),
'access arguments append' => TRUE,
),// albums
'photos' => array(
'help' => 'retrieve a list of photos for this group',
'file' => array('type' => 'inc', 'module' => 'apci_public_api_services', 'name' => 'resources/groups_resource'),
'callback' => '_apci_api_groups_photos_retrieve',
'args' => array(
array(
'name' => 'gid',
'optional' => FALSE,
'source' => array('path' => 0),
'type' => 'int',
'description' => 'The nid of the group for which to get photos',
),// uid
array(
'name' => 'fields',
'optional' => TRUE,
'type' => 'string',
'description' => 'The fields to get.',
'default value' => '*',
'source' => array('param' => 'fields'),
),// fields
array(
'name' => 'limit',
'optional' => TRUE,
'type' => 'int',
'description' => 'How many photos would you like to receive from each album',
'default value' => 10,
'source' => array('param' => 'limit'),
),// limit
),// args
'response' => $response->getResponse('photo', 'view'),
// anyone can look at photos, further filtering done in callback.
'access callback' => 'services_access_menu', // dummy
// 'access arguments' => array('view'),
// 'access arguments append' => TRUE,
),// photos
'events' => array(
'help' => 'retrieve a list of events for this group',
'file' => array('type' => 'inc', 'module' => 'apci_public_api_services', 'name' => 'resources/groups_resource'),
'callback' => '_apci_api_groups_events_retrieve',
'args' => array(
array(
'name' => 'uuid',
'optional' => FALSE,
'source' => array('path' => 0),
'type' => 'string',
'description' => 'The uuid of the group for which to get events',
),// uuid
array(
'name' => 'upcoming',
'optional' => TRUE,
'source' => array('path' => 2),
'type' => 'string',
'description' => 'Determines if the call is for upcoming events',
'default value' => NULL,
),// upcoming
array(
'name' => 'start',
'optional' => TRUE,
'source' => array('param' => 'start'),
'type' => 'string',
'default value' => '@',
'description' => 'The start date for which to retrieve events, passed in as 2011-9-22',
),// start
array(
'name' => 'end',
'optional' => TRUE,
'source' => array('param' => 'end'),
'type' => 'string',
'default value' => '@',
'description' => 'The end date for which to retrieve events, passed in as 2011-10-22',
),// end
array(
'name' => 'fields',
'optional' => TRUE,
'type' => 'string',
'description' => 'The fields to get.',
'default value' => '*',
'source' => array('param' => 'fields'),
),// fields
array(
'name' => 'limit',
'optional' => TRUE,
'type' => 'int',
'description' => 'How many events would you like to receive',
'default value' => 10,
'source' => array('param' => 'limit'),
),// limit
array(
'name' => 'offset',
'optional' => TRUE,
'type' => 'int',
'description' => 'Offset the list of events returned, default 0',
'default value' => 0,
'source' => array('param' => 'offset'),
),
),// args
// anyone can look at photos, further filtering done in callback.
'access callback' => 'services_access_menu', // dummy
// 'access arguments' => array('view'),
// 'access arguments append' => TRUE,
),// events
'members' => array(
'help' => 'retrieve a list of members for this group',
'file' => array('type' => 'inc', 'module' => 'apci_public_api_services', 'name' => 'resources/groups_resource'),
'callback' => '_apci_api_groups_members_retrieve',
'args' => array(
array(
'name' => 'uuid',
'optional' => FALSE,
'source' => array('path' => 0),
'type' => 'string',
'description' => 'The uuid of the group for which to get members',
),// uid
array(
'name' => 'fields',
'optional' => TRUE,
'type' => 'string',
'description' => 'The fields to get.',
'default value' => '*',
'source' => array('param' => 'fields'),
),// fields
array(
'name' => 'limit',
'optional' => TRUE,
'type' => 'int',
'description' => 'How many members would you like to receive',
'default value' => 10,
'source' => array('param' => 'limit'),
),// limit
array(
'name' => 'admins_only',
'optional' => TRUE,
'type' => 'int',
'description' => 'Return only members of the group.',
'default value' => NULL,
'source' => array('param' => 'admins_only'),
),// admins only
),// args
'response' => $response->getResponse('member', 'view'),
'access callback' => 'services_access_menu', // dummy
// 'access arguments' => array('view'),
// 'access arguments append' => TRUE,
),// members
'resources' => array(
'help' => 'retrieve a list of resources for this group',
'file' => array('type' => 'inc', 'module' => 'apci_public_api_services', 'name' => 'resources/groups_resource'),
'callback' => '_apci_api_groups_resources_retrieve',
'args' => array(
array(
'name' => 'uuid',
'optional' => FALSE,
'source' => array('path' => 0),
'type' => 'string',
'description' => 'The uuid of the group for which to get resources',
),// uid
array(
'name' => 'fields',
'optional' => TRUE,
'type' => 'string',
'description' => 'The fields to get.',
'default value' => '*',
'source' => array('param' => 'fields'),
),// fields
array(
'name' => 'limit',
'optional' => TRUE,
'type' => 'int',
'description' => 'How many resources to be retrieved',
'default value' => 10,
'source' => array('param' => 'limit'),
),// limit
),// args
'response' => $response->getResponse('resource', 'view'),
// anyone can look at photos, further filtering done in callback.
'access callback' => 'services_access_menu', // dummy
// 'access arguments' => array('view'),
// 'access arguments append' => TRUE,
),// resources
),// relationships
),// apci_groups
);// end groups_resource
return $apci_groups_resource;
}// end def
function _apci_api_groups_access($op, $args){
global $user;
$access = FALSE;
$logged_in = $user->uid ? TRUE : FALSE;
$node = node_get_by_uuid($args[0]);
// If the user does not have the permission to access the services, immediately return FALSE
if (user_access('consume public api services') == TRUE) {
switch ($op) {
case 'update':
$group = node_get_by_uuid($args['0']);
if (og_is_group_admin($group, NULL) == TRUE){
$access = TRUE;
}
else{
return services_error(t('You do not have sufficient priviliges to update this group'), 403);
}
break;
case 'create':
$access = $logged_in;
break;
}
// If the user has permission to administer all groups, immediately return TRUE for join/leave and addrole operations.
if (user_access('administer organic groups')) {
return TRUE;
} else {
switch ($op) {
case 'join':
$access = og_is_group_admin($node);
break;
case 'leave':
$access = og_is_group_admin($node);
break;
case 'addrole':
$access = og_is_group_admin($node);
break;
}
}
}
if ($op == 'register') {
drupal_load('module', 'spaces_og_registration');
$space = spaces_load('og', $node->nid);
if ($space) {
if (spaces_og_registration_group_access() || spaces_og_registration_user_access()) {
$access = TRUE;
}
$access = TRUE;
}
}
return $access;
}
/**
* Groups Resource Callbacks
*/
function _apci_api_groups_retrieve($uuid, $fields, $modified = NULL) {
// Format the updated date parameter as a range from $updated until now().
if (!empty($modified) && $modified = date(DATE_W3C, $modified)) {
$modified .= '--@';
}
else {
$modified = NULL;
}
$view = services_views_retrieve('services_groups', 'group_retrieve', $args = array($uuid, $modified), $offset, $limit, 'view');
if (!empty($view->args[0]) && !$view->argument['uuid']->argument_validated) {
services_error(t('Unknown user uuid: !uuid', array('!uuid' => $uuid)), 404);
return FALSE;
}
if (empty($view->result)) {
services_error(t('No results found.'), 304);
return FALSE;
}
if ($fields == '*') {
$fields = NULL;
}
else {
$fields = explode(',', $fields);
}
// Map the views data into the response
$response = new GroupResponse;
$return = $response->mapViewsData('group', 'view', $view, $fields);
// Add extra data
$return = _apci_api_groups_add_extra_views_data($return, $fields);
if (!empty($return)) {
return $return[0];
} else {
return FALSE;
}
}
function _apci_api_groups_index($search, $distance, $fields, $limit = 10, $offset = 0, $sort = 'activity', $modified = NULL) {
// Retrieve the proper view/display for this inquiry
switch($sort) {
case 'updated':
$display = 'group_index_updated';
break;
case 'activity':
default:
$display = 'group_index';
break;
}
// Format the updated date parameter as a range from $updated until now().
if (!empty($modified) && $modified = date(DATE_W3C, $modified)) {
$modified .= '--@';
}
else {
$modified = NULL;
}
$view = services_views_retrieve('services_groups', $display, $args = array($modified), $offset, $limit, 'view');
if (empty($view->result)) {
services_error(t('No results found.'), 204);
return FALSE;
}
if ($fields == '*') {
$fields = NULL;
}
else {
$fields = explode(',', $fields);
}
// Map the views data into the response
$response = new GroupResponse;
$return = $response->mapViewsData('group', 'view', $view, $fields);
// Add extra data
$return = _apci_api_groups_add_extra_views_data($return, $fields);
return $return;
}
function _apci_api_groups_albums_retrieve($uuid, $fields, $limit) {
$view = services_views_retrieve('services_photo_albums', 'page_1', $args = array($uuid), $offset = 0, $limit, 'view');
//$return = _apci_api_groups_view_prepare($return, 'albums', $fields);
if ($fields == '*') {
$fields = NULL;
}
else {
$fields = explode(',', $fields);
}
// Map the views data into the response
$response = new GroupResponse;
$return = $response->mapViewsData('album', 'view', $view, $fields);
// Add content to the return result that does not come directly from the view
foreach($return as $key => $row) {
if (array_key_exists('uri', $return[$key])) {
$return[$key]['uri'] = services_resource_uri(array('albums', $return[$key]['uuid']));
}
if (array_key_exists('rep_photo', $return[$key])) {
$rep_photo = field_file_load($return[$key]['rep_photo']);
$return[$key]['rep_photo'] = imagecache_create_url('profile_small', $rep_photo['filepath']);
}
}
return $return;
}
function _apci_api_groups_photos_retrieve($uuid, $fields, $limit) {
// $return ends up containing a structure of multiple albums, containing multiple photos
if ($fields == '*') {
$fields = NULL;
}
else {
$fields = explode(',', $fields);
}
$albums = _apci_api_groups_albums_retrieve($uuid, '*', 0, 0);
$return = array();
$photo_count = 0;
foreach ($albums as $album) {
if ($photo_count < $limit) {
$album_node = node_get_by_uuid($album['uuid']);
$to_load = $limit - $photo_count;
// original
//$return['uuid_' . $album['uuid']] = services_views_retrieve('spaces_photo_album', 'default', $args = array($album_node->nid), $offset = 0, $to_load);
// Map the views data into the response (one album at a time)
$album_view = services_views_retrieve('spaces_photo_album', 'default', $args = array($album_node->nid), $offset = 0, $to_load, 'view');
$response = new GroupResponse;
$album_key = 'uuid_' . $album['uuid'];
$return[$album_key] = $response->mapViewsData('photo', 'view', $album_view, $fields);
// Add content to the return result that does not come directly from the view
foreach($return[$album_key] as $key => $row) {
$photo_node = node_get_by_uuid($row['photo_uuid']);
// add album uuid to each record
if (array_key_exists('album_uuid', $return[$album_key][$key])) {
$return[$album_key][$key]['album_uuid'] = $album['uuid'];
}
// add groups (from a node_load()->og_groups
if (array_key_exists('group_uuid', $return[$album_key][$key])) {
$return[$album_key][$key]['group_uuid'] = array();
foreach ($photo_node->og_groups as $og_group_nid){
$return[$album_key][$key]['group_uuid'][] = uuid_get_uuid('node', 'nid', $og_group_nid); // uuid from nid
}
}
// add images: photo_thumb, photo_full
if (array_key_exists('photo_thumb', $return[$album_key][$key])) {
$photo_thumb = field_file_load($return[$album_key][$key]['photo_thumb']);
$return[$album_key][$key]['photo_thumb'] = imagecache_create_url('profile_small', $photo_thumb['filepath']);
}
if (array_key_exists('photo_full', $return[$album_key][$key])) {
$photo_full = field_file_load($return[$album_key][$key]['photo_full']);
$return[$album_key][$key]['photo_full'] = imagecache_create_url('spaces_photos_photo', $photo_full['filepath']);
}
// add uri
if (array_key_exists('uri', $return[$album_key][$key])) {
$return[$album_key][$key]['uri'] = services_resource_uri(array('photos', $return[$album_key][$key]['p_uuid']));
}
}
$photo_count += $album['photo_count'];
}
}
//$return = _apci_api_groups_view_prepare($return, 'photos', $fields);
return $return;
}
/**
* Retrieves events for a given month within a given group. If no month, defaults to current
*/
function _apci_api_groups_events_retrieve($uuid, $upcoming = NULL, $start = NULL, $end = NULL, $fields, $limit, $offset) {
if (empty($upcoming)) {
$range = $start . '--' . $end;
$view = services_views_retrieve('services_events', 'group_events', $args = array($uuid, $range), $offset = 0, $limit, 'view');
}
else{
$view = services_views_retrieve('services_events', 'group_events_upcoming', $args = array($uuid), $offset = 0, $limit, 'view');
}
if (!empty($view->argument['uuid']) && !$view->argument['uuid']->argument_validated) {
services_error(t('Unknown group uuid: !uuid', array('!uuid' => $uuid)), 404);
return FALSE;
}
if (empty($view->result)) {
services_error(t('No results found.'), 204);
return FALSE;
}
if ($fields == '*') {
$fields = NULL;
} else {
$fields = explode(',', $fields);
}
// Map the views data into the response
$response = new GroupResponse;
$return = $response->mapViewsData('event', 'view', $view, $fields);
// Add content to the return result that does not come directly from the view
foreach($return as $key => $row) {
if (array_key_exists('uri', $return[$key])) {
$return[$key]['uri'] = services_resource_uri(array('events', $return[$key]['uuid']));
}
}
return $return;
}
/**
* Retrieve members of a given group
* @param string $uuid
* @param string $fields
* @param int $limit
* @return array of group members
*/
function _apci_api_groups_members_retrieve($uuid, $fields, $limit, $admins_only = FALSE) {
$group_node = node_get_by_uuid($uuid);
$gid = $group_node->nid;
$return = array();
_apci_public_api_services_set_space($gid);
$display = ($admins_only) ? 'admins_only' : 'default';
$view = services_views_retrieve('services_group_members', $display, $args=array($uuid), 0, $limit, 'view');
if ($fields == '*') {
$fields = NULL;
}
else {
$fields = explode(',', $fields);
}
// Map the views data into the response
$response = new GroupResponse;
$return = $response->mapViewsData('member', 'view', $view, $fields);
// Add content to the return result that does not come directly from the view
// is logged-in user a member of this group?
$is_member = og_is_group_member($gid, $include_admins = FALSE, $uid = NULL);
foreach($return as $key => $row) {
// add picture, but only if requesting user is member of group
if (array_key_exists('picture', $return[$key])) {
if ($is_member) {
$return[$key]['picture'] = imagecache_create_url('profile_small', $return[$key]['picture']);
// @todo - file_get_url
}
else{
$return[$key]['picture'] = NULL;
}
}
// add user's first and last names.
if (array_key_exists('fname', $return[$key]) || array_key_exists('lname', $return[$key])) {
$member = user_get_by_uuid($return[$key]['uuid']);
$user_name = apci_user_get_username_filtered($member->uid);
$user_name = array_pop($user_name);
$user_name = split(' ', $user_name);
if(array_key_exists('fname', $return[$key])) {
$return[$key]['fname'] = $user_name[0];
}
if (array_key_exists('lname', $return[$key])) {
$return[$key]['lname'] = $user_name[1];
}
}// if
}// foreach
return $return;
}
function _apci_api_groups_resources_retrieve($uuid, $fields, $limit){
$group_node = node_get_by_uuid($uuid);
$view = services_views_retrieve('services_resources', 'defaults', $args = array($group_node->nid), $offset = 0, $limit, 'view');
if ($fields == '*') {
$fields = NULL;
}
else {
$fields = explode(',', $fields);
}
// Map the views data into the response
$response = new GroupResponse;
$return = $response->mapViewsData('resource', 'view', $view, $fields);
return $return;
}
/**
* Get UUIDs of the Groups Above (repeating cck nodereference field)
*
* @param int $gid
* @return array of nids & uuids
* Array (
* [0] => Array (
* [nid] => 109838
* [uuid] => 2238d8d0-e2e6-11e0-bc72-51167d338502
* )
* [1] => Array (
* [nid] => 189468
* [uuid] => 7df842f2-a8ce-11e0-899f-12313d18191a
* )
* )
*/
function _apci_api_groups_get_groups_above($gid) {
$sql = '
SELECT {content_field_group}.field_group_nid AS nid, {uuid_node}.uuid
FROM {content_field_group}
INNER JOIN {node} ON {content_field_group}.vid = {node}.vid
INNER JOIN {uuid_node} ON {content_field_group}.field_group_nid = {uuid_node}.nid
WHERE {content_field_group}.nid = %d
';
$replacements = array(
$gid,
);
$result = db_query($sql, $replacements);
$return = array();
while ($data = db_fetch_array($result)) {
$return[] = $data;
}
return $return;
}
/**
* Preparation Function
*
* @param array $view array of objects, data result of a view query
* @param string $call e.g. 'index', 'retrieve'
* @param array $filters Fields to be displayed
* @param integer $gid Group ID
* @return array Array for rendering
*/
function _apci_api_groups_view_prepare($view, $call, $filters, $gid = NULL) {
if ($filters && $filters[0] != '*') {
$filters = explode(',', $filters);
$filters = array_flip($filters);
}
else{
unset($filters);
}
switch ($call) {
//case 'index':
// refactored into GroupResponse.inc, _apci_api_groups_index()
//case 'albums':
// refactored into GroupResponse.inc, _apci_api_groups_albums_retrieve().
// break;
//case 'photos':
// refactored into GroupResponse.inc, _apci_api_groups_photos_retrieve().
// break;
case 'members':
// is user member of group he's getting members for?
$is_member = og_is_group_member($gid, $include_admins = FALSE, $uid = NULL);
foreach ($view as $member) {
if ($is_member) {
$img_thumb = imagecache_create_url('profile_small', $member->users_picture);
// @todo - file_get_url
}
else{
$img_thumb = NULL;
}
$user_name = apci_user_get_username_filtered($member->uid);
$user_name = array_pop($user_name);
$user_name = split(' ', $user_name);
$return[$member->uid] = array(
'uid' => $member->uid,
'picture' => $img_thumb,
'fname' => $user_name['0'],
'lname' => $user_name['1'],
);
}
break;
//case 'resources':
// refactored into GroupResponse.inc, _apci_api_groups_resources_retrieve().
// break;
case 'events':
foreach ($view as $event_row) {
// @todo there are multiple resources, not in the view. need to account for that
$enode = node_load($event_row->nid); // @todo incorporate into view?
$rnode = NULL;
if ($enode->field_ref_resource['0']['nid']){
$rnode = node_load($enode->field_ref_resource['0']['nid']);
}
$event = array(
'eid' => $event_row->nid,
'description' => $enode->body,
'title' => $event_row->node_title,
'start' => strtotime($event_row->node_data_field_date_field_date_value),
'end' => strtotime($event_row->node_data_field_date_field_date_value2),
'category' => $event_row->term_data_name,
'gid' => (string)current($enode->og_groups),
'resource' => array(
'resource_id' => $rnode->nid,
'resource_title' => $rnode->title,
'location' => array(
'lid' => $rnode->field_location['0']['lid'],
'street' => $event_row->node_node_data_field_ref_resource__location_street,
'city' => $event_row->node_node_data_field_ref_resource__location_city,
'state' => $event_row->node_node_data_field_ref_resource__location_province,
'zip' => $event_row->node_node_data_field_ref_resource__location_postal_code,
'country' => strtoupper($event_row->node_node_data_field_ref_resource__location_country),
'latitude' => $rnode->field_location['0']['latitude'],
'longitude' => $rnode->field_location['0']['longitude'],
),
),
// 'row' => $event_row,
// 'node'=> $enode,
);
if ($filters) {
$event = array_intersect_key($event, $filters);
}
$return[$event_row->nid] = $event;
}
break;
}
return $return;
}
function _apci_api_group_prepare($group, $filters) {
$group = _apci_api_groups_perms_check($group);
$return = array(
'gid' => $group->nid,
'description' => $group->og_description,
'logo' => NULL,
'title' => $group->title,
'location' => array(
'lid' => $group->field_location['0']['lid'],
'street' => $group->field_location['0']['street'],
'city' => $group->field_location['0']['city'],
'state' => $group->field_location['0']['province_name'],
'zip' => $group->field_location['0']['postal_code'],
'country' => $group->field_location['0']['country'],
'latitude' => $group->field_location['0']['latitude'],
'longitude' => $group->field_location['0']['longitude'],
),
'uri' => $node->uri = services_resource_uri(array('groups', $group->nid)), // good for services_docs
);
if ($group->field_logo['0']['filepath']) {
$return['logo'] = imagecache_create_url('profile_small', $group->field_logo['0']['filepath']);
}
if ($filters && $filters[0] != '*') {
$filters = explode(',', $filters);
$filters = array_flip($filters);
$return = array_intersect_key($return, $filters);
}
return $return;
}
/**
* Groups Access Callback
*/
function _apci_api_groups_perms_check($node) {
if ($node) {
// Apply field level content permissions
if (module_exists('content') && variable_get('services_use_content_permissions', TRUE)) {
$fields = content_fields(NULL, $node->type);
foreach ($fields as $field_name => $field_info) {
if (isset($node->$field_name)) {
$access = module_invoke_all('field_access', 'view', $field_info, $user, $node);
if (in_array(FALSE, $access)) {
unset($node->$field_name);
}
}
}
}
return $node;
}
return services_error('Node nid '. $nid .' not found', 404);
}
/*Group Update*/
function _apci_api_groups_update($uuid, $title, $description, $location, $category, $status, $groupmates_enabled) {
// Load the required includes for drupal_execute
module_load_include('inc', 'services', 'resources/node_resource');
module_load_include('module', 'hierarchical_select', 'hierarchical_select');
$loaded_group = (array)node_get_by_uuid($uuid);
//begin refactoring data for drupal_execute
//process status
//drupal execute doesn't like how the status comes back in a sub-array. fix it
if ($status){
$status = strtolower($status);
if ($status == 'active'){
$field_status = array('value' => ucfirst($status));
}
else if ($status == 'inactive'){
$field_status = array('value' => 0);
}
}
else{
$field_status = array('value' => $loaded_group['field_status']['0']['value']);
}
$loaded_group['field_status'] = $field_status;
//drupal execute doesn't like how field_accept_amex comes back. fix it
if (isset($loaded_group['field_accept_amex'])){
$loaded_group['field_accept_amex'] = array('value' => $loaded_group['field_accept_amex']['0']['value']);
}
//process groupmates
if ($groupmates){
$groupmates_enabled = strtolower($groupmates_enabled);
if ($groupmates_enabled == 'true'){
$field_group_mates = array('value'=> 'Group Mates');
}
else if ($groupmates_enabled == 'false'){
$field_group_mates = array('value'=> 0);
}
}
else{
//drupal execute not liking how groupmates comes back. fix it
$field_group_mates = array('value' => $loaded_group['field_group_mates']['0']['value']);
}
$loaded_group['field_group_mates'] = $field_group_mates;
if ($title){
$loaded_group['title'] = $title;
}
if($description){
$loaded_group['og_description'] = $description;
}
if ($location){
$field_location = $loaded_group['field_location']['0'];
$field_location['street'] = empty($location['street']) ? $field_location['street']:$location['street'];
$field_location['city'] = empty($location['city']) ? $field_location['city']:$location['city'];
$field_location['postal_code'] = empty($location['zip']) ? $field_location['postal_code']:$location['zip'];
$field_location['province'] = empty($location['state']) ? $field_location['province']:strtoupper($location['state']);
$field_location['country'] = empty($location['country']) ? $field_location['country']:strtolower($location['country']);
$loaded_group['field_location']['0'] = $field_location;
}
$hs_config = array(
'module' => 'hs_taxonomy',
'params' => array(
'vid' => '18',
),
'enforce_deepest' => 0,
'save_lineage' => 0,
'level_labels' => array(
'labels' => array(
'0' => '',
'1' => '',
),
'status' => 0,
),
'editability' => array(
'allow_new_levels' => 0,
'allowed_levels' => array(0 => 1, 1 => 1),
'item_types' => array('', ''),
'max_levels' => 1,
'status' => 0,
),
);
// process taxonomy
if ($category){
$vid = array_values($loaded_group['taxonomy']);
$vid = $vid['1']->vid;
$hs_array = _hierarchical_select_hierarchy_generate($hs_config, $selection = array(), $required = '1');
$hs_child_info = $hs_array->childinfo['0'];
$hs_array = array_flip($hs_array->levels['0']);
$hs_array = array_change_key_case($hs_array);
$taxonomy = array();
if ($t_primary = $hs_array[strtolower($category['0'])]){
$taxonomy[$vid]['hierarchical_select']['selects']['0'] = $t_primary;
}
if ($category['1'] && $hs_child_info[$t_primary] != 0){
$t_array = taxonomy_get_children($t_primary, 18);
foreach($t_array as $term){
if (strtolower($term->name) == strtolower($category['1'])){
$taxonomy[$vid]['hierarchical_select']['selects']['1'] = $term->tid;
break;
}
}
}
}
$loaded_group['taxonomy'] = $taxonomy;
/*groups_above*/
/*if (!empty($groups_above)){
$ga_nids = array();
foreach ($groups_above as $ga_uuid){
$group_above = node_get_by_uuid($ga_uuid);
$ga_nids[] = $group_above->nid;
}
$loaded_group['field_group'] = array(
'nid' => array(
'nid' => $ga_nids,
),
);
}*/
$loaded_group['purl'] = array(
'id' => NULL,
'provider' => 'spaces_og',
'value' => $loaded_group['purl'],
);
$group = _node_resource_update($loaded_group['nid'], $loaded_group);
$group = node_load($group['nid']);
return _apci_api_groups_retrieve($group->uuid, $fields = '*');
}
/**
* Group Create
*/
function _apci_api_groups_create($title, $description, $location, $category, $group_type, $web_address, $status, $groupmates_enabled) {
// Load the required includes for drupal_execute
module_load_include('inc', 'services', 'resources/node_resource');
module_load_include('module', 'hierarchical_select', 'hierarchical_select');
//process status
$status = strtolower($status);
if ($status == 'active'){
$field_status = array('value' => ucfirst($status));
}
else{
$field_status = array('value' => 0);
}
//process groupmates
$groupmates_enabled = strtolower($groupmates_enabled);
if ($groupmates_enabled == 'true'){
$field_group_mates = array('value'=> 'Group Mates');
}
else{
$field_group_mates = array('value'=> 0);
}
//process type
$group_type = strtolower($group_type);
$group_types = array(
'association',
'league',
'division',
'team',
'class',
'club',
);
if (in_array($group_type, $group_types)){
$spaces_preset_og = $group_type;
$spaces_preset_other = '';
}
else{
$spaces_preset_og = 'other';
$spaces_preset_other = $group_type;
}
//build the array
$group = array(
'title' => $title,
'og_description' => $description,
'type' => 'group',
'field_location' => array(
array(
'street' => $location['street'],
'city' => $location['city'],
'province' => strtoupper($location['state']),
'postal_code' => $location['zip'],
'country' => empty($location['country']) ? 'us' : strtolower($location['country']),
),
),
'spaces_preset_og' => $spaces_preset_og,
'spaces_preset_other' => $spaces_preset_other,
'op' => 'Create Group',
'purl' => array(
'id' => NULL,
'provider' => 'spaces_og',
'value' => $web_address == NULL ? trim(strtolower($title)).'-'.$location['zip'] : strtolower($web_address),
),
'field_status' => $field_status,
'field_accept_amex' => array(
'value' => 0,
),
'field_group_mates' => $field_group_mates,
);
$hs_config = array(
'module' => 'hs_taxonomy',
'params' => array(
'vid' => '18',
),
'enforce_deepest' => 0,
'save_lineage' => 0,
'level_labels' => array(
'labels' => array(
'0' => '',
'1' => '',
),
'status' => 0,
),
'editability' => array(
'allow_new_levels' => 0,
'allowed_levels' => array(0 => 1, 1 => 1),
'item_types' => array('', ''),
'max_levels' => 1,
'status' => 0,
),
);
// process taxonomy
$hs_array = _hierarchical_select_hierarchy_generate($hs_config, $selection = array(), $required = '1');
$hs_child_info = $hs_array->childinfo['0'];
$hs_array = array_flip($hs_array->levels['0']);
$hs_array = array_change_key_case($hs_array);
$taxonomy = array();
if ($t_primary = $hs_array[strtolower($category['0'])]){
$taxonomy['18']['hierarchical_select']['selects']['0'] = $t_primary;
}
if ($category['1'] && $hs_child_info[$t_primary] != 0){
$t_array = taxonomy_get_children($t_primary, 18);
foreach($t_array as $term){
if (strtolower($term->name) == strtolower($category['1'])){
$taxonomy['18']['hierarchical_select']['selects']['1'] = $term->tid;
break;
}
}
}
$group['taxonomy'] = $taxonomy;
/*groups_above*/
/*if (!empty($groups_above)){
$ga_nids = array();
foreach ($groups_above as $ga_uuid){
$group_above = node_get_by_uuid($ga_uuid);
$ga_nids[] = $group_above->nid;
}
$group['field_group']['nid']['nid'] = $ga_nids;
}*/
$group = _node_resource_create($group);
$group = node_load($group['nid']);
return _apci_api_groups_retrieve($group->uuid, $fields = '*');
}
/***********
* Utility Functions
**********/
/**
* Process returned views data, adding extra data
* @param array $return dataset, returned from mapViewsData()
* @return array modified $return
*/
function _apci_api_groups_add_extra_views_data(array $return, $fields) {
// Add content to the return result that does not come directly from the view
// -- logo, uri, url, groups_above_uuid
foreach($return as &$row) {
$group_node = node_get_by_uuid($row['uuid']);
// @todo - Could not relate from node.uid to uuid_users.uuid. Merlin
// recommended views upgrade for explicit node -> users relationship.
// View 6.x-3.x-alpha4 or better.
if (empty($fields) || in_array('author', $fields)) {
$row['author'] = array('uuid' => uuid_get_uuid('users', 'uid', $group_node->uid));
}
if (array_key_exists('logo', $row) && (empty($fields) || in_array('logo', $fields))) {
$logo = NULL;
if ($row['logo']) {
$logo = field_file_load($row['logo']);
$logo = imagecache_create_url('profile_small', $logo['filepath']);
}
$row['logo'] = $logo;
}
if (array_key_exists('uri', $row) && (empty($fields) || in_array('uri', $fields))) {
$row['uri'] = services_resource_uri(array('groups', $row['uuid']));
}
if (array_key_exists('url', $row) && (empty($fields) || in_array('url', $fields))) {
$row['url'] = rtrim(
url('<front>', array(
'absolute' => TRUE,
'purl' => array('provider' => 'spaces_og', 'id' => $group_node->nid),
)),
'/');
}
if (array_key_exists('groups_above_uuid', $row) && (empty($fields) || in_array('groups_above_uuid', $fields))) {
$groups_above_result = _apci_api_groups_get_groups_above($group_node->nid);
$groups_above = array();
foreach($groups_above_result as $group_above) {
$groups_above[] = $group_above['uuid'];
}
$row['groups_above_uuid'] = $groups_above;
}
} // foreach()
return $return;
}
/*
* Join callback function that saves a user's subscription to a group.
*/
function _apci_api_groups_subscribe_user($group_uuid, $uuid) {
$user = user_get_by_uuid($uuid);
$account = user_load($user->uid);
$group = node_get_by_uuid($group_uuid);
$subscribe = og_subscribe_user($group->nid, $account);
if (is_array($subscribe) && $subscribe['type'] == 'rejected') {
return services_error(implode("\n", $subscribe), 403);
}
return $subscribe;
}
/**
* Unjoin callback function.
*/
function _apci_api_groups_unsubscribe_user($group_uuid, $uuid) {
global $user;
$group = node_get_by_uuid($group_uuid);
$user_u = user_get_by_uuid($uuid);
$account = user_load($user_u->uid);
if (!$account) {
return services_error('User not found', 404);
}
og_delete_subscription($group->nid, $user_u->uid);
// If needed, reload user object to reflect unsubscribed group.
if ($user->uid == $account->uid) {
og_get_subscriptions($account->uid, 1, TRUE); // Clear cache.
$user = user_load(array('uid' => $user->uid));
$message = t('You left the group %group.', array('%group' => $group->title));
}
else {
$message = t('%user removed from %group.', array('%user' => $account->name, '%group' => $group->title));
}
return $message;
}
/*
* Add Role callback function.
*/
function _apci_api_groups_addrole($group_uuid, $uuid, $role_uuid, $options = array()) {
$defaults = array(
'should_pay' => 1,
'payment_method' => 'full'
);
$user = user_get_by_uuid($uuid);
$group = node_get_by_uuid($group_uuid);
$role = og_rap_rid_get_by_uuid($role_uuid);
apci_webform_add($group->nid, $user->uid);
return og_rap_role_add($group->nid, $user->uid, $role->rid, array_merge($defaults, (array)$options));
}
/*
* Register callback function.
*/
function _apci_api_groups_register_user($group_uuid, $uuid, $role_uuid = null, $fields = array()) {
// Load the required includes for registering
// with drupal_execute().
global $user;
ctools_include('wizard');
ctools_include('object-cache');
// register a user to a group
$og_registration = new stdClass();
$og_registration->nid = node_get_by_uuid($group_uuid)->nid;
$og_registration->uid = user_get_by_uuid($uuid)->uid;
$og_registration->rid = og_rap_rid_get_by_uuid($role_uuid)->rid;
$form_state['og_registration_obj'] = $og_registration;
$form_state['values']['uid'] = user_get_by_uuid($uuid)->uid;
$form_state['values']['rid'] = og_rap_rid_get_by_uuid($role_uuid)->rid;
// execute the register form
drupal_execute('og_registration_user_form', $form_state);
// Add the user to the role
if ($errors = form_get_errors()) {
return services_error(implode(" ", $errors), 406, array('form_errors' => $errors));
}
og_registration_basic_add_subtask_finish($form_state);
// Error if needed.
if ($errors = form_get_errors()) {
return services_error(implode(" ", $errors), 406, array('form_errors' => $errors));
} else {
return $form_state['og_registration_obj'];
}
return $form_state['og_registration_obj'];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment