You are here

function mass_contact_user in Mass Contact 6

Same name and namespace in other branches
  1. 5.2 mass_contact.module \mass_contact_user()
  2. 5 mass_contact.module \mass_contact_user()

Implementation of hook_user().

File

./mass_contact.module, line 163
This is the main code file for the Mass Contact module. This module enables users to contact multiple users through selected roles.

Code

function mass_contact_user($type, $edit, &$user, $category = NULL) {
  if ($type == 'validate') {
    return array(
      'mass_contact_optout' => $edit['mass_contact_optout'],
    );
  }
  elseif ($type == 'form' && $category == 'account') {
    if (variable_get('mass_contact_optout_d', 0) == 1) {
      $form['mail'] = array(
        '#type' => 'fieldset',
        '#title' => t('Mass contact settings'),
        '#weight' => 5,
        '#collapsible' => TRUE,
      );
      $form['mail']['mass_contact_optout'] = array(
        '#type' => 'checkbox',
        '#title' => t('Opt-out of mass e-mails'),
        '#default_value' => $edit['mass_contact_optout'],
        '#description' => variable_get('mass_contact_optout_message', t('Allows you to opt-out of receiving mass e-mails from privileged users. Note that site administrators are able to include you in mass e-mails even if you choose not to enable this feature, and the ability to opt-out may be removed by the administrator at any time.')),
      );
      return $form;
    }
    elseif (variable_get('mass_contact_optout_d', 0) == 2) {

      // Initialize the variables we're going to use.
      $users_roles = array();
      $user_role = 0;
      $included_categories = array();

      // Start with the 'authenticated user' role, as it's not in the database.
      $users_roles[] = 2;

      // Get all the roles this user is a memeber of.
      $user_roles = db_query('SELECT rid FROM {users_roles} WHERE uid = %d', $user->uid);

      // Put them into an array for later reference.
      while ($user_role = db_fetch_array($user_roles)) {
        $users_roles[] = $user_role['rid'];
      }

      // Get all Mass Contact categories.
      $categories = db_query('SELECT cid, category, recipients FROM {mass_contact}');

      // Iterate through each category.
      while ($category = db_fetch_object($categories)) {

        // Pull out the roles that are a part of this category.
        foreach (explode(',', $category->recipients) as $role_id) {

          // If the category's role is one the user is part of, show the category.
          if (in_array($role_id, $users_roles)) {
            $included_categories[$category->cid] = $category->category;
          }
        }
      }
      if ($included_categories) {
        $form['mail'] = 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 e-mails from privileged users. Note that site administrators are able to include you in mass e-mails even if you choose not to enable this feature, and the ability to opt-out may be removed by the administrator at any time.')),
        );
        foreach ($included_categories as $category_cid => $category_category) {

          // If the category's role is one the user is part of, show the category.
          $form['mail']['mass_contact_optout_' . $category_cid] = array(
            '#type' => 'checkbox',
            '#title' => t('Opt-out of mass e-mails to the %category category.', array(
              '%category' => $category_category,
            )),
            '#default_value' => $edit['mass_contact_optout_' . $category_cid],
          );
        }
      }
      return $form;
    }
  }
}