You are here

function oa_core_get_public_spaces in Open Atrium Core 7.2

Get a list of public spaces.

Necessary since og_get_entity_groups() doesn't return anything for anonymous users

Parameters

array $group_types: (optional) An associative array of node types. (default: array(OA_SPACE_TYPE => OA_SPACE_TYPE)

int $status: (optional) If specified, the node status (ex. NODE_PUBLISHED or NODE_NOT_PUBLISHED) to look for. If not specified, it return nodes of either status.

bool $include_archived: (optional) Whether to include archived nodes or not. By default, archived items aren't included.

bool $check_access: Check node access, defaults to TRUE.

bool $only_top restrict to top-level spaces:

Return value

array An array of Space NIDs.

3 calls to oa_core_get_public_spaces()
oa_core_node_grants in includes/oa_core.access.inc
Implements hook_node_grants(). Define node access grant realm for Open Atrium sections
oa_core_user_spaces_render in plugins/content_types/oa_core_user_spaces.inc
Render callback for the content visibility panel.
OgSubspacesSelectionHandler::getGidsForCreate in plugins/entityreference/selection/OgSubspacesSelectionHandler.class.php
Get group IDs from URL or OG-context, with access to create group-content.

File

includes/oa_core.util.inc, line 888
Code for Utility functions for OpenAtrium spaces

Code

function oa_core_get_public_spaces($group_types = array(
  OA_SPACE_TYPE => OA_SPACE_TYPE,
), $status = NULL, $include_archived = FALSE, $check_access = TRUE, $only_top = FALSE) {
  $cache =& drupal_static(__FUNCTION__);
  $group_types_cid = implode(',', $group_types);
  if (!isset($cache[$group_types_cid][$status][$include_archived])) {
    $query = db_select('node', 'n');
    $query
      ->join('field_data_group_access', 'g', "n.nid = g.entity_id AND g.entity_type = 'node'");
    $query
      ->fields('n', array(
      'nid',
    ));
    if ($only_top) {

      // need to restrict to space with either no parent, or just oa_group parents
      $query
        ->leftJoin('og_membership', 'og', "og.etid = n.nid AND og.entity_type = 'node'");
      $query
        ->leftJoin('node', 'pn', 'og.gid = pn.nid');
      $query
        ->fields('pn', array(
        'type',
      ));

      // trick is to group the results and count the number of different parent types
      $query
        ->addExpression('COUNT(DISTINCT pn.type)', 'num');
      $query
        ->groupBy('n.nid');

      // so we either return spaces with 0 parents, or 1 parent type that is a group
      $query
        ->having("num = 0 OR (num = 1 AND pn.type = '" . OA_GROUP_TYPE . "')");
    }
    $query
      ->condition('n.type', $group_types, 'IN')
      ->condition('g.group_access_value', 0);
    if (isset($status)) {
      $query
        ->condition('n.status', $status);
    }
    if ($check_access) {
      $query
        ->addTag('node_access');
    }
    if (module_exists('flag') && !$include_archived) {
      if ($flag = flag_get_flag('trash')) {
        $query
          ->leftJoin('flagging', 'a', "a.fid = :fid AND a.entity_id = n.nid", array(
          ':fid' => $flag->fid,
        ));

        // This makes sure that archived content isn't included, because 'uid'
        // will be NULL if the join didn't connect anything.
        $query
          ->isNull('a.uid');
      }
    }
    $cache[$group_types_cid][$status][$include_archived] = $query
      ->execute()
      ->fetchCol(0);
  }
  return $cache[$group_types_cid][$status][$include_archived];
}