You are here

function group_groups_form in Group 7

Builds the group overview table.

@todo Individual group access check on query.

Parameters

int $pager_limit: The pager limit for the result page.

1 call to group_groups_form()
GroupUIController::overviewForm in classes/group.ui_controller.inc
Builds the group overview form.

File

admin/group.inc, line 214
Group overview admin UI.

Code

function group_groups_form($pager_limit) {
  $options = array();
  $header = array(
    'title' => array(
      'data' => t('Group name'),
      'field' => 'title',
      'sort' => 'asc',
    ),
    'type' => array(
      'data' => t('Group type'),
      'field' => 'type',
    ),
    'members' => array(
      'data' => t('Members'),
      'field' => 'members',
    ),
    'operations' => t('Operations'),
  );

  // Below we build a query that is used to retrieve group ids based on
  // filters, a pager and group access. Additionally, we add a 'fake' field
  // that lists the amount of members so we can sort on that too.
  $query = db_select('groups', 'g')
    ->extend('PagerDefault')
    ->extend('TableSort');
  $query
    ->leftJoin('group_membership', 'gm', 'g.gid=gm.gid');
  $query
    ->addField('g', 'gid');
  $query
    ->addExpression('COUNT(distinct gm.uid)', 'members');
  $query
    ->groupBy('g.gid');
  $query
    ->orderByHeader($header);
  $query
    ->limit($pager_limit);

  // Retrieve all active filters.
  $filters = isset($_SESSION['group_overview_filters']) ? $_SESSION['group_overview_filters'] : array();

  // Allow other modules to alter the query by passing on the filters.
  $query
    ->addTag('group_overview');
  $query
    ->addMetaData('filters', $filters);

  // Add the groups to the table.
  foreach (group_load_multiple($query
    ->execute()
    ->fetchCol()) as $group) {
    $options[$group->gid] = array(
      'title' => l($group
        ->label(), "group/{$group->gid}"),
      'type' => group_type_load($group->type)
        ->label(),
      'members' => $group
        ->getMemberCount(),
    );

    // Start with an empty operations array.
    $options[$group->gid]['operations'] = array();

    // Gather all operation links.
    $operations = module_invoke_all('group_operation_links', $group);
    if (count($operations) > 1) {

      // Render an unordered list of operations links.
      $options[$group->gid]['operations'] = array(
        'data' => array(
          '#theme' => 'links__group_operation_links',
          '#links' => $operations,
          '#attributes' => array(
            'class' => array(
              'links',
              'inline',
            ),
          ),
        ),
      );
    }
    elseif (!empty($operations)) {

      // Render the first and only operation as a link.
      $link = reset($operations);

      // Pass in $link as $options, they share the same keys.
      $options[$group->gid]['operations'] = l($link['title'], $link['href'], $link);
    }
  }
  $form['groups'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
    '#empty' => t('No groups available'),
  );

  // Transform into a table if the user can't perform any operations.
  if (!user_access('bypass group access')) {
    unset($form['groups']['#type'], $form['groups']['#options']);
    $form['groups']['#theme'] = 'table';
    $form['groups']['#rows'] = $options;
  }
  return $form;
}