You are here

function mass_contact_form_user_profile_form_alter in Mass Contact 7

Implements hook_form_FORM_ID_alter() for user_profile_form.

File

./mass_contact.module, line 256
This is the main code file for the Mass Contact module.

Code

function mass_contact_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
  if ($form['#user_category'] == 'account') {
    $account = $form['#user'];

    // The admin allows a blanket opt out of all mass mailings.
    if (variable_get('mass_contact_optout_d', 0) == 1) {
      $form['mass_contact'] = array(
        '#type' => 'fieldset',
        '#title' => t('Mass contact settings'),
        '#weight' => 5,
        '#collapsible' => TRUE,
      );
      $form['mass_contact']['mass_contact_optout'] = array(
        '#type' => 'checkbox',
        '#title' => t('Opt-out of mass email messages'),
        '#default_value' => !empty($account->data['mass_contact_optout']) ? $account->data['mass_contact_optout'] : 0,
        '#description' => variable_get('mass_contact_optout_message', t('Allows you to opt-out of receiving mass email messages from privileged users. Note that site administrators are able to include you in mass email messages even if you choose not to enable this feature, and the ability to opt-out may be removed by the administrator at any time.')),
      );
    }
    elseif (variable_get('mass_contact_optout_d', 0) == 2) {
      $form['mass_contact'] = array(
        '#type' => 'fieldset',
        '#title' => t('Mass contact settings'),
        '#weight' => 5,
        '#collapsible' => TRUE,
        '#description' => variable_get('mass_contact_optout_message', t('Allows you to opt-out of receiving mass email messages from privileged users. Note that site administrators are able to include you in mass email messages even if you choose not to enable this feature, and the ability to opt-out may be removed by the administrator at any time.')),
      );
      $included_categories = array();
      $all_included_categories = array();

      // Add form elements provided by grouping_method plugins.
      ctools_include('plugins');

      // Get the information about all plugins that implemnent this type of
      // plugin.
      $plugins = ctools_get_plugins('mass_contact', 'grouping_method');
      foreach ($plugins as $plugin) {

        // Get the admin edit function name for this particular implementation.
        $function = ctools_plugin_get_function($plugin, 'mass_contact_user_edit');

        // Call the plugin function to add the form snippet(s).
        $included_categories = $function($form['#user']);
        if (!empty($included_categories)) {
          $all_included_categories += $included_categories;
        }
      }
      if (!empty($all_included_categories)) {

        // Eliminate duplicates.
        $all_included_categories = array_unique($all_included_categories);

        // Sort the contents, naturally and case insensitively.
        natcasesort($all_included_categories);
        foreach ($all_included_categories as $category_cid => $category_category) {

          // If the user is in the category, show the option to opt out of it.
          $form['mass_contact']['mass_contact_optout_' . $category_cid] = array(
            '#type' => 'checkbox',
            '#title' => t('Opt-out of mass email messages to the %category category.', array(
              '%category' => $category_category,
            )),
            '#default_value' => !empty($account->data['mass_contact_optout_' . $category_cid]) ? $account->data['mass_contact_optout_' . $category_cid] : 0,
          );
        }
      }
    }
  }
}