You are here

function _taxonomy_access_update_db in Taxonomy Access Control 5

2 calls to _taxonomy_access_update_db()
taxonomy_access_enable in ./taxonomy_access.module
Implementation of hook_enable().
taxonomy_access_user in ./taxonomy_access.module
Implementation of hook_user Hook_user is called when a user event occurs to check for new roles. It would make sense to have a hook_role instead. However, that hook doesn't exist so we rely on the hook_user to determine if new roles have been…

File

./taxonomy_access.module, line 507
Allows administrators to specify how each category (in the taxonomy) can be used by various roles.

Code

function _taxonomy_access_update_db($current_rids = NULL, $old_rids = NULL) {

  // BEGIN: term_access table housekeeping
  // Update the term_access table to reflect any changes that may have occured since module was disabled.
  $tids = array();
  $rids = array();

  // Create list of all term and role ids
  $query = db_query('SELECT tid FROM {term_data}');
  while ($result = db_fetch_array($query)) {
    $current_tids[] = $result['tid'];
  }
  $current_tids[] = 0;
  if (!$current_rids) {
    $query = db_query('SELECT rid FROM {role}');
    while ($result = db_fetch_array($query)) {
      $current_rids[] = $result['rid'];
    }

    // $current_rids[] = 0; no default non-roled access
  }
  $current_vids = array();
  $query = db_query('SELECT vid FROM {vocabulary}');
  while ($result = db_fetch_array($query)) {
    $current_vids[] = $result['vid'];
  }

  // Get old list of term and role ids from when term_access was disabled
  $old_tids[] = 0;
  $query = db_query('SELECT DISTINCT(tid) FROM {term_access}');
  while ($result = db_fetch_array($query)) {
    $old_tids[] = $result['tid'];
  }
  if (!$old_rids) {
    $query = db_query('SELECT DISTINCT(rid) FROM {term_access_defaults}');
    while ($result = db_fetch_array($query)) {
      $old_rids[] = $result['rid'];
    }
  }
  $old_vids = array();
  $query = db_query('SELECT DISTINCT(vid) FROM {term_access_defaults}');
  while ($result = db_fetch_array($query)) {
    $old_vids[] = $result['vid'];
  }

  // Get the difference between old and current
  $delete_tids = $delete_rids = $delete_vids = array();
  if (is_array($old_tids)) {
    $delete_tids = array_diff($old_tids, $current_tids);
  }
  if (is_array($old_rids)) {
    $delete_rids = array_diff($old_rids, $current_rids);
  }
  if (is_array($old_vids)) {
    $delete_vids = array_diff($old_vids, $current_vids);
  }

  // Delete all rows with role and term ids that no longer exist from the term_access table
  foreach ($delete_rids as $rid) {
    db_query('DELETE FROM {term_access} WHERE rid = %d', $rid);
  }
  foreach ($delete_tids as $tid) {
    db_query('DELETE FROM {term_access} WHERE tid = %d', $tid);
  }

  // Set permissions for nodes without categories if they aren't already set
  $query = db_query('SELECT tid FROM {term_access} where tid = 0');
  if (!db_result($query) && is_array($current_rids)) {
    foreach ($current_rids as $rid) {
      $query = db_query('SELECT tid FROM {term_access} where tid = 0 AND rid = %d', $rid);
      if (!db_fetch_object($query)) {
        db_query('INSERT INTO {term_access} VALUES (%d, %d, %d, %d, %d, %d, %d)', _taxonomy_access_defaults(0, $rid));
      }
    }
  }

  // Add new role and term ids to term_access table since module was last disabled and assign them default permissions
  $all_rids = $add_tids = $add_rids = $add_vids = array();
  $add_tids = array_diff($current_tids, $old_tids);
  $add_vids = array_diff($current_vids, $old_vids);
  if (is_array($current_rids) && is_array($old_rids)) {
    $add_rids = array_diff($current_rids, $old_rids);
    $all_rids = array_merge($add_rids, $current_rids);
  }

  // Add role permissions for each new taxonomy terms.
  // nysus : Default permissions assume all users can not read content in the new taxonomy
  // pyromanfo : Drupal default is actually view only, as is the case with node_access
  foreach ($add_tids as $tid) {
    if ($tid != 0) {
      foreach ($all_rids as $rid) {
        db_query('DELETE FROM {term_access} WHERE tid = %d AND rid = %d', $tid, $rid);
        db_query('INSERT INTO {term_access} VALUES (%d, %d, %d, %d, %d, %d, %d)', _taxonomy_access_defaults($tid, $rid));
      }
    }
  }

  // Add role permissions for all old taxonomy terms.
  // nysus : Default permissions assume new role does not have access to content in any category
  // pyromanfo : Drupal default is actually view only, as is the case with node_access
  foreach ($current_tids as $tid) {
    if ($tid != 0) {
      foreach ($add_rids as $rid) {
        db_query('DELETE FROM {term_access} WHERE tid = %d AND rid = %d', $tid, $rid);
        db_query('INSERT INTO {term_access} VALUES (%d, %d, %d, %d, %d, %d, %d)', _taxonomy_access_defaults($tid, $rid));
      }
    }
  }

  // END: term_access table housekeeping
  // BEGIN: term_access_defaults housekeeping
  // Setup defaults
  foreach ($delete_rids as $rid) {
    db_query('DELETE FROM {term_access_defaults} WHERE rid = %d', $rid);
  }
  foreach ($delete_vids as $vid) {
    db_query('DELETE FROM {term_access_defaults} WHERE vid = %d', $vid);
  }
  if (count($add_rids)) {
    drupal_set_message(t('Taxonomy Access: Please <a href="@settings">check default permissions</a> of new user role(s).', array(
      '@settings' => url('admin/user/taxonomy_access'),
    )));
    foreach ($current_vids as $vid) {
      foreach ($add_rids as $rid) {
        db_query('DELETE FROM {term_access_defaults} WHERE vid = %d AND rid = %d', $vid, $rid);
        db_query('INSERT INTO {term_access_defaults} VALUES (%d, %d, %d, %d, %d, %d, %d)', _taxonomy_access_defaults($vid, $rid));
      }
    }
  }
  if (count($add_vids)) {
    drupal_set_message(t('Taxonomy Access: Please <a href="@settings">check default permissions</a> of new vocabulary.', array(
      '@settings' => url('admin/user/taxonomy_access'),
    )));
    foreach ($add_vids as $vid) {
      if (is_array($current_rids)) {
        foreach ($current_rids as $rid) {
          db_query('DELETE FROM {term_access_defaults} WHERE vid = %d AND rid = %d', $vid, $rid);
          db_query('INSERT INTO {term_access_defaults} VALUES (%d, %d, %d, %d, %d, %d, %d)', _taxonomy_access_defaults($vid, $rid));
        }
      }
    }
  }

  // END: term_access_defaults housekeeping
  // Force to rebuild node_access_table to reflect the changes made to term_access table
  node_access_rebuild();
}