<?php

/**
 * @file
 * Admin settings for Organic groups module.
 */

/**
 * Menu callback; Admin settings form.
 */
function og_ui_admin_settings($form_state) {
  $form = array();

  $form['og_group_manager_full_access'] = array(
    '#type' => 'checkbox',
    '#title' => t('Group manager full permissions'),
    '#description' => t('When enabled the group manager will have all the permissions in the group.'),
    '#default_value' => variable_get('og_group_manager_full_access', TRUE),
  );

  $form['og_node_access_strict'] = array(
    '#type' => 'checkbox',
    '#title' => t('Strict node access permissions'),
    '#description' => t('When enabled Organic groups will restrict permissions for creating, updating and deleting according to the  Organic groups access settings. Example: A content editor with the <em>Edit any page content</em> permission who is not a member of a group would be denied access to modifying page content in that group. (For restricting view access use the Organic groups access control module.)'),
    '#default_value' => variable_get('og_node_access_strict', TRUE),
  );

  $form['og_ui_admin_people_view'] = array(
    '#type' => 'select',
    '#title' => t('Admin people View'),
    '#description' => t('Select the View that should be used to show and control the people in the group.'),
    '#options' => views_get_views_as_options(),
    '#default_value' => variable_get('og_ui_admin_people_view', 'og_members_admin:default'),
    '#required' => TRUE,
  );
  if ($group_bundles = og_get_all_group_bundle()) {
    $form['og_group_manager_rids'] = array(
      '#type' => 'fieldset',
      '#title' => t('Group manager default roles'),
      '#description' => t('Select the role(s) a group manager will be granted upon creating a new group.'),
    );
    // Add group manager default roles.
    $entity_info = entity_get_info();
    foreach ($group_bundles as $entity_type => $bundles) {
      foreach ($bundles as $bundle_name => $bundle_label) {
        $og_roles = og_roles($entity_type, $bundle_name, 0, FALSE, FALSE);
        if (!$og_roles) {
         continue;
        }

        $params = array(
          '@entity-label' => $entity_info[$entity_type]['label'],
          '@bundle-label' => $bundle_label,
        );

        $name = 'og_group_manager_default_rids_' . $entity_type . '_' . $bundle_name;
        $form['og_group_manager_rids'][$name] = array(
          '#type' => 'select',
          '#title' => t('Roles in @entity-label - @bundle-label', $params),
          '#options' => $og_roles,
          '#multiple' => TRUE,
          '#default_value' => variable_get($name, array()),
        );
      }
    }
  }

  return system_settings_form($form);
}

/**
 * Provide an overview of the administrator menu items.
 *
 * @param $entity_type
 *   The entity type.
 * @param $etid
 *   The entity ID.
 */
function og_ui_group_admin_overview($entity_type, $etid) {
  og_set_breadcrumb($entity_type, $etid);

  $items = og_ui_get_group_admin($entity_type, $etid);

  foreach ($items as &$item) {
    // Re-format the URL.
    $item['href'] = "group/$entity_type/$etid/" . $item['href'];
    // Imitate the menu_get_item() content, so we can pass it to
    // theme_admin_block_content(), so we add the localized_options key.
    $item['localized_options'] = array();
  }

  $form['admin_block_content'] = array('#theme' => 'admin_block_content', '#content' => $items);
  return $form;
}

/**
 * Add users to group form.
 */
