You are here

ad_permission.install in Advertisement 5.2

File

permission/ad_permission.install
View source
<?php

/**
 * Ad module permissions system database schema.
 * Copyright (c) 2008 Jeremy Andrews <jeremy@tag1consulting.com>.  
 */
function ad_permission_install() {
  switch ($GLOBALS['db_type']) {
    case 'pgsql':

      /**
       * Every ad can have one or more owners.
       */
      db_query("CREATE TABLE {ad_owners} (\n       oid SERIAL NOT NULL PRIMARY KEY,\n       aid INT NOT NULL DEFAULT '0',\n       uid INT NOT NULL DEFAULT '0'\n     );");

      /**
       * Permissions can be granted to each owner of each ad.  The same owner
       * can own multiple ads, and can have different permissions for each ad.
       */
      db_query("CREATE TABLE {ad_permissions} (\n       oid INT NOT NULL DEFAULT '0' PRIMARY KEY,\n       permissions TEXT NULL DEFAULT ''\n     );");

      /**
       * The ad_hosts table is used to configure users that can display ads
       * remotely.
       */
      db_query("CREATE TABLE {ad_hosts} (\n        uid INT NOT NULL DEFAULT '0' PRIMARY KEY,\n \n        hostid varchar(32) DEFAULT '',\n        status INT NOT NULL DEFAULT '0',\n        description TEXT NOT NULL DEFAULT ''\n      );");
      break;
    case 'mysql':
    case 'mysqli':
    default:

      /**
       * Every ad can have one or more owners.
       */
      db_query("CREATE TABLE IF NOT EXISTS {ad_owners} (\n        oid INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,\n        aid INT(10) UNSIGNED NOT NULL DEFAULT '0',\n        uid INT(10) UNSIGNED NOT NULL DEFAULT '0',\n\n        PRIMARY KEY  (oid),\n        KEY  (aid),\n        KEY  (uid)\n      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");

      /**
       * Permissions can be granted to each owner of each ad.  The same owner
       * can own multiple ads, and can have different permissions for each ad.
       */
      db_query("CREATE TABLE IF NOT EXISTS {ad_permissions} (\n        oid INT(11) UNSIGNED NOT NULL DEFAULT '0',\n        permissions LONGTEXT NULL,\n        PRIMARY KEY  (oid)\n      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");

      /**
       * The ad_hosts table is used to configure users that can display ads
       * remotely.
       */
      db_query("CREATE TABLE IF NOT EXISTS {ad_hosts} (\n        uid INT UNSIGNED NOT NULL DEFAULT '0',\n\n        hostid varchar(32) DEFAULT '',\n\n        status INT(2) UNSIGNED NOT NULL DEFAULT '0',\n        description TEXT NULL,\n\n        PRIMARY KEY  (uid),\n        KEY  (status),\n        KEY  (hostid)\n      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
      break;
  }
  drupal_set_message(t('The necessary ad module tables have been created.'));
}

/**
 * Update old-style permissions.
 */
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;
}

/**
 * Allow complete uninstallation of the ad module.
 */
function ad_permission_uninstall() {

  // Drop all ad module tables.
  db_query('DROP TABLE {ad_hosts}');
  db_query('DROP TABLE {ad_owners}');
  db_query('DROP TABLE {ad_permissions}');
}

Functions

Namesort descending Description
ad_permission_install Ad module permissions system database schema. Copyright (c) 2008 Jeremy Andrews <jeremy@tag1consulting.com>.
ad_permission_uninstall Allow complete uninstallation of the ad module.
ad_permission_update_5000 Update old-style permissions.