You are here

function ad_permission_update_5000 in Advertisement 5.2

Update old-style permissions.

File

permission/ad_permission.install, line 93

Code

function ad_permission_update_5000() {
  $ret = array();
  $result = db_query('SELECT * FROM {ad_permissions}');
  while ($permission = db_fetch_object($result)) {
    $changed = FALSE;
    $permissions = explode('|,|', $permission->permissions);

    // 'manage active ad' renamed to 'manage active image'
    $key = array_search('manage active ad', $permissions);
    if ($key !== FALSE) {
      unset($permissions[$key]);
      $permissions[] = 'manage active image';
      $changed = TRUE;
    }

    // 'manage status' replaced with numerous per-status permissions,
    // assigning defaults to all users
    $key = array_search('manage status', $permissions);
    if ($key !== FALSE) {
      unset($permissions[$key]);
      $defaults = module_invoke_all('adapi', 'permissions', NULL);
      $perms = array();
      foreach ($defaults as $text => $default) {

        // only add new 'set status' style permissions
        if ($default && !strncmp($text, 'set status', strlen('set status'))) {
          $perms[] = $text;
        }
      }
      $permissions = array_merge($permissions, $perms);
      $changed = TRUE;
    }
    if ($changed) {
      $ret[] = update_sql("UPDATE {ad_permissions} SET permissions = '" . implode('|,|', $permissions) . "' WHERE oid = {$permission->oid}");
    }
  }
  return $ret;
}