function og_ui_add_users($form, &$form_state, $group_type, $gid) {
  og_set_breadcrumb($group_type, $gid, array(l(t('Group'), "$group_type/$gid/group")));
  $group = entity_load_single($group_type, $gid);
  $label = entity_label($group_type, $group);

  $form['group_type'] = array('#type' => 'value', '#value' => $group_type);
  $form['gid'] = array('#type' => 'value', '#value' => $gid);

  $form['og_user'] = array(
    '#type' => 'fieldset',
    '#title' => t('Add a group member to %group', array('%group' => $label)),
  );
  $form['og_user']['name'] = array(
    '#type' => 'textfield',
    '#title' => t('User name'),
    '#autocomplete_path' => 'user/autocomplete',
    '#required' => TRUE,
  );
  $form['og_user']['state'] = array(
    '#type' => 'select',
    '#title' => t('Membership state'),
    '#options' => og_group_content_states(),
    '#description' => t('Select the state of the membership.'),
  );

  $field_names = og_get_group_audience_fields();
  $field_name = !empty($form_state['values']['field_name']) ? $form_state['values']['field_name'] : key($field_names);

  $form['og_user']['field_name'] = array(
    '#type' => 'select',
    '#title' => t('Field name'),
    '#options' => $field_names,
    '#default_value' => $field_name,
    '#description' => t('Select the field name, the group membership should be registered in.'),
    '#ajax' => array(
      'callback' => 'og_ui_admin_add_user_ajax_callback',
      'wrapper' => 'og-ui-field-name',
    ),
  );

  // Add group membership form.
  $values = array();

  // Add group membership form. We still don't have the user or state.
  $og_membership = og_membership_create($group_type, $gid, 'user', 0, $field_name, $values);
  $form_state['og_membership'] = $og_membership;

  $form['membership_fields'] = array(
    '#prefix' => '<div id="og-ui-field-name">',
    '#suffix' => '</div>',
    '#tree' => TRUE,
    '#parents' => array('membership_fields'),
  );
  field_attach_form('og_membership', $og_membership, $form['membership_fields'], $form_state);

  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Add users'));

  return $form;
}

/**
 * AJAX callback to attach the group membership type fields to the form.
 *
 * Since the controlling logic for populating the form is in the form builder
 * function, all we do here is select the element and return it to be updated.
 */
function og_ui_admin_add_user_ajax_callback(array $form, array &$form_state) {
  return $form['membership_fields'];
}

/**
 * Validate handler; Add users to group.
 */
function og_ui_add_users_validate($form, &$form_state) {
  $account = user_load_by_name($form_state['values']['name']);
  $group_type = $form_state['values']['group_type'];
  $gid = $form_state['values']['gid'];
  $field_name = $form_state['values']['field_name'];
  $params = array('%name' => format_username($account));
  if (!$account) {
    form_set_error('name', t('You have entered an invalid user name.'));
    return;
  }
  elseif (og_get_membership($group_type, $gid, 'user', $account->uid)) {
    form_set_error('name', t('User %name is already subscribed to group.', $params));
  }
  // Check field cardinality.
  elseif (!og_check_field_cardinality('user', $account, $field_name)) {
    $instance = field_info_instance('user', $field_name, 'user');
    $params['%field'] = $instance['label'];
    form_set_error('field_name', t('%name has reached maximum values in field %field.', $params));
  }
  else {
    $og_membership = $form_state['og_membership'];
    field_attach_form_validate('og_membership', $og_membership, $form['membership_fields'], $form_state);
  }
}

/**
 * Submit handler; Add users to group.
 */
function og_ui_add_users_submit($form, &$form_state) {
  $group_type = $form_state['values']['group_type'];
  $gid = $form_state['values']['gid'];
  $state = $form_state['values']['state'];
  $og_membership = $form_state['og_membership'];
  // Set the user and state.
  field_attach_submit('og_membership', $og_membership, $form['membership_fields'], $form_state);
  $account = user_load_by_name($form_state['values']['name']);
  // Ungroup user, in case they were already registered.
  og_ungroup($group_type, $gid, 'user', $account);

  $og_membership->etid = $account->uid;
  $og_membership->state = $state;
  $og_membership->save();

  drupal_set_message(t('%user has been added to group.', array('%user' => format_username($account))));
}

/**
 * Form builder; OG user administration page.
 */
function og_ui_admin_account($group_type, $gid) {
  og_set_breadcrumb($group_type, $gid, array(l(t('Group'), "$group_type/$gid/group")));
  // $view = explode(':', variable_get)
  list($view_name, $display) = explode(':', variable_get('og_ui_admin_people_view', 'og_members_admin:default'));
  // We can't use views_embed_view() as we have exposed filter, and we need to
  // make sure the path is overriden to the current URL.
  // @see http://drupal.org/node/525592#comment-1968604
  $view = views_get_view($view_name, $display);
  $view->set_arguments(array($group_type, $gid));
  $view->override_url = $_GET['q'];
  return $view->preview();
}

/**
 * Display an overview of group types with edit link.
 *
 * @param $type
 *   Either 'roles' or 'permissions'. Determines the edit link url.
 *
 * @return
 *   Renderable table of group types.
 */
