You are here

function _taxonomy_access_create_defaults in Taxonomy Access Control 7

Retrieve vocabularies in which the current user may create terms.

Parameters

object|null $account: (optional) The account for which to retrieve grants. If no account is passed, the current user will be used. Defaults to NULL.

Return value

array An array of term IDs, or TRUE if the user has the grant for all terms.

Related topics

1 call to _taxonomy_access_create_defaults()
taxonomy_access_user_create_defaults in ./taxonomy_access.create.inc
Retrieve terms that the current user may create.

File

./taxonomy_access.create.inc, line 340
Implements the Add Tag (create) grant on editing forms.

Code

function _taxonomy_access_create_defaults($account = NULL) {

  // If the user can administer taxonomy, return TRUE for a global grant.
  if (user_access('administer taxonomy', $account)) {
    return TRUE;
  }

  // Build a term grant query.
  $query = _taxonomy_access_grant_query(array(
    'create',
  ), TRUE);

  // Select term grants for the current user's roles.
  if (is_null($account)) {
    global $user;
    $account = $user;
  }
  $query
    ->fields('td', array(
    'vid',
  ))
    ->groupBy('td.vid')
    ->condition('tadg.rid', array_keys($account->roles), 'IN');

  // Fetch term IDs.
  $r = $query
    ->execute()
    ->fetchAll();
  $vids = array();

  // If there are results, initialize a flag to test whether the user
  // has the grant for all terms.
  $grants_for_all_vocabs = empty($r) ? FALSE : TRUE;
  foreach ($r as $record) {

    // If the user has the grant, add the term to the array.
    if ($record->grant_create) {
      $vids[] = $record->vid;
    }
    else {
      $grants_for_all_vocabs = FALSE;
    }
  }

  // If the user has the grant for all terms, return TRUE for a global grant.
  if ($grants_for_all_vocabs) {
    return TRUE;
  }
  return $vids;
}