You are here

og_notifications.install in Organic groups 5.8

Same filename and directory in other branches
  1. 5.3 og_notifications/og_notifications.install

File

og_notifications/og_notifications.install
View source
<?php

/**
 * Implementation of hook_install().
 */
function og_notifications_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("CREATE TABLE {og_notifications} (\n        uid int NOT NULL,\n        autosubscribe tinyint NOT NULL DEFAULT -1,\n        PRIMARY KEY (uid)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      break;
    case 'pgsql':
      db_query("CREATE TABLE {og_notifications} (\n        uid int NOT NULL,\n        autosubscribe smallint NOT NULL DEFAULT -1,\n        PRIMARY KEY (uid)\n      );");
      break;
  }
  if (variable_get('og_notifications_update_required', FALSE)) {
    og_notifications_og_upgrade();
  }
  drupal_set_message(t('Organic groups notifications module installation script complete.'));
}

/**
 * Update function to quick-fix OG-D5 installations that already had this module
 * enabled prior to the 5703 update.
 */
function og_notifications_update_5001() {
  if (variable_get('og_notifications_update_required', FALSE)) {
    og_notifications_install();
  }
}

/**
 * Implementation of hook_enable().
 */
function og_notifications_enable() {
  _og_notifications_populate();
}

/**
 * Implementation of hook_requirements().
 */
function og_notifications_requirements($phase) {
  if ($phase == 'install') {
    $schema = db_result(db_query("SELECT schema_version FROM {system} WHERE type = 'module' AND name = 'og'"));
    if ($schema < 5703) {
      $t = get_t();
      return array(
        'og_notifications' => array(
          'title' => $t('OG notifications'),
          'value' => $t('Please ensure that your installation of organic groups is up to date before enabling OG notifications.'),
          'severity' => REQUIREMENT_ERROR,
        ),
      );
    }
  }
}

/**
 * Notifications upgrade: Based on the upgrade flag, move existing subscriptions
 * to the notifications module.
 */
function og_notifications_og_upgrade() {
  $ret = array();

  // Load notifications and dependencies.
  drupal_load('module', 'og_notifications');
  drupal_load('module', 'notifications');
  drupal_load('module', 'token');
  drupal_load('module', 'messaging');

  // Save notification subscription for each group based on og_uid.mail_type.
  $result = db_query("SELECT nid, uid FROM {og_uid} WHERE mail_type = 1");
  while ($subscription = db_fetch_object($result)) {

    // Resort to subterfuge to avoid repeat calls to user_load.
    $account = (object) array(
      'uid' => $subscription->uid,
    );
    og_notifications_user_subscribe($account, $subscription->nid);
  }

  // Drop field notification.
  db_query("ALTER TABLE {og} DROP notification");

  // Drop field mail_type.
  db_query("ALTER TABLE {og_uid} DROP mail_type");

  // og_email is now effectively only a boolean. Users with
  // OG_NOTIFICATION_SELECTIVE are equivalent to those with autosubscribe turned
  // off.
  $autosubscribe = variable_get('og_notification', 1) == 1 ? 1 : 0;
  variable_set('og_notifications_autosubscribe', $autosubscribe);
  variable_del('og_notification');
  db_query("INSERT INTO {og_notifications} (uid, autosubscribe) SELECT oug.uid, oug.og_email FROM {og_uid_global} oug");
  db_query("UPDATE {og_notifications} SET autosubscribe = 0 WHERE autosubscribe = 2");
  db_query('DROP TABLE {og_uid_global}');
  variable_del('og_notifications_update_required');
  return $ret;
}

/**
 * Implementation of hook_uninstall().
 */
function og_notifications_uninstall() {
  db_query('DROP TABLE {og_notifications}');
  variable_del('og_notifications_autosubscribe');
  variable_del('og_notifications_content_types');

  // @TODO: Clear any queued messages in notifications?
  drupal_set_message(t('Organic groups notifications module uninstallation script complete.'));
}

/**
 * Populate the og_notifications_table with any uids added prior to installation
 * or when disabled.
 */
function _og_notifications_populate() {
  $sql = 'SELECT u.uid FROM {users} u LEFT JOIN {og_notifications} ogn ON u.uid = ogn.uid WHERE u.uid > 0 AND ogn.uid IS NULL';
  $result = db_query($sql);
  while ($row = db_fetch_object($result)) {

    // autosubscribe is set to the column default.
    db_query("INSERT INTO {og_notifications} (uid) VALUES (%d)", $row->uid);
  }
}

Functions

Namesort descending Description
og_notifications_enable Implementation of hook_enable().
og_notifications_install Implementation of hook_install().
og_notifications_og_upgrade Notifications upgrade: Based on the upgrade flag, move existing subscriptions to the notifications module.
og_notifications_requirements Implementation of hook_requirements().
og_notifications_uninstall Implementation of hook_uninstall().
og_notifications_update_5001 Update function to quick-fix OG-D5 installations that already had this module enabled prior to the 5703 update.
_og_notifications_populate Populate the og_notifications_table with any uids added prior to installation or when disabled.