function og_ui_group_types_overview($type) {
  $header = array(t('Group type'), t('Operations'));
  $rows = array();

  foreach (og_get_all_group_bundle() as $entity_type => $bundles) {
    $entity_info = entity_get_info($entity_type);
    foreach ($bundles as $bundle_name => $bundle_label) {
      $row = array();
      $row[] = array('data' => check_plain($entity_info['label'] . ' - ' . $bundle_label));
      $row[] = array('data' => l(t('edit'), "admin/config/group/$type/$entity_type/$bundle_name"));

      $rows[] = $row;
    }
  }

  $build['roles_table'] = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#empty' => t('No group types available.'),
  );

  return $build;
}

/**
 * Menu callback: administer roles.
 *
 * @ingroup forms
 * @see og_user_admin_roles_validate()
 * @see og_user_admin_roles_submit()
 * @see theme_group_user_admin_new_role()
 */
function og_ui_admin_roles($form, $form_state, $group_type = '', $gid = 0, $bundle = '', $rid = 0) {
  if ($gid) {
    og_set_breadcrumb($group_type, $gid, array(l(t('Group'), "$group_type/$gid/group")), l(t('Roles'), "group/$group_type/$gid/admin/roles"));
  }

  if ($rid) {
    // Display the edit role form.
    $role = og_role_load($rid);

    // Get group type and bundle from role.
    $bundle = $role->group_bundle;
    $group_type = $role->group_type;

    $form['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Role name'),
      '#default_value' => $role->name,
      '#size' => 30,
      '#required' => TRUE,
      '#maxlength' => 64,
      '#description' => t('The name for this role. Example: "moderator", "editorial board", "site architect".'),
    );
    $form['rid'] = array(
      '#type' => 'value',
      '#value' => $rid,
    );
    $form['actions'] = array('#type' => 'actions');
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save role'),
    );
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete role'),
    );
  }
  else {
    if ($gid) {
      $group = entity_load_single($group_type, $gid);
      list(,, $bundle) = entity_extract_ids($group_type, $group);
    }

    // No role ID.
    if (!$gid || ($group && !og_is_group_default_access($group_type, $group))) {

      $form['name'] = array(
        '#type' => 'textfield',
        '#title' => t('Name'),
        '#title_display' => 'invisible',
        '#size' => 32,
        '#maxlength' => 64,
      );
      $form['add'] = array(
        '#type' => 'submit',
        '#value' => t('Add role'),
        '#validate' => array('og_ui_admin_roles_validate'),
        '#submit' => array('og_ui_admin_roles_submit'),
      );

      // Attach the CSS to the form.
      $form['#attached'] = array(
        'css' => array (
          'type' => 'file',
          'data' => drupal_get_path('module', 'og_ui') . '/css/og_ui.css',
        ),
      );
    }
  }

  $form['group_type'] = array('#type' => 'value', '#value' => $group_type);
  $form['bundle'] = array('#type' => 'value', '#value' => $bundle);
  $form['gid'] = array('#type' => 'value', '#value' => $gid);

  $role_names = _og_ui_get_role_names($group_type, $bundle, $gid, $rid);
  $form['role_names'] = array('#type' => 'value', '#value' => $role_names);

  return $form;
}

/**
 * Validate handler; OG roles.
 */
function og_ui_admin_roles_validate($form, &$form_state) {
  if (!empty($form_state['values']['name'])) {
    $group_type = $form['group_type']['#value'];
    $bundle = $form['bundle']['#value'];
    $gid = $form['gid']['#value'];

    $roles = og_roles($group_type, $bundle, $gid);
    $unique_name = TRUE;
    if (!empty($form_state['values']['rid'])) {
      $role = og_role_load($form_state['values']['rid']);
      $unique_name = $role->name != $form_state['values']['name'];
    }
    if ($unique_name && in_array($form_state['values']['name'], $roles)) {
      form_set_error('name', t('The role name %name already exists. Please choose another role name.', array('%name' => $form_state['values']['name'])));
    }
  }
  else {
    form_set_error('name', t('You must specify a valid role name.'));
  }
}

/**
 * Submit handler; OG roles.
 */
