You are here

function mass_contact_taxonomy_user_edit in Mass Contact 7

Creates a list of categories the user is part of.

Parameters

object $user: The user object of the user for which the available categories are sought.

Return value

null|array The form snippet.

1 string reference to 'mass_contact_taxonomy_user_edit'
mass_contact_taxonomy.inc in plugins/mass_contact_taxonomy.inc

File

plugins/mass_contact_taxonomy.inc, line 234

Code

function mass_contact_taxonomy_user_edit($user) {

  // Get all the terms this user has selected.
  // Get the list of all fields on the user entity.
  $user_fields = field_info_instances('user');
  if (empty($user_fields)) {
    return;
  }

  // Get a list of all taxonomy fields.
  $taxonomy_fields = array();

  // Iterate through all the fields attached to the user entity.
  foreach ($user_fields['user'] as $user_field_instance) {

    // Get the field's information.
    $field = field_info_field($user_field_instance['field_name']);
    if (isset($field['module']) && $field['module'] == 'taxonomy') {

      // If the module providing this field is Taxonomy, add the field to the
      // field_name => vocabulary array of taxonomy fields.
      $taxonomy_fields[$field['field_name']] = $field['settings']['allowed_values'][0]['vocabulary'];
    }
  }

  // Put the term IDs into an array for later use.
  $users_terms = array();

  // Iterate through all the taxonomy fields.
  foreach ($taxonomy_fields as $field_id => $field_name) {

    // Iterate through each property in the user oblect to locate the taxonomy
    // fields.
    foreach ($user as $key => $value) {
      if ($key == $field_id) {
        if (!empty($value)) {
          foreach ($value[LANGUAGE_NONE] as $term_id) {

            // Save term.
            $users_terms[] = $term_id['tid'];
          }
        }
      }
    }
  }
  if (empty($users_terms)) {
    return;
  }

  // Get all the Mass Contact categories.
  $categories = db_select('mass_contact', 'mc')
    ->fields('mc', array(
    'cid',
    'category',
    'recipients',
  ))
    ->execute();

  // Collect all the categories which contain terms the user has selected.
  $included_categories = array();

  // Iterate through each category.
  foreach ($categories as $category) {

    // Pull out the roles that are a part of this category.
    $recipients = unserialize($category->recipients);
    foreach ($recipients['mass_contact_taxonomy'] as $term_id) {

      // If the category's term is one the user has selected, show the category.
      if (in_array($term_id, $users_terms)) {
        $included_categories[$category->cid] = check_plain($category->category);
      }
    }
  }
  return $included_categories;
}