You are here

function users_export_form in Users Export 7.2

Same name and namespace in other branches
  1. 7 users_export.admin.inc \users_export_form()

Form builder. Configure users_export.

See also

system_settings_form()

1 string reference to 'users_export_form'
users_export_menu in ./users_export.module
Implements hook_menu().

File

includes/users_export.admin.inc, line 16
Administration page callbacks for the users_export module.

Code

function users_export_form($form, &$form_state) {
  $info = loft_data_grids_export_info();

  // See if we get our visibiltiy using the new method.
  $options = loft_data_grids_exporter_options(TRUE, FALSE, FALSE);
  $needs_migrate = FALSE;
  if (empty($options)) {

    // So if there are no options because no perms have been set
    // we'll go ahead and rely on the older permissions and alert user
    // in a later update we'll remove these the hook_perms altogether.
    $options = loft_data_grids_exporter_options(FALSE, FALSE, FALSE);
    $needs_migrate = TRUE;
    foreach (array_keys($options) as $key) {
      if (!user_access('users_export:export as ' . $key)) {
        unset($options[$key]);
      }
    }
  }
  if ($needs_migrate && $options) {
    drupal_set_message(t('Please <a href="!url">go here</a> and transfer your <em>Users Export: Export As...</em> permissions to <em>Loft Data Grids: Visible in UI...</em> permissions as soon as possible.', array(
      '!url' => url('admin/people/permissions', array(
        'query' => drupal_get_destination(),
      )),
    )), 'warning', FALSE);
  }
  $form['#attached']['js'][] = array(
    'type' => 'file',
    'data' => drupal_get_path('module', 'users_export') . '/users_export.js',
    'scope' => 'footer',
  );
  $form['#attached']['js'][] = array(
    'type' => 'setting',
    'data' => array(
      'usersExport' => loft_data_grids_export_info(),
    ),
    'scope' => 'footer',
  );
  $class = variable_get('users_export_type', 'CSVExporter');
  $type = $info[$class]['extension'];
  $form['users_export_type'] = array(
    '#type' => 'select',
    '#title' => t('Export file format'),
    '#default_value' => $class,
    '#options' => $options,
  );
  $default = variable_get('users_export_filename', strtolower(preg_replace('/\\W+/', '_', variable_get('site_name', 'users_export')) . '_users'));
  $form['users_export_filename'] = array(
    '#type' => 'textfield',
    '#title' => t('Filename to save as'),
    '#default_value' => $default,
    '#required' => TRUE,
    '#field_suffix' => $type,
  );
  $test_mode = variable_get('users_export_test_mode', FALSE);
  $form['users_export_test_mode'] = array(
    '#type' => 'checkbox',
    '#title' => t('Preview mode (Enable to limit the export to only the first 10 users to check formatting.)'),
    '#default_value' => $test_mode,
  );
  $form['basic_filters'] = array(
    '#type' => 'fieldset',
    '#title' => t('Basic Filters'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['advanced'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced Settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['basic_filters']['users_export_login_frequency'] = array(
    '#type' => 'select',
    '#title' => t('Login Frequency'),
    '#default_value' => variable_get('users_export_login_frequency', 2),
    '#options' => array(
      2 => t('All Users'),
      1 => t('Get users who have logged in at least one time'),
      0 => t('Get users who have never logged in'),
    ),
  );

  // Add field to add 'blocked' users to the export as well.
  $form['basic_filters']['users_export_user_status'] = array(
    '#type' => 'select',
    '#title' => t('Export users with status'),
    '#default_value' => variable_get('users_export_user_status', 1),
    '#options' => array(
      1 => t('Active'),
      0 => t('Blocked'),
      2 => t('Both active and blocked'),
    ),
  );
  $form['basic_filters']['users_export_filter_by_role'] = [
    '#type' => 'radios',
    '#title' => t('Filter by role?'),
    '#options' => [
      0 => t('No'),
      1 => t('Yes'),
    ],
    '#default_value' => 0,
  ];
  $roles = user_roles(TRUE);
  $form['basic_filters']['users_export_user_role'] = [
    '#type' => 'checkboxes',
    '#title' => t('Export users with role'),
    '#options' => $roles,
    '#default_value' => array_keys($roles),
    '#states' => array(
      'invisible' => array(
        ':input[name="users_export_filter_by_role"]' => array(
          'value' => 0,
        ),
      ),
    ),
  ];
  $form['basic_filters']['users_export_order'] = array(
    '#type' => 'select',
    '#title' => t('Order of results'),
    '#default_value' => 0,
    '#options' => array(
      0 => t('Default (uid)'),
      1 => t('Username A-Z'),
      2 => t('E-mail A-Z'),
    ),
  );
  if (module_exists('date')) {
    $options = array();
    foreach (date_format_type_options() as $type => $title) {
      $type = date_format_type_format($type);
      $options[$type] = $title;
    }

    // Now add in those we think most users will want to use.
    $options += array(
      DATE_FORMAT_DATETIME => 'Datetime (' . date(DATE_FORMAT_DATETIME) . ')',
      DATE_FORMAT_UNIX => 'Unix Timestamp (' . date(DATE_FORMAT_UNIX) . ')',
      DATE_FORMAT_ISO => 'ISO 8601 (' . date(DATE_FORMAT_ISO) . ')',
      DATE_FORMAT_ICAL => 'iCal (' . date(DATE_FORMAT_ICAL) . ')',
    );
    $form['advanced']['users_export_date_format'] = array(
      '#type' => 'select',
      '#title' => t('Date format'),
      '#default_value' => variable_get('users_export_date_format', DATE_FORMAT_DATETIME),
      '#options' => $options,
    );
  }
  else {
    $form['advanced']['users_export_date_format'] = array(
      '#type' => 'hidden',
      '#value' => USERS_EXPORT_DEFAULT_DATE_FORMAT,
      '#prefix' => t('Enable the <a href="!url" target="blank">date module</a> to be able to control date formatting.', array(
        '!url' => url('https://drupal.org/project/date'),
      )),
    );
  }
  $options = array(
    -1 => t('- Default -'),
  );
  foreach (range(128, 2048, 32) as $value) {
    $options[$value . 'M'] = format_size($value * 1024 * 1024);
  }
  $form['advanced']['users_export_memory_limit'] = array(
    '#type' => 'select',
    '#title' => t('Memory Limit', array(
      '@size' => format_size(variable_get('users_export_last_export_memory', 0)),
    )),
    '#description' => t('If you have many users you may need to set this value higher so you the web server does not run out of memory processing the export. <strong>Depending upon your server configuration, this may or may not have any effect!</strong> For more information refer to <a href="http://php.net/manual/en/function.ini-set.php" target="blank">http://php.net/manual/en/function.ini-set.php</a>.'),
    '#default_value' => variable_get('users_export_memory_limit', 160),
    '#options' => $options,
  );
  $options = array(
    -1 => t('- Default -'),
  );
  foreach (range(30, 1800, 30) as $value) {
    $options[$value] = format_interval($value);
  }
  $form['advanced']['users_export_max_execution'] = array(
    '#type' => 'select',
    '#title' => t('Maximum Execution Time', array(
      '@time' => format_interval(variable_get('users_export_last_export_time', 0)),
    )),
    '#description' => t('If you have many users you may need to set this value higher so you the web server does not timeout. <strong>Depending upon your server configuration, this may or may not have any effect!</strong> For more information refer to <a href="http://php.net/manual/en/function.set-time-limit.php" target="blank">http://php.net/manual/en/function.set-time-limit.php</a>.'),
    '#default_value' => variable_get('users_export_max_execution', 60),
    '#options' => $options,
  );
  $form = system_settings_form($form);
  $form['actions']['submit']['#value'] = t('Download File');
  $form['#submit'][] = 'users_export_form_submit';
  return $form;
}