function og_ui_admin_roles_submit($form, &$form_state) {
  $group_type = $form['group_type']['#value'];
  $bundle = $form['bundle']['#value'];
  $gid = $form['gid']['#value'];


  if (!empty($form_state['values']['rid'])) {
    $role = og_role_load($form_state['values']['rid']);
    $role->name = $form_state['values']['name'];
  }
  else {
    $role = og_role_create($form_state['values']['name'], $group_type, $gid, $bundle);
  }
  if ($form_state['values']['op'] == t('Save role')) {
    og_role_save($role);
    drupal_set_message(t('The role has been renamed.'));
  }
  elseif ($form_state['values']['op'] == t('Add role')) {
    og_role_save($role);
    drupal_set_message(t('The role has been added.'));
  }
  elseif ($form_state['values']['op'] == t('Delete role')) {
    og_role_delete($form_state['values']['rid']);
    drupal_set_message(t('The role has been deleted.'));
  }

  if ($gid) {
    $form_state['redirect'] = "group/$group_type/$gid/admin/roles";
  }
  else {
    $form_state['redirect'] = 'admin/config/group/roles';
    if ($bundle) {
      $form_state['redirect'] .= "/$group_type/$bundle";
    }
  }
}

/**
 * Theme the new role form.
 *
 * @ingroup themeable
 */
function theme_og_ui_admin_new_role($variables) {
  $form = $variables['form'];
  $header = array(t('Name'), array('data' => t('Operations'), 'colspan' => 2));

  $group_type = $form['group_type']['#value'];
  $gid = $form['gid']['#value'];
  $bundle = $form['bundle']['#value'];
  $role_names = $form['role_names']['#value'];

  if ($gid) {
    $group = entity_load_single($group_type, $gid);
    $default_access = $group && og_is_group_default_access($group_type, $group);
  }
  else {
    $default_access = FALSE;
  }

  foreach ($role_names as $rid => $name) {
    $text = !$default_access ? t('edit permissions') : t('view permissions');
    $path = $gid ? "group/$group_type/$gid/admin" : 'admin/config/group';
    $permissions = l($text, "$path/permission/$rid/edit");

    if (!$default_access && !in_array($name, array(OG_ANONYMOUS_ROLE, OG_AUTHENTICATED_ROLE))) {
      $rows[] = array(check_plain($name), l(t('edit role'), "$path/role/$rid/edit"), $permissions);
    }
    else {
      $rows[] = array(check_plain($name), t('locked'), $permissions);
    }
  }

  $rows[] = array(array('data' => drupal_render($form['name']) . drupal_render($form['add']), 'colspan' => 4, 'class' => 'edit-name'));

  hide($form['actions']);
  $output = drupal_render_children($form);
  $output .= theme('table', array('header' => $header, 'rows' => $rows));
  $output .= render($form['actions']);

  return $output;
}

/**
 * Helper function to get role names.
 *
 * @param $group_type
 *   Group entity type. E.g. 'node'.
 * @param $bundle
 *   Group bundle.
 * @param $gid
 *   Group item ID.
 * @param $rid
 *   Role ID.
 *
 * @return array
 *   Role names according to parameters.
 */
function _og_ui_get_role_names($group_type, $bundle, $gid, $rid) {
  if ($gid) {
    $group = entity_load_single($group_type, $gid);

    list(,,$bundle) = entity_extract_ids($group_type, $group);
    $gid = og_is_group_default_access($group_type, $group) ? 0 : $gid;
  }

  $role_names = og_roles($group_type, $bundle, $gid);
  if ($rid && !empty($role_names[$rid])) {
    $role_names = array($rid => $role_names[$rid]);
  }

  return $role_names;
}

/**
 * Menu callback: administer permissions.
 *
 * @param $group_type
 *   Group entity type. E.g. 'node'.
 * @param $gid
 *   Group item ID.
 * @param $bundle
 *   Group bundle.
 * @param $rid.
 *   Role ID. When specified, edit only this role's permissions.
 *
 * @ingroup forms
 * @see og_ui_admin_permissions_submit()
 * @see theme_og_ui_admin_permissions()
 */
