You are here

fasttoggle_user.module in Fasttoggle 7

File

module/fasttoggle_user/fasttoggle_user.module
View source
<?php

/**
 * @file
 * Fasttoogle user object hooks.
 */
module_load_include('inc', 'fasttoggle_user');
module_load_include('inc', 'fasttoggle');

/**
 * Get the string to display for a user role name.
 *
 * @param string $role
 *   The name of the role.
 *
 * @return string
 *   The description to display on a link.
 */
function fasttoggle_role_name($role) {
  return "Toggle role " . $role;
}

/**
 * Implements hook_perm().
 */
function fasttoggle_user_permission() {
  $available_roles = fasttoggle_sitewide_toggleable_roles();
  $roles = array();
  if (!empty($available_roles)) {
    foreach ($available_roles as $rid => $name) {
      $roles[fasttoggle_role_name($name)] = array(
        'title' => t('Toggle role @name', array(
          '@name' => $name,
        )),
      );
    }
  }
  $fixed_roles = array(
    'moderate users' => array(
      'title' => t('Moderate users'),
    ),
  );
  $result = array_merge($roles, $fixed_roles);
  asort($result);
  return $result;
}

/**
 * Check access to toggle a particular role.
 *
 * @param object $obj
 *   Unused object parameter.
 * @param string $type
 *   Unused object type parameter.
 * @param string $group
 *   Unused setting group parameter.
 * @param string $instance
 *   The instance of the setting group (the role).
 *
 * @return int
 *   Access check result.
 */
function fasttoggle_user_role_access($obj, $type, $group, $instance) {
  static $available_roles = array();
  if (empty($available_roles)) {
    $available_roles = fasttoggle_sitewide_toggleable_roles();
  }
  if (!isset($available_roles[$instance])) {
    return FALSE;
  }
  return fasttoggle_allow_access_if(user_access("Toggle role {$available_roles[$instance]}"));
}

/**
 * Check access to toggle a user's status.
 *
 * @param object $obj
 *   The object user.
 * @param string $type
 *   Unused object type parameter.
 * @param string $group
 *   Unused setting group parameter.
 * @param string $instance
 *   Unused setting instance parameter.
 *
 * @return int
 *   Access check result.
 */
function fasttoggle_user_status_access($obj, $type, $group, $instance) {
  $allow_u1 = variable_get('fasttoggle_allow_block_user1', 0);
  if (!is_null($obj) && $obj->uid == 0) {
    return FALSE;
  }
  return fasttoggle_allow_access_if((user_access("administer users") || user_access("moderate users")) && (is_null($obj) || $obj->uid != 1 || $allow_u1));
}

/**
 * Implements hook_user().
 *
 * Add togglable links to user pages.
 */
function fasttoggle_user_view($account, $view_mode, $langcode) {
  $link_data = fasttoggle_user_link('user', $account, TRUE);
  $links = array();
  foreach ($link_data as $name => $link) {
    if ($name == 'status_status') {
      $links['status'] = array(
        'title' => l(t('@title', array(
          '@title' => $link['title'],
        )), $link['href'], $link),
        'html' => TRUE,
      );
    }
    else {
      if (strpos($name, 'role_') === 0) {
        $links[$name] = array(
          'title' => l(t('@title', array(
            '@title' => $link['title'],
          )), $link['href'], $link),
          'html' => TRUE,
        );
      }
      else {
        $links[$name] = array(
          'title' => l($link['title'], $link['href'], $link),
          'html' => TRUE,
        );
      }
    }
  }

  // If any links were made, add them to the entity's links array.
  if (isset($links)) {
    $account->content['links']['fasttoggle'] = array(
      '#theme' => 'links',
      '#links' => $links,
      '#attributes' => array(
        'class' => array(
          'links',
          'inline',
        ),
      ),
    );
  }
}

/**
 * Get whether a role is assigned.
 *
 * @param array $options
 *   The array of options (unused).
 * @param string $group
 *   The group of settings.
 * @param string $option
 *   The particular option (role).
 * @param object $object
 *   The user object to check.
 *
 * @return int
 *   Whether the role is assigned to this user.
 */
function fasttoggle_get_user_role_option_value(array $options, $group, $option, $object) {
  return intval(isset($object->roles[$option]));
}

/**
 * Toggle a role in the list of roles assigned to a user.
 *
 * @param object $account
 *   The object account.
 * @param string $role
 *   The role to be toggled.
 *
 * @return mixed
 *   The new list of roles assigned to the user.
 */
function fasttoggle_get_user_role_array($account, $role) {
  $result = $account->roles;
  if (isset($account->roles[$role])) {
    unset($result[$role]);
  }
  else {
    $result[$role] = TRUE;
  }
  return $result;
}

/**
 * Save the user object.
 *
 * @param array $options
 *   The array of options.
 * @param string $group
 *   The setting group string.
 * @param string $instance
 *   The setting instance string.
 * @param string $new_value
 *   The new value to apply.
 * @param object $object
 *   The user object.
 */
function fasttoggle_user_save(array $options, $group, $instance, $new_value, $object) {
  $edit = array();
  if ($group == 'role') {
    $edit['roles'] = $new_value;
  }
  else {
    $edit[$instance] = $new_value;
  }
  user_save($object, $edit);
}

/**
 * Implements hook_form_alter().
 */
