You are here

function node_privacy_byrole_update_5000 in node privacy byrole 5

Implementation of hook_update_N().

Converts individual default variable names to arrays of role IDs for default permission groups.

File

./node_privacy_byrole.install, line 37

Code

function node_privacy_byrole_update_5000() {
  $ret = array();
  $to_set = array();
  $r = db_query("SELECT name, value FROM {variable} WHERE name LIKE 'npbr_metaperm_%%_%%'");
  while ($p = db_fetch_array($r)) {
    preg_match("/^npbr_metaperm_(.*?)_(\\d+)\$/", $p['name'], $matches);
    if (!empty($matches[1]) && isset($matches[2]) && is_numeric($matches[2]) && unserialize($p['value'])) {
      $to_set[$matches[1]][] = (int) $matches[2];
    }
  }
  while (list($content_type, $roles) = each($to_set)) {
    variable_set("npbr_default_grant_perms_{$content_type}", $roles);
  }

  // There is also variable_del(), it would be harder to clean up all the permissions
  // have have zero set for their value whereas this new way only stores granted values
  $ret[] = update_sql("DELETE FROM {variable} WHERE name LIKE 'npbr_metaperm_%%'");
  $to_set = array();
  $result = db_query("SELECT name, value FROM {variable} WHERE name LIKE 'npbr_foredit_%%_default_%%' ");
  while ($var = db_fetch_object($result)) {
    preg_match("/^npbr_foredit_(.*?)_default_(\\d+)\$/", $var->name, $matches);
    if (!empty($matches[1]) && isset($matches[2]) && is_numeric($matches[2]) && unserialize($var->value)) {
      $to_set[$matches[1]][] = $matches[2];
    }
  }
  while (list($content_type, $roles) = each($to_set)) {
    variable_set("npbr_default_edit_perms_{$content_type}", $roles);
    variable_set("npbr_default_delete_perms_{$content_type}", $roles);
  }
  $ret[] = update_sql("DELETE FROM {variable} WHERE name LIKE 'npbr_foredit_%%'");
  $to_set = array();
  $result = db_query("SELECT name, value FROM {variable} WHERE name LIKE 'npbr_forview_%%_default_%%'");
  while ($var = db_fetch_object($result)) {
    preg_match("/^npbr_forview_(.*?)_default_(\\d+)\$/", $var->name, $matches);
    if (!empty($matches[1]) && isset($matches[2]) && is_numeric($matches[2]) && unserialize($var->value)) {
      $to_set[$matches[1]][] = $matches[2];
    }
  }
  while (list($content_type, $roles) = each($to_set)) {
    variable_set("npbr_default_view_perms_{$content_type}", $roles);
  }
  $ret[] = update_sql("DELETE FROM {variable} WHERE name LIKE 'npbr_forview_%%'");
  cache_clear_all('variables', 'cache');
  return $ret;
}