function og_ui_admin_permissions($form, $form_state, $group_type = '', $gid = 0, $bundle = '', $rid = 0) {
  if ($rid) {
    // Get group type and bundle from role.
    $role = og_role_load($rid);
    $bundle = $role->group_bundle;
    $group_type = $role->group_type;
  }

  if ($gid) {
    og_set_breadcrumb($group_type, $gid, array(l(t('Group'), "$group_type/$gid/group")));
  }

  $form['group_type'] = array('#type' => 'value', '#value' => $group_type);
  $form['bundle'] = array('#type' => 'value', '#value' => $bundle);
  $form['gid'] = array('#type' => 'value', '#value' => $gid);

  $role_names = _og_ui_get_role_names($group_type, $bundle, $gid, $rid);

  // Fetch permissions for all roles or the one selected role.
  $role_permissions = og_role_permissions($role_names);

  // Store $role_names for use when saving the data.
  $form['role_names'] = array(
    '#type' => 'value',
    '#value' => $role_names,
  );

  // Render role/permission overview:
  $options = array();
  $module_info = system_get_info('module');

  // Get a list of all the modules implementing a hook_permission() and sort by
  // display name.
  $modules = array();
  foreach (module_implements('og_permission') as $module) {
    $modules[$module] = $module_info[$module]['name'];
  }
  asort($modules);

  foreach ($modules as $module => $display_name) {
    if ($permissions = module_invoke($module, 'og_permission')) {
      $form['permission'][] = array(
        '#markup' => $module_info[$module]['name'],
        '#id' => $module,
      );
      foreach ($permissions as $perm => $perm_item) {
        // Fill in default values for the permission.
        $perm_item += array(
          'description' => '',
          'restrict access' => FALSE,
          'warning' => !empty($perm_item['restrict access']) ? t('Warning: Give to trusted roles only; this permission has security implications in the group context.') : '',
        );
        // If the user can manage permissions, but does not have administer
        // group permission, hide restricted permissions from them. This
        // prevents users from escalating their privileges.
        if ($gid && ($perm_item['restrict access'] && !og_user_access($group_type, $gid, 'administer group'))) {
          continue;
        }

        $options[$perm] = '';
        $form['permission'][$perm] = array(
          '#type' => 'item',
          '#markup' => $perm_item['title'],
          '#description' => theme('user_permission_description', array('permission_item' => $perm_item)),
        );
        foreach ($role_names as $rid => $name) {
          // Builds arrays for checked boxes for each role
          if (isset($role_permissions[$rid][$perm])) {
            $status[$rid][] = $perm;
          }
        }
      }
    }
  }

  // Have to build checkboxes here after checkbox arrays are built
  foreach ($role_names as $rid => $name) {
    $form['checkboxes'][$rid] = array(
      '#type' => 'checkboxes',
      '#options' => $options,
      '#default_value' => isset($status[$rid]) ? $status[$rid] : array(),
      '#attributes' => array('class' => array('rid-' . $rid)),
    );
    $form['role_names'][$rid] = array('#markup' => check_plain($name), '#tree' => TRUE);
  }


  if (!$gid || !og_is_group_default_access($group_type, $gid)) {
    $form['actions'] = array('#type' => 'actions');
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save permissions'),
      '#submit' => array('og_ui_admin_permissions_submit'),
    );
  }
  $form['#after_build'][] = 'og_ui_admin_permissions_after_build';
  return $form;
}

/**
 * Helper function to disable the permissions checkboxes.
 *
 * Every permission can declare to which roles it applies, as some don't make
 * sense if applied to anonymous or authenticated user (e.g. subscribe to group
 * should appear only to anonymous members).
 * A second case for disabling is when using deafult roles and permissions, and
 * the user is browsing a group's permissions page.
 */
function og_ui_admin_permissions_after_build($form) {
  $group_type = $form['group_type']['#value'];
  $gid = $form['gid']['#value'];
  $bundle = $form['bundle']['#value'];

  $role_names = $form['role_names']['#value'];

  // Indicate if all checkboxes should be disabled.
  $disable = $gid && og_is_group_default_access($group_type, $gid);

  // Authenticated roles.
  $roles = og_roles($group_type, $bundle, $gid);
  $anon_rid = array_search(OG_ANONYMOUS_ROLE, $roles);

  // Remove the anonymous member from the authenticated roles.
  unset($roles[$anon_rid]);
  $auth_rids = array_keys($roles);

  foreach (og_get_permissions() as $key => $perm) {
    $perm['roles'] = drupal_map_assoc($perm['roles']);

    if ($disable || (empty($perm['roles']) || empty($perm['roles'][OG_ANONYMOUS_ROLE]))) {
      if (in_array($anon_rid, array_keys($role_names))) {
        $form['checkboxes'][$anon_rid][$key]['#attributes']['disabled'] = TRUE;
      }
    }
    if ($disable || (empty($perm['roles']) || empty($perm['roles'][OG_AUTHENTICATED_ROLE]))) {
      foreach ($auth_rids as $auth_rid) {
        if (in_array($auth_rid, array_keys($role_names))) {
          $form['checkboxes'][$auth_rid][$key]['#attributes']['disabled'] = TRUE;
        }
      }
    }
  }

  return $form;
}

