You are here

cas_roles_og.admin.inc in CAS roles 7.2

Provides settings pages for the CAS Attributes module.

File

cas_roles_og/cas_roles_og.admin.inc
View source
<?php

/**
 * @file
 * Provides settings pages for the CAS Attributes module.
 */

/**
 * Administrative settings form.
 */
function cas_roles_og_admin_settings($form, &$form_state, $group_type, $gid) {
  $group = entity_load_single($group_type, $gid);

  /** @var EntityDrupalWrapper $group_wrapper */
  $group_wrapper = entity_metadata_wrapper($group_type, $group);
  $roles = og_roles($group_type, $group_wrapper
    ->getBundle(), $gid, FALSE, FALSE);
  $form['bundle'] = array(
    '#type' => 'hidden',
    '#value' => $group_wrapper
      ->getBundle(),
  );
  $all_settings = variable_get('cas_roles_og_' . $group_type, array());
  if (!isset($all_settings[$gid])) {
    $settings = cas_roles_og_admin_settings_default($roles);
  }
  else {
    $settings = $all_settings[$gid];
  }
  $form['sync_every_login'] = array(
    '#type' => 'radios',
    '#title' => t('Fetch CAS Roles'),
    '#default_value' => $settings['sync_every_login'],
    '#options' => array(
      0 => t('only when a CAS account is created (i.e., the first login of a CAS user).'),
      1 => t('every time a CAS user logs in.'),
    ),
    '#weight' => -10,
  );
  $form['role_attribute'] = array(
    '#type' => 'textfield',
    '#title' => t('Attribute for roles'),
    '#default_value' => $settings['role_attribute'],
    '#size' => 50,
    '#description' => t('CAS Attributes may be arrays, use the tokens syntax starting with "[cas:attribute:" global tokens are also replaced.<br />Example: [cas:attribute:drupal_roles]<br />If this field is empty the group will not be considered and the settings deleted.'),
    '#weight' => -8,
  );
  $form['require_member_role_join'] = array(
    '#type' => 'checkbox',
    '#title' => t('Require the member role explicitly'),
    '#default_value' => $settings['require_member_role_join'],
    '#description' => t('Users will be added to the group only if the "member" condition matches, otherwise any role is enough.<br />If this option is set but the member role does not have a regular expression, users will not be added or removed and only existing members roles will be managed.'),
    '#weight' => -7,
  );
  $form['require_a_role_stay'] = array(
    '#type' => 'checkbox',
    '#title' => t('Require at least one role for staying in the group'),
    '#default_value' => $settings['require_a_role_stay'],
    '#description' => t('Users will be removed from the group if they do not have any role or if they would not be allowed to join.<br />If the membership is not explicitly required and the member role does not have a regular expression, being a member will not be considered as having a role.'),
    '#weight' => -6,
  );
  $form['relations'] = array(
    '#type' => 'fieldset',
    '#title' => t('CAS roles mappings'),
    'relations' => array(),
    '#tree' => TRUE,
    '#weight' => -5,
  );
  $form['relations']['help'] = array(
    '#markup' => t('Regular expression to map <a href="@url">og roles</a>. The role is assigned if one of the roles in the attribute array matches the expression. An empty field means the role is not administrated by CAS.', array(
      '@url' => url("group/{$group_type}/{$gid}/admin/roles"),
    )),
  );
  $form['relations']['member'] = array(
    '#type' => 'textfield',
    '#title' => t('member'),
    '#default_value' => $settings['relations']['member'],
    '#size' => 50,
    '#maxlength' => 1024,
    '#element_validate' => array(
      '_cas_roles_preg_validate',
    ),
    '#description' => t('The "member" role will be assigned to all users that are allowed to join.'),
  );
  foreach ($roles as $rid => $role) {
    $form['relations'][$rid] = array(
      '#type' => 'textfield',
      '#title' => $role,
      '#default_value' => $settings['relations'][$rid],
      '#size' => 50,
      '#maxlength' => 1024,
      '#element_validate' => array(
        '_cas_roles_preg_validate',
      ),
    );
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Save Configuration',
  );
  return $form;
}

/**
 * Submit handler.
 */
function cas_roles_og_admin_settings_submit($form, &$form_state) {
  $settings_variable = 'cas_roles_og_' . $form_state['build_info']['args'][0];
  $gid = $form_state['build_info']['args'][1];

  // Get the settings.
  $all_settings = variable_get($settings_variable, array());

  // Select the subset of the form submission.
  $defaults = cas_roles_og_admin_settings_default(array());
  $values = array_intersect_key($form_state['values'], $defaults);

  // Remove the settings if the role attribute is not set.
  if (!$values['role_attribute']) {
    if (isset($all_settings[$gid])) {
      unset($all_settings[$gid]);
      variable_set($settings_variable, $all_settings);
    }
    drupal_set_message(t('The configuration options are insufficient.'));
    drupal_set_message(t('The group will not be considered in the CAS matching.'));
  }
  else {
    $all_settings[$gid] = $values;
    variable_set($settings_variable, $all_settings);
    drupal_set_message(t('The configuration options have been saved.'));
  }
}

/**
 * Default settings for cas_roles_og admin form.
 *
 * @param array $roles
 *   OG roles to be used for the default keys.
 *
 * @return array
 */
function cas_roles_og_admin_settings_default(array $roles) {
  $relations = array_fill_keys(array_merge(array(
    'member',
  ), array_keys($roles)), '');
  $settings = array(
    'bundle' => 'node',
    'sync_every_login' => 1,
    'role_attribute' => '',
    'require_member_role_join' => 0,
    'require_a_role_stay' => 1,
    'relations' => $relations,
  );
  return $settings;
}

Functions

Namesort descending Description
cas_roles_og_admin_settings Administrative settings form.
cas_roles_og_admin_settings_default Default settings for cas_roles_og admin form.
cas_roles_og_admin_settings_submit Submit handler.