You are here

function oa_notifications_autocomplete_callback in Open Atrium Notifications 7.2

Menu callback for autocomplete results

Parameters

$node:

1 string reference to 'oa_notifications_autocomplete_callback'
oa_notifications_menu in ./oa_notifications.module
Implements hook_menu().

File

./oa_notifications.module, line 948

Code

function oa_notifications_autocomplete_callback($nid, $string) {
  if ($nid) {
    $node = node_load($nid);
    $space_id = oa_core_get_group_from_node($node, array(
      OA_SPACE_TYPE,
    ));
  }
  else {
    $space_id = oa_core_get_space_context();
  }
  $lower = strtolower($string);
  $matches = array();
  $category = $lower == 'space' || $lower == 'group' || $lower == 'team' || $lower == 'user' || $lower == 'member' ? $lower : '';
  $wildcard = $lower == '%' || $lower == '*' || !empty($category);
  $type = $category == 'group' ? OA_GROUP_TYPE : ($category == 'space' ? OA_SPACE_TYPE : NULL);
  $parents = oa_core_get_parents($space_id, $type, NODE_PUBLISHED, FALSE, TRUE);

  // Include current space.
  if ($category !== 'group') {
    $parents[] = $space_id;
  }

  // Match parent groups and spaces
  if (empty($category) || $category == 'space' || $category == 'group') {
    $query = db_select('node', 'n');
    $query
      ->fields('n', array(
      'nid',
      'title',
      'type',
    ))
      ->condition('n.nid', $parents, 'IN')
      ->addTag('node_access');
    if (!$wildcard) {
      $query
        ->condition('n.title', '%' . db_like($string) . '%', 'LIKE');
    }
    $query
      ->range(0, 100);
    $result = $query
      ->execute();
    foreach ($result as $row) {
      $title = check_plain($row->title) . ' (';
      $title .= $row->type == OA_SPACE_TYPE ? t('Space') : t('Group');
      $title .= ')';
      $matches['group:' . $row->nid] = $title;
    }
  }

  // Next, match teams
  if (empty($category) || $category == 'team') {
    $query = db_select('node', 'n');
    $query
      ->rightJoin('og_membership', 'og', 'n.nid = og.etid');
    $query
      ->fields('n', array(
      'nid',
      'title',
    ))
      ->condition('n.type', OA_TEAM_TYPE)
      ->condition('og.entity_type', 'node')
      ->condition('og.gid', $space_id)
      ->condition('og.state', OG_STATE_ACTIVE, '=')
      ->addTag('node_access');
    if (!$wildcard) {
      $query
        ->condition('n.title', '%' . db_like($string) . '%', 'LIKE');
    }
    $query
      ->range(0, 100);
    $result = $query
      ->execute();
    foreach ($result as $row) {
      $matches['team:' . $row->nid] = check_plain($row->title) . ' (' . t('Team') . ')';
    }
  }

  // Finally, match users in the space
  if (empty($category) || $category == 'user' || $category == 'member') {
    if ($category == 'member' || oa_core_get_group_privacy($space_id)) {
      $query = db_select('og_membership', 'og');
      $query
        ->rightJoin('realname', 'u', 'u.uid = og.etid');
      $query
        ->fields('u', array(
        'uid',
        'realname',
      ))
        ->condition('og.gid', $parents, 'IN')
        ->condition('og.group_type', 'node', '=')
        ->condition('og.entity_type', 'user', '=')
        ->condition('og.state', OG_STATE_ACTIVE, '=');
      if ($lower == 'member') {
        $query
          ->condition('og.gid', $space_id, '=');
      }
      else {
        $query
          ->condition('og.gid', $parents, 'IN');
      }
    }
    else {

      // For public spaces, allow any user to match
      $query = db_select('realname', 'u');
      $query
        ->fields('u', array(
        'uid',
        'realname',
      ));
    }
    if (!$wildcard) {
      $query
        ->condition('u.realname', '%' . db_like($string) . '%', 'LIKE');
    }
    $query
      ->range(0, 100);
    $result = $query
      ->execute();

    // save the query to matches
    foreach ($result as $row) {
      $name = trim($row->realname);
      if (!empty($name)) {
        $matches['user:' . $row->uid] = check_plain($name) . ' (' . t('User') . ')';
      }
    }
  }

  // Return the result to the form in json
  drupal_json_output($matches);
}