/**
 * Save permissions selected on the administer permissions page.
 *
 * @see og_user_admin_permissions()
 */
function og_ui_admin_permissions_submit($form, &$form_state) {
  foreach ($form_state['values']['role_names'] as $rid => $name) {
    og_role_change_permissions($rid, $form_state['values'][$rid]);
  }

  drupal_set_message(t('The changes have been saved.'));
}

/**
 * Theme the administer permissions page.
 *
 * @ingroup themeable
 */
function theme_og_ui_admin_permissions($variables) {
  $form = $variables['form'];
  $role_names = $form['role_names']['#value'];

  foreach (element_children($form['permission']) as $key) {
    $row = array();
    // Module name.
    if (is_numeric($key)) {
      $row[] = array('data' => drupal_render($form['permission'][$key]), 'class' => array('module'), 'id' => 'module-' . $form['permission'][$key]['#id'], 'colspan' => count($form['role_names']['#value']) + 1);
    }
    else {
      // Permission row.
      $row[] = array(
        'data' => drupal_render($form['permission'][$key]),
        'class' => array('permission'),
      );

      foreach (element_children($form['checkboxes']) as $rid) {
        $form['checkboxes'][$rid][$key]['#title'] = $role_names[$rid] . ': ' . $form['permission'][$key]['#markup'];
        $form['checkboxes'][$rid][$key]['#title_display'] = 'invisible';
        $row[] = array('data' => drupal_render($form['checkboxes'][$rid][$key]), 'class' => array('checkbox'));
      }
    }
    $rows[] = $row;
  }
  $header[] = (t('Permission'));
  foreach (element_children($form['role_names']) as $rid) {
    $header[] = array('data' => drupal_render($form['role_names'][$rid]), 'class' => array('checkbox'));
  }
  $output = '';
  $output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'permissions')));
  $output .= drupal_render_children($form);
  return $output;
}

/**
 * Allow site admin to add or remove group fields from fieldable enteies.
 */
function og_ui_field_settings($form, &$form_state) {
  $form = array();
  $options = array();

  $options = array();
  foreach (entity_get_info() as $entity_type => $entity) {
    if (!empty($entity['fieldable']) && $entity_type != 'group') {
      foreach($entity['bundles'] as $bundle_name => $bundle) {
        // Prefix the bundle name with the entity type.
        $options[$entity_type][$entity_type . '__' .$bundle_name] = check_plain($bundle['label']);
      }
    }
  }

  $form['bundles'] = array(
    '#title' => t('Bundles'),
    '#type' => 'select',
    '#options' => $options,
  );

  $options = array();
  foreach (og_fields_info() as $field_name => $field) {
    foreach ($field['type'] as $type) {
      $options[$type][$field_name] = filter_xss($field['instance']['label']);
    }
  }

  $form['fields'] = array(
    '#title' => t('Fields'),
    '#type' => 'select',
    '#options' => $options,
  );

  $field_enabled = array();

  $group_fields = og_fields_info();

  $group_fields_name = array_keys($group_fields);

  $entity_info = entity_get_info();

  // Get the fields that exist in the bundle.
  foreach (field_info_fields() as $field_name => $field) {
    if (in_array($field_name, $group_fields_name) && !empty($field['bundles'])) {
      foreach ($field['bundles'] as $entity_type => $bundles) {
        foreach ($bundles as $bundle) {
          $field_enabled[$entity_type][$bundle][] = $field_name;
        }
      }
    }
  }

  if ($field_enabled) {
    $form['group_fields'] = array(
      '#type' => 'vertical_tabs',
      '#weight' => 99,
    );

    // Show all the group fields of each bundle.
    foreach ($field_enabled as $entity_type => $bundles) {
      foreach ($bundles as $bundle => $fields) {
        $options = array();
        $bundles = field_info_bundles($entity_type);
        $form['group_fields_' . $entity_type . '_' . $bundle] = array(
          '#type' => 'fieldset',
          '#title' => t('@bundle - @entity entity', array('@bundle' => $bundles[$bundle]['label'], '@entity' => $entity_info[$entity_type]['label'])),
          '#collapsible' => TRUE,
          '#group' => 'group_fields',
        );
        foreach ($fields as $field_name) {
          $options[] = array(
            check_plain($group_fields[$field_name]['instance']['label']),
            filter_xss($group_fields[$field_name]['description']),
            l(t('Delete'), "admin/config/group/fields/$entity_type/$bundle/$field_name/delete"),
          );
        }

        $header = array(t('Field'), t('Description'), t('Operations'));
        $form['group_fields_' . $entity_type . '_' . $bundle]['fields'] = array(
          '#markup' => theme('table', array('header' => $header, 'rows' => $options)),
        );

      }
    }
  }
  else {
    $form['group_fields'] = array(
      '#markup' => t('There are no Group fields attached to any bundle yet.'),
    );
  }

  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Add field'),
  );

  return $form;
}