function fasttoggle_user_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'user_admin_account':

      // Add blocked/unblocked toggle links to the user overview page.
      $config = fasttoggle_get_allowed_links('user');
      $settings = variable_get('fasttoggle_user_status_settings', array(
        'status_status' => TRUE,
      ));
      if (variable_get('fasttoggle_enhance_user_overview_page', 1) && $settings['status_status'] !== 0 && isset($form['accounts']['#options']) && (user_access('administer users') || user_access('moderate users'))) {
        $allow_u1 = variable_get('fasttoggle_allow_block_user1', 0);
        $option_info = fasttoggle_get_available_links('user');
        $label_style = variable_get('fasttoggle_label_style', FASTTOGGLE_LABEL_STATUS);
        foreach ($form['accounts']['#options'] as $key => $data) {
          if ($key != 1 || $allow_u1) {
            $status = intval($data['status'] == t('active'));
            $account_obj = new stdClass();
            $account_obj->uid = $key;
            $account_obj->status = $status;
            $form['accounts']['#options'][$key]['status'] = [];
            $current_label = $option_info['fields']['status']['instances']['status']['labels'][$label_style][$status];
            $form['accounts']['#options'][$key]['status']['data'][$current_label] = fasttoggle($option_info, 'status', 'status', $account_obj, FASTTOGGLE_FORMAT_FORM);
          }
        }
      }
      break;
  }
}

/**
 * Implements hook_link().
 */
function fasttoggle_user_link($type, $obj = NULL, $teaser = FALSE) {
  $links = array();
  $options = fasttoggle_get_allowed_links($type, $obj);
  if (!empty($options)) {
    $label_style = variable_get('fasttoggle_label_style', FASTTOGGLE_LABEL_STATUS);
    switch ($type) {

      // User is not one of the standard types for hook_link(). This
      // use enables adding of user links to a user profile. It may be called
      // from a view field handler without a proper user object loaded.
      case 'user':
        if (!empty($options['fields'])) {
          foreach ($options['fields'] as $group => $flags) {
            if (!empty($flags['instances'])) {
              foreach ($flags['instances'] as $key => $data) {
                $links[$group . "_" . $key] = fasttoggle($options, $group, $key, $obj, FASTTOGGLE_FORMAT_LINK_ARRAY);
              }
            }
          }
        }
        break;
    }
  }
  return $links;
}

/**
 * Implements hook_fasttoggle_available_links().
 */
function fasttoggle_user_fasttoggle_available_links($type = NULL, $obj = NULL) {
  if (!is_null($type) && $type != 'user') {
    return array();
  }
  $available_roles = fasttoggle_potential_toggleable_roles();
  $result = array(
    'user' => array(
      'id_field' => 'uid',
      'title_field' => 'name',
      'save_fn' => 'fasttoggle_user_save',
      'object_type' => 'user',
      'extra_settings' => array(
        'fasttoggle_allow_block_user1' => array(
          '#type' => 'checkbox',
          '#title' => t("Allow user 1's account to be blocked using Fasttoggle."),
          '#default_value' => variable_get('fasttoggle_allow_block_user1', 0),
        ),
        'fasttoggle_enhance_user_overview_page' => array(
          '#type' => 'checkbox',
          '#title' => t('Add blocked/unblocked toggle links to the user overview page.'),
          '#default_value' => variable_get('fasttoggle_enhance_user_overview_page', TRUE),
        ),
      ),
      'fields' => array(
        'status' => array(
          'instances' => array(
            'status' => array(
              'description' => t('Status <small>(unblocked/blocked)</small>'),
              'default' => TRUE,
              'access' => array(
                'fasttoggle_user_status_access',
              ),
              'labels' => array(
                FASTTOGGLE_LABEL_ACTION => array(
                  0 => t('unblock'),
                  1 => t('block'),
                ),
                FASTTOGGLE_LABEL_STATUS => array(
                  0 => t('blocked'),
                  1 => t('active'),
                ),
              ),
            ),
          ),
        ),
        'role' => array(
          '#title' => t('Roles that may be toggled'),
          'new_value_fn' => 'fasttoggle_get_user_role_array',
          'value_fn' => 'fasttoggle_get_user_role_option_value',
        ),
      ),
    ),
  );
  foreach ($available_roles as $rid => $role_name) {
    $safe_role = check_plain($role_name);
    $result['user']['fields']['role']['instances'][$rid] = array(
      'description' => ucfirst($safe_role),
      'default' => TRUE,
      'value_key' => $rid,
      'access' => array(
        'fasttoggle_user_role_access',
      ),
      'role' => $safe_role,
      'labels' => array(
        FASTTOGGLE_LABEL_ACTION => array(
          0 => t("Add role @role", array(
            '@role' => $safe_role,
          )),
          1 => t("Revoke @role role", array(
            '@role' => $safe_role,
          )),
        ),
        FASTTOGGLE_LABEL_STATUS => array(
          0 => t("Lacks role @role", array(
            '@role' => $safe_role,
          )),
          1 => t("Has @role role", array(
            '@role' => $safe_role,
          )),
        ),
      ),
    );
  }
  return $result;
}

/**
 * Implements hook_views_api().
 */
function fasttoggle_user_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'fasttoggle_user') . '/views',
  );
}

Functions

Namesort descending Description
fasttoggle_get_user_role_array Toggle a role in the list of roles assigned to a user.
fasttoggle_get_user_role_option_value Get whether a role is assigned.
fasttoggle_role_name Get the string to display for a user role name.
fasttoggle_user_fasttoggle_available_links Implements hook_fasttoggle_available_links().
fasttoggle_user_form_alter Implements hook_form_alter().
fasttoggle_user_link Implements hook_link().
fasttoggle_user_permission Implements hook_perm().
fasttoggle_user_role_access Check access to toggle a particular role.
fasttoggle_user_save Save the user object.
fasttoggle_user_status_access Check access to toggle a user's status.
fasttoggle_user_view Implements hook_user().
fasttoggle_user_views_api Implements hook_views_api().