You are here

function og_access_alter_nongroup_form in Organic groups 6

Same name and namespace in other branches
  1. 5.8 og_access.module \og_access_alter_nongroup_form()
  2. 5 og_access.module \og_access_alter_nongroup_form()
  3. 5.3 og_access.module \og_access_alter_nongroup_form()
  4. 5.7 og_access.module \og_access_alter_nongroup_form()
  5. 6.2 modules/og_access/og_access.module \og_access_alter_nongroup_form()
1 call to og_access_alter_nongroup_form()
og_access_form_alter in modules/og_access/og_access.module
Implementation of hook_form_alter().

File

modules/og_access/og_access.module, line 117

Code

function og_access_alter_nongroup_form(&$form, $form_state, $node) {
  global $user;

  // If user has no subscriptions, don't bother with Public checkbox - it's meaningless.
  if (og_is_group_post_type($node->type) && !empty($form['og_nodeapi']['visible'])) {

    // get the visibility for normal users
    $vis = variable_get('og_visibility', 0);

    // override visibility for og admins
    if (user_access('administer organic groups')) {
      if ($vis < 2) {
        $vis = $vis == OG_VISIBLE_GROUPONLY ? OG_VISIBLE_CHOOSE_PRIVATE : OG_VISIBLE_CHOOSE_PUBLIC;
      }
    }
    elseif (!og_get_subscriptions($user->uid)) {

      // don't show checkbox if no memberships. must be public.
      $vis = OG_VISIBLE_BOTH;
    }

    // We are using this form element to communicate $groups from og to og_access.
    $groups = $form['og_initial_groups']['#value'];

    // If the post is to a private group, visibility must default to one of the private options.
    $selected_groups = isset($form_state['values']['og_groups']) ? array_filter($form_state['values']['og_groups']) : $groups;
    if (!empty($selected_groups)) {
      foreach ($selected_groups as $gid) {
        $group_node = new stdClass();
        $group_node->nid = $gid;
        og_load_group($group_node);
        if (!empty($group_node->og_private)) {

          // Try not to show checkbox if admin likes to reduce decisions for node authors.
          $vis = variable_get('og_visibility', 0) == OG_VISIBLE_BOTH ? OG_VISIBLE_GROUPONLY : OG_VISIBLE_CHOOSE_PRIVATE;
          break;
        }
      }
    }
    else {

      // TODOL: No groups. Public must be checked if it is visible.
    }
    switch ($vis) {
      case OG_VISIBLE_BOTH:
        $form['og_nodeapi']['og_public'] = array(
          '#type' => 'value',
          '#value' => 1,
        );
        break;
      case OG_VISIBLE_GROUPONLY:
        $form['og_nodeapi']['og_public'] = array(
          '#type' => 'value',
          '#value' => 0,
        );
        break;

      //user decides how public the post is.
      case OG_VISIBLE_CHOOSE_PUBLIC:
        $form['og_nodeapi']['visible']['og_public'] = array(
          '#type' => 'checkbox',
          '#title' => t('Public'),
          '#default_value' => isset($node->og_public) ? $node->og_public : 1,
          '#description' => t('Show this post to everyone, or only to members of the groups checked above. Posts without any groups are always <em>public</em>.'),
          '#weight' => 2,
        );
        break;
      case OG_VISIBLE_CHOOSE_PRIVATE:
        $form['og_nodeapi']['visible']['og_public'] = array(
          '#type' => 'checkbox',
          '#title' => t('Public'),
          '#default_value' => isset($node->og_public) ? $node->og_public : 0,
          '#description' => t('Show this post to everyone, or only to members of the groups checked above. Posts without any groups are always <em>public</em>.'),
          '#weight' => 2,
        );
        break;
    }
    if (count($form['og_nodeapi']['visible']) > 1) {
      $form['og_nodeapi']['#type'] = 'fieldset';
      $form['og_nodeapi']['#title'] = t('Groups');
      $form['og_nodeapi']['#collapsible'] = TRUE;
      $form['og_nodeapi']['#collapsed'] = $selected_groups ? TRUE : FALSE;
    }
  }
}