You are here

party_user.admin.inc in Party 8.2

Same filename and directory in other branches
  1. 7 modules/party_user/party_user.admin.inc

Provide config forms for views

File

modules/party_user/party_user.admin.inc
View source
<?php

/**
 * @file Provide config forms for views
 */

/**
 * User integration settings form.
 */
function party_user_settings_form($form, &$form_state) {
  $form['party_user_on_registration'] = array(
    '#type' => 'radios',
    '#title' => t('On user registration:'),
    '#options' => array(
      'nothing' => t('Do nothing'),
      'create' => t('Create a new party'),
      'acquire' => t('Acquire an existing party, creating one if necessary'),
    ),
    '#default_value' => variable_get('party_user_on_registration', 'nothing'),
  );
  $options = array();
  foreach (party_get_data_set_info() as $data_set_name => $def) {
    if ($data_set_name != 'user') {
      $options[$data_set_name] = $def['label'];
    }
  }
  if (module_exists('party_hat')) {
    $options = array();
    foreach (party_hat_get_tree() as $hat_name => $hat) {
      $options[$hat_name] = str_repeat('-', $hat->depth) . $hat->label;
    }
    $form['registration_hats'] = array(
      '#type' => 'checkboxes',
      '#options' => $options,
      '#title' => t("What hat's should users' parties be given on registration?"),
      '#default_value' => variable_get('party_user_registration_hats', array()),
    );
  }

  // Retrieve all the data sets so we can look for appropriate fields
  $data_sets = party_get_data_set_info();
  $options = array();
  foreach ($data_sets as $data_set => $data_set_info) {
    $fields = field_info_instances($data_set_info['entity type'], $data_set_info['entity bundle']);
    foreach ($fields as $field => $field_info) {
      if ($field_info['widget']['type'] == 'text_textfield') {
        $options["{$data_set}:{$field}"] = t('@data_set_label - @label [%field_name]', array(
          '@label' => $field_info['label'],
          '@data_set_label' => $data_set_info['label'],
          '%field_name' => $field,
        ));
      }
    }
  }
  $form['email_sync'] = array(
    '#type' => 'checkboxes',
    '#options' => $options,
    '#title' => t('Fields to synchronise with user email'),
    '#description' => t("Any time the user is saved, these fields will be updated to match the user's email address."),
    '#default_value' => variable_get('party_user_email_sync_fields', array()),
  );
  $form['party_user_format_username'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use Party label to format usernames'),
    '#default_value' => variable_get('party_user_format_username', TRUE),
  );
  $form['party_user_party_delete_action'] = array(
    '#type' => 'radios',
    '#title' => t('What to do with attached users when their Party is deleted'),
    '#default_value' => variable_get('party_user_party_delete_action', 'disallow'),
    '#options' => array(
      'disallow' => 'Disallow the deletion of Parties with attached users',
      'user_cancel_reassign' => 'Delete the account and make its content belong to the Anonymous user',
      'user_cancel_delete' => 'Delete the account and its content',
    ),
    '#description' => t('When a party is deleted what should be done with the attached user entities?'),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Save configuration',
    '#weight' => 40,
  );
  return $form;
}

/**
 * Submit handler for the user integration settings form.
 */
function party_user_settings_form_submit($form, &$form_state) {
  variable_set('party_user_on_registration', $form_state['values']['party_user_on_registration']);
  variable_set('party_user_format_username', $form_state['values']['party_user_format_username']);
  variable_set('party_user_party_delete_action', $form_state['values']['party_user_party_delete_action']);
  if (module_exists('party_hat')) {
    $registration_hats = $form_state['values']['registration_hats'];
    foreach ($registration_hats as $key => $var) {
      if ($var === 0) {
        unset($registration_hats[$key]);
      }
    }
    $registration_hats = array_keys($registration_hats);
    variable_set('party_user_registration_hats', $registration_hats);
  }
  drupal_set_message(t('The configuration options have been saved.'));

  // Email Sync Settings
  $email_sync_fields = $form_state['values']['email_sync'];
  $email_sync_fields = array_keys($email_sync_fields, TRUE);
  variable_set('party_user_email_sync_fields', $email_sync_fields);
}

/**
 * User Sync Form
 *
 * @todo: Optionally use the rule event to sync
 * @todo: Limit by role
 * @todo: Choose which Party Types to create
 */
function party_user_sync_form($form, &$form_state) {
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Synchronise Users',
  );
  return $form;
}
function party_user_sync_form_submit($form, &$form_state) {

  // Get all Users
  $users = db_select('users', 'u')
    ->fields('u', array(
    'uid',
    'name',
  ))
    ->condition('uid', 0, '<>')
    ->execute()
    ->fetchAllAssoc('uid', PDO::FETCH_ASSOC);
  $batch = array(
    'operations' => array(),
    'finished' => 'party_user_sync_finished',
    'title' => t("Processing User Sync"),
    'init_message' => t('User Sync is starting'),
    'progress_message' => t('Processed @current out of @total'),
    'error_message' => t('User Sync Encountered an Error'),
    'file' => drupal_get_path('module', 'party_user') . '/party_user.batch.inc',
  );
  foreach ($users as $user) {
    $batch['operations'][] = array(
      'party_user_batch_user_sync',
      array(
        $user,
        array(),
      ),
    );
  }
  batch_set($batch);
  $form_state['redirect'] = 'admin/config/party/user/sync';
}

Functions

Namesort descending Description
party_user_settings_form User integration settings form.
party_user_settings_form_submit Submit handler for the user integration settings form.
party_user_sync_form User Sync Form
party_user_sync_form_submit