function og_ui_field_settings_validate($form, &$form_state) {
  $bundle = preg_replace('/^.*__/i', '', $form_state['values']['bundles']);
  $entity_type = str_replace('__' . $bundle, '', $form_state['values']['bundles']);

  // Check if field can be attached to entity.
  $group_field = og_fields_info($form_state['values']['fields']);
  $bundles = field_info_bundles($entity_type);
  $entity_info = entity_get_info($entity_type);

  $params = array(
    '%field' => $group_field['instance']['label'],
    '%bundle' => $bundles[$bundle]['label'],
    '%entity' => $entity_info['label'],
  );

  if ($form_state['values']['fields'] == OG_GROUP_FIELD && $entity_type == 'group') {
    form_set_error('bundles', t('It is not allowed to add the group field to the group entity.'));
  }

  // Check if field doesn't exist already.
  if (($field = field_info_field($form_state['values']['fields'])) && !empty($field['bundles'][$entity_type]) && in_array($bundle, drupal_map_assoc($field['bundles'][$entity_type]))) {
    form_set_error('bundles', t('Field %field already exists in %bundle.', $params));
  }

  // Check field can be attached to entity type.
  if (!empty($group_field['entity']) && !in_array($entity_type, $group_field['entity'])) {
    $items = array();
    foreach ($group_field['entity'] as $entity_type) {
      $info = entity_get_info($entity_type);
      $items[] = $info['label'];
    }
    form_set_error('bundles', t('Field %field can only be attached to %entities entity bundles.', $params + array('%entities' => implode(', ', $items))));
  }

}

function og_ui_field_settings_submit($form, &$form_state) {
  $bundle = preg_replace('/^.*__/i', '', $form_state['values']['bundles']);
  $entity_type = str_replace('__' . $bundle, '', $form_state['values']['bundles']);
  $field_name = $form_state['values']['fields'];

  og_create_field($field_name, $entity_type, $bundle);

  $group_field = og_fields_info($field_name);
  $bundles = field_info_bundles($entity_type);

  $params = array(
    '%field' => $group_field['instance']['label'],
    '%bundle' => $bundles[$bundle]['label'],
  );

  drupal_set_message(t('Added field %field to %bundle.', $params));
}


/**
 * Menu callback; present a form for removing a field instance from a bundle.
 */
function og_ui_field_delete_form($form, &$form_state, $instance) {
  module_load_include('inc', 'field_ui', 'field_ui.admin');
  $output = field_ui_field_delete_form($form, $form_state, $instance);

  $output['actions']['cancel'] = array(
    '#markup' => l(t('Cancel'), 'admin/config/group/fields'),
  );

  return $output;
}


function og_ui_field_delete_form_submit($form, &$form_state) {
  module_load_include('inc', 'field_ui', 'field_ui.admin');
  field_ui_field_delete_form_submit($form, $form_state);
  $form_state['redirect'] = 'admin/config/group/fields';
}
