You are here

apachesolr_og.module in Apache Solr Organic Groups Integration 7

Same filename and directory in other branches
  1. 6.3 apachesolr_og.module
  2. 6.2 apachesolr_og.module

Integrates Organic Group info with Apache Solr search application.

File

apachesolr_og.module
View source
<?php

/**
 * @file
 *   Integrates Organic Group info with Apache Solr search application.
 */

/**
 * Implements hook_apachesolr_field_mappings_alter().
 * 
 * Adds some special facets for the Audience Field as defined by OG
 */
function apachesolr_og_apachesolr_field_mappings_alter(&$mappings) {
  $mappings['per-field'][OG_AUDIENCE_FIELD] = $mappings['entityreference'];
  $mappings['per-field'][OG_AUDIENCE_FIELD]['alter callbacks'][] = 'apachesolr_og_alter_callback';
  $mappings['per-field'][OG_AUDIENCE_FIELD]['map callback'] = 'apachesolr_og_map_gid_label';
}

/**
 * Alters the build array of the OG_AUDIENCE_FIELD filter.
 * Adds a filter that allows the user to filter on his groups.
 * 
 * @global type $user
 * @param array $build
 * @param FacetapiAdapter $adapter
 * @param array $facet
 */
function apachesolr_og_alter_callback(&$build, FacetapiAdapter $adapter, $facet) {
  $count = 0;
  global $user;
  if (is_array($build) && !empty($build)) {

    // Add a filter 'Your Groups'
    $user_groups = og_get_groups_by_user($user);
    foreach ($build as $filter => $value) {

      // We could do this with the objects, but it would make it a lot more

      //complex
      $filter_values = explode(':', $filter);
      $count += isset($user_groups[$filter_values[0]][$filter_values[1]]) ? $value['#count'] : 0;
    }
    $search_string = apachesolr_og_get_my_groups_filter();
    $build[$search_string] = array(
      '#count' => $count,
    );
  }
}

/**
 * Creates a special filter for filtering on the content for the current
 * users groups
 * 
 * @global type $user
 * @return type
 */
function apachesolr_og_get_my_groups_filter() {
  global $user;
  $user_groups = og_get_groups_by_user($user);

  // Build our facet filter
  foreach ($user_groups as $entity_type => $type) {
    foreach ($type as $entity_id) {
      $key = $entity_type . ':' . $entity_id;
      $search_groups[] = '"' . $key . '"';
    }
  }
  return '(' . implode(' OR ', $search_groups) . ')';
}

/**
 * Maps prganic group names to their human friendly label and adds a Your Groups
 * facet
 *
 * @param array $values
 *   An array of indexed values being mapped.
 * @param array $options
 *   An associative array of map options containing:
 *   - entities: An array of entities that $values are bundles for.
 *
 * @return array
 *   An array mapping the indexed values to human readable values.
 */
function apachesolr_og_map_gid_label(array $ids, array $options) {
  global $user;
  $my_groups_filter_string = apachesolr_og_get_my_groups_filter();

  // Remove the value we are looking for
  if (($key = array_search($my_groups_filter_string, $ids)) !== false) {
    unset($ids[$key]);
  }
  $map = apachesolr_entityreference_facet_map_callback($ids, $options);
  $map[$my_groups_filter_string] = t('Your groups');
  return $map;
}

Functions

Namesort descending Description
apachesolr_og_alter_callback Alters the build array of the OG_AUDIENCE_FIELD filter. Adds a filter that allows the user to filter on his groups.
apachesolr_og_apachesolr_field_mappings_alter Implements hook_apachesolr_field_mappings_alter().
apachesolr_og_get_my_groups_filter Creates a special filter for filtering on the content for the current users groups
apachesolr_og_map_gid_label Maps prganic group names to their human friendly label and adds a Your Groups facet